This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
clean_feature_context.go
247 lines (230 loc) · 7.32 KB
/
clean_feature_context.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
package helpers
import (
"context"
"errors"
"fmt"
"io/ioutil"
"path"
"time"
"github.com/DATA-DOG/godog"
execplus "github.com/Originate/go-execplus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/moby/moby/client"
)
func addFile(appDir, serviceFolder, fileName string) error {
filePath := path.Join(appDir, serviceFolder, fileName)
return ioutil.WriteFile(filePath, []byte("test"), 0644)
}
// CleanFeatureContext defines the festure context for features/clean.feature
// nolint gocyclo
func CleanFeatureContext(s *godog.Suite) {
var dockerClient *client.Client
var appNetwork = "cleancontainers"
var testNetwork = "cleancontainerstests"
var thirdPartyContainer = "exosphere-third-party-test-container"
var appContainerProcess *execplus.CmdPlus
var testContainerProcess *execplus.CmdPlus
var thirdPartyContainerProcess *execplus.CmdPlus
s.BeforeSuite(func() {
var err error
dockerClient, err = client.NewEnvClient()
if err != nil {
panic(err)
}
})
s.AfterScenario(func(arg1 interface{}, arg2 error) {
if appContainerProcess != nil {
err := appContainerProcess.Kill()
if err != nil {
panic(err)
}
env := []string{fmt.Sprintf("APP_PATH=%s", appDir)}
_, err = runComposeInNetwork("down", appNetwork, path.Join(appDir, "docker-compose"), "run_development.yml", env)
if err != nil {
panic(err)
}
}
appContainerProcess = nil
if testContainerProcess != nil {
err := testContainerProcess.Kill()
if err != nil {
panic(err)
}
env := []string{fmt.Sprintf("APP_PATH=%s", appDir)}
_, err = runComposeInNetwork("down", testNetwork, path.Join(appDir, "docker-compose"), "run_development.yml", env)
if err != nil {
panic(err)
}
}
testContainerProcess = nil
if thirdPartyContainerProcess != nil {
err := thirdPartyContainerProcess.Kill()
if err != nil {
panic(err)
}
timeout := time.Second * 15
err = dockerClient.ContainerStop(context.Background(), thirdPartyContainer, &timeout)
if err != nil {
panic(err)
}
}
thirdPartyContainerProcess = nil
})
s.Step(`^my machine has both dangling and non-dangling Docker images and volumes$`, func() error {
tempAppDir, err := ioutil.TempDir("", "")
if err != nil {
return err
}
serviceRole := "mongo"
err = CheckoutApp(tempAppDir, "external-dependency")
if err != nil {
return fmt.Errorf("Error checking out app: %v", err)
}
err = runApp(tempAppDir, "MongoDB connected")
if err != nil {
return fmt.Errorf("Error setting up app (first time): %v", err)
}
err = addFile(tempAppDir, serviceRole, "test.txt")
if err != nil {
return fmt.Errorf("Error adding file: %v", err)
}
return killAppContainers(tempAppDir)
})
s.Step(`^my machine has running application containers$`, func() error {
env := []string{fmt.Sprintf("APP_PATH=%s", appDir)}
var err error
appContainerProcess, err = runComposeInNetwork("up", appNetwork, path.Join(appDir, "docker-compose"), "run_development.yml", env)
if err != nil {
return err
}
return appContainerProcess.WaitForText("Creating application-service", time.Second*10)
})
s.Step(`^my machine has running test containers$`, func() error {
env := []string{fmt.Sprintf("APP_PATH=%s", appDir)}
var err error
testContainerProcess, err = runComposeInNetwork("up", testNetwork, path.Join(appDir, "docker-compose"), "test.yml", env)
if err != nil {
return err
}
return testContainerProcess.WaitForText("Creating application-service", time.Second*5)
})
s.Step(`^my machine has stopped application containers$`, func() error {
var err error
env := []string{fmt.Sprintf("APP_PATH=%s", appDir)}
appContainerProcess, err = runComposeInNetwork("create", appNetwork, path.Join(appDir, "docker-compose"), "run_development.yml", env)
if err != nil {
return err
}
return appContainerProcess.WaitForText("Creating application-service", time.Second*5)
})
s.Step(`^my machine has stopped test containers$`, func() error {
var err error
env := []string{fmt.Sprintf("APP_PATH=%s", appDir)}
testContainerProcess, err = runComposeInNetwork("create", testNetwork, path.Join(appDir, "docker-compose"), "test.yml", env)
if err != nil {
return err
}
return testContainerProcess.WaitForText("Creating application-service", time.Second*5)
})
s.Step(`^my machine has running third party containers$`, func() error {
thirdPartyContainerProcess = execplus.NewCmdPlus("docker", "run", "--name", thirdPartyContainer, "--rm", "alpine", "sleep", "1000")
err := thirdPartyContainerProcess.Start()
if err != nil {
return err
}
return waitForContainer(dockerClient, thirdPartyContainer)
})
s.Step(`^it removes application and test containers$`, func() error {
doesHaveAppContainer, err := hasContainer(dockerClient, "app-test-container")
if err != nil {
return err
}
if doesHaveAppContainer {
return errors.New("expected app-test-container to be removed but it wasn't")
}
doesHaveServiceContainer, err := hasContainer(dockerClient, "service-test-container")
if err != nil {
return err
}
if doesHaveServiceContainer {
return errors.New("expected service-test-container to be removed but it wasn't")
}
hasAppNetwork, err := hasNetwork(dockerClient, appNetwork)
if err != nil {
return err
}
if hasAppNetwork {
return fmt.Errorf("expected network '%s' to have been removed", appNetwork)
}
hasTestNetwork, err := hasNetwork(dockerClient, testNetwork)
if err != nil {
return err
}
if hasTestNetwork {
return fmt.Errorf("expected network '%s' to have been removed", testNetwork)
}
return nil
})
s.Step(`^it does not stop any third party containers$`, func() error {
containers, err := listContainersInNetwork(dockerClient, "bridge")
if err != nil {
return err
}
hasContainer := false
for _, container := range containers {
if container == thirdPartyContainer {
hasContainer = true
}
}
if !hasContainer {
return fmt.Errorf("expected third party container '%s' to be running", thirdPartyContainer)
}
return nil
})
s.Step(`^it has non-dangling images$`, func() error {
ctx := context.Background()
filtersArgs := filters.NewArgs()
filtersArgs.Add("dangling", "false")
imageSummaries, err := dockerClient.ImageList(ctx, types.ImageListOptions{
All: false,
Filters: filtersArgs,
})
if err != nil {
return err
}
if len(imageSummaries) == 0 {
return fmt.Errorf("expected non-dangling images but there are none")
}
return nil
})
s.Step(`^it does not have dangling images$`, func() error {
ctx := context.Background()
filtersArgs := filters.NewArgs()
filtersArgs.Add("dangling", "true")
imageSummaries, err := dockerClient.ImageList(ctx, types.ImageListOptions{
All: false,
Filters: filtersArgs,
})
if err != nil {
return err
}
if len(imageSummaries) != 0 {
return fmt.Errorf("expected no dangling images but there are %d", len(imageSummaries))
}
return nil
})
s.Step(`^it does not have dangling volumes$`, func() error {
ctx := context.Background()
filtersArgs := filters.NewArgs()
filtersArgs.Add("dangling", "true")
volumesListOKBody, err := dockerClient.VolumeList(ctx, filtersArgs)
if err != nil {
return err
}
if len(volumesListOKBody.Volumes) != 0 {
return fmt.Errorf("expected no dangling volumes but there are %d", len(volumesListOKBody.Volumes))
}
return nil
})
}