From cdf3e6408a6f7e924b1cae0fdfd104f8a69acfd0 Mon Sep 17 00:00:00 2001 From: Vincent Simonin <vincent.simonin@ext.ec.europa.eu> Date: Fri, 21 Jul 2023 18:04:54 +0200 Subject: [PATCH] Start testing environment * Add start and halt testing environment Ansible playbook * Add test CI stage * Change tag for OVH --- .gitlab-ci.yml | 13 +++++-- ansible/deploy_on_test.yml | 34 +++++++++++++++++++ ansible/halt_test.yml | 16 +++++++++ docker-compose.yml | 5 +-- .../tests/e2e/test_mapping_creation.py | 13 ++++--- .../tests/e2e/test_mapping_unique.py | 18 ++++++---- .../tests/e2e/test_unauthenticated.py | 15 +++++--- .../tests/e2e/test_url_max_length.py | 18 ++++++---- tests/docker-compose.test.yml | 15 +++++--- tests/env/netbox.env | 4 ++- 10 files changed, 120 insertions(+), 31 deletions(-) create mode 100644 ansible/deploy_on_test.yml create mode 100644 ansible/halt_test.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5f91aa1..457626b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,11 +1,11 @@ default: tags: - - lab - - shell + - ovh stages: - default_validate - build + - test - deliver include: @@ -18,6 +18,15 @@ build-job: # This job runs in the build stage, which runs first. script: - ansible-playbook ansible/build.yml +run-test: + stage: test + before_script: + - chmod 600 $ANSIBLE_PRIVATE_KEY_FILE + - env ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_PRIVATE_KEY_FILE="$ANSIBLE_PRIVATE_KEY_FILE" ansible-playbook -i "$TESTING_HOSTS" -u debian ansible/deploy_on_test.yml + script: echo "testing" + after_script: + - env ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_PRIVATE_KEY_FILE="$ANSIBLE_PRIVATE_KEY_FILE" ansible-playbook -i "$TESTING_HOSTS" -u debian ansible/halt_test.yml + deliver-latest-job: stage: deliver variables: diff --git a/ansible/deploy_on_test.yml b/ansible/deploy_on_test.yml new file mode 100644 index 0000000..fe9ca14 --- /dev/null +++ b/ansible/deploy_on_test.yml @@ -0,0 +1,34 @@ +- name: Deploy build for testing + hosts: all + + tasks: + - name: Create netbox directory + ansible.builtin.file: + path: "/home/debian/netbox/{{ lookup('ansible.builtin.env','CI_PIPELINE_ID') }}" + state: directory + + - name: Copy docker-compose file + ansible.builtin.copy: + src: "../docker-compose.yml" + dest: "/home/debian/netbox/{{ lookup('ansible.builtin.env','CI_PIPELINE_ID') }}/docker-compose.yml" + + - name: Copy testing docker-compose file + ansible.builtin.copy: + src: "../tests/docker-compose.test.yml" + dest: "/home/debian/netbox/{{ lookup('ansible.builtin.env','CI_PIPELINE_ID') }}/docker-compose.override.yml" + + - name: Copy testing env variables + ansible.builtin.copy: + src: "../tests/env" + dest: "/home/debian/netbox/{{ lookup('ansible.builtin.env','CI_PIPELINE_ID') }}/" + + - name: Create .env file + ansible.builtin.copy: + dest: "/home/debian/netbox/{{ lookup('ansible.builtin.env','CI_PIPELINE_ID') }}/.env" + content: "TAG={{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}" + + - name: Run `docker-compose up` + community.docker.docker_compose: + project_src: "/home/debian/netbox/{{ lookup('ansible.builtin.env','CI_PIPELINE_ID') }}/" + state: present + pull: true diff --git a/ansible/halt_test.yml b/ansible/halt_test.yml new file mode 100644 index 0000000..49c43af --- /dev/null +++ b/ansible/halt_test.yml @@ -0,0 +1,16 @@ +- name: Halt testing environnement + hosts: all + + tasks: + - name: Run `docker-compose down` + community.docker.docker_compose: + project_src: "/home/debian/netbox/{{ lookup('ansible.builtin.env','CI_PIPELINE_ID') }}/" + state: absent + remove_volumes: true + remove_images: "all" + remove_orphans: true + + - name: Remove netbox directory + ansible.builtin.file: + path: "/home/debian/netbox/{{ lookup('ansible.builtin.env','CI_PIPELINE_ID') }}" + state: absent diff --git a/docker-compose.yml b/docker-compose.yml index a58825c..80cffc2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,11 +6,9 @@ services: - postgres - redis - redis-cache - ports: - - 8080:8080 user: 'unit:root' healthcheck: - start_period: 60s + start_period: 180s timeout: 3s interval: 15s test: "curl -f http://localhost:8080/api/ || exit 1" @@ -82,4 +80,3 @@ volumes: driver: local netbox-scripts-files: driver: local - diff --git a/plugins/netbox-rps-plugin/tests/e2e/test_mapping_creation.py b/plugins/netbox-rps-plugin/tests/e2e/test_mapping_creation.py index e5390d1..403a7f8 100644 --- a/plugins/netbox-rps-plugin/tests/e2e/test_mapping_creation.py +++ b/plugins/netbox-rps-plugin/tests/e2e/test_mapping_creation.py @@ -1,14 +1,19 @@ import unittest import requests import json +import os +HOST = os.getenv('HOST', default='localhost') +PORT = os.getenv('PORT', default='8080') +API_KEY = os.getenv('API_KEY', 'only4testingpurpose') + class TestMappingCreation(unittest.TestCase): mappingId=None def test_that_mapping_is_created(self): r = requests.post( - url='http://localhost:8000/api/plugins/rps/mapping/', + url='http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT), json={ "source": "https://truc6.com/api", "target": "http://10.10.10.10:1886/api", @@ -16,7 +21,7 @@ class TestMappingCreation(unittest.TestCase): "testingpage": None }, headers={ - "Authorization": "Token 52121418bdd7411f6a0ee99b41561099810a8785" + "Authorization": 'Token {}'.format(API_KEY) } ) @@ -26,12 +31,12 @@ class TestMappingCreation(unittest.TestCase): def tearDown(self) -> None: requests.delete( - url='http://localhost:8000/api/plugins/rps/mapping/', + url='http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT), json=[{ "id": self.mappingId }], headers={ - "Authorization": "Token 52121418bdd7411f6a0ee99b41561099810a8785" + "Authorization": 'Token {}'.format(API_KEY) } ) diff --git a/plugins/netbox-rps-plugin/tests/e2e/test_mapping_unique.py b/plugins/netbox-rps-plugin/tests/e2e/test_mapping_unique.py index 97bef4a..56076b1 100644 --- a/plugins/netbox-rps-plugin/tests/e2e/test_mapping_unique.py +++ b/plugins/netbox-rps-plugin/tests/e2e/test_mapping_unique.py @@ -1,6 +1,12 @@ import unittest import requests import json +import os + + +HOST = os.getenv('HOST', default='localhost') +PORT = os.getenv('PORT', default='8080') +API_KEY = os.getenv('API_KEY', 'only4testingpurpose') class TestMappingUnique(unittest.TestCase): @@ -8,7 +14,7 @@ class TestMappingUnique(unittest.TestCase): def test_that_mapping_is_unique(self): r = requests.post( - url='http://localhost:8000/api/plugins/rps/mapping/', + url='http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT), json={ "source": "https://truc7.com/api", "target": "http://10.10.10.10:1886/api", @@ -16,7 +22,7 @@ class TestMappingUnique(unittest.TestCase): "testingpage": None }, headers={ - "Authorization": "Token 52121418bdd7411f6a0ee99b41561099810a8785" + "Authorization": 'Token {}'.format(API_KEY) } ) @@ -25,7 +31,7 @@ class TestMappingUnique(unittest.TestCase): self.mappingId = json.loads(r.content)['id']; r = requests.post( - url='http://localhost:8000/api/plugins/rps/mapping/', + url='http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT), json={ "source": "https://truc7.com/api", "target": "http://10.10.10.10:1886/api", @@ -33,7 +39,7 @@ class TestMappingUnique(unittest.TestCase): "testingpage": None }, headers={ - "Authorization": "Token 52121418bdd7411f6a0ee99b41561099810a8785" + "Authorization": 'Token {}'.format(API_KEY) } ) @@ -42,12 +48,12 @@ class TestMappingUnique(unittest.TestCase): def tearDown(self) -> None: requests.delete( - url='http://localhost:8000/api/plugins/rps/mapping/', + url='http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT), json=[{ "id": self.mappingId }], headers={ - "Authorization": "Token 52121418bdd7411f6a0ee99b41561099810a8785" + "Authorization": 'Token {}'.format(API_KEY) } ) diff --git a/plugins/netbox-rps-plugin/tests/e2e/test_unauthenticated.py b/plugins/netbox-rps-plugin/tests/e2e/test_unauthenticated.py index d4a4129..a80fd2a 100644 --- a/plugins/netbox-rps-plugin/tests/e2e/test_unauthenticated.py +++ b/plugins/netbox-rps-plugin/tests/e2e/test_unauthenticated.py @@ -1,25 +1,32 @@ import unittest import requests +import os + + +HOST = os.getenv('HOST', default='localhost') +PORT = os.getenv('PORT', default='8080') +API_KEY = os.getenv('API_KEY', 'only4testingpurpose') + class TestUnauthenticatedMappings(unittest.TestCase): def test_mappings_get_unauthenticated(self): - r = requests.get('http://localhost:8000/api/plugins/rps/mapping/') + r = requests.get('http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT)) self.assertEqual(r.status_code, 403) def test_mappings_post_unauthenticated(self): - r = requests.post('http://localhost:8000/api/plugins/rps/mapping/') + r = requests.post('http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT)) self.assertEqual(r.status_code, 403) def test_mappings_patch_unauthenticated(self): - r = requests.patch('http://localhost:8000/api/plugins/rps/mapping/') + r = requests.patch('http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT)) self.assertEqual(r.status_code, 403) def test_mappings_delete_unauthenticated(self): - r = requests.delete('http://localhost:8000/api/plugins/rps/mapping/') + r = requests.delete('http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT)) self.assertEqual(r.status_code, 403) diff --git a/plugins/netbox-rps-plugin/tests/e2e/test_url_max_length.py b/plugins/netbox-rps-plugin/tests/e2e/test_url_max_length.py index 44f33d8..dffdc49 100644 --- a/plugins/netbox-rps-plugin/tests/e2e/test_url_max_length.py +++ b/plugins/netbox-rps-plugin/tests/e2e/test_url_max_length.py @@ -1,12 +1,18 @@ import unittest import requests +import os + + +HOST = os.getenv('HOST', default='localhost') +PORT = os.getenv('PORT', default='8080') +API_KEY = os.getenv('API_KEY', 'only4testingpurpose') class TestMappingCreation(unittest.TestCase): def test_that_source_url_has_max_length(self): r = requests.post( - url='http://localhost:8000/api/plugins/rps/mapping/', + url='http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT), json={ "source": "https://truc.com/api" + ('i' * 1981), "target": "http://10.10.10.10:1886/api", @@ -14,7 +20,7 @@ class TestMappingCreation(unittest.TestCase): "testingpage": None }, headers={ - "Authorization": "Token 52121418bdd7411f6a0ee99b41561099810a8785" + "Authorization": 'Token {}'.format(API_KEY) } ) @@ -23,7 +29,7 @@ class TestMappingCreation(unittest.TestCase): def test_that_target_url_has_max_length(self): r = requests.post( - url='http://localhost:8000/api/plugins/rps/mapping/', + url='http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT), json={ "source": "https://truc.com/api", "target": "http://10.10.10.10:1886/api" + ('i' * 1974), @@ -31,7 +37,7 @@ class TestMappingCreation(unittest.TestCase): "testingpage": None }, headers={ - "Authorization": "Token 52121418bdd7411f6a0ee99b41561099810a8785" + "Authorization": 'Token {}'.format(API_KEY) } ) @@ -40,7 +46,7 @@ class TestMappingCreation(unittest.TestCase): def test_that_testingpage_url_has_max_length(self): r = requests.post( - url='http://localhost:8000/api/plugins/rps/mapping/', + url='http://{}:{}/api/plugins/rps/mapping/'.format(HOST, PORT), json={ "source": "https://truc.com/api", "target": "http://10.10.10.10:1886/api", @@ -48,7 +54,7 @@ class TestMappingCreation(unittest.TestCase): "testingpage": "https://truc.com/api" + ('i' * 1981), }, headers={ - "Authorization": "Token 52121418bdd7411f6a0ee99b41561099810a8785" + "Authorization": 'Token {}'.format(API_KEY) } ) diff --git a/tests/docker-compose.test.yml b/tests/docker-compose.test.yml index fa65ff8..38f733d 100644 --- a/tests/docker-compose.test.yml +++ b/tests/docker-compose.test.yml @@ -1,10 +1,17 @@ version: '3.4' services: netbox: - env_file: env/netbox.env + image: "code.europa.eu:4567/digit-c4/netbox-plugins:${TAG}" + ports: + - 8080:8080 + env_file: env/netbox.env + netbox-worker: + env_file: env/netbox.env + netbox-housekeeping: + env_file: env/netbox.env postgres: - env_file: env/postgres.env + env_file: env/postgres.env redis: - env_file: env/redis.env + env_file: env/redis.env redis-cache: - env_file: env/redis-cache.env + env_file: env/redis-cache.env diff --git a/tests/env/netbox.env b/tests/env/netbox.env index 9e490d0..6660997 100644 --- a/tests/env/netbox.env +++ b/tests/env/netbox.env @@ -30,5 +30,7 @@ REDIS_PASSWORD=H733Kdjndks81 REDIS_SSL=false RELEASE_CHECK_URL=https://api.github.com/repos/netbox-community/netbox/releases SECRET_KEY=r(m)9nLGnz$(_q3N4z1k(EFsMCjjjzx08x9VhNVcfd%6RF#r!6DE@+V5Zk2X -SKIP_SUPERUSER=true +SKIP_SUPERUSER=false +SUPERUSER_PASSWORD=thisnetboxisnot4u +SUPERUSER_API_TOKEN=only4testingpurpose WEBHOOKS_ENABLED=true -- GitLab