|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 5 | +# or more contributor license agreements. See the NOTICE file |
| 6 | +# distributed with this work for additional information |
| 7 | +# regarding copyright ownership. The ASF licenses this file |
| 8 | +# to you under the Apache License, Version 2.0 (the |
| 9 | +# "License"); you may not use this file except in compliance |
| 10 | +# with the License. You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +# |
| 20 | +# Used to test `.build/run-ci` |
| 21 | +# Run with `python .build/run-ci.d/run-ci-test.py` |
| 22 | +# |
| 23 | +# |
| 24 | +# lint with: |
| 25 | +# `pylint --disable=C0301,W0511,C0114,C0103,W0702,C0415,C0116,C0115,R0914,W0603,R0915,R0913,R0911 run-ci-test.py` |
| 26 | + |
| 27 | + |
| 28 | +import argparse |
| 29 | +from pathlib import Path |
| 30 | +import unittest |
| 31 | +from unittest.mock import patch, MagicMock |
| 32 | + |
| 33 | + |
| 34 | +# Import the functions from the script |
| 35 | +from run_ci import ( |
| 36 | + debug, |
| 37 | + install_jenkins, |
| 38 | + get_jenkins, |
| 39 | + trigger_jenkins_build, |
| 40 | + spin_while, |
| 41 | + delete_remote_junit_files, |
| 42 | + cleanup_and_maybe_teardown, |
| 43 | + helm_installation_lock, |
| 44 | +) |
| 45 | + |
| 46 | +class TestCIPipeline(unittest.TestCase): |
| 47 | + |
| 48 | + def setUp(self): |
| 49 | + print("\ntesting ", self._testMethodName) |
| 50 | + |
| 51 | + @patch('run_ci.os.environ.get') |
| 52 | + @patch('run_ci.print') |
| 53 | + def test_debug(self, mock_print, mock_get): |
| 54 | + mock_get.return_value = "1" |
| 55 | + debug("Test message") |
| 56 | + mock_print.assert_called_with("Test message") |
| 57 | + |
| 58 | + @patch('run_ci.subprocess.run') |
| 59 | + def test_install_jenkins(self, mock_run): |
| 60 | + mock_run.return_value = MagicMock(returncode=0) |
| 61 | + install_jenkins("test-namespace", Path("/fake/cassandra/dir"), "default") |
| 62 | + mock_run.assert_any_call(["helm", "repo", "add", "jenkins", "https://charts.jenkins.io"], check=True) |
| 63 | + mock_run.assert_any_call(["helm", "repo", "update"], check=True) |
| 64 | + |
| 65 | + @patch('run_ci.subprocess.run') |
| 66 | + @patch('run_ci.jenkins.Jenkins') |
| 67 | + def test_get_jenkins(self, mock_jenkins, mock_run): |
| 68 | + mock_k8s_client = MagicMock() |
| 69 | + mock_run.return_value = MagicMock(stdout="fake-password") |
| 70 | + mock_jenkins_instance = MagicMock() |
| 71 | + mock_jenkins.return_value = mock_jenkins_instance |
| 72 | + # hack – use False values instead of None |
| 73 | + args = argparse.Namespace(kubeconfig="/fake/kubeconfig", kubecontext="test-context", user=False, url=False) |
| 74 | + _, server = get_jenkins(mock_k8s_client, args, "default") |
| 75 | + self.assertEqual(server, mock_jenkins_instance) |
| 76 | + |
| 77 | + @patch('run_ci.jenkins.Jenkins.build_job') |
| 78 | + @patch('run_ci.wait_for_build_number') |
| 79 | + def test_trigger_jenkins_build(self, mock_wait_for_build_number, mock_build_job): |
| 80 | + mock_server = MagicMock() |
| 81 | + mock_build_job.return_value = mock_server.build_job.return_value = 123 |
| 82 | + mock_wait_for_build_number.return_value = 456 |
| 83 | + with patch('run_ci.spin_while', side_effect=lambda msg, condition: 0): |
| 84 | + queue_item = trigger_jenkins_build(mock_server, "test-job", param1="value1") |
| 85 | + self.assertEqual(queue_item, 123) |
| 86 | + |
| 87 | + def test_spin_while(self): |
| 88 | + result = spin_while("Testing", lambda: True) |
| 89 | + self.assertEqual(result, 0) |
| 90 | + |
| 91 | + @patch('run_ci.stream.stream') |
| 92 | + def test_delete_remote_junit_files(self, mock_stream): |
| 93 | + mock_k8s_client = MagicMock() |
| 94 | + delete_remote_junit_files(mock_k8s_client, "test-pod", "test-namespace", 456) |
| 95 | + mock_stream.assert_called() |
| 96 | + |
| 97 | + @patch('run_ci.subprocess.run') |
| 98 | + def test_cleanup_and_maybe_teardown(self, mock_run): |
| 99 | + cleanup_and_maybe_teardown(None, None, "test-namespace", True) |
| 100 | + mock_run.assert_called_with(["helm", "--namespace", "test-namespace", "uninstall", "cassius"], check=True) |
| 101 | + |
| 102 | + @patch('run_ci.fcntl.flock') |
| 103 | + def test_helm_installation_lock(self, mock_flock): |
| 104 | + with helm_installation_lock(Path("/tmp/.fake.lock")): |
| 105 | + mock_flock.assert_called() |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + unittest.main() |
0 commit comments