-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathtest_config_functions.py
More file actions
149 lines (129 loc) · 5.72 KB
/
test_config_functions.py
File metadata and controls
149 lines (129 loc) · 5.72 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
"""
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.toml.load")
@patch("builtins.open", new_callable=mock_open())
def test_set_credentials(self, mock_file, mock_toml_load):
"""
Tests the set_credentials function.
"""
mock_toml_load.return_value = ""
functions.set_credentials("RUNPOD_API_KEY")
mock_file.assert_called_with(functions.CREDENTIAL_FILE, "w", encoding="UTF-8")
with self.assertRaises(ValueError) as context:
mock_toml_load.return_value = {"default": True}
functions.set_credentials("RUNPOD_API_KEY")
self.assertEqual(
str(context.exception),
"Profile already exists. Use `update_credentials` instead.",
)
@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.Path.touch")
@patch("runpod.cli.groups.config.functions.os.makedirs")
@patch("runpod.cli.groups.config.functions.toml.load")
@patch("builtins.open", new_callable=mock_open())
def test_set_credentials_corrupted_toml_allows_overwrite(
self, _mock_file, mock_toml_load, _mock_makedirs, _mock_touch
):
"""set_credentials with overwrite=True ignores corrupted existing file."""
mock_toml_load.side_effect = ValueError("Invalid TOML")
# overwrite=True skips the toml.load check entirely
functions.set_credentials("NEW_KEY", overwrite=True)
@patch("runpod.cli.groups.config.functions.Path.touch")
@patch("runpod.cli.groups.config.functions.os.makedirs")
@patch("runpod.cli.groups.config.functions.toml.load")
@patch("builtins.open", new_callable=mock_open())
def test_set_credentials_corrupted_toml_no_overwrite(
self, _mock_file, mock_toml_load, _mock_makedirs, _mock_touch
):
"""set_credentials without overwrite treats corrupted file as empty."""
mock_toml_load.side_effect = ValueError("Invalid TOML")
# Should not raise — corrupted file is treated as having no profiles
functions.set_credentials("NEW_KEY", overwrite=False)