Skip to content

Commit

Permalink
Fixed Mist check URL and wrote unit tests for test_mist_cloud method
Browse files Browse the repository at this point in the history
  • Loading branch information
crvallance committed Feb 3, 2025
1 parent 51aab79 commit 032d351
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 3 deletions.
6 changes: 3 additions & 3 deletions fpms/modules/cloud_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def test_mist_cloud(self, g_vars):
1. Is eth0 port up?
2. Do we get an IP address via DHCP?
3. Can we resolve address ep-terminator.mistsys.net
4. Can get get a http 200 response to https://ep-terminator.mistsys.net/about
4. Can get get a http 200 response to https://ep-terminator.mistsys.net/test
"""

Expand Down Expand Up @@ -362,8 +362,8 @@ def test_mist_cloud(self, g_vars):
item_list[2] = "DNS: FAIL"

if not test_fail:
# Can we get an http 200 from https://ep-terminator.mistsys.net/about ?
cmd = 'curl -k -s -o /dev/null -w "%{http_code}" https://ep-terminator.mistsys.net/about'
# Can we get an http 200 from https://ep-terminator.mistsys.net/test ?
cmd = 'curl -k -s -o /dev/null -w "%{http_code}" https://ep-terminator.mistsys.net/test'
result = subprocess.check_output(cmd, shell=True).decode()

if result == "200":
Expand Down
17 changes: 17 additions & 0 deletions tests/modules/fakes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Alert():
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

def display_popup_alert(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

class SimpleTable():
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

def display_simple_table(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
228 changes: 228 additions & 0 deletions tests/modules/test_cloud_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
import pytest
import sys


@pytest.fixture
def patch_imports():
sys.modules["fpms.modules.pages.alert"] = __import__("fakes")
sys.modules["fpms.modules.pages.simpletable"] = __import__("fakes")


def test_test_mist_cloud_cached(patch_imports):
"""Tests the fall through if the results cached value is true"""
# Arrange
from fpms.modules.cloud_tests import CloudUtils

test_g_vars = {"result_cache": True}
# Act
test_object = CloudUtils(g_vars=test_g_vars)
test_object.test_mist_cloud(test_g_vars)
# Assert
# If g_vars["result_cache"] is True then
# self.alert_obj.display_popup_alert is never called
# so we look to see that the running arg is not stored
# FIXME: This test shows that this method should probably be refactored
with pytest.raises(ValueError):
index = test_object.alert_obj.args.index("Running...")


def test_test_mist_cloud_all_checks_pass(patch_imports, monkeypatch):
"""Tests if port is up, ip is present, dns is ok, and call returns 200"""
# Arrange
from fpms.modules.cloud_tests import CloudUtils

test_g_vars = {"result_cache": False}
test_link_state = {"set": "yes", "expected": "Eth0 Port Up: YES"}
test_ip = {"set": "192.0.2.1", "expected": "MyIP: 192.0.2.1"}
test_return_code = {"set": "200", "expected": "HTTP: OK"}
test_socket = {"set": True, "expected": "DNS: OK"}

def faked_eth0(*args, **kwargs):
if args[0] == "/sbin/ethtool eth0 | grep 'Link detected'| awk '{print $3}'":
return test_link_state["set"].encode("utf-8")
if (
args[0]
== "ip address show eth0 | grep 'inet ' | awk '{print $2}' | awk -F'/' '{print $1}'"
):
return test_ip["set"].encode("utf-8")
if (
args[0]
== 'curl -k -s -o /dev/null -w "%{http_code}" https://ep-terminator.mistsys.net/test'
):
return test_return_code["set"].encode("utf-8")

def faked_socket(*args, **kwargs):
if args[0] == "ep-terminator.mistsys.net":
test_socket["set"]

monkeypatch.setattr("subprocess.check_output", faked_eth0)
monkeypatch.setattr("socket.gethostbyname", faked_socket)

# Act
test_object = CloudUtils(g_vars=test_g_vars)
test_object.test_mist_cloud(test_g_vars)
# Assert
assert test_object.simple_table_obj.args == (
{"result_cache": True, "disable_keys": False},
[
test_link_state["expected"],
test_ip["expected"],
test_socket["expected"],
test_return_code["expected"],
],
)
assert test_object.simple_table_obj.kwargs == {"title": "Mist Cloud"}


@pytest.mark.parametrize(
"nic_state", [("no", "Eth0 Port Up: NO"), ("BROKEN", "Eth0 Port Up: Unknown")]
)
def test_test_mist_cloud_eth0_down(patch_imports, monkeypatch, nic_state):
"""Tests if eth0 is not up or unknown value comes back"""
# Arrange
from fpms.modules.cloud_tests import CloudUtils

test_g_vars = {"result_cache": False}
test_link_state = {"set": nic_state[0], "expected": nic_state[1]}

def faked_eth0(*args, **kwargs):
if args[0] == "/sbin/ethtool eth0 | grep 'Link detected'| awk '{print $3}'":
return test_link_state["set"].encode("utf-8")

monkeypatch.setattr("subprocess.check_output", faked_eth0)

# Act
test_object = CloudUtils(g_vars=test_g_vars)
test_object.test_mist_cloud(test_g_vars)
# Assert
assert test_object.simple_table_obj.args == (
{"result_cache": True, "disable_keys": False},
[test_link_state["expected"], "", "", ""],
)
assert test_object.simple_table_obj.kwargs == {"title": "Mist Cloud"}


def test_test_mist_cloud_eth0_up_no_ip(patch_imports, monkeypatch):
"""Tests if eth0 is up but there is no IP"""
# Arrange
from fpms.modules.cloud_tests import CloudUtils

test_g_vars = {"result_cache": False}
test_link_state = {"set": "yes", "expected": "Eth0 Port Up: YES"}
test_ip = {"set": "", "expected": "MyIP: None"}

def faked_eth0(*args, **kwargs):
if args[0] == "/sbin/ethtool eth0 | grep 'Link detected'| awk '{print $3}'":
return test_link_state["set"].encode("utf-8")
if (
args[0]
== "ip address show eth0 | grep 'inet ' | awk '{print $2}' | awk -F'/' '{print $1}'"
):
return test_ip["set"].encode("utf-8")

monkeypatch.setattr("subprocess.check_output", faked_eth0)

# Act
test_object = CloudUtils(g_vars=test_g_vars)
test_object.test_mist_cloud(test_g_vars)
# Assert
assert test_object.simple_table_obj.args == (
{"result_cache": True, "disable_keys": False},
[
test_link_state["expected"],
test_ip["expected"],
"",
"",
],
)
assert test_object.simple_table_obj.kwargs == {"title": "Mist Cloud"}


def test_test_mist_cloud_eth0_up_has_ip_no_dns(patch_imports, monkeypatch):
"""Tests if eth0 is up and has an IP but DNS fails"""
# Arrange
from fpms.modules.cloud_tests import CloudUtils

test_g_vars = {"result_cache": False}
test_link_state = {"set": "yes", "expected": "Eth0 Port Up: YES"}
test_ip = {"set": "192.0.2.1", "expected": "MyIP: 192.0.2.1"}
test_socket = {"expected": "DNS: FAIL"}

def faked_eth0(*args, **kwargs):
if args[0] == "/sbin/ethtool eth0 | grep 'Link detected'| awk '{print $3}'":
return test_link_state["set"].encode("utf-8")
if (
args[0]
== "ip address show eth0 | grep 'inet ' | awk '{print $2}' | awk -F'/' '{print $1}'"
):
return test_ip["set"].encode("utf-8")

def faked_socket(*args, **kwargs):
if args[0] == "ep-terminator.mistsys.net":
raise Exception

monkeypatch.setattr("subprocess.check_output", faked_eth0)
monkeypatch.setattr("socket.gethostbyname", faked_socket)

# Act
test_object = CloudUtils(g_vars=test_g_vars)
test_object.test_mist_cloud(test_g_vars)
# Assert
assert test_object.simple_table_obj.args == (
{"result_cache": True, "disable_keys": False},
[
test_link_state["expected"],
test_ip["expected"],
test_socket["expected"],
"",
],
)
assert test_object.simple_table_obj.kwargs == {"title": "Mist Cloud"}


def test_test_mist_cloud_eth0_up_has_ip_has_dns_no_200(patch_imports, monkeypatch):
"""Tests if eth0 is up, has an IP, has DNS, but does not get 200 back"""
# Arrange
from fpms.modules.cloud_tests import CloudUtils

test_g_vars = {"result_cache": False}
test_link_state = {"set": "yes", "expected": "Eth0 Port Up: YES"}
test_ip = {"set": "192.0.2.1", "expected": "MyIP: 192.0.2.1"}
test_return_code = {"set": "418", "expected": "HTTP: FAIL"}
test_socket = {"set": True, "expected": "DNS: OK"}

def faked_eth0(*args, **kwargs):
if args[0] == "/sbin/ethtool eth0 | grep 'Link detected'| awk '{print $3}'":
return test_link_state["set"].encode("utf-8")
if (
args[0]
== "ip address show eth0 | grep 'inet ' | awk '{print $2}' | awk -F'/' '{print $1}'"
):
return test_ip["set"].encode("utf-8")
if (
args[0]
== 'curl -k -s -o /dev/null -w "%{http_code}" https://ep-terminator.mistsys.net/test'
):
return test_return_code["set"].encode("utf-8")

def faked_socket(*args, **kwargs):
if args[0] == "ep-terminator.mistsys.net":
test_socket["set"]

monkeypatch.setattr("subprocess.check_output", faked_eth0)
monkeypatch.setattr("socket.gethostbyname", faked_socket)

# Act
test_object = CloudUtils(g_vars=test_g_vars)
test_object.test_mist_cloud(test_g_vars)
# Assert
assert test_object.simple_table_obj.args == (
{"result_cache": True, "disable_keys": False},
[
test_link_state["expected"],
test_ip["expected"],
test_socket["expected"],
test_return_code["expected"],
],
)
assert test_object.simple_table_obj.kwargs == {"title": "Mist Cloud"}

0 comments on commit 032d351

Please sign in to comment.