validate duration flags:--delay, --timeout,--replication-ttl

Signed-off-by: Sun Hongliang <allen.sun@daocloud.io>
This commit is contained in:
Sun Hongliang 2016-03-13 19:08:39 +08:00
parent 4b2f0fe637
commit 5aa339ed38
6 changed files with 92 additions and 0 deletions

View File

@ -39,6 +39,9 @@ func join(c *cli.Context) {
if err != nil {
log.Fatalf("invalid --delay: %v", err)
}
if joinDelay < time.Duration(0)*time.Second {
log.Fatalf("--delay should not be a negative number")
}
hb, err := time.ParseDuration(c.String("heartbeat"))
if err != nil {
@ -60,6 +63,8 @@ func join(c *cli.Context) {
log.Fatal(err)
}
// if joinDelay is 0, no delay will be executed
// if joinDelay is larger than 0,
// add a random delay between 0s and joinDelay at start to avoid synchronized registration
if joinDelay > 0 {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))

View File

@ -18,6 +18,9 @@ func list(c *cli.Context) {
if err != nil {
log.Fatalf("invalid --timeout: %v", err)
}
if timeout <= time.Duration(0)*time.Second {
log.Fatalf("--timeout should be a positive number")
}
d, err := discovery.New(dflag, timeout, 0, getDiscoveryOpt(c))
if err != nil {

View File

@ -319,6 +319,9 @@ func manage(c *cli.Context) {
if err != nil {
log.Fatalf("invalid --replication-ttl: %v", err)
}
if leaderTTL <= time.Duration(0)*time.Second {
log.Fatalf("--replication-ttl should be a positive number")
}
setupReplication(c, cl, server, discovery, addr, leaderTTL, tlsConfig)
} else {

View File

@ -0,0 +1,45 @@
#!/usr/bin/env bats
load helpers
# Discovery parameter for Swarm
DISCOVERY="consul://127.0.0.1:5555/test"
@test "swarm join" {
# --advertise
run swarm join --heartbeat=1s --ttl=10s --delay=1s --advertise="" "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"missing mandatory --advertise flag"* ]]
run swarm join --heartbeat=1s --ttl=10s --delay=1s --advertise=127.0.0.1ac:sh25 "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--advertise should be of the form ip:port or hostname:port"* ]]
# --delay
run swarm join --heartbeat=1s --ttl=10s --delay=asdf --advertise=127.0.0.1:2376 "$DISCOVERY"
[ "$status" -ne 0 ]
run swarm join --heartbeat=1s --ttl=10s --delay=-30s --advertise=127.0.0.1:2376 "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--delay should not be a negative number"* ]]
# --heartbeat
run swarm join --heartbeat=asdf --ttl=10s --delay=1s --advertise=127.0.0.1:2376 "$DISCOVERY"
[ "$status" -ne 0 ]
run swarm join --heartbeat=-10s --ttl=10s --delay=1s --advertise=127.0.0.1:2376 "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--heartbeat should be at least one second"* ]]
# --ttl
run swarm join --heartbeat=1s --ttl=asdf --delay=1s --advertise=127.0.0.1:2376 "$DISCOVERY"
[ "$status" -ne 0 ]
run swarm join --heartbeat=1s --ttl=-10s --delay=1s --advertise=127.0.0.1:2376 "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--ttl must be strictly superior to the heartbeat value"* ]]
run swarm join --heartbeat=2s --ttl=1s --delay=1s --advertise=127.0.0.1:2376 "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--ttl must be strictly superior to the heartbeat value"* ]]
}

View File

@ -0,0 +1,18 @@
#!/usr/bin/env bats
load helpers
# Discovery parameter for Swarm
DISCOVERY="consul://127.0.0.1:5555/test"
@test "swarm list" {
# --timeout
run swarm list --timeout "-10s" "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--timeout should be a positive number"* ]]
# --timeout
run swarm list --timeout "0s" "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--timeout should be a positive number"* ]]
}

View File

@ -32,6 +32,24 @@ function teardown() {
stop_store
}
@test "replication options" {
# Bring up one manager
# --advertise
run swarm manage --replication --replication-ttl "4s" --advertise "" "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--advertise address must be provided when using --leader-election"* ]]
# --advertise
run swarm manage --replication --replication-ttl "4s" --advertise 127.0.0.1ab:1bcde "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--advertise should be of the form ip:port or hostname:port"* ]]
# --replication-ttl
run swarm manage --replication --replication-ttl "-20s" --advertise 127.0.0.1:$SWARM_BASE_PORT "$DISCOVERY"
[ "$status" -ne 0 ]
[[ "${output}" == *"--replication-ttl should be a positive number"* ]]
}
@test "leader election" {
local i=${#SWARM_MANAGE_PID[@]}
local port=$(($SWARM_BASE_PORT + $i))