From ec34a44c3618efe0abfa17e59460634fd535b269 Mon Sep 17 00:00:00 2001 From: Sayali Gaikawad <61760125+gaiksaya@users.noreply.github.com> Date: Fri, 11 Oct 2024 13:57:22 -0700 Subject: [PATCH] Automate adding manifest to integTest notification cron (#5098) Signed-off-by: Sayali Gaikawad --- src/manifests_workflow/input_manifests.py | 23 +++++++++++++++++++ .../test_input_manifests.py | 17 ++++++++++++++ .../test_input_manifests_opensearch.py | 10 ++++++-- ...t_input_manifests_opensearch_dashboards.py | 10 ++++++-- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/manifests_workflow/input_manifests.py b/src/manifests_workflow/input_manifests.py index 9e6193bcb9..4c33120e29 100644 --- a/src/manifests_workflow/input_manifests.py +++ b/src/manifests_workflow/input_manifests.py @@ -51,6 +51,10 @@ def jenkins_path(self) -> str: def cron_jenkinsfile(self) -> str: return os.path.join(self.jenkins_path(), "check-for-build.jenkinsfile") + @classmethod + def cron_integTest_notification_jenkinsfile(self) -> str: + return os.path.join(self.jenkins_path(), "integ-test-notification.jenkinsfile") + @classmethod def os_versionincrement_workflow(self) -> str: return os.path.join(self.workflows_path(), "os-increment-plugin-versions.yml") @@ -120,6 +124,7 @@ def update( for new_version_entry in new_versions: self.write_manifest(new_version_entry, branch_versions[new_version_entry], known_versions) self.add_to_cron(new_version_entry) + self.add_to_integTest_notification_cron(new_version_entry) self.add_to_versionincrement_workflow(new_version_entry) known_versions.append(new_version_entry) @@ -212,6 +217,24 @@ def add_to_cron(self, version: str) -> None: logging.info(f"Wrote {jenkinsfile}") + def add_to_integTest_notification_cron(self, version: str) -> None: + logging.info(f"Adding new version to integ test notification cron: {version}") + integTestJenkinsfile = self.cron_integTest_notification_jenkinsfile() + with open(integTestJenkinsfile, "r") as f: + data = f.read() + + cron_entry = f"H */6 * * * %INPUT_MANIFEST={version}/{self.prefix}-{version}.yml\n" + + if cron_entry in data: + raise ValueError(f"{integTestJenkinsfile} already contains an entry for {self.prefix} {version}") + + data = data.replace("parameterizedCron('''\n", f"parameterizedCron('''\n{' ' * 12}{cron_entry}") + + with open(integTestJenkinsfile, "w") as f: + f.write(data) + + logging.info(f"Wrote {integTestJenkinsfile}") + def add_to_versionincrement_workflow(self, version: str) -> None: versionincrement_workflow_files = [self.os_versionincrement_workflow(), self.osd_versionincrement_workflow()] yaml = ruamel.yaml.YAML() diff --git a/tests/tests_manifests_workflow/test_input_manifests.py b/tests/tests_manifests_workflow/test_input_manifests.py index 75ed969516..7b7cc5d472 100644 --- a/tests/tests_manifests_workflow/test_input_manifests.py +++ b/tests/tests_manifests_workflow/test_input_manifests.py @@ -213,6 +213,12 @@ def test_cron_jenkinsfile(self) -> None: os.path.realpath(os.path.join(os.path.dirname(__file__), "..", "..", "jenkins", "check-for-build.jenkinsfile")) ) + def test_cron_integTest_notification_jenkinsfilee(self) -> None: + self.assertEqual( + InputManifests.cron_integTest_notification_jenkinsfile(), + os.path.realpath(os.path.join(os.path.dirname(__file__), "..", "..", "jenkins", "integ-test-notification.jenkinsfile")) + ) + @patch("builtins.open", new_callable=mock_open) def test_add_to_cron(self, mock_open: MagicMock) -> None: mock_open().read.return_value = "parameterizedCron '''\n" @@ -226,6 +232,17 @@ def test_add_to_cron(self, mock_open: MagicMock) -> None: "TEST_MANIFEST=0.1.2/test-0.1.2-test.yml;TEST_PLATFORM=linux;TEST_DISTRIBUTION=tar\n" ) + @patch("builtins.open", new_callable=mock_open) + def test_add_to_integTest_notification_cron(self, mock_open: MagicMock) -> None: + mock_open().read.return_value = "parameterizedCron('''\n" + input_manifests = InputManifests("test") + input_manifests.add_to_integTest_notification_cron('0.1.2') + mock_open.assert_has_calls([call(InputManifests.cron_integTest_notification_jenkinsfile(), 'w')]) + mock_open.assert_has_calls([call(InputManifests.cron_integTest_notification_jenkinsfile(), 'r')]) + mock_open().write.assert_called_once_with( + "parameterizedCron('''\n H */6 * * * %INPUT_MANIFEST=0.1.2/test-0.1.2.yml\n" + ) + def test_os_versionincrement_workflow(self) -> None: self.assertEqual( InputManifests.os_versionincrement_workflow(), diff --git a/tests/tests_manifests_workflow/test_input_manifests_opensearch.py b/tests/tests_manifests_workflow/test_input_manifests_opensearch.py index b3386bab85..a7d9c5d6af 100644 --- a/tests/tests_manifests_workflow/test_input_manifests_opensearch.py +++ b/tests/tests_manifests_workflow/test_input_manifests_opensearch.py @@ -32,9 +32,10 @@ def test_files(self) -> None: @patch("os.chdir") @patch("manifests_workflow.input_manifests.InputManifests.add_to_versionincrement_workflow") @patch("manifests_workflow.input_manifests.InputManifests.add_to_cron") + @patch("manifests_workflow.input_manifests.InputManifests.add_to_integTest_notification_cron") @patch("manifests.manifest.Manifest.to_file") @patch("manifests_workflow.input_manifests_opensearch.ComponentOpenSearchMin") - def test_update(self, mock_component_opensearch_min: MagicMock, mock_manifest_to_file: MagicMock, + def test_update(self, mock_component_opensearch_min: MagicMock, mock_manifest_to_file: MagicMock, mock_add_to_integTest_notification_cron: MagicMock, mock_add_to_cron: MagicMock, mock_add_to_versionincrement_workflow: MagicMock, *mocks: MagicMock) -> None: mock_component_opensearch_min.return_value = MagicMock(name="OpenSearch") @@ -53,6 +54,9 @@ def test_update(self, mock_component_opensearch_min: MagicMock, mock_manifest_to ) ] mock_manifest_to_file.assert_has_calls(calls) + mock_add_to_integTest_notification_cron.assert_has_calls([ + call('2.1000.1000'), + ]) mock_add_to_cron.assert_has_calls([ call('2.1000.1000'), ]) @@ -62,9 +66,10 @@ def test_update(self, mock_component_opensearch_min: MagicMock, mock_manifest_to @patch("manifests_workflow.input_manifests.InputManifests.add_to_versionincrement_workflow") @patch("manifests_workflow.input_manifests.InputManifests.add_to_cron") + @patch("manifests_workflow.input_manifests.InputManifests.add_to_integTest_notification_cron") @patch("manifests.manifest.Manifest.to_file") @patch("manifests_workflow.input_manifests_opensearch.ComponentOpenSearchMin") - def test_update_outdated_branch(self, mock_component_opensearch_min: MagicMock, mock_manifest_to_file: MagicMock, + def test_update_outdated_branch(self, mock_component_opensearch_min: MagicMock, mock_manifest_to_file: MagicMock, mock_add_to_integTest_notification_cron: MagicMock, mock_add_to_cron: MagicMock, mock_add_to_versionincrement_workflow: MagicMock) -> None: mock_component_opensearch_min.return_value = MagicMock(name="OpenSearch") mock_component_opensearch_min.branches.return_value = ["1.2"] @@ -73,4 +78,5 @@ def test_update_outdated_branch(self, mock_component_opensearch_min: MagicMock, manifests.update() self.assertEqual(mock_manifest_to_file.call_count, 0) self.assertEqual(mock_add_to_cron.call_count, 0) + self.assertEqual(mock_add_to_integTest_notification_cron.call_count, 0) self.assertEqual(mock_add_to_versionincrement_workflow.call_count, 0) diff --git a/tests/tests_manifests_workflow/test_input_manifests_opensearch_dashboards.py b/tests/tests_manifests_workflow/test_input_manifests_opensearch_dashboards.py index 395c4c25bd..54e4749398 100644 --- a/tests/tests_manifests_workflow/test_input_manifests_opensearch_dashboards.py +++ b/tests/tests_manifests_workflow/test_input_manifests_opensearch_dashboards.py @@ -32,9 +32,10 @@ def test_files(self) -> None: @patch("os.chdir") @patch("manifests_workflow.input_manifests.InputManifests.add_to_versionincrement_workflow") @patch("manifests_workflow.input_manifests.InputManifests.add_to_cron") + @patch("manifests_workflow.input_manifests.InputManifests.add_to_integTest_notification_cron") @patch("manifests.manifest.Manifest.to_file") @patch("manifests_workflow.input_manifests_opensearch_dashboards.ComponentOpenSearchDashboardsMin") - def test_update(self, mock_component_opensearch_dashboards_min: MagicMock, mock_manifest_to_file: MagicMock, + def test_update(self, mock_component_opensearch_dashboards_min: MagicMock, mock_manifest_to_file: MagicMock, mock_add_to_integTest_notification_cron: MagicMock, mock_add_to_cron: MagicMock, mock_add_to_versionincrement_workflow: MagicMock, mock_os_chdir: MagicMock, mock_os_makedirs: MagicMock) -> None: mock_component_opensearch_dashboards_min.return_value = MagicMock(name="OpenSearch-Dashboards") @@ -57,15 +58,19 @@ def test_update(self, mock_component_opensearch_dashboards_min: MagicMock, mock_ mock_add_to_cron.assert_has_calls([ call('2.1000.1000') ]) + mock_add_to_integTest_notification_cron.assert_has_calls([ + call('2.1000.1000') + ]) mock_add_to_versionincrement_workflow.assert_has_calls([ call('2.1000.1000') ]) @patch("manifests_workflow.input_manifests.InputManifests.add_to_versionincrement_workflow") @patch("manifests_workflow.input_manifests.InputManifests.add_to_cron") + @patch("manifests_workflow.input_manifests.InputManifests.add_to_integTest_notification_cron") @patch("manifests.manifest.Manifest.to_file") @patch("manifests_workflow.input_manifests_opensearch_dashboards.ComponentOpenSearchDashboardsMin") - def test_update_outdated_branch(self, mock_component_opensearch_dashboards_min: MagicMock, mock_manifest_to_file: MagicMock, + def test_update_outdated_branch(self, mock_component_opensearch_dashboards_min: MagicMock, mock_manifest_to_file: MagicMock, mock_add_to_integTest_notification_cron: MagicMock, mock_add_to_cron: MagicMock, mock_add_to_versionincrement_workflow: MagicMock) -> None: mock_component_opensearch_dashboards_min.return_value = MagicMock(name="OpenSearch-Dashboards") mock_component_opensearch_dashboards_min.branches.return_value = ["1.2"] @@ -75,4 +80,5 @@ def test_update_outdated_branch(self, mock_component_opensearch_dashboards_min: manifests.update() self.assertEqual(mock_manifest_to_file.call_count, 0) self.assertEqual(mock_add_to_cron.call_count, 0) + self.assertEqual(mock_add_to_integTest_notification_cron.call_count, 0) self.assertEqual(mock_add_to_versionincrement_workflow.call_count, 0)