Skip to content

Commit 8f37299

Browse files
authored
add missing test for all existing custom types (#1)
* increase tests for ansible param type * use tempfile for vault * check vault data after managing it * implement tests for net param type * add github test workflow * parametrize tests as needed
1 parent 15995ad commit 8f37299

File tree

5 files changed

+133
-25
lines changed

5 files changed

+133
-25
lines changed

.github/workflows/main.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: CI
2+
3+
on:
4+
- push
5+
6+
jobs:
7+
tests:
8+
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Set up Python
14+
uses: actions/setup-python@v2
15+
with:
16+
python-version: '3.x'
17+
- name: Prepare test environment
18+
run: |
19+
python -m pip install --upgrade pip
20+
make test-setup
21+
- name: Run all tests
22+
run:
23+
make test-all

tests/conftest.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"""Provide some base configurations for tests."""
2+
import os
23
import pytest
34
import py.path # pyright: reportMissingModuleSource=false
45

6+
from ansible.errors import AnsibleError
7+
from ansible_vault import Vault
58

69
TEST_CASES_PATH = py.path.local(__file__).realpath() / '..' / 'test_cases'
710

@@ -56,3 +59,28 @@ def vcrmode(request):
5659
def cassette_name(test_name=None):
5760
"""Generate cassette_name."""
5861
return 'tests/fixtures/{0}.yml'.format(test_name)
62+
63+
64+
def open_vault(vault: str = None, secret: str = None):
65+
s = os.getenv(secret)
66+
v = Vault(s)
67+
68+
try:
69+
return dict(v.load(open(vault).read()))
70+
except (AnsibleError, Exception) as e:
71+
raise Exception(f"failed to open vault '{vault}': {str(e)}")
72+
73+
74+
def get_value_from_key(data: dict = None, keys: list = None):
75+
76+
if keys:
77+
78+
key = keys[0]
79+
80+
if isinstance(key, str) and len(keys) > 1:
81+
if key in data:
82+
return get_value_from_key(data[key], keys[1:])
83+
else:
84+
raise Exception(f"key '{key}' not in dictionary")
85+
elif isinstance(key, str):
86+
return data[key]

tests/test_cases/ansible_types.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
import os
33
import random
44
import string
5+
import tempfile
56

7+
from tests.conftest import open_vault, get_value_from_key
68
from click.testing import CliRunner
79
from click_types.ansible import AnsibleVaultParamType as vault
810

911

1012
sec_env = 'TEST_VAULT_PASSPHRASE'
1113
key_path = 'key.subkey.subsubkey'
12-
vault_file = '/tmp/xyz.yml'
14+
vault_file = tempfile.NamedTemporaryFile().name
1315
os.environ[sec_env] = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(10))
1416

1517
runner = CliRunner()
@@ -21,14 +23,27 @@ def vault_wrapper():
2123

2224

2325
def test_create_value():
24-
response = runner.invoke(vault_wrapper, input='initial value')
26+
value = 'initial value'
27+
response = runner.invoke(vault_wrapper, input=value)
28+
29+
decrypted_data = open_vault(vault_file, sec_env)
30+
2531
assert response.exit_code == 0
32+
assert get_value_from_key(decrypted_data, key_path.split('.')) == value
2633

2734

2835
def test_update_value():
29-
response = runner.invoke(vault_wrapper, input='updated value')
36+
value = 'updated value'
37+
response = runner.invoke(vault_wrapper, input=value)
38+
39+
decrypted_data = open_vault(vault_file, sec_env)
40+
3041
assert response.exit_code == 0
42+
assert get_value_from_key(decrypted_data, key_path.split('.')) == value
43+
3144

45+
def test_missing_env_var():
46+
del os.environ[sec_env]
47+
response = runner.invoke(vault_wrapper, input='dummy')
3248

33-
if __name__ == "__main__":
34-
vault_wrapper()
49+
assert response.exit_code != 0

tests/test_cases/coding_types.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import click
2+
import pytest
23

34
from click.testing import CliRunner
45
from click_types.coding import SemVerParamType
@@ -13,27 +14,15 @@ def semver_wrapper(version):
1314
pass
1415

1516

16-
def test_success():
17+
@pytest.mark.parametrize('param', ['1.2.3', '1.2.3-alpha', '1.2.3-beta+build1337'])
18+
def test_success(param):
1719

18-
positive_semvers = [
19-
'1.2.3',
20-
'1.2.3-alpha',
21-
'1.2.3-beta+build1337'
22-
]
23-
for s in positive_semvers:
24-
print(s)
25-
response = runner.invoke(semver_wrapper, ['--version', s])
26-
assert response.exit_code == 0
20+
response = runner.invoke(semver_wrapper, ['--version', param])
21+
assert response.exit_code == 0
2722

2823

29-
def test_failure():
24+
@pytest.mark.parametrize('param', ['1.2', '1.2.3+build1337', '1.2.3-alpha-build4711'])
25+
def test_failure(param):
3026

31-
negative_semvers = [
32-
'1.2',
33-
'1.2.3+build1337',
34-
'1.2.3-alpha-build4711'
35-
]
36-
37-
for s in negative_semvers:
38-
response = runner.invoke(semver, ['--version', s])
39-
assert response.exit_code == 1
27+
response = runner.invoke(semver, ['--version', param])
28+
assert response.exit_code != 0

tests/test_cases/net_types.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import click
2+
import pytest
3+
4+
from click.testing import CliRunner
5+
from click_types.net import CIDRParamType, VlanIDParamType
6+
7+
cidr = CIDRParamType()
8+
vlan = VlanIDParamType()
9+
runner = CliRunner()
10+
11+
12+
@click.command()
13+
@click.option('--cidr', type=cidr)
14+
def cidr_wrapper(cidr):
15+
pass
16+
17+
18+
@click.command()
19+
@click.option('--vlan', type=vlan)
20+
def vlan_wrapper(vlan):
21+
pass
22+
23+
24+
@pytest.mark.parametrize('param', ['192.0.2.0/24', '2001:db8::/32'])
25+
def test_cidr_success(param):
26+
27+
response = runner.invoke(cidr_wrapper, ['--cidr', param])
28+
print(response.output)
29+
assert response.exit_code == 0
30+
31+
32+
@pytest.mark.parametrize('param', ['192.0.2.0', 'fe80::250/56'])
33+
def test_cidr_failure(param):
34+
35+
response = runner.invoke(cidr_wrapper, ['--cidr', param])
36+
print(response.output)
37+
assert response.exit_code != 0
38+
39+
40+
@pytest.mark.parametrize('param', ['1', '1234', '4094'])
41+
def test_vlan_success(param):
42+
43+
response = runner.invoke(vlan_wrapper, ['--vlan', param])
44+
print(response.stdout)
45+
assert response.exit_code == 0
46+
47+
48+
@pytest.mark.parametrize('param', ['0', '4095', -1, 0.1, 'ABCD'])
49+
def test_vlan_failure(param):
50+
51+
response = runner.invoke(vlan_wrapper, ['--vlan', param])
52+
print(response.output)
53+
assert response.exit_code != 0

0 commit comments

Comments
 (0)