test_ansible.py 5.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""SuperBench Ansible Client test."""

import os
import unittest
import tempfile
from pathlib import Path

from omegaconf import OmegaConf

13
14
15
from superbench.common.utils import LazyImport

AnsibleClient = LazyImport('superbench.runner.ansible', 'AnsibleClient')
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42


class AnsibleClientTestCase(unittest.TestCase):
    """A class for ansible client test cases."""
    def setUp(self):
        """Hook method for setting up the test fixture before exercising it."""
        fd, self.host_file = tempfile.mkstemp()
        os.write(
            fd, (
                'all:\n'
                '  hosts:\n'
                '    10.0.0.10:\n'
                '    10.0.0.11:\n'
                '    10.0.0.12:\n'
                '    10.0.0.13:\n'
                '    10.0.0.14:\n'
            ).encode()
        )
        os.close(fd)

        self.ansible_client = AnsibleClient(
            OmegaConf.create({
                'host_file': self.host_file,
                'host_username': 'user',
                'host_password': 'pass',
            })
        )
43
        _, self.test_mpi_host_file = tempfile.mkstemp()
44
45
46
47

    def tearDown(self):
        """Hook method for deconstructing the test fixture after testing it."""
        Path(self.host_file).unlink()
48
        Path(self.test_mpi_host_file).unlink()
49
50
51
52
53
54

    def test_init_config(self):
        """Test initial config of client."""
        self.assertDictEqual(
            self.ansible_client._config, {
                'host_pattern': 'all',
55
                'cmdline': f'--forks 5 --inventory {self.host_file} --user user --ask-pass --ask-become-pass',
56
57
58
59
60
61
62
                'passwords': {
                    'password': 'pass',
                    'passphrase': 'pass',
                },
            }
        )

Yifan Xiong's avatar
Yifan Xiong committed
63
64
65
66
67
    def test_update_mpi_config(self):
        """Test update_mpi_config of client."""
        self.assertDictEqual(
            self.ansible_client.update_mpi_config(self.ansible_client._config), {
                **self.ansible_client._config,
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                'host_pattern': '10.0.0.10',
            }
        )

    def test_update_mpi_config_for_different_inventory(self):
        """Test update_mpi_config of client for different inventory."""
        # Test for out-of-order
        with open(self.test_mpi_host_file, 'w') as fd:
            fd.write('all:\n  hosts:\n    10.0.0.12:\n    10.0.0.11:\n    10.0.0.10:\n    10.0.0.13:\n    10.0.0.14:\n')
        mess_hosts = AnsibleClient(
            OmegaConf.create(
                {
                    'host_file': self.test_mpi_host_file,
                    'host_username': 'user',
                    'host_password': 'pass',
                }
            )
        )
        self.assertDictEqual(
            mess_hosts.update_mpi_config(mess_hosts._config), {
                **mess_hosts._config,
                'host_pattern': '10.0.0.10',
            }
        )
        # Test for localhost
        with open(self.test_mpi_host_file, 'w') as fd:
            fd.write('all:\n  hosts:\n    localhost:\n')
        localhost = AnsibleClient(
            OmegaConf.create(
                {
                    'host_file': self.test_mpi_host_file,
                    'host_username': 'user',
                    'host_password': 'pass',
                }
            )
        )
        self.assertDictEqual(
            localhost.update_mpi_config(localhost._config), {
                **localhost._config,
                'host_pattern': 'localhost',
            }
        )
        # Test for no host
        with open(self.test_mpi_host_file, 'w') as fd:
            fd.write('all:\n  hosts:\n')
        no_hosts = AnsibleClient(
            OmegaConf.create(
                {
                    'host_file': self.test_mpi_host_file,
                    'host_username': 'user',
                    'host_password': 'pass',
                }
            )
        )
        self.assertDictEqual(
            no_hosts.update_mpi_config(no_hosts._config), {
                **no_hosts._config,
Yifan Xiong's avatar
Yifan Xiong committed
125
126
127
128
                'host_pattern': 'all[0]',
            }
        )

129
130
131
132
133
134
    def test_get_shell_config(self):
        """Test get_shell_config of client."""
        cmd = 'ls -la'
        self.assertDictEqual(
            self.ansible_client.get_shell_config(cmd), {
                'host_pattern': 'all',
135
                'cmdline': f'--forks 5 --inventory {self.host_file} --user user --ask-pass --ask-become-pass',
136
137
138
139
140
141
142
143
144
145
146
147
148
149
                'passwords': {
                    'password': 'pass',
                    'passphrase': 'pass',
                },
                'module': 'shell',
                'module_args': cmd,
            }
        )

    def test_get_playbook_config(self):
        """Test get_playbook_config of client."""
        self.assertDictEqual(
            self.ansible_client.get_playbook_config('play', {'foo': 'bar'}), {
                'host_pattern': 'all',
150
                'cmdline': f'--forks 5 --inventory {self.host_file} --user user --ask-pass --ask-become-pass',
151
152
153
154
155
156
157
158
159
160
                'passwords': {
                    'password': 'pass',
                    'passphrase': 'pass',
                },
                'extravars': {
                    'foo': 'bar',
                },
                'playbook': str(self.ansible_client._playbook_path / 'play'),
            }
        )