-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_licence.py
More file actions
174 lines (144 loc) · 5.61 KB
/
test_licence.py
File metadata and controls
174 lines (144 loc) · 5.61 KB
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
import json
import pytest
from ....commands.policy.license import create, delete, ls, update
from ...utils import random_bool, random_str
def create_license_policy_config_file(
directory,
name,
description,
allow_unknown_licenses,
package_query_string,
spdx_identifiers,
on_violation_quarantine,
):
"""Create a license policy config file in `directory` and return its path."""
data = {
"name": name,
"description": description,
"spdx_identifiers": list(spdx_identifiers),
"allow_unknown_licenses": allow_unknown_licenses,
"package_query_string": package_query_string,
"on_violation_quarantine": on_violation_quarantine,
}
file_path = directory / "LICENSE-POLICY-CONFIG.json"
file_path.write_text(str(json.dumps(data)))
return file_path
def parse_table_from_output(output, policy_name):
"""Return a dict of license policy properties parsed from tabular cli output."""
headers = []
row = []
separator = "|"
for line in output.split("\n"):
if not headers and line.startswith("Name"):
headers = [header.strip() for header in line.split(separator)]
elif line.startswith(policy_name):
if row:
raise Exception("Multiple license policies detected - expected 1.")
row = [val.strip() for val in line.split(separator)]
if not headers:
raise Exception("Table not found in output!")
if not row:
raise Exception("No license policies found - expected 1.")
return dict(zip(headers, row))
def assert_output_matches_policy_config(output, config_file_path):
"""Assert that tabular output from a command invocation matches policy config."""
config = json.loads(config_file_path.read_text())
output_table = parse_table_from_output(output, policy_name=config["name"])
# Assert that configurable values are set correctly
assert output_table["Name"] == config["name"]
assert output_table["Description"] == config["description"]
assert output_table["SPDX Identifiers"] == str(config["spdx_identifiers"])
assert (
output_table["Allow Unknown Licenses"]
== str(config["allow_unknown_licenses"]).lower()
)
assert (
output_table["Quarantine On Violation"]
== str(config["on_violation_quarantine"]).lower()
)
assert output_table["Package Query"] == str(config["package_query_string"])
# We just require non-configurable values to be truthy
assert output_table["Created"]
assert output_table["Updated"]
assert output_table["Identifier"]
# Return the slug_perm in case we need it for other cli calls
return output_table["Identifier"]
@pytest.mark.usefixtures(
"set_api_key_env_var", "set_api_host_env_var", "set_no_warn_env_var"
)
def test_license_policy_commands(runner, organization, tmp_path):
"""Test CRUD operations for license policies."""
# Generate the license policy configuration file.
policy_name = random_str()
policy_config_file_path = create_license_policy_config_file(
directory=tmp_path,
name=policy_name,
description=random_str(),
allow_unknown_licenses=random_bool(),
on_violation_quarantine=random_bool(),
package_query_string="format:python AND downloads:>50",
spdx_identifiers=["Apache-2.0"],
)
# Create the license policy
result = runner.invoke(
create,
args=[organization, str(policy_config_file_path)],
catch_exceptions=False,
)
assert (
"Creating " + policy_name + " license policy for the cloudsmith namespace ...OK"
in result.output
)
slug_perm = assert_output_matches_policy_config(
result.output, policy_config_file_path
)
# Use the cli to get the policy
result = runner.invoke(ls, args=[organization], catch_exceptions=False)
assert "Getting license policies ... OK" in result.output
assert_output_matches_policy_config(result.output, policy_config_file_path)
# Change the values in the config file
policy_config_file_path = create_license_policy_config_file(
directory=tmp_path,
name=random_str(),
description=random_str(),
allow_unknown_licenses=random_bool(),
on_violation_quarantine=random_bool(),
package_query_string="format:go AND downloads:>15",
spdx_identifiers=["Apache-1.0"],
)
# Use the cli to update the policy
result = runner.invoke(
update,
args=[organization, slug_perm, str(policy_config_file_path)],
catch_exceptions=False,
)
assert (
"Updating " + slug_perm + " license policy in the cloudsmith namespace ...OK"
in result.output
)
assert_output_matches_policy_config(result.output, policy_config_file_path)
# Check that delete prompts for confirmation
result = runner.invoke(
delete, args=[organization, slug_perm], input="N", catch_exceptions=False
)
assert (
"Are you absolutely certain you want to delete the "
+ slug_perm
+ " license policy from the cloudsmith namespace? [y/N]: N"
in result.output
)
assert "OK, phew! Close call. :-)" in result.output
# Then actually delete it
result = runner.invoke(
delete, args=[organization, slug_perm], input="Y", catch_exceptions=False
)
assert (
"Are you absolutely certain you want to delete the "
+ slug_perm
+ " license policy from the cloudsmith namespace? [y/N]: Y"
in result.output
)
assert (
"Deleting " + slug_perm + " from the cloudsmith namespace ... OK"
in result.output
)