← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~maas-committers/maas-ci/+git/system-tests:ansible-systemtests-performance into ~maas-committers/maas-ci/+git/system-tests:master

 

Jack Lloyd-Walters has proposed merging ~maas-committers/maas-ci/+git/system-tests:ansible-systemtests-performance into ~maas-committers/maas-ci/+git/system-tests:master.

Commit message:
Implement performance and log-file changes in ansible system-tests

Requested reviews:
  MAAS Committers (maas-committers)

For more details, see:
https://code.launchpad.net/~maas-committers/maas-ci/+git/system-tests/+merge/439191
-- 
Your team MAAS Committers is requested to review the proposed merge of ~maas-committers/maas-ci/+git/system-tests:ansible-systemtests-performance into ~maas-committers/maas-ci/+git/system-tests:master.
diff --git a/config.yaml.sample b/config.yaml.sample
index e656a27..b22e4a9 100644
--- a/config.yaml.sample
+++ b/config.yaml.sample
@@ -121,3 +121,4 @@ o11y:
 ansible-playbooks:
     git-repo: https://github.com/maas/maas-ansible-playbook.git
     git-branch: main
+    verbosity: 
diff --git a/systemtests/ansible.py b/systemtests/ansible.py
index 8db10f9..8cf0ab2 100644
--- a/systemtests/ansible.py
+++ b/systemtests/ansible.py
@@ -17,7 +17,7 @@ from .lxd import Instance
 if TYPE_CHECKING:
     from logging import Logger
 
-    from .lxd import CLILXD, _FileWrapper
+    from .lxd import CLILXD
 NAME = "systemtests.ansible"
 LOG = getLogger(NAME)
 
@@ -284,6 +284,7 @@ class AnsibleMain:
         playbooks_repo: str,
         playbooks_branch: str,
         proxy_env: Optional[dict[str, str]],
+        debug: str = "",
     ) -> None:
         self._lxd = lxd
         self.instance = instance
@@ -297,6 +298,7 @@ class AnsibleMain:
         self._inventory_: set[AnsibleHost] = set()
 
         self.ansible_repo_path = "/home/ubuntu/ansible_repo"
+        self.default_debug = debug or ""
 
     def setup(self) -> None:
         self.logger.info("Installing python3-pip")
@@ -465,6 +467,7 @@ class AnsibleMain:
                 ["maas", "apikey", "--username", user]
             ).stdout.rstrip("\n")
             _, authd_client = maas_client.log_in("admin", api_key)
+            authd_client.refresh()
             return authd_client
         raise HostWithoutRegion()
 
@@ -504,33 +507,41 @@ class AnsibleMain:
                 ]
             )
         file_content = "\n".join(inv)
-        LOG.info(f"Ansible hosts file generated:\n{file_content}")
+        if self.default_debug:
+            self.logger.info("Ansible hosts file generated:")
+            for line in file_content:
+                self.logger.info(line)
         self._hosts_file.write(file_content)
 
     def create_config_file(self) -> None:
-        def append_if_not_found(
-            content: str, file_: _FileWrapper, loose_check: bool = True
-        ) -> None:
-            search = content if not loose_check else content.split()[0]
-            if search not in file_.read():
-                file_.append(content)
+        cfg_dict = {
+            "host_key_checking": "False",
+            "remote_user": "ubuntu",
+            "deprecation_warnings": "False",
+            "callback_result_format": "yaml",
+            "timeout": "60",
+            "display_skipped_hosts": "False",
+            "display_ok_hosts": "False",
+        }
 
         path = f"{self.ansible_repo_path}/ansible.cfg"
         ansible_cfg = self.instance.files[path]
         if not ansible_cfg.exists():
             ansible_cfg.write("[defaults]\ninventory = hosts\n")
-        append_if_not_found("host_key_checking = False", ansible_cfg)
-        append_if_not_found("remote_user = ubuntu", ansible_cfg)
-        append_if_not_found("deprecation_warnings = False", ansible_cfg)
+        ansible_cfg_content = ansible_cfg.read().split("\n")
+        
         if self.use_timeout:
-            append_if_not_found(
-                f"task_timeout = {int(self.timeout.total_seconds())}", ansible_cfg
-            )
+            cfg_dict["task_timeout"] = f"{int(self.timeout.total_seconds())}"
+        for k, v in cfg_dict.items():
+            if k not in ansible_cfg_content:
+                ansible_cfg_content.append(f"{k} = {v}")
+
+        ansible_cfg.write("\n".join(ansible_cfg_content))
         self.instance.execute(["mkdir", "-p", "/etc/ansible"])
         etc_ansible_cfg = self.instance.files["/etc/ansible/ansible.cfg"]
         etc_ansible_cfg.write(ansible_cfg.read())
 
-    def run_playbook(self, playbook: str = "site.yaml", debug: str = "-v") -> None:
+    def run_playbook(self, playbook: str = "site.yaml", debug: str = "") -> None:
         self.create_hosts_file()
         cmd: list[str] = [
             "eatmydata",
@@ -541,6 +552,6 @@ class AnsibleMain:
             "--private-key",
             self.ssh_key_file[:-4],
         ]
-        if _debug := re.match(r"-(v)+", debug):
+        if _debug := re.match(r"-(v)+", debug or self.default_debug or ""):
             cmd.append(str(_debug.group()))
         self.instance.execute(cmd, environment=self._proxy_env)
diff --git a/systemtests/ansible_tests/test_ansible.py b/systemtests/ansible_tests/test_ansible.py
index 0b89249..10fb754 100644
--- a/systemtests/ansible_tests/test_ansible.py
+++ b/systemtests/ansible_tests/test_ansible.py
@@ -9,6 +9,7 @@ from systemtests.ansible import AnsibleMain, pip_package_exists
 if TYPE_CHECKING:
     from logging import Logger
 
+DEFAULT_MAAS_VERSION = "3.3"
 
 @pytest.mark.skip_if_ansible_playbooks_unconfigured(
     "Needs Ansible playbook configuration"
@@ -62,12 +63,12 @@ class TestAnsibleMAAS:
                     "maas_installation_type": installation_type,
                     "maas_postgres_password": "sekret",
                     "maas_url": f"http://{regionrack_host.ip}:5240/MAAS";,
-                    "maas_version": "3.3",
+                    "maas_version": DEFAULT_MAAS_VERSION,
                 }
             )
-            ansible_main.run_playbook("site.yaml", "-vv")
+            ansible_main.run_playbook("site.yaml")
             region = ansible_main.fetch_region(regionrack_host)
-            assert region.read_version_information()["version"][:3] == "3.3"
+            assert region.read_version_information()["version"][:3] == DEFAULT_MAAS_VERSION
         assert not ansible_main._inventory_
         assert not regionrack_host.instance.exists()
         assert not database.instance.exists()
@@ -77,9 +78,9 @@ class TestAnsibleMAAS:
     ) -> None:
         ansible_main.logger = testlog
         start_version = "3.1"
-        upgrade_version = "3.3"
+        upgrade_version = DEFAULT_MAAS_VERSION
         database = ansible_main.add_host().add_postgres_primary()
-        host = ansible_main.add_host().add_region_rack()
+        host = ansible_main.add_host(image="ubuntu:22.04").add_region_rack()
 
         with ansible_main.collect_inventory():
             ansible_main.update_config(
@@ -90,12 +91,13 @@ class TestAnsibleMAAS:
                     "maas_version": start_version,
                 }
             )
-            ansible_main.run_playbook("site.yaml", "-vvv")
+            ansible_main.run_playbook("site.yaml")
             region = ansible_main.fetch_region(host)
             assert region.read_version_information()["version"][:3] == start_version
 
             ansible_main.update_config({"maas_version": upgrade_version})
-            ansible_main.run_playbook("site.yaml", "-vvv")
+            ansible_main.run_playbook("site.yaml")
+            region = ansible_main.fetch_region(host)
             region.refresh()
             assert region.read_version_information()["version"][:3] == upgrade_version
         assert not ansible_main._inventory_
diff --git a/systemtests/fixtures.py b/systemtests/fixtures.py
index f035b4e..ce4c5cd 100644
--- a/systemtests/fixtures.py
+++ b/systemtests/fixtures.py
@@ -61,6 +61,7 @@ def ansible_main(config: dict[str, Any]) -> Optional[Iterator[AnsibleMain]]:
         playbooks_repo=playbooks_repo,
         playbooks_branch=playbooks_branch,
         proxy_env=proxy_env,
+        debug=playbooks_config.get("verbosity", ""),
     )
     main.setup()
     yield main
@@ -79,7 +80,7 @@ def maas_from_ansible(ansible_main: AnsibleMain) -> Iterator[AuthenticatedAPICli
                 "maas_url": f"http://{host.ip}:5240/MAAS";,
             }
         )
-        ansible_main.run_playbook("site.yaml", "-vv")
+        ansible_main.run_playbook("site.yaml")
         yield ansible_main.fetch_region(host)
 
 

Follow ups