mirror of https://github.com/docker/docs.git
validate duration flags:--delay, --timeout,--replication-ttl
Signed-off-by: Sun Hongliang <allen.sun@daocloud.io>
This commit is contained in:
parent
4b2f0fe637
commit
5aa339ed38
|
@ -39,6 +39,9 @@ func join(c *cli.Context) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("invalid --delay: %v", err)
|
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"))
|
hb, err := time.ParseDuration(c.String("heartbeat"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -60,6 +63,8 @@ func join(c *cli.Context) {
|
||||||
log.Fatal(err)
|
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
|
// add a random delay between 0s and joinDelay at start to avoid synchronized registration
|
||||||
if joinDelay > 0 {
|
if joinDelay > 0 {
|
||||||
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
|
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
|
||||||
|
|
|
@ -18,6 +18,9 @@ func list(c *cli.Context) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("invalid --timeout: %v", err)
|
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))
|
d, err := discovery.New(dflag, timeout, 0, getDiscoveryOpt(c))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -319,6 +319,9 @@ func manage(c *cli.Context) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("invalid --replication-ttl: %v", err)
|
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)
|
setupReplication(c, cl, server, discovery, addr, leaderTTL, tlsConfig)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -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"* ]]
|
||||||
|
}
|
|
@ -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"* ]]
|
||||||
|
}
|
|
@ -32,6 +32,24 @@ function teardown() {
|
||||||
stop_store
|
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" {
|
@test "leader election" {
|
||||||
local i=${#SWARM_MANAGE_PID[@]}
|
local i=${#SWARM_MANAGE_PID[@]}
|
||||||
local port=$(($SWARM_BASE_PORT + $i))
|
local port=$(($SWARM_BASE_PORT + $i))
|
||||||
|
|
Loading…
Reference in New Issue