File: test_env_plugin.py

package info (click to toggle)
freeipa 4.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 367,240 kB
  • sloc: javascript: 562,763; python: 310,289; ansic: 49,809; sh: 7,176; makefile: 2,589; xml: 343; sed: 16
file content (101 lines) | stat: -rw-r--r-- 3,489 bytes parent folder | download
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
#
# Copyright (C) 2021  FreeIPA Contributors see COPYING for license
#
"""Test `env` plugin
"""

import pytest

from ipalib import api, errors
from ipatests.test_xmlrpc.xmlrpc_test import XMLRPC_test


@pytest.mark.tier1
class TestEnv(XMLRPC_test):
    """Test `env` plugin
    """
    EXPECTED_KEYS = ("result", "count", "total", "summary")

    def run_env(self, *args, **options):
        cmd = api.Command.env
        cmd_result = cmd(*args, **options)
        return cmd_result

    def assert_result(self, cmd_result):
        assert sorted(tuple(cmd_result.keys())) == sorted(self.EXPECTED_KEYS)
        result = cmd_result["result"]
        assert isinstance(result, dict)

        total_count = cmd_result["total"]
        assert isinstance(total_count, int)

        actual_count = cmd_result["count"]
        assert isinstance(actual_count, int)
        assert actual_count <= total_count
        assert len(result) == actual_count

        if actual_count > 1:
            assert cmd_result["summary"] == f"{actual_count} variables"
        else:
            assert cmd_result["summary"] is None

    @pytest.mark.parametrize(
        "server", [True, False, None], ids=["server", "local", "local_default"]
    )
    def test_env(self, server):
        options = {}
        if server is not None:
            options = {"server": server}
        cmd_result = self.run_env(**options)
        self.assert_result(cmd_result)
        actual_count = cmd_result["count"]
        assert actual_count >= 1
        assert cmd_result["total"] == actual_count
        assert cmd_result["result"]["in_server"] is (server is True)

    @pytest.mark.parametrize(
        "args, kwargs",
        [(("in_server",), {}), ((), {"variables": "in_server"})],
        ids=["var_as_pos_arg", "var_as_known_arg"],
    )
    @pytest.mark.parametrize(
        "server", [True, False], ids=["server", "local"]
    )
    def test_env_with_variables_one(self, args, kwargs, server):
        kwargs["server"] = server
        cmd_result = self.run_env(*args, **kwargs)
        self.assert_result(cmd_result)
        result = cmd_result["result"]
        assert result["in_server"] is server
        assert cmd_result["count"] == 1

    @pytest.mark.parametrize(
        "args, kwargs",
        [
            (("in_server", "version"), {}),
            ((), {"variables": ("in_server", "version")}),
        ],
        ids=["vars_as_pos_args", "vars_as_known_args"],
    )
    @pytest.mark.parametrize(
        "server", [True, False], ids=["server", "local"]
    )
    def test_env_with_variables_several(self, args, kwargs, server):
        kwargs["server"] = server
        cmd_result = self.run_env(*args, **kwargs)
        self.assert_result(cmd_result)
        result = cmd_result["result"]
        assert result["in_server"] is server
        assert cmd_result["count"] == 2

    @pytest.mark.parametrize("server", [True, False], ids=["server", "local"])
    def test_env_with_variables_missing_var(self, server):
        cmd_result = self.run_env("nonexistentvariable", server=server)
        self.assert_result(cmd_result)
        assert cmd_result["count"] == 0

    @pytest.mark.parametrize("server", [True, False], ids=["server", "local"])
    def test_env_with_nonexistent_option(self, server):
        with pytest.raises(errors.OptionError) as e:
            self.run_env(nonexistentoption="nonexistentoption", server=server)
        assert "Unknown option: nonexistentoption" in str(e.value)