OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from infra.services.gnumbd.support import config_ref, git | 5 from infra.libs.git2 import config_ref |
6 from infra.services.gnumbd.test import git_test | |
7 | 6 |
8 class TestConfigRef(git_test.TestBasis): | 7 from infra.libs.git2.test import git2_test |
| 8 |
| 9 META_REF = 'refs/metaconfig' |
| 10 |
| 11 class ExampleRef(config_ref.ConfigRef): |
| 12 CONVERT = { |
| 13 'interval': lambda self, val: float(val), |
| 14 'pending_tag_prefix': lambda self, val: str(val), |
| 15 'pending_ref_prefix': lambda self, val: str(val), |
| 16 'enabled_refglobs': lambda self, val: map(str, list(val)), |
| 17 } |
| 18 DEFAULTS = { |
| 19 'interval': 5.0, |
| 20 'pending_tag_prefix': 'refs/pending-tags', |
| 21 'pending_ref_prefix': 'refs/pending', |
| 22 'enabled_refglobs': [], |
| 23 } |
| 24 REF = META_REF |
| 25 |
| 26 |
| 27 class TestConfigRef(git2_test.TestBasis): |
9 def writeConfig(self, config_data): | 28 def writeConfig(self, config_data): |
10 ref = 'refs/metaconfig' | |
11 def inner(): | 29 def inner(): |
12 g = self.repo.git | 30 g = self.repo.git |
13 if g('rev-parse', ref).stdout.strip() != ref: | 31 if g('rev-parse', META_REF).stdout.strip() != META_REF: |
14 g('checkout', ref) | 32 g('checkout', META_REF) |
15 else: | 33 else: |
16 g('checkout', '--orphan', 'config') | 34 g('checkout', '--orphan', 'config') |
17 g('rm', '-rf', '.') | 35 g('rm', '-rf', '.') |
18 with open('config.json', 'w') as f: | 36 with open('config.json', 'w') as f: |
19 f.write(config_data) | 37 f.write(config_data) |
20 g('add', 'config.json') | 38 g('add', 'config.json') |
21 self.repo.git_commit('a bad config file') | 39 self.repo.git_commit('a bad config file') |
22 g('update-ref', ref, 'HEAD') | 40 g('update-ref', META_REF, 'HEAD') |
23 self.repo.run(inner) | 41 self.repo.run(inner) |
24 | 42 |
25 def testNonExist(self): | 43 def testNonExist(self): |
26 r = self.mkRepo() | 44 r = self.mkRepo() |
27 c = config_ref.ConfigRef(git.Ref(r, 'refs/metaconfig')) | 45 c = ExampleRef(r) |
28 self.assertEqual(c.current, c.DEFAULTS) | 46 self.assertEqual(c.current, c.DEFAULTS) |
29 self.assertEqual(c['interval'], c.DEFAULTS['interval']) | 47 self.assertEqual(c['interval'], c.DEFAULTS['interval']) |
30 | 48 |
31 def testExistsBad(self): | 49 def testExistsBad(self): |
32 self.writeConfig("not valid config") | 50 self.writeConfig("not valid config") |
33 r = self.mkRepo() | 51 r = self.mkRepo() |
34 c = config_ref.ConfigRef(git.Ref(r, 'refs/metaconfig')) | 52 c = ExampleRef(r) |
35 c.evaluate() | 53 c.evaluate() |
36 self.assertEqual(c.current, c.DEFAULTS) | 54 self.assertEqual(c.current, c.DEFAULTS) |
37 | 55 |
38 self.writeConfig("[]") | 56 self.writeConfig("[]") |
39 self.capture_stdio(r.run, 'fetch') | 57 self.capture_stdio(r.run, 'fetch') |
40 c.evaluate() | 58 c.evaluate() |
41 self.assertEqual(c.current, c.DEFAULTS) | 59 self.assertEqual(c.current, c.DEFAULTS) |
42 | 60 |
43 def testExistsGood(self): | 61 def testExistsGood(self): |
44 self.writeConfig('{"interval": 100}') | 62 self.writeConfig('{"interval": 100}') |
45 r = self.mkRepo() | 63 r = self.mkRepo() |
46 c = config_ref.ConfigRef(git.Ref(r, 'refs/metaconfig')) | 64 c = ExampleRef(r) |
47 self.assertAlmostEqual(c['interval'], 100.0) | 65 self.assertAlmostEqual(c['interval'], 100.0) |
48 | 66 |
49 self.writeConfig('{"interval": "cat"}') | 67 self.writeConfig('{"interval": "cat"}') |
50 self.capture_stdio(r.run, 'fetch') | 68 self.capture_stdio(r.run, 'fetch') |
51 c.evaluate() | 69 c.evaluate() |
52 self.assertAlmostEqual(c['interval'], 100.0) | 70 self.assertAlmostEqual(c['interval'], 100.0) |
OLD | NEW |