Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(625)

Side by Side Diff: appengine/gce-backend/config_test.py

Issue 2243923002: Refactors GCE backend config updates. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-py@master
Patch Set: . Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « appengine/gce-backend/config.py ('k') | appengine/gce-backend/cron.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright 2016 The LUCI Authors. All rights reserved. 2 # Copyright 2016 The LUCI Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 3 # Use of this source code is governed under the Apache License, Version 2.0
4 # that can be found in the LICENSE file. 4 # that can be found in the LICENSE file.
5 5
6 """Unit tests for config.py.""" 6 """Unit tests for config.py."""
7 7
8 import unittest 8 import unittest
9 9
10 import test_env 10 import test_env
11 test_env.setup_test_env() 11 test_env.setup_test_env()
12 12
13 from components import datastore_utils 13 from components import datastore_utils
14 from test_support import test_case 14 from test_support import test_case
15 15
16 import config 16 import config
17 from proto import config_pb2 17 from proto import config_pb2
18 18
19 19
20 class UpdateConfigTest(test_case.TestCase): 20 class UpdateConfigTest(test_case.TestCase):
21 """Tests for config.update_config.""" 21 """Tests for config.update_template_configs."""
22 22
23 def tearDown(self, *args, **kwargs): 23 def tearDown(self, *args, **kwargs):
24 """Performs post-test case tear-down.""" 24 """Performs post-test case tear-down."""
25 super(UpdateConfigTest, self).tearDown(*args, **kwargs) 25 super(UpdateConfigTest, self).tearDown(*args, **kwargs)
26 26
27 # Even though the datastore resets between test cases and 27 # Even though the datastore resets between test cases and
28 # the underlying entity doesn't persist, the cache does. 28 # the underlying entity doesn't persist, the cache does.
29 config.Configuration.clear_cache() 29 config.Configuration.clear_cache()
30 30
31 def install_mock( 31 def install_mock(
32 self, 32 self,
33 revision=None, 33 revision=None,
34 template_config=None, 34 template_config=None,
35 manager_config=None, 35 manager_config=None,
36 ): 36 ):
37 """Installs a mock for config.config.get_self_config. 37 """Installs a mock for config.config.get_self_config.
38 38
39 Args: 39 Args:
40 template_config: What to return when templates.cfg is requested. Defaults 40 template_config: What to return when templates.cfg is requested. Defaults
41 to an empty config_pb2.InstanceTemplateConfig instance. 41 to an empty config_pb2.InstanceTemplateConfig instance.
42 manager_config: What to return when managers.cfg is requested. Defaults 42 manager_config: What to return when managers.cfg is requested. Defaults
43 to an empty config_pb2.InstanceGroupManagerConfig instance. 43 to an empty config_pb2.InstanceGroupManagerConfig instance.
44 """ 44 """
45 def get(_, path, **kwargs): 45 def get_self_config(path, _, **kwargs):
46 self.assertIn(path, ('templates.cfg', 'managers.cfg')) 46 self.assertIn(path, ('templates.cfg', 'managers.cfg'))
47 if path == 'templates.cfg': 47 if path == 'templates.cfg':
48 proto = template_config or config_pb2.InstanceTemplateConfig() 48 proto = template_config or config_pb2.InstanceTemplateConfig()
49 elif path == 'managers.cfg': 49 elif path == 'managers.cfg':
50 proto = manager_config or config_pb2.InstanceGroupManagerConfig() 50 proto = manager_config or config_pb2.InstanceGroupManagerConfig()
51 return revision or 'mock-revision', proto 51 return revision or 'mock-revision', proto
52 self.mock(config.config, 'get', get) 52 self.mock(config.config, 'get_self_config', get_self_config)
53 53
54 def test_empty_configs(self): 54 def test_empty_configs(self):
55 """Ensures empty configs are successfully stored.""" 55 """Ensures empty configs are successfully stored."""
56 self.install_mock() 56 self.install_mock()
57 57
58 config.update_config() 58 config.update_template_configs()
59 self.failIf(config.Configuration.cached().template_config) 59 self.failIf(config.Configuration.cached().template_config)
60 self.failIf(config.Configuration.cached().manager_config) 60 self.failIf(config.Configuration.cached().manager_config)
61 self.assertEqual(config.Configuration.cached().revision, 'mock-revision') 61 self.assertEqual(config.Configuration.cached().revision, 'mock-revision')
62 62
63 def test_repeated_base_names(self): 63 def test_repeated_base_names(self):
64 """Ensures duplicate base names reject the entire config.""" 64 """Ensures duplicate base names reject the entire config."""
65 template_config = config_pb2.InstanceTemplateConfig( 65 template_config = config_pb2.InstanceTemplateConfig(
66 templates=[ 66 templates=[
67 config_pb2.InstanceTemplateConfig.InstanceTemplate( 67 config_pb2.InstanceTemplateConfig.InstanceTemplate(
68 base_name='base-name-1', 68 base_name='base-name-1',
(...skipping 10 matching lines...) Expand all
79 config_pb2.InstanceTemplateConfig.InstanceTemplate( 79 config_pb2.InstanceTemplateConfig.InstanceTemplate(
80 base_name='base-name-2', 80 base_name='base-name-2',
81 ), 81 ),
82 config_pb2.InstanceTemplateConfig.InstanceTemplate( 82 config_pb2.InstanceTemplateConfig.InstanceTemplate(
83 base_name='base-name-3', 83 base_name='base-name-3',
84 ), 84 ),
85 ], 85 ],
86 ) 86 )
87 self.install_mock(template_config=template_config) 87 self.install_mock(template_config=template_config)
88 88
89 config.update_config() 89 config.update_template_configs()
90 self.failIf(config.Configuration.cached().template_config) 90 self.failIf(config.Configuration.cached().template_config)
91 self.failIf(config.Configuration.cached().manager_config) 91 self.failIf(config.Configuration.cached().manager_config)
92 self.failIf(config.Configuration.cached().revision) 92 self.failIf(config.Configuration.cached().revision)
93 93
94 def test_repeated_zone_different_base_name(self): 94 def test_repeated_zone_different_base_name(self):
95 """Ensures repeated zones in different base names are valid.""" 95 """Ensures repeated zones in different base names are valid."""
96 manager_config = config_pb2.InstanceGroupManagerConfig( 96 manager_config = config_pb2.InstanceGroupManagerConfig(
97 managers=[ 97 managers=[
98 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager( 98 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
99 template_base_name='base-name-1', 99 template_base_name='base-name-1',
100 zone='us-central1-a', 100 zone='us-central1-a',
101 ), 101 ),
102 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager( 102 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
103 template_base_name='base-name-2', 103 template_base_name='base-name-2',
104 zone='us-central1-a', 104 zone='us-central1-a',
105 ), 105 ),
106 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager( 106 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
107 template_base_name='base-name-3', 107 template_base_name='base-name-3',
108 zone='us-central1-a', 108 zone='us-central1-a',
109 ), 109 ),
110 ], 110 ],
111 ) 111 )
112 self.install_mock(manager_config=manager_config) 112 self.install_mock(manager_config=manager_config)
113 113
114 config.update_config() 114 config.update_template_configs()
115 self.failIf(config.Configuration.cached().template_config) 115 self.failIf(config.Configuration.cached().template_config)
116 self.failUnless(config.Configuration.cached().manager_config) 116 self.failUnless(config.Configuration.cached().manager_config)
117 self.assertEqual(config.Configuration.cached().revision, 'mock-revision') 117 self.assertEqual(config.Configuration.cached().revision, 'mock-revision')
118 118
119 def test_repeated_zone_same_base_name(self): 119 def test_repeated_zone_same_base_name(self):
120 """Ensures repeated zones in a base name reject the entire config.""" 120 """Ensures repeated zones in a base name reject the entire config."""
121 manager_config = config_pb2.InstanceGroupManagerConfig( 121 manager_config = config_pb2.InstanceGroupManagerConfig(
122 managers=[ 122 managers=[
123 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager( 123 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
124 template_base_name='base-name-1', 124 template_base_name='base-name-1',
125 zone='us-central1-a', 125 zone='us-central1-a',
126 ), 126 ),
127 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager( 127 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
128 template_base_name='base-name-2', 128 template_base_name='base-name-2',
129 zone='us-central1-b', 129 zone='us-central1-b',
130 ), 130 ),
131 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager( 131 config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
132 template_base_name='base-name-1', 132 template_base_name='base-name-1',
133 zone='us-central1-a', 133 zone='us-central1-a',
134 ), 134 ),
135 ], 135 ],
136 ) 136 )
137 self.install_mock(manager_config=manager_config) 137 self.install_mock(manager_config=manager_config)
138 138
139 config.update_config() 139 config.update_template_configs()
140 self.failIf(config.Configuration.cached().template_config) 140 self.failIf(config.Configuration.cached().template_config)
141 self.failIf(config.Configuration.cached().manager_config) 141 self.failIf(config.Configuration.cached().manager_config)
142 self.failIf(config.Configuration.cached().revision) 142 self.failIf(config.Configuration.cached().revision)
143 143
144 def test_update_configs(self): 144 def test_update_template_configs(self):
145 """Ensures config is updated when revision changes.""" 145 """Ensures config is updated when revision changes."""
146 manager_config = config_pb2.InstanceGroupManagerConfig( 146 manager_config = config_pb2.InstanceGroupManagerConfig(
147 managers=[config_pb2.InstanceGroupManagerConfig.InstanceGroupManager()], 147 managers=[config_pb2.InstanceGroupManagerConfig.InstanceGroupManager()],
148 ) 148 )
149 self.install_mock(revision='revision-1', manager_config=manager_config) 149 self.install_mock(revision='revision-1', manager_config=manager_config)
150 150
151 config.update_config() 151 config.update_template_configs()
152 self.failIf(config.Configuration.cached().template_config) 152 self.failIf(config.Configuration.cached().template_config)
153 self.failUnless(config.Configuration.cached().manager_config) 153 self.failUnless(config.Configuration.cached().manager_config)
154 self.assertEqual(config.Configuration.cached().revision, 'revision-1') 154 self.assertEqual(config.Configuration.cached().revision, 'revision-1')
155 155
156 template_config = config_pb2.InstanceTemplateConfig( 156 template_config = config_pb2.InstanceTemplateConfig(
157 templates=[config_pb2.InstanceTemplateConfig.InstanceTemplate()], 157 templates=[config_pb2.InstanceTemplateConfig.InstanceTemplate()],
158 ) 158 )
159 self.install_mock(revision='revision-2', template_config=template_config) 159 self.install_mock(revision='revision-2', template_config=template_config)
160 160
161 config.update_config() 161 config.update_template_configs()
162 self.failUnless(config.Configuration.cached().template_config) 162 self.failUnless(config.Configuration.cached().template_config)
163 self.failIf(config.Configuration.cached().manager_config) 163 self.failIf(config.Configuration.cached().manager_config)
164 self.assertEqual(config.Configuration.cached().revision, 'revision-2') 164 self.assertEqual(config.Configuration.cached().revision, 'revision-2')
165 165
166 166
167 def test_update_configs_same_revision(self): 167 def test_update_template_configs_same_revision(self):
168 """Ensures config is not updated when revision doesn't change.""" 168 """Ensures config is not updated when revision doesn't change."""
169 manager_config = config_pb2.InstanceGroupManagerConfig( 169 manager_config = config_pb2.InstanceGroupManagerConfig(
170 managers=[config_pb2.InstanceGroupManagerConfig.InstanceGroupManager()], 170 managers=[config_pb2.InstanceGroupManagerConfig.InstanceGroupManager()],
171 ) 171 )
172 self.install_mock(manager_config=manager_config) 172 self.install_mock(manager_config=manager_config)
173 173
174 config.update_config() 174 config.update_template_configs()
175 self.failIf(config.Configuration.cached().template_config) 175 self.failIf(config.Configuration.cached().template_config)
176 self.failUnless(config.Configuration.cached().manager_config) 176 self.failUnless(config.Configuration.cached().manager_config)
177 self.assertEqual(config.Configuration.cached().revision, 'mock-revision') 177 self.assertEqual(config.Configuration.cached().revision, 'mock-revision')
178 178
179 template_config = config_pb2.InstanceTemplateConfig( 179 template_config = config_pb2.InstanceTemplateConfig(
180 templates=[config_pb2.InstanceTemplateConfig.InstanceTemplate()], 180 templates=[config_pb2.InstanceTemplateConfig.InstanceTemplate()],
181 ) 181 )
182 self.install_mock(template_config=template_config) 182 self.install_mock(template_config=template_config)
183 183
184 config.update_config() 184 config.update_template_configs()
185 self.failIf(config.Configuration.cached().template_config) 185 self.failIf(config.Configuration.cached().template_config)
186 self.failUnless(config.Configuration.cached().manager_config) 186 self.failUnless(config.Configuration.cached().manager_config)
187 self.assertEqual(config.Configuration.cached().revision, 'mock-revision') 187 self.assertEqual(config.Configuration.cached().revision, 'mock-revision')
188 188
189 189
190 if __name__ == '__main__': 190 if __name__ == '__main__':
191 unittest.main() 191 unittest.main()
OLDNEW
« no previous file with comments | « appengine/gce-backend/config.py ('k') | appengine/gce-backend/cron.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698