diff --git a/.github/workflows/unit-test.yaml b/.github/workflows/unit-test.yaml new file mode 100644 index 0000000..d34202c --- /dev/null +++ b/.github/workflows/unit-test.yaml @@ -0,0 +1,16 @@ +on: [push] + +jobs: + plain_test: + runs-on: ubuntu-latest + name: Unit test + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' + - name: Install dependencies + run: python -m pip install --upgrade pip prettytable + - name: Run suite + run: python test_summary.py -v diff --git a/.gitignore b/.gitignore index 69adda3..dc8d798 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # editor and IDE paraphernalia .idea +__pycache__/ \ No newline at end of file diff --git a/DEVELOPERS_DEVELOPERS_DEVELOPERS.md b/DEVELOPERS_DEVELOPERS_DEVELOPERS.md index 6d49fd2..f9ffa9c 100644 --- a/DEVELOPERS_DEVELOPERS_DEVELOPERS.md +++ b/DEVELOPERS_DEVELOPERS_DEVELOPERS.md @@ -39,6 +39,14 @@ act -b --env GITHUB_STEP_SUMMARY='github-summary.md' Running this should create a file `github-summary.md` with the test summary. +## Unit tests + +CIclops has the beginnig of a unit test suite. You can run it with: + +``` sh +python3 -m unittest +``` + ## How it works The files in this repository are needed for the Dockerfile to build and run, of diff --git a/few-artifacts/id1_0b185c51a60964ecab5bb7d97458ca95fd421f325f3896ed239d5d3f.json b/few-artifacts/id1_0b185c51a60964ecab5bb7d97458ca95fd421f325f3896ed239d5d3f.json new file mode 100644 index 0000000..fa02b5b --- /dev/null +++ b/few-artifacts/id1_0b185c51a60964ecab5bb7d97458ca95fd421f325f3896ed239d5d3f.json @@ -0,0 +1,17 @@ +{ + "name": "InitDB settings - custom default locale -- use the custom default locale specified", + "state": "passed", + "start_time": "2021-11-29T18:29:12.613387+01:00", + "end_time": "2021-11-29T18:31:07.988145+01:00", + "error": "", + "error_file": "", + "error_line": 0, + "platform": "local", + "postgres_kind": "PostgreSQL", + "matrix_id": "id1", + "postgres_version": "11.1", + "k8s_version": "22", + "workflow_id": 12, + "repo": "my-repo", + "branch": "my-branch" +} \ No newline at end of file diff --git a/few-artifacts/id1_4902843ff6a60bc4fdb76000698c46de8e7f9763a1a0fe63f70fd5f8.json b/few-artifacts/id1_4902843ff6a60bc4fdb76000698c46de8e7f9763a1a0fe63f70fd5f8.json new file mode 100644 index 0000000..857513e --- /dev/null +++ b/few-artifacts/id1_4902843ff6a60bc4fdb76000698c46de8e7f9763a1a0fe63f70fd5f8.json @@ -0,0 +1,17 @@ +{ + "name": "InitDB settings - initdb custom post-init SQL scripts -- can find the tables created by the post-init SQL queries", + "state": "failed", + "start_time": "2021-11-29T18:28:37.198931+01:00", + "end_time": "2021-11-29T18:29:12.613157+01:00", + "error": "Expected\n : 1\\n\nto equal\n : 2\\n", + "error_file": "/Users/jaime.silvela/repos/cloud-native-postgresql/tests/e2e/initdb_test.go", + "error_line": 80, + "platform": "local", + "postgres_kind": "PostgreSQL", + "matrix_id": "id1", + "postgres_version": "11.1", + "k8s_version": "22", + "workflow_id": 12, + "repo": "my-repo", + "branch": "my-branch" +} \ No newline at end of file diff --git a/few-artifacts/id1_9891d0d1caa1ec8fd0adfe622e341f84dd3d1df1cffee843e1fb84a0.json b/few-artifacts/id1_9891d0d1caa1ec8fd0adfe622e341f84dd3d1df1cffee843e1fb84a0.json new file mode 100644 index 0000000..5767544 --- /dev/null +++ b/few-artifacts/id1_9891d0d1caa1ec8fd0adfe622e341f84dd3d1df1cffee843e1fb84a0.json @@ -0,0 +1,17 @@ +{ + "name": "ignoreFails on e2e tests -- generates a failing tests that should be ignored", + "state": "ignoreFailed", + "start_time": "0001-01-01T00:00:00Z", + "end_time": "0001-01-01T00:00:00Z", + "error": "", + "error_file": "", + "error_line": 0, + "platform": "local", + "postgres_kind": "PostgreSQL", + "matrix_id": "id1", + "postgres_version": "11.1", + "k8s_version": "22", + "workflow_id": 12, + "repo": "my-repo", + "branch": "my-branch" +} \ No newline at end of file diff --git a/test_summary.py b/test_summary.py new file mode 100644 index 0000000..d1b81cf --- /dev/null +++ b/test_summary.py @@ -0,0 +1,70 @@ +# +# Copyright The CloudNativePG Contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import unittest +import summarize_test_results +import datetime + + +class TestIsFailed(unittest.TestCase): + def test_compute_summary(self): + self.maxDiff = None + summary = summarize_test_results.compute_test_summary("few-artifacts") + self.assertEqual(summary["total_run"], 3) + self.assertEqual(summary["total_failed"], 1) + + self.assertEqual( + summary["by_code"]["total"], + { + "/Users/jaime.silvela/repos/cloud-native-postgresql/tests/e2e/initdb_test.go:80": 1 + }, + "unexpected summary", + ) + self.assertEqual( + summary["by_code"]["tests"], + { + "/Users/jaime.silvela/repos/cloud-native-postgresql/tests/e2e/initdb_test.go:80": { + "InitDB settings - initdb custom post-init SQL scripts -- can find the tables created by the post-init SQL queries": True + } + }, + "unexpected summary", + ) + self.assertEqual( + summary["by_matrix"], {"total": {"id1": 3}, "failed": {"id1": 1}} + ) + self.assertEqual(summary["by_k8s"], {"total": {"22": 3}, "failed": {"22": 1}}) + self.assertEqual( + summary["by_platform"], {"total": {"local": 3}, "failed": {"local": 1}} + ) + self.assertEqual( + summary["by_postgres"], + {"total": {"PostgreSQL-11.1": 3}, "failed": {"PostgreSQL-11.1": 1}}, + ) + self.assertEqual( + summary["suite_durations"], + { + "end_time": { + "local": {"id1": datetime.datetime(2021, 11, 29, 18, 31, 7)} + }, + "start_time": { + "local": {"id1": datetime.datetime(2021, 11, 29, 18, 28, 37)} + }, + }, + ) + + +if __name__ == "__main__": + unittest.main()