- name: Delete test repo
  zypper_repository:
    name: test
    state: absent
  register: zypper_result

- name: Add test repo
  zypper_repository:
    name: test
    state: present
    repo: http://dl.google.com/linux/chrome/rpm/stable/x86_64
  register: zypper_result

- name: verify repo addition
  assert:
    that:
      - "zypper_result.changed"

- name: Add same repo again
  zypper_repository:
    name: test
    state: present
    repo: http://dl.google.com/linux/chrome/rpm/stable/x86_64
  register: zypper_result

- name: verify no change on second install
  assert:
    that:
      - "not zypper_result.changed"

- name: Change repo URL
  zypper_repository:
    name: test
    state: present
    repo: http://download.videolan.org/pub/vlc/SuSE/Leap_{{ ansible_distribution_version }}/
  register: zypper_result

- name: Verify change on URL only change
  assert:
    that:
      - "zypper_result.changed"

- name: use refresh option
  zypper_repository:
    name: testrefresh
    refresh: no
    state: present
    repo: http://download.videolan.org/pub/vlc/SuSE/Leap_{{ ansible_distribution_version }}/

- name: check refreshoption
  command: zypper -x lr testrefresh
  register: zypper_result

- assert:
    that:
      - '"autorefresh=\"0\"" in zypper_result.stdout'

- name: set repo priority
  zypper_repository:
    name: testprio
    priority: 55
    state: present
    repo: http://download.videolan.org/pub/vlc/SuSE/Leap_{{ ansible_distribution_version }}/

- name: check refreshoption
  command: zypper -x lr testprio
  register: zypper_result

- assert:
    that:
      - '"priority=\"55\"" in zypper_result.stdout'

- name: add two repos with same url
  zypper_repository:
    name: "{{item}}"
    state: present
    repo: http://dl.google.com/linux/chrome/rpm/stable/x86_64
  with_items:
    - chrome1
    - chrome2

- name: check repo is updated by url
  command: zypper lr chrome1
  register: zypper_result1
  ignore_errors: yes

- name: check repo is updated by url
  command: zypper lr chrome2
  register: zypper_result2

- assert:
    that:
      - "zypper_result1.rc != 0"
      - "'not found' in zypper_result1.stderr"
      - "zypper_result2.rc == 0"
      - "'http://dl.google.com/linux/chrome/rpm/stable/x86_64' in zypper_result2.stdout"

- name: add two repos with same name
  zypper_repository:
    name: samename
    state: present
    repo: "{{ item }}"
  with_items:
    - http://download.opensuse.org/repositories/science/openSUSE_Leap_{{ ansible_distribution_version }}/
    - http://download.opensuse.org/repositories/devel:/languages:/ruby/openSUSE_Leap_{{ ansible_distribution_version }}/

- name: check repo is updated by name
  command: zypper lr samename
  register: zypper_result

- assert:
    that:
      - "'/science/' not in zypper_result.stdout"
      - "'/devel:/languages:/ruby/' in zypper_result.stdout"

- name: remove last added repos (by URL to test that)
  zypper_repository:
    repo: http://download.opensuse.org/repositories/devel:/languages:/ruby/openSUSE_Leap_{{ ansible_distribution_version }}/
    state: absent

- name: "Test adding a repo with custom GPG key"
  zypper_repository:
    name: "Apache_Modules"
    repo: "http://download.opensuse.org/repositories/Apache:/Modules/openSUSE_Leap_{{ ansible_distribution_version }}/"
    priority: 100
    auto_import_keys: true
    state: "present"
