github.com/kunnos/engine@v1.13.1/cli/compose/convert/service_test.go (about) 1 package convert 2 3 import ( 4 "sort" 5 "strings" 6 "testing" 7 "time" 8 9 "github.com/docker/docker/api/types/container" 10 "github.com/docker/docker/api/types/swarm" 11 composetypes "github.com/docker/docker/cli/compose/types" 12 "github.com/docker/docker/pkg/testutil/assert" 13 ) 14 15 func TestConvertRestartPolicyFromNone(t *testing.T) { 16 policy, err := convertRestartPolicy("no", nil) 17 assert.NilError(t, err) 18 assert.Equal(t, policy, (*swarm.RestartPolicy)(nil)) 19 } 20 21 func TestConvertRestartPolicyFromUnknown(t *testing.T) { 22 _, err := convertRestartPolicy("unknown", nil) 23 assert.Error(t, err, "unknown restart policy: unknown") 24 } 25 26 func TestConvertRestartPolicyFromAlways(t *testing.T) { 27 policy, err := convertRestartPolicy("always", nil) 28 expected := &swarm.RestartPolicy{ 29 Condition: swarm.RestartPolicyConditionAny, 30 } 31 assert.NilError(t, err) 32 assert.DeepEqual(t, policy, expected) 33 } 34 35 func TestConvertRestartPolicyFromFailure(t *testing.T) { 36 policy, err := convertRestartPolicy("on-failure:4", nil) 37 attempts := uint64(4) 38 expected := &swarm.RestartPolicy{ 39 Condition: swarm.RestartPolicyConditionOnFailure, 40 MaxAttempts: &attempts, 41 } 42 assert.NilError(t, err) 43 assert.DeepEqual(t, policy, expected) 44 } 45 46 func TestConvertEnvironment(t *testing.T) { 47 source := map[string]string{ 48 "foo": "bar", 49 "key": "value", 50 } 51 env := convertEnvironment(source) 52 sort.Strings(env) 53 assert.DeepEqual(t, env, []string{"foo=bar", "key=value"}) 54 } 55 56 func TestConvertResourcesFull(t *testing.T) { 57 source := composetypes.Resources{ 58 Limits: &composetypes.Resource{ 59 NanoCPUs: "0.003", 60 MemoryBytes: composetypes.UnitBytes(300000000), 61 }, 62 Reservations: &composetypes.Resource{ 63 NanoCPUs: "0.002", 64 MemoryBytes: composetypes.UnitBytes(200000000), 65 }, 66 } 67 resources, err := convertResources(source) 68 assert.NilError(t, err) 69 70 expected := &swarm.ResourceRequirements{ 71 Limits: &swarm.Resources{ 72 NanoCPUs: 3000000, 73 MemoryBytes: 300000000, 74 }, 75 Reservations: &swarm.Resources{ 76 NanoCPUs: 2000000, 77 MemoryBytes: 200000000, 78 }, 79 } 80 assert.DeepEqual(t, resources, expected) 81 } 82 83 func TestConvertResourcesOnlyMemory(t *testing.T) { 84 source := composetypes.Resources{ 85 Limits: &composetypes.Resource{ 86 MemoryBytes: composetypes.UnitBytes(300000000), 87 }, 88 Reservations: &composetypes.Resource{ 89 MemoryBytes: composetypes.UnitBytes(200000000), 90 }, 91 } 92 resources, err := convertResources(source) 93 assert.NilError(t, err) 94 95 expected := &swarm.ResourceRequirements{ 96 Limits: &swarm.Resources{ 97 MemoryBytes: 300000000, 98 }, 99 Reservations: &swarm.Resources{ 100 MemoryBytes: 200000000, 101 }, 102 } 103 assert.DeepEqual(t, resources, expected) 104 } 105 106 func TestConvertHealthcheck(t *testing.T) { 107 retries := uint64(10) 108 source := &composetypes.HealthCheckConfig{ 109 Test: []string{"EXEC", "touch", "/foo"}, 110 Timeout: "30s", 111 Interval: "2ms", 112 Retries: &retries, 113 } 114 expected := &container.HealthConfig{ 115 Test: source.Test, 116 Timeout: 30 * time.Second, 117 Interval: 2 * time.Millisecond, 118 Retries: 10, 119 } 120 121 healthcheck, err := convertHealthcheck(source) 122 assert.NilError(t, err) 123 assert.DeepEqual(t, healthcheck, expected) 124 } 125 126 func TestConvertHealthcheckDisable(t *testing.T) { 127 source := &composetypes.HealthCheckConfig{Disable: true} 128 expected := &container.HealthConfig{ 129 Test: []string{"NONE"}, 130 } 131 132 healthcheck, err := convertHealthcheck(source) 133 assert.NilError(t, err) 134 assert.DeepEqual(t, healthcheck, expected) 135 } 136 137 func TestConvertHealthcheckDisableWithTest(t *testing.T) { 138 source := &composetypes.HealthCheckConfig{ 139 Disable: true, 140 Test: []string{"EXEC", "touch"}, 141 } 142 _, err := convertHealthcheck(source) 143 assert.Error(t, err, "test and disable can't be set") 144 } 145 146 func TestConvertServiceNetworksOnlyDefault(t *testing.T) { 147 networkConfigs := networkMap{} 148 networks := map[string]*composetypes.ServiceNetworkConfig{} 149 150 configs, err := convertServiceNetworks( 151 networks, networkConfigs, NewNamespace("foo"), "service") 152 153 expected := []swarm.NetworkAttachmentConfig{ 154 { 155 Target: "foo_default", 156 Aliases: []string{"service"}, 157 }, 158 } 159 160 assert.NilError(t, err) 161 assert.DeepEqual(t, configs, expected) 162 } 163 164 func TestConvertServiceNetworks(t *testing.T) { 165 networkConfigs := networkMap{ 166 "front": composetypes.NetworkConfig{ 167 External: composetypes.External{ 168 External: true, 169 Name: "fronttier", 170 }, 171 }, 172 "back": composetypes.NetworkConfig{}, 173 } 174 networks := map[string]*composetypes.ServiceNetworkConfig{ 175 "front": { 176 Aliases: []string{"something"}, 177 }, 178 "back": { 179 Aliases: []string{"other"}, 180 }, 181 } 182 183 configs, err := convertServiceNetworks( 184 networks, networkConfigs, NewNamespace("foo"), "service") 185 186 expected := []swarm.NetworkAttachmentConfig{ 187 { 188 Target: "foo_back", 189 Aliases: []string{"other", "service"}, 190 }, 191 { 192 Target: "fronttier", 193 Aliases: []string{"something", "service"}, 194 }, 195 } 196 197 sortedConfigs := byTargetSort(configs) 198 sort.Sort(&sortedConfigs) 199 200 assert.NilError(t, err) 201 assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(sortedConfigs), expected) 202 } 203 204 type byTargetSort []swarm.NetworkAttachmentConfig 205 206 func (s byTargetSort) Len() int { 207 return len(s) 208 } 209 210 func (s byTargetSort) Less(i, j int) bool { 211 return strings.Compare(s[i].Target, s[j].Target) < 0 212 } 213 214 func (s byTargetSort) Swap(i, j int) { 215 s[i], s[j] = s[j], s[i] 216 }