---

- name: ensure zypper ref works
  command: zypper -n ref

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

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

- debug: var=zypper_result

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

- name: Add 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: Remove repo by name (also to not mess up later tasks)
  zypper_repository:
    name: test
    state: absent

- name: use refresh option
  zypper_repository:
    name: testrefresh
    refresh: no
    state: present
    repo: http://download.opensuse.org/distribution/leap/{{ ansible_distribution_version }}/repo/oss/

- 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.opensuse.org/distribution/leap/{{ ansible_distribution_version }}/repo/oss/

- 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://download.opensuse.org/distribution/leap/{{ ansible_distribution_version }}/repo/oss/
  with_items:
    - oss1
    - oss2

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

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

- assert:
    that:
      - "zypper_result1.rc == 6"
      - "'not found' in zypper_result1.stderr"
      - "zypper_result2.rc == 0"
      - "'http://download.opensuse.org/distribution/leap/{{ ansible_distribution_version }}/repo/oss/' in zypper_result2.stdout"


- name: reset oss repo (to not break zypper later)
  zypper_repository:
    name: OSS
    state: present
    repo: http://download.opensuse.org/distribution/leap/{{ ansible_distribution_version }}/repo/oss/
    priority: 99
    refresh: yes

- 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:/python/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:/python/' in zypper_result.stdout"

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

- name: ensure zypper ref still works
  command: zypper -n ref
