forked from linode/linode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
164 lines (129 loc) · 4.8 KB
/
Copy pathtest_cli.py
File metadata and controls
164 lines (129 loc) · 4.8 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
from __future__ import annotations
import copy
import math
import os
import re
import pytest
import requests
import requests_mock
from pytest import MonkeyPatch
from tests.unit.conftest import FIXTURES_PATH, open_fixture
if True:
from linodecli import CLI
from linodecli.api_request import get_all_pages
from linodecli.baked.operation import OpenAPIOperation
class MockResponse:
def __init__(
self,
page: int,
pages: int,
results: int,
status_code: int = 200,
headers: dict = None,
):
self.page = page
self.pages = pages
self.headers = headers
self.results = results
self.status_code = status_code
def json(self):
return {
"data": ["test_data" for _ in range(500)],
"page": self.page,
"pages": self.pages,
"results": self.results,
}
class TestCLI:
"""
Unit tests for linodecli.cli
"""
def test_find_operation(
self, mock_cli: CLI, list_operation: OpenAPIOperation
):
target_operation = list_operation
target_operation.command = "foo"
target_operation.action = "list"
target_operation.action_aliases = ["ls"]
other_operation = copy.deepcopy(list_operation)
other_operation.command = "cool"
other_operation.action = "list"
other_operation.action_aliases = ["ls"]
mock_cli.ops = {
"foo": {"list": target_operation},
"cool": {"list": other_operation},
}
assert mock_cli.find_operation("foo", "list") == target_operation
assert mock_cli.find_operation("foo", "ls") == target_operation
assert mock_cli.find_operation("cool", "list") == other_operation
assert mock_cli.find_operation("cool", "ls") == other_operation
with pytest.raises(ValueError, match=r"Command not found: *"):
mock_cli.find_operation("bad", "list")
with pytest.raises(ValueError, match=r"Action not found for command *"):
mock_cli.find_operation("foo", "cool")
mock_cli.find_operation("cool", "cool")
def test_user_agent(self, mock_cli: CLI):
assert re.compile(
r"linode-cli/[0-9]+\.[0-9]+\.[0-9]+ linode-api-openapi/[0-9]+\.[0-9]+\.[0-9]+ python/[0-9]+\.[0-9]+\.[0-9]+"
).match(mock_cli.user_agent)
def test_load_openapi_spec_json(self):
url_base = "https://localhost/"
path = "cli_test_load.json"
url = f"{url_base}{path}"
with open_fixture(path) as f:
content = f.read()
with requests_mock.Mocker() as m:
m.get(url, text=content)
parsed_json_local = CLI._load_openapi_spec(
str(os.path.join(FIXTURES_PATH, path))
)
parsed_json_http = CLI._load_openapi_spec(url)
assert m.call_count == 1
assert parsed_json_http.raw_element == parsed_json_local.raw_element
def test_load_openapi_spec_yaml(self):
url_base = "https://localhost/"
path = "cli_test_load.yaml"
url = f"{url_base}{path}"
with open_fixture(path) as f:
content = f.read()
with requests_mock.Mocker() as m:
m.get(url, text=content)
parsed_json_local = CLI._load_openapi_spec(
str(os.path.join(FIXTURES_PATH, path))
)
parsed_json_http = CLI._load_openapi_spec(url)
assert m.call_count == 1
assert parsed_json_http.raw_element == parsed_json_local.raw_element
def test_bake_missing_cmd_ext(self, mock_cli: CLI):
try:
mock_cli.bake(
str(
os.path.join(
FIXTURES_PATH, "cli_test_bake_missing_cmd_ext.yaml"
)
),
save=False,
)
except KeyError as err:
assert (
str(err)
== "'GET /foo/bar: Missing x-linode-cli-command extension'"
)
else:
raise AssertionError("Expected a KeyError exception")
def test_get_all_pages(
mock_cli: CLI, list_operation: OpenAPIOperation, monkeypatch: MonkeyPatch
):
TOTAL_DATA = 2000
def mock_get(url: str, *args, **kwargs):
# assume page_size is always 500
page = int(re.search(r"\?page=(.*?)&page_size", url).group(1))
pages = math.ceil(TOTAL_DATA / 500)
if page > pages:
page = pages
return MockResponse(page, pages, pages * 500)
monkeypatch.setattr(requests, "get", mock_get)
merged_result = get_all_pages(mock_cli, list_operation, [])
assert len(merged_result["data"]) == TOTAL_DATA
assert merged_result["page"] == 1
assert merged_result["pages"] == 1
assert merged_result["results"] == TOTAL_DATA