diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml deleted file mode 100644 index 3f234d5..0000000 --- a/.github/workflows/ansible-test.yml +++ /dev/null @@ -1,48 +0,0 @@ -name: CI -on: - push: - pull_request: - schedule: - - cron: '0 6 * * *' -env: - NAMESPACE: grafana - COLLECTION_NAME: grafana - -jobs: - - sanity: - name: Sanity (Ⓐ${{ matrix.ansible }}) - strategy: - matrix: - ansible: - - stable-2.10 - - stable-2.11 - - stable-2.12 - - devel - runs-on: ubuntu-latest - steps: - - - name: Check out code - uses: actions/checkout@v3 - with: - path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} - - - name: Set up Python - uses: actions/setup-python@v3 - with: - python-version: '3.9' - - - name: Install ansible-base (${{ matrix.ansible }}) - run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz --disable-pip-version-check - - - name: Run sanity tests - run: ansible-test sanity --docker -v --color --coverage - working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} - - - name: Generate coverage report - run: ansible-test coverage xml -v --requirements --group-by command --group-by version - working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} - - - uses: codecov/codecov-action@v3 - with: - fail_ci_if_error: false diff --git a/.github/workflows/ci-test.yml b/.github/workflows/ci-test.yml new file mode 100644 index 0000000..cf2c565 --- /dev/null +++ b/.github/workflows/ci-test.yml @@ -0,0 +1,88 @@ +name: CI Tests +on: + push: + pull_request: + schedule: + - cron: '0 6 * * *' +env: + NAMESPACE: grafana + COLLECTION_NAME: grafana + +jobs: + + sanity: + name: Sanity (Ⓐ${{ matrix.ansible }}) + strategy: + matrix: + ansible: + - stable-2.10 + - stable-2.11 + - stable-2.12 + - devel + runs-on: ubuntu-latest + steps: + + - name: Check out code + uses: actions/checkout@v3 + with: + path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} + + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: '3.9' + + - name: Install ansible-base (${{ matrix.ansible }}) + run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz --disable-pip-version-check + + - name: Run sanity tests + run: ansible-test sanity --docker -v --color --coverage + working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} + + - name: Generate coverage report + run: ansible-test coverage xml -v --requirements --group-by command --group-by version + working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} + + - uses: codecov/codecov-action@v3 + with: + fail_ci_if_error: false + + integration: + runs-on: ubuntu-latest + name: Integration ${{ matrix.ansible }}-py${{ matrix.python }}) + strategy: + fail-fast: false + max-parallel: 1 + matrix: + ansible: + - stable-2.13 + python: + - '3.9' + + steps: + - name: Check out code + uses: actions/checkout@v2 + with: + path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} + + - name: create integration_config + working-directory: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}/tests/integration + run: | + cat < integration_config.yml + stack_name: ${{ secrets.ANSIBLE_TEST_STACK_NAME }} + org_name: ${{ secrets.ANSIBLE_TEST_ORG_NAME }} + grafana_cloud_api_key: ${{ secrets.ANSIBLE_TEST_CLOUD_API_KEY }} + grafana_api_key: ${{ secrets.ANSIBLE_TEST_GRAFANA_API_KEY }} + EOF + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install ansible-base (${{ matrix.ansible }}) + run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz --disable-pip-version-check + + - name: Run integration test + run: ansible-test integration -v --color --retry-on-error --continue-on-error --diff --python ${{ matrix.python }} --docker --coverage + working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} \ No newline at end of file diff --git a/.github/workflows/full-integration-test.yml b/.github/workflows/full-integration-test.yml new file mode 100644 index 0000000..2e28932 --- /dev/null +++ b/.github/workflows/full-integration-test.yml @@ -0,0 +1,69 @@ +name: Full Integration Test +on: + schedule: + - cron: '0 0 * * 0' +env: + NAMESPACE: grafana + COLLECTION_NAME: grafana + +jobs: + + integration: + runs-on: ubuntu-latest + name: ${{ matrix.ansible }}-py${{ matrix.python }}) + strategy: + fail-fast: false + max-parallel: 1 + matrix: + ansible: + - stable-2.9 + - stable-2.10 + - stable-2.11 + - stable-2.12 + - stable-2.13 + # - devel + # - milestone + python: + - '2.7' + - '3.5' + - '3.6' + - '3.7' + - '3.8' + - '3.9' + exclude: + - ansible: stable-2.9 + python: '3.9' + - ansible: stable-2.9 + python: '3.10' + - ansible: stable-2.10 + python: '3.10' + - ansible: stable-2.11 + python: '3.10' + + steps: + - name: Check out code + uses: actions/checkout@v2 + with: + path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} + + - name: create integration_config + working-directory: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}/tests/integration + run: | + cat < integration_config.yml + stack_name: ${{ secrets.ANSIBLE_TEST_STACK_NAME }} + org_name: ${{ secrets.ANSIBLE_TEST_ORG_NAME }} + grafana_cloud_api_key: ${{ secrets.ANSIBLE_TEST_CLOUD_API_KEY }} + grafana_api_key: ${{ secrets.ANSIBLE_TEST_GRAFANA_API_KEY }} + EOF + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install ansible-base (${{ matrix.ansible }}) + run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz --disable-pip-version-check + + - name: Run integration test + run: ansible-test integration -v --color --retry-on-error --continue-on-error --diff --python ${{ matrix.python }} --docker --coverage + working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}} \ No newline at end of file diff --git a/tests/integration/targets/alert_contact_point/tasks/main.yml b/tests/integration/targets/alert_contact_point/tasks/main.yml new file mode 100644 index 0000000..36665db --- /dev/null +++ b/tests/integration/targets/alert_contact_point/tasks/main.yml @@ -0,0 +1,53 @@ +- name: Create Alerting contact point + grafana.grafana.alert_contact_point: + name: ops-email + uid: opsemail + type: email + settings: { + addresses: "ops@mydomain.com,devs@mydomain.com" + } + stack_slug: "{{ stack_name }}" + grafana_api_key: "{{ grafana_api_key }}" + state: present + register: add_result + +- assert: + that: + - add_result.failed == false + - add_result.output.provenance == "api" + +- name: Update Alerting contact point + grafana.grafana.alert_contact_point: + name: ops-email + uid: opsemail + type: email + settings: { + addresses: "ops@mydomain.com,devs@mydomain.com,admin@mydomain.com" + } + stack_slug: "{{ stack_name }}" + grafana_api_key: "{{ grafana_api_key }}" + state: present + register: update_result + +- assert: + that: + - update_result.failed == false + - update_result.output.provenance == "api" + +- name: Delete Alerting contact point + grafana.grafana.alert_contact_point: + name: ops-email + uid: opsemail + type: email + settings: { + addresses: "ops@mydomain.com,devs@mydomain.com,admin@mydomain.com" + } + stack_slug: "{{ stack_name }}" + grafana_api_key: "{{ grafana_api_key }}" + state: absent + register: delete_result + +- assert: + that: + - delete_result.failed == false + - delete_result.output.message == "contactpoint deleted" \ No newline at end of file diff --git a/tests/integration/targets/alert_notification_policy/tasks/main.yml b/tests/integration/targets/alert_notification_policy/tasks/main.yml new file mode 100644 index 0000000..681ff53 --- /dev/null +++ b/tests/integration/targets/alert_notification_policy/tasks/main.yml @@ -0,0 +1,16 @@ +- name: Set Notification policy tree + grafana.grafana.alert_notification_policy: + stack_slug: "{{ stack_name }}" + grafana_api_key: "{{ grafana_api_key }}" + routes: [ + { + receiver: grafana-default-email, + object_matchers: [["env", "=", "Production"]], + } + ] + register: result + +- assert: + that: + - result.failed == false + - result.output.provenance == "api" \ No newline at end of file diff --git a/tests/integration/targets/cloud_api_key/tasks/main.yml b/tests/integration/targets/cloud_api_key/tasks/main.yml new file mode 100644 index 0000000..bc8601a --- /dev/null +++ b/tests/integration/targets/cloud_api_key/tasks/main.yml @@ -0,0 +1,43 @@ +- name: Create Grafana Cloud API key + grafana.grafana.cloud_api_key: + name: ansible-integration-test + role: Admin + org_slug: "{{ org_name }}" + existing_cloud_api_key: "{{ grafana_cloud_api_key }}" + fail_if_already_created: False + state: present + register: add_result + +- assert: + that: + - add_result.changed == true + - add_result.output.name == "ansible-integration-test" + +- name: Re-run previous task + grafana.grafana.cloud_api_key: + name: ansible-integration-test + role: Admin + org_slug: "{{ org_name }}" + existing_cloud_api_key: "{{ grafana_cloud_api_key }}" + fail_if_already_created: False + state: present + register: update_result + +- assert: + that: + - update_result.changed == false + - update_result.output == "A Cloud API key with the same name already exists" + +- name: Delete Grafana Cloud API key + grafana.grafana.cloud_api_key: + name: ansible-integration-test + role: Admin + org_slug: "{{ org_name }}" + existing_cloud_api_key: "{{ grafana_cloud_api_key }}" + state: absent + register: delete_result + +- assert: + that: + - delete_result.changed == true + - delete_result.output == "Cloud API key is deleted" \ No newline at end of file diff --git a/tests/integration/targets/cloud_plugin/tasks/main.yml b/tests/integration/targets/cloud_plugin/tasks/main.yml new file mode 100644 index 0000000..7c6272d --- /dev/null +++ b/tests/integration/targets/cloud_plugin/tasks/main.yml @@ -0,0 +1,41 @@ +- name: Add a plugin + grafana.grafana.cloud_plugin: + name: grafana-github-datasource + version: 1.0.14 + stack_slug: "{{ stack_name }}" + cloud_api_key: "{{ grafana_cloud_api_key }}" + state: present + register: add_result + +- assert: + that: + - add_result.changed == true + - add_result.pluginName == "GitHub" + +- name: update a plugin + grafana.grafana.cloud_plugin: + name: grafana-github-datasource + version: 1.0.15 + stack_slug: "{{ stack_name }}" + cloud_api_key: "{{ grafana_cloud_api_key }}" + state: present + register: update_result + +- assert: + that: + - update_result.changed == true + - update_result.pluginName == "GitHub" + +- name: Delete a plugin + grafana.grafana.cloud_plugin: + name: grafana-github-datasource + version: 1.0.15 + stack_slug: "{{ stack_name }}" + cloud_api_key: "{{ grafana_cloud_api_key }}" + state: absent + register: delete_result + +- assert: + that: + - delete_result.changed == true + - delete_result.pluginName == "GitHub" \ No newline at end of file diff --git a/tests/integration/targets/cloud_stack/tasks/main.yml b/tests/integration/targets/cloud_stack/tasks/main.yml new file mode 100644 index 0000000..945833d --- /dev/null +++ b/tests/integration/targets/cloud_stack/tasks/main.yml @@ -0,0 +1,30 @@ +- name: Create a Grafana Cloud stack + grafana.grafana.cloud_stack: + name: ansiblestack + stack_slug: ansiblestack + cloud_api_key: "{{ grafana_cloud_api_key }}" + org_slug: "{{ org_name }}" + state: present + register: create_result + +- assert: + that: + - create_result.url == "https://ansiblestack.grafana.net" + +- name: Sleep for 30 seconds + ansible.builtin.wait_for: + timeout: 30 + +- name: Delete a Grafana Cloud stack + grafana.grafana.cloud_stack: + name: ansiblestack + stack_slug: ansiblestack + cloud_api_key: "{{ grafana_cloud_api_key }}" + org_slug: "{{ org_name }}" + state: absent + register: delete_result + +- assert: + that: + - delete_result.changed == true + - delete_result.url == "https://ansiblestack.grafana.net" \ No newline at end of file diff --git a/tests/integration/targets/dashboard/tasks/main.yml b/tests/integration/targets/dashboard/tasks/main.yml new file mode 100644 index 0000000..242a288 --- /dev/null +++ b/tests/integration/targets/dashboard/tasks/main.yml @@ -0,0 +1,47 @@ +- name: Create/Update a dashboard + grafana.grafana.dashboard: + dashboard: { + "dashboard": { + "uid": test1234, + "title": "Ansible Integration Test", + "tags": [ "templated" ], + "timezone": "browser", + "schemaVersion": 16, + "version": 0, + "refresh": "25s" + }, + "overwrite": true + } + stack_slug: "{{ stack_name }}" + grafana_api_key: "{{ grafana_api_key }}" + state: present + register: result_present + +- assert: + that: + - result_present.changed == true + - result_present.output.status == "success" + +- name: Delete dashboard + grafana.grafana.dashboard: + dashboard: { + "dashboard": { + "uid": test1234, + "title": "Ansible Integration Test", + "tags": [ "templated" ], + "timezone": "browser", + "schemaVersion": 16, + "version": 0, + "refresh": "25s" + }, + "overwrite": true + } + stack_slug: "{{ stack_name }}" + grafana_api_key: "{{ grafana_api_key }}" + state: absent + register: result_absent + +- assert: + that: + - result_absent.changed == true + - result_absent.output.message == "Dashboard Ansible Integration Test deleted" \ No newline at end of file diff --git a/tests/integration/targets/datasource/tasks/main.yml b/tests/integration/targets/datasource/tasks/main.yml new file mode 100644 index 0000000..0e84d54 --- /dev/null +++ b/tests/integration/targets/datasource/tasks/main.yml @@ -0,0 +1,23 @@ +- name: Create/Update a Data Source + grafana.grafana.datasource: + datasource: { + name: "ansible-integration", + type: "influxdb", + url: "https://grafana.github.com/grafana-ansible-collection", + user: "user", + secureJsonData: + { password: "password" }, + database: "db-name", + id: 123, + uid: "ansibletest", + access: "proxy" + } + stack_slug: "{{ stack_name }}" + grafana_api_key: "{{ grafana_api_key }}" + state: present + register: create_result + +- assert: + that: + - create_result.changed == true + - create_result.output.message == "Datasource added" or create_result.output.message == "Datasource updated" \ No newline at end of file diff --git a/tests/integration/targets/folder/tasks/main.yml b/tests/integration/targets/folder/tasks/main.yml new file mode 100644 index 0000000..6ec3432 --- /dev/null +++ b/tests/integration/targets/folder/tasks/main.yml @@ -0,0 +1,29 @@ +- name: Create/Update a Folder in Grafana + grafana.grafana.folder: + title: Ansible Integration test + uid: test123 + overwrite: true + stack_slug: "{{ stack_name }}" + grafana_api_key: "{{ grafana_api_key }}" + state: present + register: create_result + +- assert: + that: + - create_result.changed == true + - create_result.output.url == "/dashboards/f/test123/ansible-integration-test" + +- name: Delete a folder + grafana.grafana.folder: + title: Ansible Integration test + uid: test123 + overwrite: true + stack_slug: "{{ stack_name }}" + grafana_api_key: "{{ grafana_api_key }}" + state: absent + register: delete_result + +- assert: + that: + - delete_result.changed == true + - delete_result.output.message == "Folder Ansible Integration test deleted" \ No newline at end of file