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