Skip to content

Commit 7f5f6e8

Browse files
committed
Add unit tests for CloudProxy functionality
- Introduce tests for the main module to verify the execution of the main function when run as a script. - Implement comprehensive tests for DigitalOcean provider functions, including error handling for proxy deletion and firewall creation. - Add tests for GCP provider functions to ensure proper instance management and error handling. - Create additional tests for Hetzner provider functions, focusing on deletion and alive checks. - Establish tests for settings configuration to validate environment variable handling and instance creation. - Enhance setup tests to ensure the integrity of the setup.py structure and content.
1 parent 59028ff commit 7f5f6e8

7 files changed

+1021
-1
lines changed

tests/test_main_module.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
from unittest.mock import patch
3+
4+
@patch('cloudproxy.main.main')
5+
def test_main_executed_when_run_as_script(mock_main):
6+
"""Test that the main function is called when the module is run as a script."""
7+
# Import the module which will call main() if __name__ == "__main__"
8+
# The patch above will prevent actual execution of main()
9+
import cloudproxy.__main__
10+
11+
# Since we're importing the module, and __name__ != "__main__",
12+
# main() should not be called
13+
mock_main.assert_not_called()
14+
15+
# Now simulate if __name__ == "__main__"
16+
cloudproxy.__main__.__name__ = "__main__"
17+
18+
# Execute the script's main condition
19+
if cloudproxy.__main__.__name__ == "__main__":
20+
cloudproxy.__main__.main()
21+
22+
# Verify main() was called
23+
mock_main.assert_called_once()
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import pytest
2+
from unittest.mock import patch, MagicMock
3+
import digitalocean
4+
from cloudproxy.providers.digitalocean.functions import (
5+
delete_proxy,
6+
create_firewall,
7+
DOFirewallExistsException
8+
)
9+
10+
11+
class TestDeleteProxyErrorHandling:
12+
"""Tests for error handling in delete_proxy function."""
13+
14+
@patch('cloudproxy.providers.digitalocean.functions.get_manager')
15+
def test_delete_proxy_with_droplet_object(self, mock_get_manager):
16+
"""Test delete_proxy when called with a droplet object instead of just ID."""
17+
# Create a mock droplet object
18+
mock_droplet = MagicMock()
19+
mock_droplet.id = 12345
20+
21+
# Mock the manager and its methods
22+
mock_manager = MagicMock()
23+
mock_manager.get_droplet.return_value = mock_droplet
24+
mock_get_manager.return_value = mock_manager
25+
26+
# Mock the destroy method
27+
mock_droplet.destroy.return_value = True
28+
29+
# Call the function with the droplet object
30+
result = delete_proxy(mock_droplet)
31+
32+
# Verify the right methods were called
33+
mock_get_manager.assert_called_once()
34+
mock_manager.get_droplet.assert_called_once_with(12345)
35+
assert result == True
36+
37+
@patch('cloudproxy.providers.digitalocean.functions.get_manager')
38+
def test_delete_proxy_droplet_not_found(self, mock_get_manager):
39+
"""Test delete_proxy when the droplet is not found."""
40+
# Mock the manager
41+
mock_manager = MagicMock()
42+
# Make get_droplet raise an exception with "not found" in the message
43+
mock_manager.get_droplet.side_effect = Exception("Droplet not found")
44+
mock_get_manager.return_value = mock_manager
45+
46+
# Call the function
47+
result = delete_proxy(12345)
48+
49+
# Verify it considers a missing droplet as successfully deleted
50+
mock_manager.get_droplet.assert_called_once_with(12345)
51+
assert result == True
52+
53+
@patch('cloudproxy.providers.digitalocean.functions.get_manager')
54+
def test_delete_proxy_with_droplet_object_not_found(self, mock_get_manager):
55+
"""Test delete_proxy with a droplet object when the droplet is not found."""
56+
# Create a mock droplet object
57+
mock_droplet = MagicMock()
58+
mock_droplet.id = 12345
59+
60+
# Mock the manager
61+
mock_manager = MagicMock()
62+
# Make get_droplet raise an exception with "not found" in the message
63+
mock_manager.get_droplet.side_effect = Exception("Droplet not found")
64+
mock_get_manager.return_value = mock_manager
65+
66+
# Call the function with the droplet object
67+
result = delete_proxy(mock_droplet)
68+
69+
# Verify it considers a missing droplet as successfully deleted
70+
mock_manager.get_droplet.assert_called_once_with(12345)
71+
assert result == True
72+
73+
@patch('cloudproxy.providers.digitalocean.functions.get_manager')
74+
def test_delete_proxy_with_error_in_destroy(self, mock_get_manager):
75+
"""Test delete_proxy when the destroy method raises an exception."""
76+
# Create mock droplet and manager
77+
mock_droplet = MagicMock()
78+
mock_manager = MagicMock()
79+
mock_manager.get_droplet.return_value = mock_droplet
80+
mock_get_manager.return_value = mock_manager
81+
82+
# Make destroy raise a non-404 exception
83+
mock_droplet.destroy.side_effect = Exception("Some other error")
84+
85+
# Call the function and expect the exception to be raised
86+
with pytest.raises(Exception, match="Some other error"):
87+
delete_proxy(12345)
88+
89+
# Verify the right methods were called
90+
mock_manager.get_droplet.assert_called_once()
91+
mock_droplet.destroy.assert_called_once()
92+
93+
@patch('cloudproxy.providers.digitalocean.functions.get_manager')
94+
def test_delete_proxy_with_404_in_destroy(self, mock_get_manager):
95+
"""Test delete_proxy when the destroy method raises a 404 exception."""
96+
# Create mock droplet and manager
97+
mock_droplet = MagicMock()
98+
mock_manager = MagicMock()
99+
mock_manager.get_droplet.return_value = mock_droplet
100+
mock_get_manager.return_value = mock_manager
101+
102+
# Make destroy raise a 404 exception
103+
mock_droplet.destroy.side_effect = Exception("404 Not Found")
104+
105+
# Call the function
106+
result = delete_proxy(12345)
107+
108+
# Verify it treats 404 as success
109+
mock_manager.get_droplet.assert_called_once()
110+
mock_droplet.destroy.assert_called_once()
111+
assert result == True

0 commit comments

Comments
 (0)