summaryrefslogtreecommitdiffstats
path: root/test/units/modules/test_service.py
blob: 684645ee238a8577979ebc5ae4c2c74540ed2127 (plain)
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
# Copyright: (c) 2021, Ansible Project
# Copyright: (c) 2021, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import annotations


import json
import platform

import pytest
from ansible.modules import service
from ansible.module_utils.basic import AnsibleModule
from units.modules.utils import set_module_args


def mocker_sunos_service(mocker):
    """
    Configure common mocker object for SunOSService
    """
    platform_system = mocker.patch.object(platform, "system")
    platform_system.return_value = "SunOS"

    get_bin_path = mocker.patch.object(AnsibleModule, "get_bin_path")
    get_bin_path.return_value = "/usr/bin/svcs"

    # Read a mocked /etc/release file
    mocked_etc_release_data = mocker.mock_open(
        read_data=" Oracle Solaris 12.0")
    builtin_open = "builtins.open"
    mocker.patch(builtin_open, mocked_etc_release_data)

    service_status = mocker.patch.object(
        service.Service, "modify_service_state")
    service_status.return_value = (0, "", "")

    get_sunos_svcs_status = mocker.patch.object(
        service.SunOSService, "get_sunos_svcs_status")
    get_sunos_svcs_status.return_value = "offline"
    get_service_status = mocker.patch.object(
        service.Service, "get_service_status")
    get_service_status.return_value = ""

    mocker.patch('ansible.module_utils.common.sys_info.distro.id', return_value='')


@pytest.fixture
def mocked_sunos_service(mocker):
    mocker_sunos_service(mocker)


def test_sunos_service_start(mocked_sunos_service, capfd):
    """
    test SunOS Service Start
    """
    set_module_args(
        {
            "name": "environment",
            "state": "started",
        }
    )
    with pytest.raises(SystemExit):
        service.main()

    out, dummy = capfd.readouterr()
    results = json.loads(out)
    assert not results.get("failed")
    assert results["changed"]