forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
106 lines (81 loc) · 4.51 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cluster
import (
"testing"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/stretchr/testify/assert"
)
func TestBuildContainerConfig(t *testing.T) {
config := BuildContainerConfig(container.Config{}, container.HostConfig{}, network.NetworkingConfig{})
assert.Empty(t, config.Env)
assert.Empty(t, config.Labels)
config = BuildContainerConfig(container.Config{Env: []string{"test=true"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.Len(t, config.Env, 1)
assert.Empty(t, config.Labels)
config = BuildContainerConfig(container.Config{Env: []string{"constraint:test==true"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.Empty(t, config.Env)
assert.Len(t, config.Labels, 1)
config = BuildContainerConfig(container.Config{Env: []string{"affinity:container==test"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.Empty(t, config.Env)
assert.Len(t, config.Labels, 1)
config = BuildContainerConfig(container.Config{Env: []string{"test=true", "constraint:test==true", "affinity:container==test"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.Len(t, config.Env, 1)
assert.Len(t, config.Labels, 2)
}
func TestSwarmID(t *testing.T) {
// Getter / Setter
config := BuildContainerConfig(container.Config{}, container.HostConfig{}, network.NetworkingConfig{})
assert.Empty(t, config.SwarmID())
config.SetSwarmID("foo")
assert.Equal(t, config.SwarmID(), "foo")
assert.Equal(t, config.Labels[SwarmLabelNamespace+".id"], "foo")
// Retrieve an existing ID.
config = BuildContainerConfig(container.Config{Labels: map[string]string{SwarmLabelNamespace + ".id": "test"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.Equal(t, config.SwarmID(), "test")
}
func TestConstraints(t *testing.T) {
config := BuildContainerConfig(container.Config{}, container.HostConfig{}, network.NetworkingConfig{})
assert.Empty(t, config.Constraints())
config = BuildContainerConfig(container.Config{Env: []string{"constraint:test==true"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.Len(t, config.Constraints(), 1)
config = BuildContainerConfig(container.Config{Env: []string{"test=true", "constraint:test==true", "affinity:container==test"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.Len(t, config.Constraints(), 1)
}
func TestAffinities(t *testing.T) {
config := BuildContainerConfig(container.Config{}, container.HostConfig{}, network.NetworkingConfig{})
assert.Empty(t, config.Affinities())
config = BuildContainerConfig(container.Config{Env: []string{"affinity:container==test"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.Len(t, config.Affinities(), 1)
config = BuildContainerConfig(container.Config{Env: []string{"test=true", "constraint:test==true", "affinity:container==test"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.Len(t, config.Affinities(), 1)
assert.Equal(t, len(config.Affinities()), 1)
}
func TestConsolidateResourceFields(t *testing.T) {
config := BuildContainerConfig(container.Config{}, container.HostConfig{Resources: container.Resources{Memory: 4242, MemorySwap: 4343, CPUShares: 4444, CpusetCpus: "1-2"}}, network.NetworkingConfig{})
assert.Equal(t, config.HostConfig.Memory, int64(4242))
assert.Equal(t, config.HostConfig.MemorySwap, int64(4343))
assert.Equal(t, config.HostConfig.CPUShares, int64(4444))
assert.Equal(t, config.HostConfig.CpusetCpus, "1-2")
}
func TestAddAffinity(t *testing.T) {
config := BuildContainerConfig(container.Config{}, container.HostConfig{}, network.NetworkingConfig{})
assert.Empty(t, config.Affinities())
config.AddAffinity("image==~testimage")
assert.Len(t, config.Affinities(), 1)
}
func TestRemoveAffinity(t *testing.T) {
config := BuildContainerConfig(container.Config{}, container.HostConfig{}, network.NetworkingConfig{})
assert.Empty(t, config.Affinities())
config.AddAffinity("image==~testimage1")
config.AddAffinity("image==~testimage2")
assert.Len(t, config.Affinities(), 2)
config.RemoveAffinity("image==~testimage1")
assert.Len(t, config.Affinities(), 1)
assert.Equal(t, config.Affinities()[0], "image==~testimage2")
}
func TestHaveNodeConstraint(t *testing.T) {
config := BuildContainerConfig(container.Config{}, container.HostConfig{}, network.NetworkingConfig{})
assert.False(t, config.HaveNodeConstraint())
config = BuildContainerConfig(container.Config{Env: []string{"constraint:node==node1"}}, container.HostConfig{}, network.NetworkingConfig{})
assert.True(t, config.HaveNodeConstraint())
}