7658: update connect error test to use stream handler to read the log file, instead...
[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 price = 0.8
28
29 [Logging]
30 file = /dev/null
31 level = DEBUG
32 testlogger = INFO
33 """
34
35     def load_config(self, config=None, config_str=None):
36         if config is None:
37             config = nmconfig.NodeManagerConfig()
38         if config_str is None:
39             config_str = self.TEST_CONFIG
40         with io.StringIO(config_str) as config_fp:
41             config.readfp(config_fp)
42         return config
43
44     def test_seeded_defaults(self):
45         config = nmconfig.NodeManagerConfig()
46         sec_names = set(config.sections())
47         self.assertIn('Arvados', sec_names)
48         self.assertIn('Daemon', sec_names)
49         self.assertFalse(any(name.startswith('Size ') for name in sec_names))
50
51     def test_list_sizes(self):
52         config = self.load_config()
53         client = config.new_cloud_client()
54         sizes = config.node_sizes(client.list_sizes())
55         self.assertEqual(1, len(sizes))
56         size, kwargs = sizes[0]
57         self.assertEqual('Small', size.name)
58         self.assertEqual(1, kwargs['cores'])
59         self.assertEqual(0.8, kwargs['price'])
60
61     def test_shutdown_windows(self):
62         config = self.load_config()
63         self.assertEqual([52, 6, 2], config.shutdown_windows())
64
65     def test_log_levels(self):
66         config = self.load_config()
67         self.assertEqual({'level': logging.DEBUG,
68                           'testlogger': logging.INFO},
69                          config.log_levels())
70
71     def check_dispatch_classes(self, config, module):
72         setup, shutdown, update, monitor = config.dispatch_classes()
73         self.assertIs(setup, module.ComputeNodeSetupActor)
74         self.assertIs(shutdown, module.ComputeNodeShutdownActor)
75         self.assertIs(update, module.ComputeNodeUpdateActor)
76         self.assertIs(monitor, module.ComputeNodeMonitorActor)
77
78     def test_default_dispatch(self):
79         config = self.load_config()
80         self.check_dispatch_classes(config, dispatch)
81
82     def test_custom_dispatch(self):
83         config = self.load_config(
84             config_str=self.TEST_CONFIG + "[Daemon]\ndispatcher=slurm\n")
85         self.check_dispatch_classes(config, slurm_dispatch)