Skip to content

Commit

Permalink
[FIX] cetmix_tower_server: Tests
Browse files Browse the repository at this point in the history
- Fix tests falling due to missing variable.

Task: 4273
  • Loading branch information
GabbasovDinar committed Jan 17, 2025
1 parent a2ebeda commit d6b2cf5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 55 deletions.
6 changes: 4 additions & 2 deletions cetmix_tower_server/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright (C) 2022 Cetmix OÜ
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from unittest.mock import MagicMock

from odoo import _
from odoo.exceptions import ValidationError
from odoo.tests import TransactionCase
Expand Down Expand Up @@ -216,9 +218,9 @@ def setUp(self, *args, **kwargs):
self.PlanLog = self.env["cx.tower.plan.log"]

# Patch methods for testing
def _get_ssh_client_patch(self, raise_on_error=True):
def _get_ssh_client_patch(self, raise_on_error=True, timeout=5000):
"""Mock method for connection"""
return True
return MagicMock()

def _execute_command_using_ssh_patch(
self,
Expand Down
69 changes: 16 additions & 53 deletions cetmix_tower_server/tests/test_cetmix_tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from unittest.mock import patch

from ..models.constants import SSH_CONNECTION_ERROR, SSH_CONNECTION_TIMEOUT
from ..models.constants import SSH_CONNECTION_ERROR
from .common import TestTowerCommon


Expand Down Expand Up @@ -99,63 +99,26 @@ def test_server_check_ssh_connection(self):
either returns a dictionary or raises an exception.
"""

def mock_server_check_ssh_connection(
this, server_reference, attempts=5, timeout=15
):
if server_reference == self.server_test_1.reference:
return {
"code": 0,
"message": "Mocked: SSH connection successful",
}
elif server_reference == "invalid_server":
raise ValueError("Mocked: Invalid server reference")
elif server_reference == "timeout_server":
raise TimeoutError("Mocked: SSH connection timed out")
elif server_reference == "max_attempts_timeout":
return {
"code": SSH_CONNECTION_TIMEOUT,
"message": "Mocked: SSH connection timeout on last attempt",
}
else:
return {
"code": SSH_CONNECTION_ERROR,
"message": "Mocked: Unknown server",
}

with patch(
"odoo.addons.cetmix_tower_server.models.cetmix_tower.CetmixTower.server_check_ssh_connection",
mock_server_check_ssh_connection,
):
# Test successful connection
result = self.env["cetmix.tower"].server_check_ssh_connection(
self.server_test_1.reference
)
self.assertEqual(result["code"], 0, "SSH connection should be successful.")

# Test invalid server
with self.assertRaises(ValueError):
self.env["cetmix.tower"].server_check_ssh_connection("invalid_server")
# Test successful connection
result = self.env["cetmix.tower"].server_check_ssh_connection(
self.server_test_1.reference,
)
self.assertEqual(result["exit_code"], 0, "SSH connection should be successful.")

# Test connection timeout
with self.assertRaises(TimeoutError):
self.env["cetmix.tower"].server_check_ssh_connection("timeout_server")
def test_ssh_connection(this, *args, **kwargs):
return {"status": -1}

with patch.object(
self.registry["cx.tower.server"], "test_ssh_connection", test_ssh_connection
):
# Test connection timeout after max attempts
result = self.env["cetmix.tower"].server_check_ssh_connection(
"max_attempts_timeout", attempts=5
self.server_test_1.reference,
attempts=2,
wait_time=1,
)
self.assertEqual(
result["code"],
SSH_CONNECTION_TIMEOUT,
"SSH connection should timeout after maximum attempts.",
)

# Test unknown server
result = self.env["cetmix.tower"].server_check_ssh_connection(
"unknown_server"
)
self.assertEqual(
result["code"],
result["exit_code"],
SSH_CONNECTION_ERROR,
"Unknown server should return code 503.",
"SSH connection should timeout after maximum attempts.",
)

0 comments on commit d6b2cf5

Please sign in to comment.