forked from runpod/runpod-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config_functions.py
More file actions
229 lines (204 loc) · 9.63 KB
/
test_config_functions.py
File metadata and controls
229 lines (204 loc) · 9.63 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""
Unit tests for the config command.
"""
import unittest
from unittest.mock import mock_open, patch
from runpod.cli.groups.config import functions
class TestConfig(unittest.TestCase):
"""Unit tests for the config function."""
def setUp(self) -> None:
self.sample_credentials = "[default]\n" 'api_key = "RUNPOD_API_KEY"\n'
@patch("runpod.cli.groups.config.functions.os.replace")
@patch("runpod.cli.groups.config.functions.os.unlink")
@patch("runpod.cli.groups.config.functions.os.fdopen", new_callable=mock_open)
@patch("runpod.cli.groups.config.functions.tempfile.mkstemp")
@patch("runpod.cli.groups.config.functions.tomlkit.dump")
@patch("runpod.cli.groups.config.functions.tomlkit.document")
@patch("runpod.cli.groups.config.functions.Path.touch")
@patch("runpod.cli.groups.config.functions.os.makedirs")
@patch("builtins.open", new_callable=mock_open, read_data="")
def test_set_credentials(
self, mock_file, _mock_makedirs, _mock_touch, mock_document,
mock_dump, mock_mkstemp, _mock_fdopen, _mock_unlink, _mock_replace,
):
"""
Tests the set_credentials function.
"""
mock_mkstemp.return_value = (99, "/tmp/cred.toml")
mock_document.side_effect = [{}, {"default": True}]
functions.set_credentials("RUNPOD_API_KEY")
assert any(
call.args[0] == functions.CREDENTIAL_FILE
and call.args[1] == "r"
and call.kwargs.get("encoding") == "UTF-8"
for call in mock_file.call_args_list
)
assert mock_dump.called
with self.assertRaises(ValueError) as context:
functions.set_credentials("RUNPOD_API_KEY")
self.assertEqual(
str(context.exception),
"Profile already exists. Use set_credentials(overwrite=True) to update.",
)
@patch("runpod.cli.groups.config.functions.os.replace")
@patch("runpod.cli.groups.config.functions.os.unlink")
@patch("runpod.cli.groups.config.functions.os.fdopen", new_callable=mock_open)
@patch("runpod.cli.groups.config.functions.tempfile.mkstemp")
@patch("runpod.cli.groups.config.functions.tomlkit.dump")
@patch("runpod.cli.groups.config.functions.Path.touch")
@patch("runpod.cli.groups.config.functions.os.makedirs")
@patch(
"builtins.open",
new_callable=mock_open,
read_data='[default]\napi_key = "EXISTING_KEY"\n\n[profile1]\napi_key = "KEY1"\n',
)
def test_set_credentials_preserves_existing_profiles(
self, _mock_file, _mock_makedirs, _mock_touch, mock_dump,
mock_mkstemp, _mock_fdopen, _mock_unlink, _mock_replace,
):
"""Adding a new profile must preserve all existing profiles."""
mock_mkstemp.return_value = (99, "/tmp/cred.toml")
functions.set_credentials("NEW_KEY", profile="profile2")
dumped_config = mock_dump.call_args[0][0]
assert "default" in dumped_config
assert dumped_config["default"]["api_key"] == "EXISTING_KEY"
assert "profile1" in dumped_config
assert dumped_config["profile1"]["api_key"] == "KEY1"
assert "profile2" in dumped_config
assert dumped_config["profile2"]["api_key"] == "NEW_KEY"
@patch("builtins.open", new_callable=mock_open())
@patch("runpod.cli.groups.config.functions.toml.load")
@patch("runpod.cli.groups.config.functions.os.path.exists")
def test_check_credentials(self, mock_exists, mock_toml_load, mock_file):
"""mock_open_call
Tests the check_credentials function.
"""
mock_exists.return_value = False
passed, _ = functions.check_credentials()
assert passed is False
mock_exists.return_value = True
mock_toml_load.return_value = ""
passed, _ = functions.check_credentials()
assert mock_file.called
assert passed is False
mock_exists.return_value = True
mock_toml_load.return_value = dict({"default": "something"})
passed, _ = functions.check_credentials()
assert passed is False
mock_toml_load.return_value = ValueError
passed, _ = functions.check_credentials()
assert passed is False
mock_toml_load.return_value = dict({"default": "api_key"})
passed, _ = functions.check_credentials()
assert passed is True
@patch("os.path.exists", return_value=True)
@patch("runpod.cli.groups.config.functions.toml.load")
@patch(
"builtins.open", new_callable=mock_open, read_data='[default]\nkey = "value"'
)
def test_get_credentials_existing_profile(
self, mock_open_call, mock_toml_load, mock_exists
):
"""
Tests the get_credentials function.
"""
mock_toml_load.return_value = {"default": {"key": "value"}}
result = functions.get_credentials("default")
assert result == {"key": "value"}
assert mock_open_call.called
assert mock_exists.called
@patch("os.path.exists", return_value=True)
@patch("runpod.cli.groups.config.functions.toml.load")
@patch(
"builtins.open", new_callable=mock_open, read_data='[default]\nkey = "value"'
)
def test_get_credentials_non_existent_profile(
self, mock_open_call, mock_toml_load, mock_exists
): # pylint: disable=line-too-long
"""
Tests the get_credentials function.
"""
mock_toml_load.return_value = {"default": {"key": "value"}}
result = functions.get_credentials("non_existent")
assert result is None
assert mock_open_call.called
assert mock_exists.called
@patch("runpod.cli.groups.config.functions.os.path.exists", return_value=True)
@patch(
"runpod.cli.groups.config.functions.toml.load",
side_effect=ValueError("Invalid value"),
)
@patch("builtins.open", new_callable=mock_open)
def test_get_credentials_corrupted_toml(
self, _mock_open_call, _mock_toml_load, _mock_exists
):
"""get_credentials returns None when config.toml contains invalid TOML."""
result = functions.get_credentials("default")
assert result is None
@patch("runpod.cli.groups.config.functions.os.path.exists", return_value=True)
@patch(
"runpod.cli.groups.config.functions.toml.load",
side_effect=TypeError("bad type"),
)
@patch("builtins.open", new_callable=mock_open)
def test_get_credentials_type_error(
self, _mock_open_call, _mock_toml_load, _mock_exists
):
"""get_credentials returns None on TypeError from corrupted file."""
result = functions.get_credentials("default")
assert result is None
@patch("runpod.cli.groups.config.functions.os.replace")
@patch("runpod.cli.groups.config.functions.os.unlink")
@patch("runpod.cli.groups.config.functions.os.fdopen", new_callable=mock_open)
@patch("runpod.cli.groups.config.functions.tempfile.mkstemp")
@patch("runpod.cli.groups.config.functions.tomlkit.dump")
@patch("runpod.cli.groups.config.functions.Path.touch")
@patch("runpod.cli.groups.config.functions.os.makedirs")
@patch(
"builtins.open",
new_callable=mock_open,
read_data='[default]\napi_key = "OLD_KEY"\n',
)
def test_set_credentials_overwrite_replaces_existing_profile(
self, _mock_file, _mock_makedirs, _mock_touch, mock_dump,
mock_mkstemp, _mock_fdopen, _mock_unlink, _mock_replace,
):
"""overwrite=True replaces an existing profile's api_key."""
mock_mkstemp.return_value = (99, "/tmp/cred.toml")
functions.set_credentials("NEW_KEY", profile="default", overwrite=True)
dumped_config = mock_dump.call_args[0][0]
assert dumped_config["default"]["api_key"] == "NEW_KEY"
@patch("runpod.cli.groups.config.functions.os.replace")
@patch("runpod.cli.groups.config.functions.os.unlink")
@patch("runpod.cli.groups.config.functions.os.fdopen", new_callable=mock_open)
@patch("runpod.cli.groups.config.functions.tempfile.mkstemp")
@patch("runpod.cli.groups.config.functions.tomlkit.dump", side_effect=OSError("disk full"))
@patch("runpod.cli.groups.config.functions.Path.touch")
@patch("runpod.cli.groups.config.functions.os.makedirs")
@patch("builtins.open", new_callable=mock_open, read_data="")
def test_set_credentials_cleans_up_temp_on_dump_failure(
self, _mock_file, _mock_makedirs, _mock_touch, _mock_dump,
mock_mkstemp, _mock_fdopen, mock_unlink, mock_replace,
):
"""Temp file is removed and original config untouched when dump fails."""
mock_mkstemp.return_value = (99, "/tmp/cred.toml")
with self.assertRaises(OSError):
functions.set_credentials("KEY")
mock_unlink.assert_called_once_with("/tmp/cred.toml")
mock_replace.assert_not_called()
@patch("runpod.cli.groups.config.functions.os.replace")
@patch("runpod.cli.groups.config.functions.os.unlink")
@patch("runpod.cli.groups.config.functions.tempfile.mkstemp")
@patch("runpod.cli.groups.config.functions.tomlkit.dump")
@patch("runpod.cli.groups.config.functions.Path.touch")
@patch("runpod.cli.groups.config.functions.os.makedirs")
@patch("builtins.open", new_callable=mock_open, read_data="not valid toml {{{")
def test_set_credentials_corrupted_toml_raises(
self, _mock_file, _mock_makedirs, _mock_touch, _mock_dump,
_mock_mkstemp, _mock_unlink, _mock_replace,
):
"""set_credentials raises ValueError on corrupted TOML regardless of overwrite."""
with self.assertRaises(ValueError):
functions.set_credentials("NEW_KEY", overwrite=True)
with self.assertRaises(ValueError):
functions.set_credentials("NEW_KEY", overwrite=False)