Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / tasks / tests / test_buildpackages.py
1 # py.test -v -s tests/test_buildpackages.py
2
3 from mock import patch, Mock
4
5 from .. import buildpackages
6 from teuthology import packaging
7
8 def test_get_tag_branch_sha1():
9     gitbuilder = packaging.GitbuilderProject(
10         'ceph',
11         {
12             'os_type': 'centos',
13             'os_version': '7.0',
14         })
15     (tag, branch, sha1) = buildpackages.get_tag_branch_sha1(gitbuilder)
16     assert tag == None
17     assert branch == None
18     assert sha1 is not None
19
20     gitbuilder = packaging.GitbuilderProject(
21         'ceph',
22         {
23             'os_type': 'centos',
24             'os_version': '7.0',
25             'sha1': 'asha1',
26         })
27     (tag, branch, sha1) = buildpackages.get_tag_branch_sha1(gitbuilder)
28     assert tag == None
29     assert branch == None
30     assert sha1 == 'asha1'
31
32     remote = Mock
33     remote.arch = 'x86_64'
34     remote.os = Mock
35     remote.os.name = 'ubuntu'
36     remote.os.version = '14.04'
37     remote.os.codename = 'trusty'
38     remote.system_type = 'deb'
39     ctx = Mock
40     ctx.cluster = Mock
41     ctx.cluster.remotes = {remote: ['client.0']}
42
43     expected_tag = 'v0.94.1'
44     expected_sha1 = 'expectedsha1'
45     def check_output(cmd, shell):
46         assert shell == True
47         return expected_sha1 + " refs/tags/" + expected_tag
48     with patch.multiple(
49             buildpackages,
50             check_output=check_output,
51     ):
52         gitbuilder = packaging.GitbuilderProject(
53             'ceph',
54             {
55                 'os_type': 'centos',
56                 'os_version': '7.0',
57                 'sha1': 'asha1',
58                 'all': {
59                     'tag': tag,
60                 },
61             },
62             ctx = ctx,
63             remote = remote)
64         (tag, branch, sha1) = buildpackages.get_tag_branch_sha1(gitbuilder)
65         assert tag == expected_tag
66         assert branch == None
67         assert sha1 == expected_sha1
68
69     expected_branch = 'hammer'
70     expected_sha1 = 'otherexpectedsha1'
71     def check_output(cmd, shell):
72         assert shell == True
73         return expected_sha1 + " refs/heads/" + expected_branch
74     with patch.multiple(
75             buildpackages,
76             check_output=check_output,
77     ):
78         gitbuilder = packaging.GitbuilderProject(
79             'ceph',
80             {
81                 'os_type': 'centos',
82                 'os_version': '7.0',
83                 'sha1': 'asha1',
84                 'all': {
85                     'branch': branch,
86                 },
87             },
88             ctx = ctx,
89             remote = remote)
90         (tag, branch, sha1) = buildpackages.get_tag_branch_sha1(gitbuilder)
91         assert tag == None
92         assert branch == expected_branch
93         assert sha1 == expected_sha1
94
95 def test_lookup_configs():
96     expected_system_type = 'deb'
97     def make_remote():
98         remote = Mock()
99         remote.arch = 'x86_64'
100         remote.os = Mock()
101         remote.os.name = 'ubuntu'
102         remote.os.version = '14.04'
103         remote.os.codename = 'trusty'
104         remote.system_type = expected_system_type
105         return remote
106     ctx = Mock()
107     class cluster:
108         remote1 = make_remote()
109         remote2 = make_remote()
110         remotes = {
111             remote1: ['client.0'],
112             remote2: ['mon.a','osd.0'],
113         }
114         def only(self, role):
115             result = Mock()
116             if role in ('client.0',):
117                 result.remotes = { cluster.remote1: None }
118             elif role in ('osd.0', 'mon.a'):
119                 result.remotes = { cluster.remote2: None }
120             else:
121                 result.remotes = None
122             return result
123     ctx.cluster = cluster()
124     ctx.config = {
125         'roles': [ ['client.0'], ['mon.a','osd.0'] ],
126     }
127
128     # nothing -> nothing
129     assert buildpackages.lookup_configs(ctx, {}) == []
130     assert buildpackages.lookup_configs(ctx, {1:[1,2,3]}) == []
131     assert buildpackages.lookup_configs(ctx, [[1,2,3]]) == []
132     assert buildpackages.lookup_configs(ctx, None) == []
133
134     #
135     # the overrides applies to install and to install.upgrade
136     # that have no tag, branch or sha1
137     #
138     config = {
139         'overrides': {
140             'install': {
141                 'ceph': {
142                     'sha1': 'overridesha1',
143                     'tag': 'overridetag',
144                     'branch': 'overridebranch',
145                 },
146             },
147         },
148         'tasks': [
149             {
150                 'install': {
151                     'sha1': 'installsha1',
152                 },
153             },
154             {
155                 'install.upgrade': {
156                     'osd.0': {
157                     },
158                     'client.0': {
159                         'sha1': 'client0sha1',
160                     },
161                 },
162             }
163         ],
164     }
165     ctx.config = config
166     expected_configs = [{'branch': 'overridebranch', 'sha1': 'overridesha1', 'tag': 'overridetag'},
167                         {'project': 'ceph', 'branch': 'overridebranch', 'sha1': 'overridesha1', 'tag': 'overridetag'},
168                         {'project': 'ceph', 'sha1': 'client0sha1'}]
169
170     assert buildpackages.lookup_configs(ctx, config) == expected_configs