summaryrefslogtreecommitdiff
path: root/pkg/bindings/test/system_test.go
blob: 76f0b074be9e5c2e86ee68a5fe72d0c66caa22cd (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
package test_bindings

import (
	"time"

	"github.com/containers/libpod/pkg/bindings"
	"github.com/containers/libpod/pkg/bindings/containers"
	"github.com/containers/libpod/pkg/bindings/images"
	"github.com/containers/libpod/pkg/bindings/pods"
	"github.com/containers/libpod/pkg/bindings/system"
	"github.com/containers/libpod/pkg/bindings/volumes"
	"github.com/containers/libpod/pkg/domain/entities"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
)

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

	BeforeEach(func() {
		bt = newBindingTest()
		bt.RestoreImagesFromCache()
		newpod = "newpod"
		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("podman events", func() {
		eChan := make(chan entities.Event, 1)
		var messages []entities.Event
		cancelChan := make(chan bool, 1)
		go func() {
			for e := range eChan {
				messages = append(messages, e)
			}
		}()
		go func() {
			system.Events(bt.conn, eChan, cancelChan, nil, nil, nil)
		}()

		_, err := bt.RunTopContainer(nil, nil, nil)
		Expect(err).To(BeNil())
		cancelChan <- true
		Expect(len(messages)).To(BeNumerically("==", 3))
	})

	It("podman system prune - pod,container stopped", func() {
		// Start and stop a pod to enter in exited state.
		_, err := pods.Start(bt.conn, newpod)
		Expect(err).To(BeNil())
		_, err = pods.Stop(bt.conn, newpod, nil)
		Expect(err).To(BeNil())
		// Start and stop a container to enter in exited state.
		var name = "top"
		_, err = bt.RunTopContainer(&name, &bindings.PFalse, nil)
		Expect(err).To(BeNil())
		err = containers.Stop(bt.conn, name, nil)
		Expect(err).To(BeNil())

		systemPruneResponse, err := system.Prune(bt.conn, &bindings.PTrue, &bindings.PFalse)
		Expect(err).To(BeNil())
		Expect(len(systemPruneResponse.PodPruneReport)).To(Equal(1))
		Expect(len(systemPruneResponse.ContainerPruneReport.ID)).To(Equal(1))
		Expect(len(systemPruneResponse.ImagePruneReport.Report.Id)).
			To(BeNumerically(">", 0))
		Expect(systemPruneResponse.ImagePruneReport.Report.Id).
			To(ContainElement("docker.io/library/alpine:latest"))
		Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(0))
	})

	It("podman system prune running alpine container", func() {
		// Start and stop a pod to enter in exited state.
		_, err := pods.Start(bt.conn, newpod)
		Expect(err).To(BeNil())
		_, err = pods.Stop(bt.conn, newpod, nil)
		Expect(err).To(BeNil())

		// Start and stop a container to enter in exited state.
		var name = "top"
		_, err = bt.RunTopContainer(&name, &bindings.PFalse, nil)
		Expect(err).To(BeNil())
		err = containers.Stop(bt.conn, name, nil)
		Expect(err).To(BeNil())

		// Start container and leave in running
		var name2 = "top2"
		_, err = bt.RunTopContainer(&name2, &bindings.PFalse, nil)
		Expect(err).To(BeNil())

		// Adding an unused volume
		_, err = volumes.Create(bt.conn, entities.VolumeCreateOptions{})
		Expect(err).To(BeNil())

		systemPruneResponse, err := system.Prune(bt.conn, &bindings.PTrue, &bindings.PFalse)
		Expect(err).To(BeNil())
		Expect(len(systemPruneResponse.PodPruneReport)).To(Equal(1))
		Expect(len(systemPruneResponse.ContainerPruneReport.ID)).To(Equal(1))
		Expect(len(systemPruneResponse.ImagePruneReport.Report.Id)).
			To(BeNumerically(">", 0))
		// Alpine image should not be pruned as used by running container
		Expect(systemPruneResponse.ImagePruneReport.Report.Id).
			ToNot(ContainElement("docker.io/library/alpine:latest"))
		// Though unsed volume is available it should not be pruned as flag set to false.
		Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(0))
	})

	It("podman system prune running alpine container volume prune", func() {
		// Start a pod and leave it running
		_, err := pods.Start(bt.conn, newpod)
		Expect(err).To(BeNil())

		// Start and stop a container to enter in exited state.
		var name = "top"
		_, err = bt.RunTopContainer(&name, &bindings.PFalse, nil)
		Expect(err).To(BeNil())
		err = containers.Stop(bt.conn, name, nil)
		Expect(err).To(BeNil())

		// Start second container and leave in running
		var name2 = "top2"
		_, err = bt.RunTopContainer(&name2, &bindings.PFalse, nil)
		Expect(err).To(BeNil())

		// Adding an unused volume should work
		_, err = volumes.Create(bt.conn, entities.VolumeCreateOptions{})
		Expect(err).To(BeNil())

		systemPruneResponse, err := system.Prune(bt.conn, &bindings.PTrue, &bindings.PTrue)
		Expect(err).To(BeNil())
		Expect(len(systemPruneResponse.PodPruneReport)).To(Equal(0))
		Expect(len(systemPruneResponse.ContainerPruneReport.ID)).To(Equal(1))
		Expect(len(systemPruneResponse.ImagePruneReport.Report.Id)).
			To(BeNumerically(">", 0))
		// Alpine image should not be pruned as used by running container
		Expect(systemPruneResponse.ImagePruneReport.Report.Id).
			ToNot(ContainElement("docker.io/library/alpine:latest"))
		// Volume should be pruned now as flag set true
		Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(1))
	})

	It("podman system reset", func() {
		// Adding an unused volume should work
		_, err := volumes.Create(bt.conn, entities.VolumeCreateOptions{})
		Expect(err).To(BeNil())

		vols, err := volumes.List(bt.conn, nil)
		Expect(err).To(BeNil())
		Expect(len(vols)).To(Equal(1))

		// Start a pod and leave it running
		_, err = pods.Start(bt.conn, newpod)
		Expect(err).To(BeNil())

		imageSummary, err := images.List(bt.conn, nil, nil)
		Expect(err).To(BeNil())
		// Since in the begin context images are created
		Expect(len(imageSummary)).To(Equal(3))

		err = system.Reset(bt.conn)
		Expect(err).To(BeNil())

		// re-establish connection
		s = bt.startAPIService()
		time.Sleep(1 * time.Second)

		// No pods
		podSummary, err := pods.List(bt.conn, nil)
		Expect(err).To(BeNil())
		Expect(len(podSummary)).To(Equal(0))

		// No images
		imageSummary, err = images.List(bt.conn, &bindings.PTrue, nil)
		Expect(err).To(BeNil())
		Expect(len(imageSummary)).To(Equal(0))

		// no volumes
		vols, err = volumes.List(bt.conn, nil)
		Expect(err).To(BeNil())
		Expect(len(vols)).To(BeZero())
	})
})