Skip to content

Commit f2f568c

Browse files
authored
Add unit tests for cloudera.exe.jdk_facts (#266)
Signed-off-by: Webster Mudge <[email protected]>
1 parent a3ba75f commit f2f568c

File tree

2 files changed

+183
-3
lines changed

2 files changed

+183
-3
lines changed

tests/unit/conftest.py

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,63 @@
1818

1919
__metaclass__ = type
2020

21-
import logging
21+
import json
22+
import sys
2223
import pytest
24+
import yaml
2325

24-
LOG = logging.getLogger(__name__)
26+
from ansible.module_utils import basic
27+
from ansible.module_utils.common.text.converters import to_bytes
2528

26-
# Pytest fixtures
29+
from tests.unit import (
30+
AnsibleFailJson,
31+
AnsibleExitJson,
32+
)
33+
34+
35+
@pytest.fixture(autouse=True)
36+
def skip_python():
37+
if sys.version_info < (3, 6):
38+
pytest.skip(
39+
"Skipping on Python %s. cloudera.cloud supports Python 3.6 and higher."
40+
% sys.version,
41+
)
42+
43+
44+
@pytest.fixture(autouse=True)
45+
def patch_module(monkeypatch):
46+
"""Patch AnsibleModule to raise exceptions on success and failure"""
47+
48+
def exit_json(*args, **kwargs):
49+
if "changed" not in kwargs:
50+
kwargs["changed"] = False
51+
raise AnsibleExitJson(kwargs)
52+
53+
def fail_json(*args, **kwargs):
54+
kwargs["failed"] = True
55+
raise AnsibleFailJson(kwargs)
56+
57+
monkeypatch.setattr(basic.AnsibleModule, "exit_json", exit_json)
58+
monkeypatch.setattr(basic.AnsibleModule, "fail_json", fail_json)
59+
60+
61+
@pytest.fixture
62+
def module_args():
63+
"""Prepare module arguments"""
64+
65+
def prep_args(args=dict()):
66+
args = json.dumps({"ANSIBLE_MODULE_ARGS": args})
67+
basic._ANSIBLE_ARGS = to_bytes(args)
68+
69+
return prep_args
70+
71+
72+
@pytest.fixture
73+
def yaml_args():
74+
"""Prepare module arguments from YAML"""
75+
76+
def prep_args(args: str = ""):
77+
output = json.dumps({"ANSIBLE_MODULE_ARGS": yaml.safe_load(args)})
78+
basic._ANSIBLE_ARGS = to_bytes(output)
79+
80+
return prep_args
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2025 Cloudera, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from __future__ import absolute_import, division, print_function
18+
19+
__metaclass__ = type
20+
21+
import pytest
22+
23+
24+
from tests.unit import (
25+
AnsibleExitJson,
26+
)
27+
28+
from ansible.module_utils.basic import AnsibleModule
29+
30+
from ansible_collections.cloudera.exe.plugins.modules import jdk_facts
31+
32+
33+
def test_execution_error(module_args, monkeypatch):
34+
module_args()
35+
36+
def mock_exec(*args, **kwargs):
37+
return 1, None, "ERROR"
38+
39+
monkeypatch.setattr(AnsibleModule, "run_command", mock_exec)
40+
41+
with pytest.raises(AnsibleExitJson) as e:
42+
jdk_facts.main()
43+
44+
assert e.value.ansible_facts["jdk"]
45+
assert e.value.ansible_facts["jdk"]["provider"] == None
46+
47+
48+
def test_oracle_5(module_args, monkeypatch):
49+
module_args()
50+
51+
def mock_exec(*args, **kwargs):
52+
stderr = [
53+
'java version "1.5.0_07"',
54+
"Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)",
55+
"Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode, sharing)",
56+
]
57+
return 0, "", "\n".join(stderr)
58+
59+
monkeypatch.setattr(AnsibleModule, "run_command", mock_exec)
60+
61+
with pytest.raises(AnsibleExitJson) as e:
62+
jdk_facts.main()
63+
64+
assert e.value.ansible_facts["jdk"]
65+
assert e.value.ansible_facts["jdk"]["provider"] == "Oracle"
66+
assert e.value.ansible_facts["jdk"]["version"] == "1.5.0_07-b03"
67+
assert e.value.ansible_facts["jdk"]["major"] == "1"
68+
assert e.value.ansible_facts["jdk"]["minor"] == "5"
69+
assert e.value.ansible_facts["jdk"]["patch"] == "0"
70+
assert e.value.ansible_facts["jdk"]["release"] == "07"
71+
assert e.value.ansible_facts["jdk"]["build"] == "b03"
72+
assert e.value.ansible_facts["jdk"]["update"] == ""
73+
74+
75+
def test_openjdk_23(module_args, monkeypatch):
76+
module_args()
77+
78+
def mock_exec(*args, **kwargs):
79+
stderr = [
80+
'openjdk version "23" 2024-09-17',
81+
"OpenJDK Runtime Environment Homebrew (build 23)",
82+
"OpenJDK 64-Bit Server VM Homebrew (build 23, mixed mode, sharing)",
83+
]
84+
return 0, "", "\n".join(stderr)
85+
86+
monkeypatch.setattr(AnsibleModule, "run_command", mock_exec)
87+
88+
with pytest.raises(AnsibleExitJson) as e:
89+
jdk_facts.main()
90+
91+
assert e.value.ansible_facts["jdk"]
92+
assert e.value.ansible_facts["jdk"]["provider"] == "OpenJDK"
93+
assert e.value.ansible_facts["jdk"]["version"] == "23"
94+
assert e.value.ansible_facts["jdk"]["major"] == "23"
95+
assert e.value.ansible_facts["jdk"]["minor"] == ""
96+
assert e.value.ansible_facts["jdk"]["patch"] == ""
97+
assert e.value.ansible_facts["jdk"]["release"] == ""
98+
assert e.value.ansible_facts["jdk"]["build"] == ""
99+
assert e.value.ansible_facts["jdk"]["update"] == "2024-09-17"
100+
101+
102+
def test_openjdk_8(module_args, monkeypatch):
103+
module_args()
104+
105+
def mock_exec(*args, **kwargs):
106+
stderr = [
107+
'openjdk version "1.8.0_432"',
108+
"OpenJDK Runtime Environment (build 1.8.0_432-b06)",
109+
"OpenJDK 64-Bit Server VM (build 25.432-b06, mixed mode)",
110+
]
111+
return 0, "", "\n".join(stderr)
112+
113+
monkeypatch.setattr(AnsibleModule, "run_command", mock_exec)
114+
115+
with pytest.raises(AnsibleExitJson) as e:
116+
jdk_facts.main()
117+
118+
assert e.value.ansible_facts["jdk"]
119+
assert e.value.ansible_facts["jdk"]["provider"] == "OpenJDK"
120+
assert e.value.ansible_facts["jdk"]["version"] == "1.8.0_432-b06"
121+
assert e.value.ansible_facts["jdk"]["major"] == "1"
122+
assert e.value.ansible_facts["jdk"]["minor"] == "8"
123+
assert e.value.ansible_facts["jdk"]["patch"] == "0"
124+
assert e.value.ansible_facts["jdk"]["release"] == "432"
125+
assert e.value.ansible_facts["jdk"]["build"] == "b06"
126+
assert e.value.ansible_facts["jdk"]["update"] == ""

0 commit comments

Comments
 (0)