summaryrefslogtreecommitdiff
path: root/pkg/bindings/test/pods_test.go
blob: b06ff31a2d7d22d813d0754f1f2e25060e193508 (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package test_bindings

import (
	"net/http"
	"strings"
	"time"

	"github.com/containers/podman/v3/libpod/define"
	"github.com/containers/podman/v3/pkg/bindings"
	"github.com/containers/podman/v3/pkg/bindings/pods"
	"github.com/containers/podman/v3/pkg/specgen"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("Podman pods", func() {
	var (
		bt     *bindingTest
		s      *gexec.Session
		newpod string
		err    error
	)

	BeforeEach(func() {
		bt = newBindingTest()
		newpod = "newpod"
		bt.RestoreImagesFromCache()
		bt.Podcreate(&newpod)
		s = bt.startAPIService()
		time.Sleep(1 * time.Second)
		err := bt.NewConnection()
		Expect(err).To(BeNil())
	})

	AfterEach(func() {
		s.Kill()
		bt.cleanup()
	})

	It("inspect pod", func() {
		//Inspect an invalid pod name
		_, err := pods.Inspect(bt.conn, "dummyname", nil)
		Expect(err).ToNot(BeNil())
		code, _ := bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusNotFound))

		//Inspect an valid pod name
		response, err := pods.Inspect(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		Expect(response.Name).To(Equal(newpod))
	})

	// Test validates the list all api returns
	It("list pod", func() {
		//List all the pods in the current instance
		podSummary, err := pods.List(bt.conn, nil)
		Expect(err).To(BeNil())
		Expect(len(podSummary)).To(Equal(1))

		// Start the pod
		_, err = pods.Start(bt.conn, newpod, nil)
		Expect(err).To(BeNil())

		// Adding an alpine container to the existing pod
		_, err = bt.RunTopContainer(nil, &newpod)
		Expect(err).To(BeNil())
		podSummary, err = pods.List(bt.conn, nil)
		// Verify no errors.
		Expect(err).To(BeNil())
		// Verify number of containers in the pod.
		Expect(len(podSummary[0].Containers)).To(Equal(2))

		// Add multiple pods and verify them by name and size.
		var newpod2 string = "newpod2"
		bt.Podcreate(&newpod2)
		podSummary, err = pods.List(bt.conn, nil)
		Expect(len(podSummary)).To(Equal(2))
		var names []string
		for _, i := range podSummary {
			names = append(names, i.Name)
		}
		Expect(StringInSlice(newpod, names)).To(BeTrue())
		Expect(StringInSlice("newpod2", names)).To(BeTrue())
	})

	// The test validates the list pod endpoint with passing filters as the params.
	It("List pods with filters", func() {
		newpod2 := "newpod2"
		bt.Podcreate(&newpod2)

		// Start the pod
		_, err = pods.Start(bt.conn, newpod, nil)
		Expect(err).To(BeNil())

		_, err = bt.RunTopContainer(nil, &newpod)
		Expect(err).To(BeNil())

		// Expected err with invalid filter params
		filters := make(map[string][]string)
		filters["dummy"] = []string{"dummy"}
		options := new(pods.ListOptions).WithFilters(filters)
		filteredPods, err := pods.List(bt.conn, options)
		Expect(err).ToNot(BeNil())
		code, _ := bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusInternalServerError))

		// Expected empty response with invalid filters
		filters = make(map[string][]string)
		filters["name"] = []string{"dummy"}
		options = new(pods.ListOptions).WithFilters(filters)
		filteredPods, err = pods.List(bt.conn, options)
		Expect(err).To(BeNil())
		Expect(len(filteredPods)).To(BeNumerically("==", 0))

		// Validate list pod with name filter
		filters = make(map[string][]string)
		filters["name"] = []string{newpod2}
		options = new(pods.ListOptions).WithFilters(filters)
		filteredPods, err = pods.List(bt.conn, options)
		Expect(err).To(BeNil())
		Expect(len(filteredPods)).To(BeNumerically("==", 1))
		var names []string
		for _, i := range filteredPods {
			names = append(names, i.Name)
		}
		Expect(StringInSlice("newpod2", names)).To(BeTrue())

		// Validate list pod with id filter
		filters = make(map[string][]string)
		response, err := pods.Inspect(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		id := response.ID
		filters["id"] = []string{id}
		options = new(pods.ListOptions).WithFilters(filters)
		filteredPods, err = pods.List(bt.conn, options)
		Expect(err).To(BeNil())
		Expect(len(filteredPods)).To(BeNumerically("==", 1))
		names = names[:0]
		for _, i := range filteredPods {
			names = append(names, i.Name)
		}
		Expect(StringInSlice("newpod", names)).To(BeTrue())

		// Using multiple filters
		filters["name"] = []string{newpod}
		options = new(pods.ListOptions).WithFilters(filters)
		filteredPods, err = pods.List(bt.conn, options)
		Expect(err).To(BeNil())
		Expect(len(filteredPods)).To(BeNumerically("==", 1))
		names = names[:0]
		for _, i := range filteredPods {
			names = append(names, i.Name)
		}
		Expect(StringInSlice("newpod", names)).To(BeTrue())
	})

	// The test validates if the exists responds
	It("exists pod", func() {
		response, err := pods.Exists(bt.conn, "dummyName", nil)
		Expect(err).To(BeNil())
		Expect(response).To(BeFalse())

		// Should exit with no error and response should be true
		response, err = pods.Exists(bt.conn, "newpod", nil)
		Expect(err).To(BeNil())
		Expect(response).To(BeTrue())
	})

	// This test validates if All running containers within
	// each specified pod are paused and unpaused
	It("pause unpause pod", func() {
		// TODO fix this
		Skip("Pod behavior is jacked right now.")
		// Pause invalid container
		_, err := pods.Pause(bt.conn, "dummyName", nil)
		Expect(err).ToNot(BeNil())
		code, _ := bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusNotFound))

		// Adding an alpine container to the existing pod
		_, err = bt.RunTopContainer(nil, &newpod)
		Expect(err).To(BeNil())

		// Binding needs to be modified to inspect the pod state.
		// Since we don't have a pod state we inspect the states of the containers within the pod.
		// Pause a valid container
		_, err = pods.Pause(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		response, err := pods.Inspect(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		Expect(response.State).To(Equal(define.PodStatePaused))
		for _, i := range response.Containers {
			Expect(define.StringToContainerStatus(i.State)).
				To(Equal(define.ContainerStatePaused))
		}

		// Unpause a valid container
		_, err = pods.Unpause(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		response, err = pods.Inspect(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		Expect(response.State).To(Equal(define.PodStateRunning))
		for _, i := range response.Containers {
			Expect(define.StringToContainerStatus(i.State)).
				To(Equal(define.ContainerStateRunning))
		}
	})

	It("start stop restart pod", func() {
		// Start an invalid pod
		_, err = pods.Start(bt.conn, "dummyName", nil)
		Expect(err).ToNot(BeNil())
		code, _ := bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusNotFound))

		// Stop an invalid pod
		_, err = pods.Stop(bt.conn, "dummyName", nil)
		Expect(err).ToNot(BeNil())
		code, _ = bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusNotFound))

		// Restart an invalid pod
		_, err = pods.Restart(bt.conn, "dummyName", nil)
		Expect(err).ToNot(BeNil())
		code, _ = bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusNotFound))

		// Start a valid pod and inspect status of each container
		_, err = pods.Start(bt.conn, newpod, nil)
		Expect(err).To(BeNil())

		response, err := pods.Inspect(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		Expect(response.State).To(Equal(define.PodStateRunning))
		for _, i := range response.Containers {
			Expect(define.StringToContainerStatus(i.State)).
				To(Equal(define.ContainerStateRunning))
		}

		// Start an already running  pod
		_, err = pods.Start(bt.conn, newpod, nil)
		Expect(err).To(BeNil())

		// Stop the running pods
		_, err = pods.Stop(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		response, _ = pods.Inspect(bt.conn, newpod, nil)
		Expect(response.State).To(Equal(define.PodStateExited))
		for _, i := range response.Containers {
			Expect(define.StringToContainerStatus(i.State)).
				To(Equal(define.ContainerStateExited))
		}

		// Stop an already stopped pod
		_, err = pods.Stop(bt.conn, newpod, nil)
		Expect(err).To(BeNil())

		_, err = pods.Restart(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		response, _ = pods.Inspect(bt.conn, newpod, nil)
		Expect(response.State).To(Equal(define.PodStateRunning))
		for _, i := range response.Containers {
			Expect(define.StringToContainerStatus(i.State)).
				To(Equal(define.ContainerStateRunning))
		}
	})

	// Test to validate all the pods in the stopped/exited state are pruned successfully.
	It("prune pod", func() {
		// Add a new pod
		var newpod2 string = "newpod2"
		bt.Podcreate(&newpod2)
		// No pods pruned since no pod in exited state
		pruneResponse, err := pods.Prune(bt.conn, nil)
		Expect(err).To(BeNil())
		podSummary, err := pods.List(bt.conn, nil)
		Expect(err).To(BeNil())
		Expect(len(podSummary)).To(Equal(2))

		// Prune only one pod which is in exited state.
		// Start then stop a pod.
		// pod moves to exited state one pod should be pruned now.
		_, err = pods.Start(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		_, err = pods.Stop(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		response, err := pods.Inspect(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		Expect(response.State).To(Equal(define.PodStateExited))
		pruneResponse, err = pods.Prune(bt.conn, nil)
		Expect(err).To(BeNil())
		// Validate status and record pod id of pod to be pruned
		Expect(response.State).To(Equal(define.PodStateExited))
		podID := response.ID
		// Check if right pod was pruned
		Expect(len(pruneResponse)).To(Equal(1))
		Expect(pruneResponse[0].Id).To(Equal(podID))
		// One pod is pruned hence only one pod should be active.
		podSummary, err = pods.List(bt.conn, nil)
		Expect(err).To(BeNil())
		Expect(len(podSummary)).To(Equal(1))

		// Test prune multiple pods.
		bt.Podcreate(&newpod)
		_, err = pods.Start(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		_, err = pods.Start(bt.conn, newpod2, nil)
		Expect(err).To(BeNil())
		_, err = pods.Stop(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		response, err = pods.Inspect(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		Expect(response.State).To(Equal(define.PodStateExited))
		for _, i := range response.Containers {
			Expect(define.StringToContainerStatus(i.State)).
				To(Equal(define.ContainerStateExited))
		}
		_, err = pods.Stop(bt.conn, newpod2, nil)
		Expect(err).To(BeNil())
		response, err = pods.Inspect(bt.conn, newpod2, nil)
		Expect(err).To(BeNil())
		Expect(response.State).To(Equal(define.PodStateExited))
		for _, i := range response.Containers {
			Expect(define.StringToContainerStatus(i.State)).
				To(Equal(define.ContainerStateExited))
		}
		_, err = pods.Prune(bt.conn, nil)
		Expect(err).To(BeNil())
		podSummary, err = pods.List(bt.conn, nil)
		Expect(err).To(BeNil())
		Expect(len(podSummary)).To(Equal(0))
	})

	It("simple create pod", func() {
		ps := specgen.PodSpecGenerator{}
		ps.Name = "foobar"
		_, err := pods.CreatePodFromSpec(bt.conn, &ps, nil)
		Expect(err).To(BeNil())

		exists, err := pods.Exists(bt.conn, "foobar", nil)
		Expect(err).To(BeNil())
		Expect(exists).To(BeTrue())
	})

	// Test validates the pod top bindings
	It("pod top", func() {
		var name string = "podA"

		bt.Podcreate(&name)
		_, err := pods.Start(bt.conn, name, nil)
		Expect(err).To(BeNil())

		// By name
		_, err = pods.Top(bt.conn, name, nil)
		Expect(err).To(BeNil())

		// With descriptors
		options := new(pods.TopOptions).WithDescriptors([]string{"user,pid,hpid"})
		output, err := pods.Top(bt.conn, name, options)
		Expect(err).To(BeNil())
		header := strings.Split(output[0], "\t")
		for _, d := range []string{"USER", "PID", "HPID"} {
			Expect(d).To(BeElementOf(header))
		}

		// With bogus ID
		_, err = pods.Top(bt.conn, "IdoNotExist", nil)
		Expect(err).ToNot(BeNil())

		// With bogus descriptors
		options = new(pods.TopOptions).WithDescriptors([]string{"Me,Neither"})
		_, err = pods.Top(bt.conn, name, options)
		Expect(err).ToNot(BeNil())
	})
})