|
| 1 | +import os |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import csle_common.constants.constants as constants |
| 4 | +from csle_common.util.cluster_util import ClusterUtil |
| 5 | + |
| 6 | + |
| 7 | +class TestClusterUtilSuite: |
| 8 | + """ |
| 9 | + Test suite for cluster util |
| 10 | + """ |
| 11 | + |
| 12 | + def test_am_i_leader(self) -> None: |
| 13 | + """ |
| 14 | + Test function that checks if a given IP is a leader or not |
| 15 | + """ |
| 16 | + config = MagicMock() |
| 17 | + config.cluster_config.cluster_nodes.ip = "192.168.1.1" |
| 18 | + config.cluster_config.cluster_nodes.leader = False |
| 19 | + config.cluster_config.cluster_nodes.cpus = 1 |
| 20 | + config.cluster_config.cluster_nodes.gpus = 2 |
| 21 | + config.cluster_config.cluster_nodes.RAM = 8 |
| 22 | + assert not ClusterUtil.am_i_leader("192.168.1.1", config) |
| 23 | + |
| 24 | + @patch("psycopg.connect") |
| 25 | + @patch("csle_common.util.cluster_util.ClusterUtil.get_config") |
| 26 | + @patch.dict(os.environ, {"CSLE_HOME": "/mock/csle/home"}) |
| 27 | + def test_get_config(self, mock_get_config, mock_connect) -> None: |
| 28 | + """ |
| 29 | + Test function that gets the current cluster config from the metastore or from disk |
| 30 | + depending on if it is the leader node or not |
| 31 | +
|
| 32 | + :param mock_get_config: mock_get_config |
| 33 | + :param mock_connect: mock_connect |
| 34 | +
|
| 35 | + :return: None |
| 36 | + """ |
| 37 | + mock_config = MagicMock() |
| 38 | + mock_get_config.return_value = mock_config |
| 39 | + from csle_common.util.cluster_util import ClusterUtil |
| 40 | + |
| 41 | + result = ClusterUtil.get_config() |
| 42 | + assert result == mock_config |
| 43 | + |
| 44 | + @patch.dict(os.environ, {"CSLE_HOME": "/mock/csle/home"}) |
| 45 | + @patch("csle_common.util.cluster_util.ClusterUtil.get_config") |
| 46 | + def test_set_config_parameters_from_config_file(self, mock_get_config) -> None: |
| 47 | + """ |
| 48 | + Test function that reads the config file from $CSLE_HOME/config.json and initializes certain config parameters |
| 49 | +
|
| 50 | + :param mock_get_config: mock_get_config |
| 51 | +
|
| 52 | + :return: None |
| 53 | + """ |
| 54 | + mock_config = MagicMock() |
| 55 | + mock_config.management_admin_username_default = "admin" |
| 56 | + mock_config.management_admin_password_default = "admin_pw" |
| 57 | + mock_config. management_admin_email_default = "[email protected]" |
| 58 | + mock_config.management_admin_organization_default = "Example Org" |
| 59 | + mock_config.management_admin_first_name_default = "Admin" |
| 60 | + mock_config.management_admin_last_name_default = "User" |
| 61 | + mock_config.ssh_admin_username = "ssh_admin" |
| 62 | + mock_config.ssh_admin_password = "ssh_pw" |
| 63 | + mock_config.management_guest_username_default = "guest" |
| 64 | + mock_config.management_guest_password_default = "guest_pw" |
| 65 | + mock_config. management_guest_email_default = "[email protected]" |
| 66 | + mock_config.management_guest_organization_default = "Example Org" |
| 67 | + mock_config.management_guest_first_name_default = "Guest" |
| 68 | + mock_config.management_guest_last_name_default = "User" |
| 69 | + mock_config.ssh_agent_username = "agent" |
| 70 | + mock_config.ssh_agent_password = "agent_pw" |
| 71 | + mock_config.metastore_user = "metastore_user" |
| 72 | + mock_config.metastore_password = "metastore_pw" |
| 73 | + mock_config.metastore_database_name = "metastore_db" |
| 74 | + mock_config.pgadmin_username = "pgadmin" |
| 75 | + mock_config.pgadmin_password = "pgadmin_pw" |
| 76 | + mock_config.grafana_username = "grafana" |
| 77 | + mock_config.grafana_password = "grafana_pw" |
| 78 | + mock_config.node_exporter_port = 9100 |
| 79 | + mock_config.grafana_port = 3000 |
| 80 | + mock_config.management_system_port = 8000 |
| 81 | + mock_config.cadvisor_port = 8080 |
| 82 | + mock_config.pgadmin_port = 5050 |
| 83 | + mock_config.prometheus_port = 9090 |
| 84 | + mock_config.node_exporter_pid_file = "/var/run/node_exporter.pid" |
| 85 | + mock_config.csle_mgmt_webapp_pid_file = "/var/run/csle_mgmt_webapp.pid" |
| 86 | + mock_config.node_exporter_log_file = "/var/log/node_exporter.log" |
| 87 | + mock_config.docker_stats_manager_outfile = "/var/log/docker_stats.out" |
| 88 | + mock_config.docker_stats_manager_pidfile = "/var/run/docker_stats.pid" |
| 89 | + mock_config.prometheus_pid_file = "/var/run/prometheus.pid" |
| 90 | + mock_config.prometheus_log_file = "/var/log/prometheus.log" |
| 91 | + mock_config.postgresql_log_dir = "/var/log/postgresql" |
| 92 | + mock_config.nginx_log_dir = "/var/log/nginx" |
| 93 | + mock_config.flask_log_file = "/var/log/flask.log" |
| 94 | + mock_config.default_log_dir = "/var/log" |
| 95 | + |
| 96 | + mock_get_config.return_value = mock_config |
| 97 | + |
| 98 | + ClusterUtil.set_config_parameters_from_config_file() |
| 99 | + |
| 100 | + assert constants.CONFIG_FILE.PARSED_CONFIG == mock_config |
| 101 | + assert constants.CSLE_ADMIN.MANAGEMENT_USER == "admin" |
| 102 | + assert constants.CSLE_ADMIN.MANAGEMENT_PW == "admin_pw" |
| 103 | + assert constants. CSLE_ADMIN. MANAGEMENT_EMAIL == "[email protected]" |
| 104 | + assert constants.CSLE_ADMIN.MANAGEMENT_ORGANIZATION == "Example Org" |
| 105 | + assert constants.CSLE_ADMIN.MANAGEMENT_FIRST_NAME == "Admin" |
| 106 | + assert constants.CSLE_ADMIN.MANAGEMENT_LAST_NAME == "User" |
| 107 | + assert constants.CSLE_ADMIN.SSH_USER == "ssh_admin" |
| 108 | + assert constants.CSLE_ADMIN.SSH_PW == "ssh_pw" |
| 109 | + assert constants.CSLE_GUEST.MANAGEMENT_USER == "guest" |
| 110 | + assert constants.CSLE_GUEST.MANAGEMENT_PW == "guest_pw" |
| 111 | + assert constants. CSLE_GUEST. MANAGEMENT_EMAIL == "[email protected]" |
| 112 | + assert constants.CSLE_GUEST.MANAGEMENT_ORGANIZATION == "Example Org" |
| 113 | + assert constants.CSLE_GUEST.MANAGEMENT_FIRST_NAME == "Guest" |
| 114 | + assert constants.CSLE_GUEST.MANAGEMENT_LAST_NAME == "User" |
| 115 | + assert constants.AGENT.USER == "agent" |
| 116 | + assert constants.AGENT.PW == "agent_pw" |
| 117 | + assert constants.METADATA_STORE.USER == "metastore_user" |
| 118 | + assert constants.METADATA_STORE.PASSWORD == "metastore_pw" |
| 119 | + assert constants.METADATA_STORE.DBNAME == "metastore_db" |
| 120 | + assert constants.COMMANDS.PGADMIN_USERNAME == "pgadmin" |
| 121 | + assert constants.COMMANDS.PGADMIN_PW == "pgadmin_pw" |
| 122 | + assert constants.COMMANDS.GRAFANA_USERNAME == "grafana" |
| 123 | + assert constants.COMMANDS.GRAFANA_PW == "grafana_pw" |
| 124 | + assert constants.COMMANDS.NODE_EXPORTER_PORT == 9100 |
| 125 | + assert constants.COMMANDS.GRAFANA_PORT == 3000 |
| 126 | + assert constants.COMMANDS.MANAGEMENT_SYSTEM_PORT == 8000 |
| 127 | + assert constants.COMMANDS.CADVISOR_PORT == 8080 |
| 128 | + assert constants.COMMANDS.PGADMIN_PORT == 5050 |
| 129 | + assert constants.COMMANDS.PROMETHEUS_PORT == 9090 |
| 130 | + assert constants.COMMANDS.NODE_EXPORTER_PID_FILE == "/var/run/node_exporter.pid" |
| 131 | + assert constants.COMMANDS.CSLE_MGMT_WEBAPP_PID_FILE == "/var/run/csle_mgmt_webapp.pid" |
| 132 | + assert constants.COMMANDS.NODE_EXPORTER_LOG_FILE == "/var/log/node_exporter.log" |
| 133 | + assert constants.COMMANDS.DOCKER_STATS_MANAGER_OUTFILE == "/var/log/docker_stats.out" |
| 134 | + assert constants.COMMANDS.DOCKER_STATS_MANAGER_PIDFILE == "/var/run/docker_stats.pid" |
| 135 | + assert constants.COMMANDS.PROMETHEUS_PID_FILE == "/var/run/prometheus.pid" |
| 136 | + assert constants.COMMANDS.PROMETHEUS_LOG_FILE == "/var/log/prometheus.log" |
| 137 | + assert constants.COMMANDS.POSTGRESQL_LOG_DIR == "/var/log/postgresql" |
| 138 | + assert constants.COMMANDS.NGINX_LOG_DIR == "/var/log/nginx" |
| 139 | + assert constants.COMMANDS.FLASK_LOG_FILE == "/var/log/flask.log" |
| 140 | + assert constants.LOGGING.DEFAULT_LOG_DIR == "/var/log" |
0 commit comments