2016-06-15 13:11:23 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2016-10-13 14:28:32 -04:00
|
|
|
"reflect"
|
2016-06-15 13:11:23 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-10-13 14:28:32 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-11-01 13:12:29 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-06-16 17:55:50 -04:00
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
2016-06-15 13:11:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemBytesString(t *testing.T) {
|
|
|
|
var mem memBytes = 1048576
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, mem.String(), "1 MiB")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemBytesSetAndValue(t *testing.T) {
|
|
|
|
var mem memBytes
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.NilError(t, mem.Set("5kb"))
|
|
|
|
assert.Equal(t, mem.Value(), int64(5120))
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNanoCPUsString(t *testing.T) {
|
2016-11-01 13:12:29 -04:00
|
|
|
var cpus opts.NanoCPUs = 6100000000
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, cpus.String(), "6.100")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNanoCPUsSetAndValue(t *testing.T) {
|
2016-11-01 13:12:29 -04:00
|
|
|
var cpus opts.NanoCPUs
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.NilError(t, cpus.Set("0.35"))
|
|
|
|
assert.Equal(t, cpus.Value(), int64(350000000))
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDurationOptString(t *testing.T) {
|
|
|
|
dur := time.Duration(300 * 10e8)
|
|
|
|
duration := DurationOpt{value: &dur}
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, duration.String(), "5m0s")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDurationOptSetAndValue(t *testing.T) {
|
|
|
|
var duration DurationOpt
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.NilError(t, duration.Set("300s"))
|
|
|
|
assert.Equal(t, *duration.Value(), time.Duration(300*10e8))
|
2016-10-13 14:28:32 -04:00
|
|
|
assert.NilError(t, duration.Set("-300s"))
|
|
|
|
assert.Equal(t, *duration.Value(), time.Duration(-300*10e8))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPositiveDurationOptSetAndValue(t *testing.T) {
|
|
|
|
var duration PositiveDurationOpt
|
|
|
|
assert.NilError(t, duration.Set("300s"))
|
|
|
|
assert.Equal(t, *duration.Value(), time.Duration(300*10e8))
|
|
|
|
assert.Error(t, duration.Set("-300s"), "cannot be negative")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint64OptString(t *testing.T) {
|
|
|
|
value := uint64(2345678)
|
|
|
|
opt := Uint64Opt{value: &value}
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, opt.String(), "2345678")
|
2016-06-15 13:11:23 -04:00
|
|
|
|
|
|
|
opt = Uint64Opt{}
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, opt.String(), "none")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint64OptSetAndValue(t *testing.T) {
|
|
|
|
var opt Uint64Opt
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.NilError(t, opt.Set("14445"))
|
|
|
|
assert.Equal(t, *opt.Value(), uint64(14445))
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:28:32 -04:00
|
|
|
func TestHealthCheckOptionsToHealthConfig(t *testing.T) {
|
|
|
|
dur := time.Second
|
|
|
|
opt := healthCheckOptions{
|
|
|
|
cmd: "curl",
|
|
|
|
interval: PositiveDurationOpt{DurationOpt{value: &dur}},
|
|
|
|
timeout: PositiveDurationOpt{DurationOpt{value: &dur}},
|
|
|
|
retries: 10,
|
|
|
|
}
|
|
|
|
config, err := opt.toHealthConfig()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, reflect.DeepEqual(config, &container.HealthConfig{
|
|
|
|
Test: []string{"CMD-SHELL", "curl"},
|
|
|
|
Interval: time.Second,
|
|
|
|
Timeout: time.Second,
|
|
|
|
Retries: 10,
|
|
|
|
}), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthCheckOptionsToHealthConfigNoHealthcheck(t *testing.T) {
|
|
|
|
opt := healthCheckOptions{
|
|
|
|
noHealthcheck: true,
|
|
|
|
}
|
|
|
|
config, err := opt.toHealthConfig()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, reflect.DeepEqual(config, &container.HealthConfig{
|
|
|
|
Test: []string{"NONE"},
|
|
|
|
}), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthCheckOptionsToHealthConfigConflict(t *testing.T) {
|
|
|
|
opt := healthCheckOptions{
|
|
|
|
cmd: "curl",
|
|
|
|
noHealthcheck: true,
|
|
|
|
}
|
|
|
|
_, err := opt.toHealthConfig()
|
|
|
|
assert.Error(t, err, "--no-healthcheck conflicts with --health-* options")
|
|
|
|
}
|