summaryrefslogtreecommitdiff
path: root/ick2/build_tests.py
blob: 47b2db8ae20141af449e7ec585b05e2f01f37ab6 (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
# Copyright (C) 2018  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import unittest


import ick2


class BuildTests(unittest.TestCase):

    def setUp(self):
        as_dict = {
            'status': 'triggered',
        }
        self.resource = ick2.resource_from_dict(as_dict)

    def test_sets_status_from_triggered_only_when_acceptable(self):
        build = ick2.Build(self.resource)

        with self.assertRaises(ick2.WrongBuildStatusChange):
            build.set_status('triggered')
        self.assertEqual(build.get_status(), 'triggered')

        with self.assertRaises(ick2.WrongBuildStatusChange):
            build.set_status('done')
        self.assertEqual(build.get_status(), 'triggered')

        with self.assertRaises(ick2.WrongBuildStatusChange):
            build.set_status('failed')
        self.assertEqual(build.get_status(), 'triggered')

        build.set_status('building')
        self.assertEqual(build.get_status(), 'building')

    def test_refuses_changing_status_from_building_when_unacceptable(self):
        build = ick2.Build(self.resource)
        build.set_status('building')

        with self.assertRaises(ick2.WrongBuildStatusChange):
            build.set_status('triggered')
        self.assertEqual(build.get_status(), 'building')

        with self.assertRaises(ick2.WrongBuildStatusChange):
            build.set_status('building')
        self.assertEqual(build.get_status(), 'building')

    def test_changes_status_from_building_to_done(self):
        build = ick2.Build(self.resource)
        build.set_status('building')
        build.set_status('done')
        self.assertEqual(build.get_status(), 'done')

    def test_changes_status_from_building_to_failed(self):
        build = ick2.Build(self.resource)
        build.set_status('building')
        build.set_status('failed')
        self.assertEqual(build.get_status(), 'failed')

    def test_has_empty_build_graph_initially(self):
        build = ick2.Build(self.resource)
        graph = build.get_graph()
        self.assertTrue(isinstance(graph, ick2.BuildGraph))
        self.assertEqual(graph.get_actions(), {})

    def test_has_build_graph_from_resource(self):
        self.resource['graph'] = {
            1: {
                'action': {'action': 'foo'},
                'status': 'ready',
                'depends': [],
            },
        }
        build = ick2.Build(self.resource)
        graph = build.get_graph()
        self.assertEqual(graph.get_actions(), self.resource['graph'])

    def test_appending_actions_to_build_graph_shows_in_build_resource(self):
        build = ick2.Build(self.resource)
        graph = build.get_graph()
        action_id = graph.append_action({'action': 'foo'})
        self.assertEqual(
            self.resource['graph'],
            {
                action_id: {
                    'action': {'action': 'foo'},
                    'status': 'ready',
                    'depends': [],
                },
            }
        )

    def test_updating_actions_in_build_graph_changes_build_resource(self):
        build = ick2.Build(self.resource)
        graph = build.get_graph()
        action_id = graph.append_action({'action': 'foo'})
        graph.set_action_status(action_id, 'building')
        self.assertEqual(
            self.resource['graph'],
            {
                action_id: {
                    'action': {'action': 'foo'},
                    'status': 'building',
                    'depends': [],
                },
            }
        )