summaryrefslogtreecommitdiff
path: root/contrib/python/test/test_containers.py
blob: 9f6123e0508cc6cc4e58ee3a9ae99886b668be79 (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
import os
import time
import unittest
from test.podman_testcase import PodmanTestCase

import podman
from podman import datetime_parse


class TestContainers(PodmanTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()

    @classmethod
    def tearDownClass(cls):
        super().tearDownClass()

    def setUp(self):
        self.tmpdir = os.environ['TMPDIR']
        self.host = os.environ['PODMAN_HOST']

        self.pclient = podman.Client(self.host)
        self.ctns = self.loadCache()
        # TODO: Change to start() when Implemented
        self.alpine_ctnr.restart()

    def tearDown(self):
        pass

    def loadCache(self):
        with podman.Client(self.host) as pclient:
            self.ctns = list(pclient.containers.list())

        self.alpine_ctnr = next(
            iter([c for c in self.ctns if 'alpine' in c['image']] or []), None)
        return self.ctns

    def test_list(self):
        actual = self.loadCache()
        self.assertGreaterEqual(len(actual), 2)
        self.assertIsNotNone(self.alpine_ctnr)
        self.assertIn('alpine', self.alpine_ctnr.image)

    def test_delete_stopped(self):
        before = self.loadCache()
        self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.stop())
        actual = self.pclient.containers.delete_stopped()
        self.assertIn(self.alpine_ctnr.id, actual)
        after = self.loadCache()

        self.assertLess(len(after), len(before))
        TestContainers.setUpClass()
        self.loadCache()

    def test_create(self):
        with self.assertRaisesNotImplemented():
            self.pclient.containers.create()

    def test_get(self):
        actual = self.pclient.containers.get(self.alpine_ctnr.id)
        for k in ['id', 'status', 'ports']:
            self.assertEqual(actual[k], self.alpine_ctnr[k])

        with self.assertRaises(podman.ContainerNotFound):
            self.pclient.containers.get("bozo")

    def test_attach(self):
        with self.assertRaisesNotImplemented():
            self.alpine_ctnr.attach()

    def test_processes(self):
        actual = list(self.alpine_ctnr.processes())
        self.assertGreaterEqual(len(actual), 2)

    def test_start_stop_wait(self):
        self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.stop())
        self.alpine_ctnr.refresh()
        self.assertFalse(self.alpine_ctnr['running'])

        self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.restart())
        self.alpine_ctnr.refresh()
        self.assertTrue(self.alpine_ctnr.running)

        self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.stop())
        self.alpine_ctnr.refresh()
        self.assertFalse(self.alpine_ctnr['containerrunning'])

        actual = self.alpine_ctnr.wait()
        self.assertEqual(0, actual)

    def test_changes(self):
        actual = self.alpine_ctnr.changes()

        self.assertListEqual(
            sorted(['changed', 'added', 'deleted']), sorted(
                list(actual.keys())))

        # TODO: brittle, depends on knowing history of ctnr
        self.assertGreaterEqual(len(actual['changed']), 2)
        self.assertGreaterEqual(len(actual['added']), 3)
        self.assertEqual(len(actual['deleted']), 0)

    def test_kill(self):
        self.assertTrue(self.alpine_ctnr.running)
        self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.kill(9))
        time.sleep(2)

        self.alpine_ctnr.refresh()
        self.assertFalse(self.alpine_ctnr.running)

    def test_inspect(self):
        actual = self.alpine_ctnr.inspect()
        self.assertEqual(actual.id, self.alpine_ctnr.id)
        self.assertEqual(
            datetime_parse(actual.created),
            datetime_parse(self.alpine_ctnr.createdat))

    def test_export(self):
        target = os.path.join(self.tmpdir, 'alpine_export_ctnr.tar')

        actual = self.alpine_ctnr.export(target)
        self.assertEqual(actual, target)
        self.assertTrue(os.path.isfile(target))
        self.assertGreater(os.path.getsize(target), 0)

    def test_remove(self):
        before = self.loadCache()

        with self.assertRaises(podman.ErrorOccurred):
            self.alpine_ctnr.remove()

        self.assertEqual(
            self.alpine_ctnr.id, self.alpine_ctnr.remove(force=True))
        after = self.loadCache()

        self.assertLess(len(after), len(before))
        TestContainers.setUpClass()
        self.loadCache()

    def test_restart(self):
        self.assertTrue(self.alpine_ctnr.running)
        before = self.alpine_ctnr.runningfor

        self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.restart())

        self.alpine_ctnr.refresh()
        after = self.alpine_ctnr.runningfor
        self.assertTrue(self.alpine_ctnr.running)

        # TODO: restore check when restart zeros counter
        # self.assertLess(after, before)

    def test_rename(self):
        with self.assertRaisesNotImplemented():
            self.alpine_ctnr.rename('new_alpine')

    def test_resize_tty(self):
        with self.assertRaisesNotImplemented():
            self.alpine_ctnr.resize_tty(132, 43)

    def test_pause_unpause(self):
        self.assertTrue(self.alpine_ctnr.running)

        self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.pause())
        self.alpine_ctnr.refresh()
        self.assertFalse(self.alpine_ctnr.running)

        self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.unpause())
        self.alpine_ctnr.refresh()
        self.assertTrue(self.alpine_ctnr.running)

    def test_stats(self):
        self.alpine_ctnr.restart()
        actual = self.alpine_ctnr.stats()
        self.assertEqual(self.alpine_ctnr.id, actual.id)
        self.assertEqual(self.alpine_ctnr.names, actual.name)

    def test_logs(self):
        self.alpine_ctnr.restart()
        actual = list(self.alpine_ctnr.logs())
        self.assertIsNotNone(actual)


if __name__ == '__main__':
    unittest.main()