# SPDX-License-Identifier: MIT
---
- name: Basic repository enablement/disablement test
  hosts: all
  gather_facts: false
  become: true
  tags:
    - tests::slow
  tasks:
    - name: Test repositories
      block:
        - name: Setup Candlepin
          import_tasks: tasks/setup_candlepin.yml

        - name: Register
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_auth:
              login:
                username: "{{ lsr_rhc_test_data.reg_username }}"
                password: "{{ lsr_rhc_test_data.reg_password }}"
            rhc_insights:
              state: absent
            rhc_organization: "{{ lsr_rhc_test_data.reg_organization }}"
            rhc_server:
              hostname: "{{ lsr_rhc_test_data.candlepin_host }}"
              port: "{{ lsr_rhc_test_data.candlepin_port }}"
              prefix: "{{ lsr_rhc_test_data.candlepin_prefix }}"
              insecure: "{{ lsr_rhc_test_data.candlepin_insecure }}"
            rhc_baseurl: "{{ lsr_rhc_test_data.baseurl | d(omit) }}"
            rhc_repositories: "{{ lsr_rhc_test_data.repositories }}"

        - name: Get enabled repositories
          include_tasks: tasks/list_repositories.yml
          vars:
            mode: list-enabled

        - name: Check repositories to enable are enabled
          assert:
            that:
              - item.name in test_repositories.stdout_lines
          when: item.state | d("enabled") == "enabled"
          loop: "{{ lsr_rhc_test_data.repositories }}"

        - name: Get disabled repositories
          include_tasks: tasks/list_repositories.yml
          vars:
            mode: list-disabled

        - name: Check repositories to disable are disabled
          assert:
            that:
              - item.name in test_repositories.stdout_lines
          when: item.state | d("enabled") == "disabled"
          loop: "{{ lsr_rhc_test_data.repositories }}"

        - name: Disable all the repositories
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_insights:
              state: absent
            rhc_repositories:
              - {name: "*", state: disabled}

        - name: Get enabled repositories
          include_tasks: tasks/list_repositories.yml
          vars:
            mode: list-enabled

        - name: Check there are no repositories enabled
          assert:
            that:
              - test_repositories.stdout_lines | length == 0

        - name: Enable repo on already registered system
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_insights:
              state: absent
            rhc_repositories:
              - "{{ lsr_rhc_test_data.repositories.0 }}"

        - name: Get enabled repositories
          include_tasks: tasks/list_repositories.yml
          vars:
            mode: list-enabled

        - name: Verify that repository is enabled
          assert:
            that: >
              lsr_rhc_test_data.repositories.0.name in
              test_repositories.stdout_lines

      always:
        - name: Unregister
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_state: absent

        - name: Clean up Candlepin container
          include_tasks: tasks/teardown_candlepin.yml
