15107: Add LoginCluster test.
[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 [Size 1.preemptible]
33 instance_type = 1
34 preemptible = true
35 cores = 1
36 price = 0.8
37
38 [Logging]
39 file = /dev/null
40 level = DEBUG
41 testlogger = INFO
42 """
43
44     def load_config(self, config=None, config_str=None):
45         if config is None:
46             config = nmconfig.NodeManagerConfig()
47         if config_str is None:
48             config_str = self.TEST_CONFIG
49         with io.StringIO(config_str) as config_fp:
50             config.readfp(config_fp)
51         return config
52
53     def test_seeded_defaults(self):
54         config = nmconfig.NodeManagerConfig()
55         sec_names = set(config.sections())
56         self.assertIn('Arvados', sec_names)
57         self.assertIn('Daemon', sec_names)
58         self.assertFalse(any(name.startswith('Size ') for name in sec_names))
59
60     def test_list_sizes(self):
61         config = self.load_config()
62         sizes = config.node_sizes()
63         self.assertEqual(2, len(sizes))
64         size, kwargs = sizes[0]
65         self.assertEqual('Small', size.name)
66         self.assertEqual(1, kwargs['cores'])
67         self.assertEqual(0.8, kwargs['price'])
68         # preemptible is False by default
69         self.assertEqual(False, kwargs['preemptible'])
70         # instance_type == arvados node size id by default
71         self.assertEqual(kwargs['id'], kwargs['instance_type'])
72         # Now retrieve the preemptible version
73         size, kwargs = sizes[1]
74         self.assertEqual('Small', size.name)
75         self.assertEqual('1.preemptible', kwargs['id'])
76         self.assertEqual(1, kwargs['cores'])
77         self.assertEqual(0.8, kwargs['price'])
78         self.assertEqual(True, kwargs['preemptible'])
79         self.assertEqual('1', kwargs['instance_type'])
80
81
82     def test_default_node_mem_scaling(self):
83         config = self.load_config()
84         self.assertEqual(0.95, config.getfloat('Daemon', 'node_mem_scaling'))
85
86     def test_shutdown_windows(self):
87         config = self.load_config()
88         self.assertEqual([52, 6, 2], config.shutdown_windows())
89
90     def test_log_levels(self):
91         config = self.load_config()
92         self.assertEqual({'level': logging.DEBUG,
93                           'testlogger': logging.INFO},
94                          config.log_levels())
95
96     def check_dispatch_classes(self, config, module):
97         setup, shutdown, update, monitor = config.dispatch_classes()
98         self.assertIs(setup, module.ComputeNodeSetupActor)
99         self.assertIs(shutdown, module.ComputeNodeShutdownActor)
100         self.assertIs(update, module.ComputeNodeUpdateActor)
101         self.assertIs(monitor, module.ComputeNodeMonitorActor)
102
103     def test_default_dispatch(self):
104         config = self.load_config()
105         self.check_dispatch_classes(config, dispatch)
106
107     def test_custom_dispatch(self):
108         config = self.load_config(
109             config_str=self.TEST_CONFIG + "[Daemon]\ndispatcher=slurm\n")
110         self.check_dispatch_classes(config, slurm_dispatch)