This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
director_upstream_state_test.go
263 lines (230 loc) · 8.15 KB
/
director_upstream_state_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package sockguard
import (
"fmt"
)
// Simplified mocked out upstream state of Docker networks, for use in create container/create network/delete network/check owner tests
// NOTE: there is no locking around accesses in this type, assumed that each test block will have it's own instance
type upstreamState struct {
// Key = container name/ID
containers map[string]upstreamStateContainer
// Key = image name/ID
images map[string]upstreamStateImage
// Key = network name/ID
networks map[string]upstreamStateNetwork
// Key = volume name
volumes map[string]upstreamStateVolume
}
type upstreamStateContainer struct {
owner string
attachedNetworks []upstreamStateContainerAttachedNetwork
}
type upstreamStateContainerAttachedNetwork struct {
name string
// Alias hostnames used to talk to this container via this attached network
// Can be empty. Also more than 1 container can have the same alias, and Docker will round-robin them.
aliases []string
}
type upstreamStateImage struct {
owner string
}
type upstreamStateNetwork struct {
owner string
}
type upstreamStateVolume struct {
owner string
}
func (u *upstreamState) ownerLabelContent(owner string) string {
ownerLabel := ""
if owner != "" {
ownerLabel = fmt.Sprintf("\"com.buildkite.sockguard.owner\":\"%s\"", owner)
}
return ownerLabel
}
//////////////
// containers
func (u *upstreamState) createContainer(idOrName string, theOwner string, networks []upstreamStateContainerAttachedNetwork) error {
// Deny if already exists
if u.doesContainerExist(idOrName) == true {
return fmt.Errorf("Cannot create container with ID/Name '%s', already exists", idOrName)
}
// "Create" it
u.containers[idOrName] = upstreamStateContainer{
owner: theOwner,
attachedNetworks: networks,
}
return nil
}
func (u *upstreamState) deleteContainer(idOrName string) error {
// Deny if does not exist
if u.doesContainerExist(idOrName) == false {
return fmt.Errorf("Cannot delete container with ID/Name '%s', does not exist", idOrName)
}
// "Delete" it
delete(u.containers, idOrName)
return nil
}
func (u *upstreamState) doesContainerExist(idOrName string) bool {
_, ok := u.containers[idOrName]
return ok
}
func (u *upstreamState) getContainerOwner(idOrName string) string {
return u.containers[idOrName].owner
}
func (u *upstreamState) getContainerAttachedNetworks(idOrName string) []upstreamStateContainerAttachedNetwork {
return u.containers[idOrName].attachedNetworks
}
//////////////
// images
func (u *upstreamState) createImage(idOrName string, theOwner string) error {
// Deny if already exists
if u.doesImageExist(idOrName) == true {
return fmt.Errorf("Cannot create image with ID/Name '%s', already exists", idOrName)
}
// "Create" it
u.images[idOrName] = upstreamStateImage{
owner: theOwner,
}
return nil
}
func (u *upstreamState) deleteImage(idOrName string) error {
// Deny if does not exist
if u.doesImageExist(idOrName) == false {
return fmt.Errorf("Cannot delete image with ID/Name '%s', does not exist", idOrName)
}
// TODOLATER: images cannot be deleted if a container is using them, add logic if/when test coverage requires it
// "Delete" it
delete(u.images, idOrName)
return nil
}
func (u *upstreamState) doesImageExist(idOrName string) bool {
_, ok := u.images[idOrName]
return ok
}
func (u *upstreamState) getImageOwner(idOrName string) string {
return u.images[idOrName].owner
}
//////////////
// networks
func (u *upstreamState) createNetwork(idOrName string, theOwner string) error {
// Deny if already exists
if _, ok := u.networks[idOrName]; ok {
return fmt.Errorf("Cannot create network with ID/Name '%s', already exists", idOrName)
}
// "Create" it
u.networks[idOrName] = upstreamStateNetwork{
owner: theOwner,
}
return nil
}
func (u *upstreamState) deleteNetwork(idOrName string) error {
// Deny if does not exist
if _, ok := u.networks[idOrName]; ok == false {
return fmt.Errorf("Cannot delete network with ID/Name '%s', does not exist", idOrName)
}
// You can't delete a network that has attached "endpoints" on a real Docker daemon, simulate
// that for containers only for now.
for k1, v1 := range u.containers {
for _, v2 := range v1.attachedNetworks {
if v2.name == idOrName {
return fmt.Errorf("Cannot delete network with ID/Name '%s', endpoint still attached (container '%s')", idOrName, k1)
}
}
}
// "Delete" it
delete(u.networks, idOrName)
return nil
}
func (u *upstreamState) doesNetworkExist(idOrName string) bool {
_, ok := u.networks[idOrName]
return ok
}
func (u *upstreamState) getNetworkOwner(idOrName string) string {
return u.networks[idOrName].owner
}
func (u *upstreamState) networkConnectDisconnectChecks(containerIdOrName string, networkIdOrName string) error {
if _, ok := u.containers[containerIdOrName]; ok == false {
return fmt.Errorf("container does not exist")
}
if _, ok := u.networks[networkIdOrName]; ok == false {
return fmt.Errorf("network does not exist")
}
return nil
}
func (u *upstreamState) isContainerConnectedToNetwork(containerIdOrName string, networkIdOrName string) bool {
// TODOLATER: check the container exists before proceeding? considering what's executing this, skipping duplication for now
for _, v := range u.containers[containerIdOrName].attachedNetworks {
if v.name == networkIdOrName {
return true
}
}
return false
}
func (u *upstreamState) connectContainerToNetwork(containerIdOrName string, networkIdOrName string, containerAliases []string) error {
// Deny if container or network does not exist
if err := u.networkConnectDisconnectChecks(containerIdOrName, networkIdOrName); err != nil {
return fmt.Errorf("Cannot connect container '%s' to network '%s', %s", containerIdOrName, networkIdOrName, err.Error())
}
// Check if container is already attached to this network, if so deny
if u.isContainerConnectedToNetwork(containerIdOrName, networkIdOrName) == true {
return fmt.Errorf("Cannot connect container '%s' to network '%s', already attached", containerIdOrName, networkIdOrName)
}
// "Connect" the container to the network
container := u.containers[containerIdOrName]
containerNetwork := upstreamStateContainerAttachedNetwork{
name: networkIdOrName,
aliases: containerAliases,
}
container.attachedNetworks = append(container.attachedNetworks, containerNetwork)
u.containers[containerIdOrName] = container
return nil
}
func (u *upstreamState) disconnectContainerToNetwork(containerIdOrName string, networkIdOrName string) error {
// Deny if container or network does not exist
if err := u.networkConnectDisconnectChecks(containerIdOrName, networkIdOrName); err != nil {
return fmt.Errorf("Cannot disconnect container '%s' from network '%s', %s", containerIdOrName, networkIdOrName, err.Error())
}
// Check if container is already attached to this network, if not deny
if u.isContainerConnectedToNetwork(containerIdOrName, networkIdOrName) == false {
return fmt.Errorf("Cannot disconnect container '%s' from network '%s', not attached", containerIdOrName, networkIdOrName)
}
// "Disconnect" the container from the network
newAttachedNetworks := []upstreamStateContainerAttachedNetwork{}
for _, v := range u.containers[containerIdOrName].attachedNetworks {
if v.name != networkIdOrName {
newAttachedNetworks = append(newAttachedNetworks, v)
}
}
container := u.containers[containerIdOrName]
container.attachedNetworks = newAttachedNetworks
u.containers[containerIdOrName] = container
return nil
}
//////////////
// volumes
func (u *upstreamState) createVolume(name string, theOwner string) error {
// Deny if already exists
if u.doesVolumeExist(name) == true {
return fmt.Errorf("Cannot create volume with Name '%s', already exists", name)
}
// "Create" it
u.volumes[name] = upstreamStateVolume{
owner: theOwner,
}
return nil
}
func (u *upstreamState) deleteVolume(name string) error {
// Deny if does not exist
if u.doesVolumeExist(name) == false {
return fmt.Errorf("Cannot delete volume with Name '%s', does not exist", name)
}
// "Delete" it
delete(u.volumes, name)
return nil
}
func (u *upstreamState) doesVolumeExist(name string) bool {
_, ok := u.volumes[name]
return ok
}
func (u *upstreamState) getVolumeOwner(name string) string {
return u.volumes[name].owner
}