aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unittests/juju/juju_cluster_tests.py
blob: 1c8054f69df553808109bf57090e4ce96540cbfa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# Copyright (c) 2023 Canonical Ltd., Chi Wai Chan <chiwai.chan@canonical.com>

# This file is part of the sos project: https://github.com/sosreport/sos
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# version 2 of the GNU General Public License.
#
# See the LICENSE file in the source distribution for further information.
import json
import pathlib
import unittest
from unittest.mock import call, patch

from sos.collector.clusters.juju import _parse_option_string, juju, _get_index
from sos.options import ClusterOption


class MockOptions:

    def __init__(self):
        self.cluster_options = []


def get_juju_output(model):
    dir = pathlib.Path(__file__).parent.resolve()
    with open(dir / "data" / f"juju_output_{model}.json") as f:
        return f.read()


def get_juju_status(cmd):
    if "-m" in cmd:
        model = cmd.split()[3]
    else:
        model = "sos"

    return {
        "status": 0,
        "output": get_juju_output(model),
    }


def get_juju_version():
    return "2.9.45"


def test_parse_option_string():
    result = _parse_option_string("    a,b,c")
    assert result == ["a", "b", "c"]

    result = _parse_option_string()
    assert result == []


class JujuTest(unittest.TestCase):
    """Test for juju cluster."""

    @patch(
        "sos.collector.clusters.juju.juju.exec_primary_cmd",
        side_effect=get_juju_status,
    )
    def test_get_nodes_no_filter(self, mock_exec_primary_cmd):
        """No filter."""
        mock_opts = MockOptions()
        cluster = juju(
            commons={
                "tmpdir": "/tmp",
                "cmdlineopts": mock_opts,
            }
        )
        nodes = cluster.get_nodes()
        assert nodes == []

    @patch(
        "sos.collector.clusters.juju.juju._get_juju_version",
        side_effect=get_juju_version,
    )
    @patch(
        "sos.collector.clusters.juju.juju.exec_primary_cmd",
        side_effect=get_juju_status,
    )
    def test_get_nodes_app_filter(
        self, mock_exec_primary_cmd, mock_get_juju_version
    ):
        """Application filter."""
        mock_opts = MockOptions()
        mock_opts.cluster_options.append(
            ClusterOption(
                name="apps",
                opt_type=str,
                value="ubuntu",
                cluster=juju.__name__,
            )
        )
        cluster = juju(
            commons={
                "tmpdir": "/tmp",
                "cmdlineopts": mock_opts,
            }
        )
        nodes = cluster.get_nodes()
        nodes.sort()
        assert nodes == [":0", ":2", ":3"]
        mock_exec_primary_cmd.assert_called_once_with(
            "juju status  --format json"
        )

    @patch(
        "sos.collector.clusters.juju.juju._get_juju_version",
        side_effect=get_juju_version,
    )
    @patch(
        "sos.collector.clusters.juju.juju.exec_primary_cmd",
        side_effect=get_juju_status,
    )
    def test_get_nodes_app_regex_filter(
        self, mock_exec_primary_cmd, mock_get_juju_version
    ):
        """Application filter."""
        mock_opts = MockOptions()
        mock_opts.cluster_options.append(
            ClusterOption(
                name="apps",
                opt_type=str,
                value="ubuntu|nginx",
                cluster=juju.__name__,
            )
        )
        cluster = juju(
            commons={
                "tmpdir": "/tmp",
                "cmdlineopts": mock_opts,
            }
        )
        nodes = cluster.get_nodes()
        nodes.sort()
        assert nodes == [":0", ":2", ":3", ":4"]
        mock_exec_primary_cmd.assert_called_once_with(
            "juju status  --format json"
        )

    @patch(
        "sos.collector.clusters.juju.juju._get_juju_version",
        side_effect=get_juju_version,
    )
    @patch(
        "sos.collector.clusters.juju.juju.exec_primary_cmd",
        side_effect=get_juju_status,
    )
    def test_get_nodes_model_filter_multiple_models(
        self, mock_exec_primary_cmd, mock_get_juju_version
    ):
        """Multiple model filter."""
        mock_opts = MockOptions()
        mock_opts.cluster_options.append(
            ClusterOption(
                name="models",
                opt_type=str,
                value="sos,sos2",
                cluster=juju.__name__,
            ),
        )
        mock_opts.cluster_options.append(
            ClusterOption(
                name="apps",
                opt_type=str,
                value="ubuntu",
                cluster=juju.__name__,
            ),
        )
        cluster = juju(
            commons={
                "tmpdir": "/tmp",
                "cmdlineopts": mock_opts,
            }
        )
        nodes = cluster.get_nodes()
        nodes.sort()
        assert nodes == [
            "sos2:0",
            "sos2:1",
            "sos:0",
            "sos:2",
            "sos:3",
        ]
        mock_exec_primary_cmd.assert_has_calls(
            [
                call("juju status -m sos --format json"),
                call("juju status -m sos2 --format json"),
            ]
        )

    @patch(
        "sos.collector.clusters.juju.juju._get_juju_version",
        side_effect=get_juju_version,
    )
    @patch(
        "sos.collector.clusters.juju.juju.exec_primary_cmd",
        side_effect=get_juju_status,
    )
    def test_get_nodes_model_filter(
        self, mock_exec_primary_cmd, mock_get_juju_version
    ):
        """Model filter."""
        mock_opts = MockOptions()
        mock_opts.cluster_options.append(
            ClusterOption(
                name="models",
                opt_type=str,
                value="sos",
                cluster=juju.__name__,
            )
        )
        mock_opts.cluster_options.append(
            ClusterOption(
                name="apps",
                opt_type=str,
                value="ubuntu",
                cluster=juju.__name__,
            ),
        )
        cluster = juju(
            commons={
                "tmpdir": "/tmp",
                "cmdlineopts": mock_opts,
            }
        )
        nodes = cluster.get_nodes()
        nodes.sort()
        assert nodes == [
            "sos:0",
            "sos:2",
            "sos:3",
        ]
        mock_exec_primary_cmd.assert_has_calls(
            [
                call("juju status -m sos --format json"),
            ]
        )

    @patch(
        "sos.collector.clusters.juju.juju._get_juju_version",
        side_effect=get_juju_version,
    )
    @patch(
        "sos.collector.clusters.juju.juju.exec_primary_cmd",
        side_effect=get_juju_status,
    )
    def test_get_nodes_unit_filter(
        self, mock_exec_primary_cmd, mock_get_juju_version
    ):
        """Node filter."""
        mock_opts = MockOptions()
        mock_opts.cluster_options.append(
            ClusterOption(
                name="units",
                opt_type=str,
                value="ubuntu/0,ubuntu/1",
                cluster=juju.__name__,
            )
        )
        cluster = juju(
            commons={
                "tmpdir": "/tmp",
                "cmdlineopts": mock_opts,
            }
        )
        nodes = cluster.get_nodes()
        nodes.sort()
        assert nodes == [":0", ":2"]

    @patch(
        "sos.collector.clusters.juju.juju._get_juju_version",
        side_effect=get_juju_version,
    )
    @patch(
        "sos.collector.clusters.juju.juju.exec_primary_cmd",
        side_effect=get_juju_status,
    )
    def test_get_nodes_machine_filter(
        self, mock_exec_primary_cmd, mock_get_juju_version
    ):
        """Machine filter."""
        mock_opts = MockOptions()
        mock_opts.cluster_options.append(
            ClusterOption(
                name="machines",
                opt_type=str,
                value="0,2",
                cluster=juju.__name__,
            )
        )
        cluster = juju(
            commons={
                "tmpdir": "/tmp",
                "cmdlineopts": mock_opts,
            }
        )
        nodes = cluster.get_nodes()
        nodes.sort()
        print(nodes)
        assert nodes == [":0", ":2"]

    @patch(
        "sos.collector.clusters.juju.juju._get_juju_version",
        side_effect=get_juju_version,
    )
    @patch(
        "sos.collector.clusters.juju.juju.exec_primary_cmd",
        side_effect=get_juju_status,
    )
    def test_subordinates(self, mock_exec_primary_cmd, mock_get_juju_version):
        """Subordinate filter."""
        mock_opts = MockOptions()
        mock_opts.cluster_options.append(
            ClusterOption(
                name="apps",
                opt_type=str,
                value="nrpe",
                cluster=juju.__name__,
            )
        )
        cluster = juju(
            commons={
                "tmpdir": "/tmp",
                "cmdlineopts": mock_opts,
            }
        )
        nodes = cluster.get_nodes()
        nodes.sort()
        assert nodes == [":0", ":2", ":3"]
        mock_exec_primary_cmd.assert_called_once_with(
            "juju status  --format json"
        )


class IndexTest(unittest.TestCase):

    def test_subordinate_parent_miss_units(self):
        """Fix if subordinate's parent is missing units."""
        model = "sos"
        index = _get_index(model_name=model)

        juju_status = json.loads(get_juju_output(model=model))
        juju_status["applications"]["ubuntu"].pop("units")

        # Ensure these commands won't fall even when
        # subordinate's parent's units is missing.
        index.add_principals(juju_status)
        index.add_subordinates(juju_status)

    def test_subordinate_miss_parent(self):
        """Fix if subordinate is missing parent."""
        model = "sos"
        index = _get_index(model_name=model)

        juju_status = json.loads(get_juju_output(model=model))
        index.add_principals(juju_status)

        index.apps.pop("ubuntu")
        # Ensure command won't fall even when
        # subordinate's parent is missing
        index.add_subordinates(juju_status)


# vim: set et ts=4 sw=4 :