Skip to content

Commit

Permalink
fix / object comparisson always results in no differences (#124)
Browse files Browse the repository at this point in the history
* fix object comparisson

* clean up test

* reformat everything
  • Loading branch information
Kim Fehrs authored May 30, 2024
1 parent 89a3e08 commit 291849a
Show file tree
Hide file tree
Showing 17 changed files with 224 additions and 196 deletions.
6 changes: 3 additions & 3 deletions node-runner-cli/api/CustomAPIClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def set_default_header(self, header_name, header_value):
self.default_headers[header_name] = header_value

def prepare(
self,
http_method,
http_path,
self,
http_method,
http_path,
):
req = requests.Request(
http_method, f"{self._base_path}{http_path}", headers=self.default_headers
Expand Down
6 changes: 5 additions & 1 deletion node-runner-cli/commands/dockercommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ def install(args):

########## Update existing Config
docker_config: DockerConfig = DockerSetup.load_settings(argument_object.config_file)

original_config_dict = docker_config.to_dict()

docker_config_updated_versions = (
DockerSetup.update_versions(docker_config, argument_object.autoapprove)
if argument_object.update
Expand All @@ -251,8 +254,9 @@ def install(args):
docker_config_updated_versions = DockerSetup.check_set_passwords(
docker_config_updated_versions
)

DockerSetup.confirm_config_changes(
argument_object, docker_config, docker_config_updated_versions
argument_object, original_config_dict, docker_config_updated_versions
)

########## Install dependent services
Expand Down
12 changes: 10 additions & 2 deletions node-runner-cli/config/BaseConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import yaml

from deepdiff import DeepDiff


class BaseConfig:
def __iter__(self):
Expand Down Expand Up @@ -52,8 +54,8 @@ def to_dict(self):
for attr, value in class_variables.items():
returning_dict[attr] = ""
if (
type(self.__getattribute__(attr)) not in (str, int, bool, dict)
and self.__getattribute__(attr) is not None
type(self.__getattribute__(attr)) not in (str, int, bool, dict)
and self.__getattribute__(attr) is not None
):
returning_dict[attr] = self.__getattribute__(attr).to_dict()
else:
Expand All @@ -79,6 +81,12 @@ def to_file(self, config_file):
f.write("# than manually edit this file\n")
yaml.dump(config_to_dump, f, sort_keys=True, default_flow_style=False)

def compare_to_dict(self, config_as_dict: dict) -> dict:
return dict(DeepDiff(config_as_dict, self.to_dict()))

def compare_to_object(self, config_object: BaseConfig) -> dict:
return dict(DeepDiff(config_object.to_dict(), self.to_dict()))


class SetupMode:
_instance = None
Expand Down
2 changes: 1 addition & 1 deletion node-runner-cli/config/Genesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create_genesis_file(genesis_json_location: str, genesis: str):

@staticmethod
def copy_genesis_file(
genesis_bin_data_file: str, genesis_files="testnet-genesis"
genesis_bin_data_file: str, genesis_files="testnet-genesis"
) -> str:
bundle_dir = getattr(sys, "_MEIPASS", os.getcwd())
path_to_genesis_bin_file = getenv(
Expand Down
2 changes: 1 addition & 1 deletion node-runner-cli/config/Renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Renderer:
def load_file_based_template(
self, template_file_name: str, templates_dir="templates"
self, template_file_name: str, templates_dir="templates"
):
templates_path = join(dirname(dirname(__file__)), templates_dir)
self.env = Environment(
Expand Down
2 changes: 1 addition & 1 deletion node-runner-cli/config/SystemDConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def append_advanced_user_config(self, config_file: str, advanced_config_file: st
f1.write(f2.read())

def create_service_file(
self, service_file_path="/etc/systemd/system/radixdlt-node.service"
self, service_file_path="/etc/systemd/system/radixdlt-node.service"
):
# This may need to be moved to jinja template
tmp_service: str = "/tmp/radixdlt-node.service"
Expand Down
38 changes: 21 additions & 17 deletions node-runner-cli/setup/DockerSetup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import getpass
import json
import os
import sys

Expand Down Expand Up @@ -36,6 +35,7 @@ def print_questionary_header(config_file):


class DockerSetup(BaseSetup):

@staticmethod
def save_config(config: DockerConfig, config_file: str, autoapprove=False):
to_update = ""
Expand Down Expand Up @@ -330,8 +330,9 @@ def questionary(argument_object: DockerConfigArguments) -> DockerConfig:
docker_config.common_config.ask_enable_nginx_for_core(
argument_object.nginx_on_core
)
docker_config.core_node.engine_state_enabled = BaseSetup.ask_engine_state_api(
argument_object.autoapprove)
docker_config.core_node.engine_state_enabled = (
BaseSetup.ask_engine_state_api(argument_object.autoapprove)
)
else:
del docker_config.core_node

Expand All @@ -345,7 +346,6 @@ def questionary(argument_object: DockerConfigArguments) -> DockerConfig:
else:
del docker_config.gateway


if (
"MIGRATION" in argument_object.setupmode.mode
and docker_config.core_node is not None
Expand All @@ -372,11 +372,12 @@ def compare_config_file_with_config_object(
if os.path.exists(config_file):
old_config: DockerConfig = DockerSetup.load_settings(config_file)
if old_config is not None:
differences = config_object.compare_to_object(old_config)
logger.info(
f"""
{Helpers.section_headline("Differences")}
Difference between existing config file and new config that you are creating
{dict(DeepDiff(old_config, config_object.to_dict()))}
{differences}
"""
)

Expand All @@ -400,23 +401,20 @@ def render_docker_compose(docker_config: DockerConfig):
@staticmethod
def confirm_config_changes(
argument_object: DockerInstallArguments,
docker_config,
docker_config_updated_versions,
original_config_dict: dict,
updated_config_object: DockerConfig,
):
config_differences = dict(
DeepDiff(docker_config, docker_config_updated_versions)
)
config_differences = updated_config_object.compare_to_dict(original_config_dict)

if len(config_differences) != 0:
print(
f"""
{Helpers.section_headline("Differences in config file with updated software versions")}
Difference between existing config file and new config that you are creating
{config_differences}
"""
{config_differences} """
)
DockerSetup.save_config(
docker_config_updated_versions,
updated_config_object,
argument_object.config_file,
argument_object.autoapprove,
)
Expand All @@ -425,13 +423,17 @@ def confirm_config_changes(
def confirm_docker_compose_file_changes(
docker_config: DockerConfig, autoapprove: bool
):
docker_compose_yaml: yaml = DockerSetup.render_docker_compose(docker_config)
docker_compose_yaml_rendered: yaml = DockerSetup.render_docker_compose(
docker_config
)
backup_time = Helpers.get_current_date_time()
compose_file_yaml = DockerSetup.get_existing_compose_file(
docker_compose_yaml_from_file = DockerSetup.get_existing_compose_file(
docker_config.common_config.docker_compose
)
compose_file = docker_config.common_config.docker_compose
compose_file_difference = dict(DeepDiff(compose_file_yaml, docker_compose_yaml))
compose_file_difference = dict(
DeepDiff(docker_compose_yaml_from_file, docker_compose_yaml_rendered)
)
if len(compose_file_difference) != 0:
logger.info(
f"""
Expand All @@ -451,7 +453,9 @@ def confirm_docker_compose_file_changes(
if Helpers.check_Yes(to_update) or autoapprove:
if os.path.exists(compose_file):
Helpers.backup_file(compose_file, f"{compose_file}_{backup_time}")
DockerSetup.save_compose_file(compose_file, docker_compose_yaml)
DockerSetup.save_compose_file(
compose_file, docker_compose_yaml_rendered
)
run_shell_command(f"cat {compose_file}", shell=True)
return compose_file

Expand Down
8 changes: 5 additions & 3 deletions node-runner-cli/setup/SystemDSetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,14 @@ def load_settings(config_file) -> SystemDConfig:
def compare_old_and_new_config(config_file: str, systemd_config: SystemDConfig):
old_config_object = SystemDSetup.load_settings(config_file)
old_config = old_config_object.to_dict()
config_to_dump = systemd_config.to_dict()
if old_config is not None:
if len(old_config) != 0:
differences = systemd_config.compare_to_dict(old_config)
print(
f"""
{Helpers.section_headline("Differences")}
Difference between existing config file and new config that you are creating
{dict(DeepDiff(old_config, config_to_dump))}
{differences}
"""
)

Expand Down Expand Up @@ -513,7 +513,9 @@ def ask_core_node(argument_object: SystemDConfigArguments) -> CoreSystemdConfig:
systemd_config.core_node.keydetails = BaseSetup.ask_keydetails(
argument_object.keystore_password, argument_object.new_keystore
)
systemd_config.core_node.engine_state_enabled = BaseSetup.ask_engine_state_api(argument_object.auto_approve)
systemd_config.core_node.engine_state_enabled = BaseSetup.ask_engine_state_api(
argument_object.auto_approve
)
return systemd_config.core_node

@staticmethod
Expand Down
10 changes: 10 additions & 0 deletions node-runner-cli/tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import yaml
from yaml import UnsafeLoader
from deepdiff import DeepDiff

from config.DockerConfig import DockerConfig
from config.Nginx import SystemdNginxConfig
from config.SystemDConfig import SystemDConfig
from utils.Network import Network
Expand Down Expand Up @@ -39,6 +41,14 @@ def test_network_id_can_be_parsed(self):
self.assertEqual(Network.validate_network_id("S"), 2)
self.assertEqual(Network.validate_network_id("stokenet"), 2)

def test_compare_to_dict(self):
config: DockerConfig = DockerConfig({})
config_dict = config.to_dict()
config.core_node.core_release = "randomvalue"
self.assertTrue(len(config.compare_to_dict(config_dict)) != 0)
config_dict = config.to_dict()
self.assertTrue(len(config.compare_to_dict(config_dict)) == 0)


def suite():
"""This defines all the tests of a module"""
Expand Down
32 changes: 16 additions & 16 deletions node-runner-cli/tests/unit/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ class DockerUnitTests(unittest.TestCase):
def test_docker_config(self, mockout):
urllib3.disable_warnings()
with patch(
"builtins.input",
side_effect=["N", "N", "/home/runner/docker-compose.yml", "N"],
"builtins.input",
side_effect=["N", "N", "/home/runner/docker-compose.yml", "N"],
):
with patch(
"sys.argv",
[
"main",
"docker",
"config",
"-m",
"DETAILED",
"-n",
"2",
"-k",
"radix",
"-nk",
"-a",
],
"sys.argv",
[
"main",
"docker",
"config",
"-m",
"DETAILED",
"-n",
"2",
"-k",
"radix",
"-nk",
"-a",
],
):
main()

Expand Down
2 changes: 1 addition & 1 deletion node-runner-cli/tests/unit/test_gateway_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test_setup_gateway_compose_file_fixture_test(self, mockout):
self.assertEqual(f1.read(), f2.read())

def expect_ask_gateway_inputs_get_inserted_into_object(
self, config, questionary_keyboard_input
self, config, questionary_keyboard_input
):
self.assertEqual(
questionary_keyboard_input[0],
Expand Down
14 changes: 7 additions & 7 deletions node-runner-cli/tests/unit/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,24 @@ def test_template_failure(self, mock_stdout):
@mock.patch("sys.stdout", new_callable=StringIO)
def test_monitoring_config(self, mock_out):
with mock.patch(
"builtins.input",
side_effect=["Y", "https://45.152.180.182", "metrics", "testpassword", "n"],
"builtins.input",
side_effect=["Y", "https://45.152.180.182", "metrics", "testpassword", "n"],
):
with mock.patch("sys.argv", ["main", "monitoring", "config"]):
main()

with mock.patch(
"sys.argv",
["main", "monitoring", "config", "-m", "MONITOR_CORE", "-cm", "test"],
"sys.argv",
["main", "monitoring", "config", "-m", "MONITOR_CORE", "-cm", "test"],
):
main()

with mock.patch(
"builtins.input",
side_effect=["Y", "https://45.152.180.182", "metrics", "testpassword", "n"],
"builtins.input",
side_effect=["Y", "https://45.152.180.182", "metrics", "testpassword", "n"],
):
with mock.patch(
"sys.argv", ["main", "monitoring", "config", "-m", "DETAILED"]
"sys.argv", ["main", "monitoring", "config", "-m", "DETAILED"]
):
main()

Expand Down
Loading

0 comments on commit 291849a

Please sign in to comment.