2015-01-20 01:47:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2016-01-06 00:40:35 +00:00
|
|
|
"strings"
|
2015-01-20 01:47:17 +00:00
|
|
|
"testing"
|
2016-01-06 01:59:58 +00:00
|
|
|
"time"
|
2015-01-20 01:47:17 +00:00
|
|
|
|
2016-01-06 01:59:58 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2017-02-08 00:16:33 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2015-01-20 01:47:17 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
func testLockCommand(t *testing.T) (*cli.MockUi, *LockCommand) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
return ui, &LockCommand{
|
2017-02-08 00:16:33 +00:00
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-08 00:16:33 +00:00
|
|
|
Flags: base.FlagSetHTTP,
|
2016-10-09 04:10:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 01:47:17 +00:00
|
|
|
func TestLockCommand_implements(t *testing.T) {
|
|
|
|
var _ cli.Command = &LockCommand{}
|
|
|
|
}
|
|
|
|
|
2016-01-06 01:59:58 +00:00
|
|
|
func argFail(t *testing.T, args []string, expected string) {
|
2016-10-09 04:10:40 +00:00
|
|
|
ui, c := testLockCommand(t)
|
2016-01-06 01:59:58 +00:00
|
|
|
if code := c.Run(args); code != 1 {
|
2016-01-06 00:40:35 +00:00
|
|
|
t.Fatalf("expected return code 1, got %d", code)
|
|
|
|
}
|
2016-01-06 01:59:58 +00:00
|
|
|
if reason := ui.ErrorWriter.String(); !strings.Contains(reason, expected) {
|
|
|
|
t.Fatalf("bad reason: got='%s', expected='%s'", reason, expected)
|
2016-01-06 00:40:35 +00:00
|
|
|
}
|
2016-01-06 01:59:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockCommand_BadArgs(t *testing.T) {
|
2017-02-07 01:50:51 +00:00
|
|
|
argFail(t, []string{"-try=blah", "test/prefix", "date"}, "invalid duration")
|
|
|
|
argFail(t, []string{"-try=-10s", "test/prefix", "date"}, "Timeout must be positive")
|
2016-01-06 01:59:58 +00:00
|
|
|
argFail(t, []string{"-monitor-retry=-5", "test/prefix", "date"}, "must be >= 0")
|
2016-01-06 00:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockCommand_Run(t *testing.T) {
|
2015-01-20 01:47:17 +00:00
|
|
|
a1 := testAgent(t)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
waitForLeader(t, a1.httpAddr)
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
ui, c := testLockCommand(t)
|
2015-01-20 01:47:17 +00:00
|
|
|
filePath := filepath.Join(a1.dir, "test_touch")
|
|
|
|
touchCmd := fmt.Sprintf("touch '%s'", filePath)
|
|
|
|
args := []string{"-http-addr=" + a1.httpAddr, "test/prefix", touchCmd}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the file
|
|
|
|
_, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:40:35 +00:00
|
|
|
|
2016-01-06 01:59:58 +00:00
|
|
|
func TestLockCommand_Try_Lock(t *testing.T) {
|
2016-01-06 00:40:35 +00:00
|
|
|
a1 := testAgent(t)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
waitForLeader(t, a1.httpAddr)
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
ui, c := testLockCommand(t)
|
2016-01-06 01:59:58 +00:00
|
|
|
filePath := filepath.Join(a1.dir, "test_touch")
|
|
|
|
touchCmd := fmt.Sprintf("touch '%s'", filePath)
|
|
|
|
args := []string{"-http-addr=" + a1.httpAddr, "-try=10s", "test/prefix", touchCmd}
|
|
|
|
|
|
|
|
// Run the command.
|
|
|
|
var lu *LockUnlock
|
|
|
|
code := c.run(args, &lu)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
_, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2016-01-06 00:40:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 01:59:58 +00:00
|
|
|
// Make sure the try options were set correctly.
|
|
|
|
opts, ok := lu.rawOpts.(*api.LockOptions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type")
|
|
|
|
}
|
|
|
|
if !opts.LockTryOnce || opts.LockWaitTime != 10*time.Second {
|
|
|
|
t.Fatalf("bad: %#v", opts)
|
|
|
|
}
|
2016-01-06 00:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockCommand_Try_Semaphore(t *testing.T) {
|
2016-01-06 01:59:58 +00:00
|
|
|
a1 := testAgent(t)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
waitForLeader(t, a1.httpAddr)
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
ui, c := testLockCommand(t)
|
2016-01-06 01:59:58 +00:00
|
|
|
filePath := filepath.Join(a1.dir, "test_touch")
|
|
|
|
touchCmd := fmt.Sprintf("touch '%s'", filePath)
|
|
|
|
args := []string{"-http-addr=" + a1.httpAddr, "-n=3", "-try=10s", "test/prefix", touchCmd}
|
|
|
|
|
|
|
|
// Run the command.
|
|
|
|
var lu *LockUnlock
|
|
|
|
code := c.run(args, &lu)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
_, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the try options were set correctly.
|
|
|
|
opts, ok := lu.rawOpts.(*api.SemaphoreOptions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type")
|
|
|
|
}
|
|
|
|
if !opts.SemaphoreTryOnce || opts.SemaphoreWaitTime != 10*time.Second {
|
|
|
|
t.Fatalf("bad: %#v", opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 02:34:22 +00:00
|
|
|
func TestLockCommand_MonitorRetry_Lock_Default(t *testing.T) {
|
2016-01-06 01:59:58 +00:00
|
|
|
a1 := testAgent(t)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
waitForLeader(t, a1.httpAddr)
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
ui, c := testLockCommand(t)
|
2016-01-06 01:59:58 +00:00
|
|
|
filePath := filepath.Join(a1.dir, "test_touch")
|
|
|
|
touchCmd := fmt.Sprintf("touch '%s'", filePath)
|
2016-01-06 02:34:22 +00:00
|
|
|
args := []string{"-http-addr=" + a1.httpAddr, "test/prefix", touchCmd}
|
2016-01-06 01:59:58 +00:00
|
|
|
|
|
|
|
// Run the command.
|
|
|
|
var lu *LockUnlock
|
|
|
|
code := c.run(args, &lu)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
_, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the monitor options were set correctly.
|
|
|
|
opts, ok := lu.rawOpts.(*api.LockOptions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type")
|
|
|
|
}
|
2016-01-06 02:34:22 +00:00
|
|
|
if opts.MonitorRetries != defaultMonitorRetry ||
|
|
|
|
opts.MonitorRetryTime != defaultMonitorRetryTime {
|
|
|
|
t.Fatalf("bad: %#v", opts)
|
2016-01-06 01:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 02:34:22 +00:00
|
|
|
func TestLockCommand_MonitorRetry_Semaphore_Default(t *testing.T) {
|
2016-01-06 01:59:58 +00:00
|
|
|
a1 := testAgent(t)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
waitForLeader(t, a1.httpAddr)
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
ui, c := testLockCommand(t)
|
2016-01-06 01:59:58 +00:00
|
|
|
filePath := filepath.Join(a1.dir, "test_touch")
|
|
|
|
touchCmd := fmt.Sprintf("touch '%s'", filePath)
|
2016-01-06 02:34:22 +00:00
|
|
|
args := []string{"-http-addr=" + a1.httpAddr, "-n=3", "test/prefix", touchCmd}
|
2016-01-06 01:59:58 +00:00
|
|
|
|
|
|
|
// Run the command.
|
|
|
|
var lu *LockUnlock
|
|
|
|
code := c.run(args, &lu)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
_, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the monitor options were set correctly.
|
|
|
|
opts, ok := lu.rawOpts.(*api.SemaphoreOptions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type")
|
|
|
|
}
|
2016-01-06 02:34:22 +00:00
|
|
|
if opts.MonitorRetries != defaultMonitorRetry ||
|
|
|
|
opts.MonitorRetryTime != defaultMonitorRetryTime {
|
|
|
|
t.Fatalf("bad: %#v", opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockCommand_MonitorRetry_Lock_Arg(t *testing.T) {
|
|
|
|
a1 := testAgent(t)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
waitForLeader(t, a1.httpAddr)
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
ui, c := testLockCommand(t)
|
2016-01-06 02:34:22 +00:00
|
|
|
filePath := filepath.Join(a1.dir, "test_touch")
|
|
|
|
touchCmd := fmt.Sprintf("touch '%s'", filePath)
|
|
|
|
args := []string{"-http-addr=" + a1.httpAddr, "-monitor-retry=9", "test/prefix", touchCmd}
|
|
|
|
|
|
|
|
// Run the command.
|
|
|
|
var lu *LockUnlock
|
|
|
|
code := c.run(args, &lu)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
_, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the monitor options were set correctly.
|
|
|
|
opts, ok := lu.rawOpts.(*api.LockOptions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type")
|
|
|
|
}
|
|
|
|
if opts.MonitorRetries != 9 ||
|
|
|
|
opts.MonitorRetryTime != defaultMonitorRetryTime {
|
|
|
|
t.Fatalf("bad: %#v", opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockCommand_MonitorRetry_Semaphore_Arg(t *testing.T) {
|
|
|
|
a1 := testAgent(t)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
waitForLeader(t, a1.httpAddr)
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
ui, c := testLockCommand(t)
|
2016-01-06 02:34:22 +00:00
|
|
|
filePath := filepath.Join(a1.dir, "test_touch")
|
|
|
|
touchCmd := fmt.Sprintf("touch '%s'", filePath)
|
|
|
|
args := []string{"-http-addr=" + a1.httpAddr, "-n=3", "-monitor-retry=9", "test/prefix", touchCmd}
|
|
|
|
|
|
|
|
// Run the command.
|
|
|
|
var lu *LockUnlock
|
|
|
|
code := c.run(args, &lu)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
_, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the monitor options were set correctly.
|
|
|
|
opts, ok := lu.rawOpts.(*api.SemaphoreOptions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad type")
|
|
|
|
}
|
|
|
|
if opts.MonitorRetries != 9 ||
|
|
|
|
opts.MonitorRetryTime != defaultMonitorRetryTime {
|
|
|
|
t.Fatalf("bad: %#v", opts)
|
2016-01-06 01:59:58 +00:00
|
|
|
}
|
2016-01-06 00:40:35 +00:00
|
|
|
}
|