summaryrefslogtreecommitdiff
path: root/pkg/bindings/test/pods_test.go
blob: 4bea2f8d7707b5be2b037ed1584bc892b0b03cec (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
package test_bindings

import (
	"context"
	"net/http"
	"time"

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

var _ = Describe("Podman images", func() {
	var (
		bt       *bindingTest
		s        *gexec.Session
		connText context.Context
		newpod   string
		err      error
		trueFlag bool = true
	)

	BeforeEach(func() {
		bt = newBindingTest()
		newpod = "newpod"
		bt.RestoreImagesFromCache()
		bt.Podcreate(&newpod)
		s = bt.startAPIService()
		time.Sleep(1 * time.Second)
		connText, err = bindings.NewConnection(context.Background(), bt.sock)
		Expect(err).To(BeNil())
	})

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

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

		//Inspect an valid pod name
		response, err := pods.Inspect(connText, newpod)
		Expect(err).To(BeNil())
		Expect(response.Config.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(connText, nil)
		Expect(err).To(BeNil())
		Expect(len(podSummary)).To(Equal(1))
		// Adding an alpine container to the existing pod
		bt.RunTopContainer(nil, &trueFlag, &newpod)
		podSummary, err = pods.List(connText, 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(connText, nil)
		Expect(len(podSummary)).To(Equal(2))
		var names []string
		for _, i := range podSummary {
			names = append(names, i.Config.Name)
		}
		Expect(StringInSlice(newpod, names)).To(BeTrue())
		Expect(StringInSlice("newpod2", names)).To(BeTrue())

		// TODO not working  Because: code to list based on filter
		// "not yet implemented",
		// Validate list pod with filters
		//filters := make(map[string][]string)
		//filters["name"] = []string{newpod}
		//filteredPods, err := pods.List(connText, filters)
		//Expect(err).To(BeNil())
		//Expect(len(filteredPods)).To(BeNumerically("==", 1))
	})

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

		// Should exit with no error and response should be true
		response, err = pods.Exists(connText, "newpod")
		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 upause pod", func() {
		// Pause invalid container
		err := pods.Pause(connText, "dummyName")
		Expect(err).ToNot(BeNil())
		code, _ := bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusNotFound))

		// Adding an alpine container to the existing pod
		bt.RunTopContainer(nil, &trueFlag, &newpod)
		response, err := pods.Inspect(connText, newpod)
		Expect(err).To(BeNil())

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

		// Unpause a valid container
		err = pods.Unpause(connText, newpod)
		Expect(err).To(BeNil())
		response, err = pods.Inspect(connText, newpod)
		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(connText, "dummyName")
		Expect(err).ToNot(BeNil())
		code, _ := bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusNotFound))

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

		// Restart an invalid pod
		err = pods.Restart(connText, "dummyName")
		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(connText, newpod)
		Expect(err).To(BeNil())

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

		// Start an already running  pod
		err = pods.Start(connText, newpod)
		Expect(err).To(BeNil())

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

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

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

	// Remove all stopped pods and their container to be implemented.
	It("prune pod", func() {
	})
})