Skip to content

Instantly share code, notes, and snippets.

@shantanoo-desai
Last active April 20, 2023 15:44
Show Gist options
  • Select an option

  • Save shantanoo-desai/52a533c17a1e2613c27cdd7d5c12dcbe to your computer and use it in GitHub Desktop.

Select an option

Save shantanoo-desai/52a533c17a1e2613c27cdd7d5c12dcbe to your computer and use it in GitHub Desktop.

Revisions

  1. shantanoo-desai revised this gist Apr 20, 2023. 1 changed file with 0 additions and 24 deletions.
    24 changes: 0 additions & 24 deletions Output
    Original file line number Diff line number Diff line change
    @@ -1,24 +0,0 @@
    ```
    ansible-playbook ansible/gitlab-single-test.yml -e '{"custom":[]}'
    ```
    ```bash
    PLAY [localhost] *********************************************************************************************************************************************************************

    TASK [Obtaining Test Cases Information from the "Tests" Directory] *******************************************************************************************************************
    ok: [localhost]

    TASK [Obtaining Specified Test Cases Information from the "Tests" Directory] *********************************************************************************************************

    TASK [set_fact] **********************************************************************************************************************************************************************
    fatal: [localhost]: FAILED! => {"msg": "'dict object' has no attribute 'files'"}

    PLAY RECAP ***************************************************************************************************************************************************************************
    localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
    ```

    BUT this works!

    ```
    ansible-playbook ansible/gitlab-single-test.yml -e '{"custom":["001","002","004"]}'
    ```

  2. shantanoo-desai revised this gist Apr 20, 2023. 1 changed file with 2 additions and 21 deletions.
    23 changes: 2 additions & 21 deletions gitlab-single-test.yml
    Original file line number Diff line number Diff line change
    @@ -7,35 +7,16 @@
    paths: "{{ playbook_dir }}/../Tests"
    recurse: false
    excludes: '000_Template'
    patterns: "{{ omit if custom is not defined else (custom | map('regex_replace', '^(.*)$', '\\1_*')) }}"
    file_type: directory
    register: testcases
    when: "custom | length == 0"

    - debug: var=testcases

    - name: Obtaining Specified Test Cases Information from the "Tests" Directory
    ansible.builtin.find:
    paths: "{{ playbook_dir }}/../Tests"
    recurse: false
    excludes: '000_Template'
    patterns:
    - "{{ item }}_*"
    file_type: directory
    loop: "{{ custom }}"
    register: testcases
    when: "custom | length > 0"


    - debug: var=testcases
    - set_fact:
    tests: "{{ tests|default([]) + [ { 'number': item | basename | split('_') | first, 'name': item | basename } ] }}"
    loop: "{{ testcases.files | map(attribute='path') | list }}"
    when: "custom | length == 0"

    - set_fact:
    tests: "{{ tests|default([]) + [ { 'number': item[0].path | basename | split('_') | first, 'name': item[0].path | basename } ] }}"
    loop: "{{ testcases.results | map(attribute='files') }}"
    when: "custom | length > 0"


    - name: generate Gitlab-CI File
    template:
  3. shantanoo-desai revised this gist Apr 20, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gitlab-single-test.yml
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@
    file_type: directory
    loop: "{{ custom }}"
    register: testcases
    when: "custom | length > 0"


    - debug: var=testcases
  4. shantanoo-desai created this gist Apr 20, 2023.
    15 changes: 15 additions & 0 deletions .gitlab-ci-single_test.yml.j2
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    include: '.gitlab-ci-config.yml'

    stages:
    - single_test

    {% for testcase in tests %}
    test_{{ testcase.number }}:
    extends:
    - .test
    script:
    - ci/prepare_tests.sh -s {{ testcase.number }}
    - ci/run_test.sh
    - ci/qemu/stop_qemu.sh

    {% endfor %}
    24 changes: 24 additions & 0 deletions Output
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    ```
    ansible-playbook ansible/gitlab-single-test.yml -e '{"custom":[]}'
    ```
    ```bash
    PLAY [localhost] *********************************************************************************************************************************************************************

    TASK [Obtaining Test Cases Information from the "Tests" Directory] *******************************************************************************************************************
    ok: [localhost]

    TASK [Obtaining Specified Test Cases Information from the "Tests" Directory] *********************************************************************************************************

    TASK [set_fact] **********************************************************************************************************************************************************************
    fatal: [localhost]: FAILED! => {"msg": "'dict object' has no attribute 'files'"}

    PLAY RECAP ***************************************************************************************************************************************************************************
    localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
    ```

    BUT this works!

    ```
    ansible-playbook ansible/gitlab-single-test.yml -e '{"custom":["001","002","004"]}'
    ```

    42 changes: 42 additions & 0 deletions gitlab-single-test.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    ---
    - hosts: localhost
    gather_facts: false
    tasks:
    - name: Obtaining Test Cases Information from the "Tests" Directory
    ansible.builtin.find:
    paths: "{{ playbook_dir }}/../Tests"
    recurse: false
    excludes: '000_Template'
    file_type: directory
    register: testcases
    when: "custom | length == 0"

    - debug: var=testcases

    - name: Obtaining Specified Test Cases Information from the "Tests" Directory
    ansible.builtin.find:
    paths: "{{ playbook_dir }}/../Tests"
    recurse: false
    excludes: '000_Template'
    patterns:
    - "{{ item }}_*"
    file_type: directory
    loop: "{{ custom }}"
    register: testcases


    - debug: var=testcases
    - set_fact:
    tests: "{{ tests|default([]) + [ { 'number': item | basename | split('_') | first, 'name': item | basename } ] }}"
    loop: "{{ testcases.files | map(attribute='path') | list }}"
    when: "custom | length == 0"

    - set_fact:
    tests: "{{ tests|default([]) + [ { 'number': item[0].path | basename | split('_') | first, 'name': item[0].path | basename } ] }}"
    loop: "{{ testcases.results | map(attribute='files') }}"
    when: "custom | length > 0"

    - name: generate Gitlab-CI File
    template:
    src: "{{ playbook_dir }}/templates/.gitlab-ci-single_test.yml.j2"
    dest: "./.gitlab-ci-single_test.yml"
    15 changes: 15 additions & 0 deletions testsdir
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    Tests/
    ├── 000_Template
    ├── 001_startup_completion
    ├── 002_NR_Influx_Std
    ├── 003_PY_Influx_Std
    ├── 004_PY_MYSQL_Std
    ├── 005_PY_Influx_MQTT
    ├── 007_Chronograf
    ├── 008_Grafana
    ├── 010_MQTT_Basic_Test
    ├── 011_MySQL_Basic_Test
    ├── 012_MySQL_User_Privileges
    ├── 013_influxdb_user_privileges_test
    ├── 014_Grafana_Datasources
    ├── 900_changePW_frontend_playwright