Merge branch 'master' into 7255-manifests-in-datamanager
[arvados.git] / services / nodemanager / tests / test_config.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import io
6 import logging
7 import unittest
8
9 import arvnodeman.computenode.dispatch as dispatch
10 import arvnodeman.computenode.dispatch.slurm as slurm_dispatch
11 import arvnodeman.config as nmconfig
12
13 class NodeManagerConfigTestCase(unittest.TestCase):
14     TEST_CONFIG = u"""
15 [Cloud]
16 provider = dummy
17 shutdown_windows = 52, 6, 2
18
19 [Cloud Credentials]
20 creds = dummy_creds
21
22 [Cloud List]
23 [Cloud Create]
24
25 [Size 1]
26 cores = 1
27
28 [Logging]
29 file = /dev/null
30 level = DEBUG
31 testlogger = INFO
32 """
33
34     def load_config(self, config=None, config_str=None):
35         if config is None:
36             config = nmconfig.NodeManagerConfig()
37         if config_str is None:
38             config_str = self.TEST_CONFIG
39         with io.StringIO(config_str) as config_fp:
40             config.readfp(config_fp)
41         return config
42
43     def test_seeded_defaults(self):
44         config = nmconfig.NodeManagerConfig()
45         sec_names = set(config.sections())
46         self.assertIn('Arvados', sec_names)
47         self.assertIn('Daemon', sec_names)
48         self.assertFalse(any(name.startswith('Size ') for name in sec_names))
49
50     def test_list_sizes(self):
51         config = self.load_config()
52         client = config.new_cloud_client()
53         sizes = config.node_sizes(client.list_sizes())
54         self.assertEqual(1, len(sizes))
55         size, kwargs = sizes[0]
56         self.assertEqual('Small', size.name)
57         self.assertEqual(1, kwargs['cores'])
58
59     def test_shutdown_windows(self):
60         config = self.load_config()
61         self.assertEqual([52, 6, 2], config.shutdown_windows())
62
63     def test_log_levels(self):
64         config = self.load_config()
65         self.assertEqual({'level': logging.DEBUG,
66                           'testlogger': logging.INFO},
67                          config.log_levels())
68
69     def check_dispatch_classes(self, config, module):
70         setup, shutdown, update, monitor = config.dispatch_classes()
71         self.assertIs(setup, module.ComputeNodeSetupActor)
72         self.assertIs(shutdown, module.ComputeNodeShutdownActor)
73         self.assertIs(update, module.ComputeNodeUpdateActor)
74         self.assertIs(monitor, module.ComputeNodeMonitorActor)
75
76     def test_default_dispatch(self):
77         config = self.load_config()
78         self.check_dispatch_classes(config, dispatch)
79
80     def test_custom_dispatch(self):
81         config = self.load_config(
82             config_str=self.TEST_CONFIG + "[Daemon]\ndispatcher=slurm\n")
83         self.check_dispatch_classes(config, slurm_dispatch)