- name: run functional tst
  hosts: all
  vars:
    zuul_work_dir: "{{ zuul.projects['opendev.org/zuul/zuul-operator'].src_dir }}"
    runtime: minikube
  tasks:
    - name: Install ingress
      include_tasks: tasks/ingress.yaml

    - name: Create temp git root directory
      tempfile:
        state: directory
      register: git_root

    - name: get cluster ip
      when: runtime == 'minikube'
      command: /tmp/minikube ip
      register: _cluster_ip

    - name: set cluster ip
      when: runtime == 'minikube'
      set_fact:
        cluster_ip: "{{ _cluster_ip.stdout_lines[0] }}"

    - name: get cluster ip
      when: runtime == 'kind'
      command: docker inspect -f "{% raw %}{{ .NetworkSettings.IPAddress }}{% endraw %}" kind-control-plane
      register: _cluster_ip

    - name: set cluster ip
      when: runtime == 'kind'
      set_fact:
        cluster_ip: "{{ _cluster_ip.stdout_lines[0] }}"

    - name: set fact zuul_web_url
      set_fact:
        zuul_web_url: "http://{{ cluster_ip }}"
        zuul_ws_url: "ws://{{ cluster_ip }}"

    - name: ensure zuul web api is working
      when: skip_check is not defined
      include_tasks: tasks/zuul_web_check.yaml
      vars:
        endpoint: "/api/tenants"
        expected:
          - name: local
            projects: 1
            queue: 0

    - name: setup git service
      include_tasks: tasks/git_setup.yaml

    - name: get git-server port
      command: kubectl get svc git-server -o jsonpath='{.spec.ports[0].nodePort}'
      register: git_server_port

    - name: set git-server port
      set_fact:
        git_server_port: '{{ git_server_port.stdout_lines[0] }}'

    - name: create a config project
      include_tasks: tasks/create_config.yaml

    - name: update kubernetes resources
      vars:
        tenants:
          - tenant:
              name: local
              source:
                opendev.org:
                  config-projects:
                    - zuul/zuul-base-jobs
                  untrusted-projects:
                    - zuul/zuul-jobs
                local-git:
                  config-projects:
                    - config
      block:
        - k8s:
            namespace: default
            definition:
              apiVersion: v1
              kind: Secret
              metadata:
                name: "zuul-yaml-conf"
              stringData:
                main.yaml: "{{ tenants | to_yaml }}"

        - include_tasks: tasks/apply_cr.yaml
          vars:
            spec:
              database:
                allowUnsafeConfig: true
              executor:
                count: 1
                sshkey:
                  secretName: executor-ssh-key
              merger:
                count: 1
              scheduler:
                config:
                  secretName: zuul-yaml-conf
              launcher:
                config:
                  secretName: nodepool-yaml-conf
              connections:
                opendev.org:
                  baseurl: https://opendev.org
                  driver: git
                local-git:
                  baseurl: "git://git-server.default/"
                  driver: git
              externalConfig:
                kubernetes:
                  secretName: nodepool-kube-config
              jobVolumes:
                - context: trusted
                  access: rw
                  path: /system-dbus/
                  dir: /system-dbus/
                  volume:
                    name: system-dbus
                    hostPath:
                      path: /run/dbus
                      type: DirectoryOrCreate

    - name: ensure a job is running
      when: skip_check is not defined
      include_tasks: tasks/zuul_web_check.yaml
      vars:
        endpoint: "/api/tenants"
        expected:
          - name: local
            projects: 1
            # 1 queue means a job is running
            queue: 1

    - name: get build results
      uri:
        url: "{{ zuul_web_url }}/api/tenant/local/builds?complete=true"
      register: result
      until: "result.json is defined and result.json and (result.json|length) > 1"
      retries: 600
      delay: 1

    - name: get buillds results
      include_tasks: tasks/zuul_web_check.yaml
      vars:
        endpoint: "/api/tenant/local/builds?complete=true"

    - name: ensure success build
      assert:
        that:
          - result.json[0].result == 'SUCCESS'
          - result.json[1].result == 'SUCCESS'

    - name: grab job uuid
      shell: |
        curl -s {{ zuul_web_url }}/api/tenant/local/status | jq -r '.pipelines[].change_queues[].heads[][].jobs[].uuid'
      register: _job_uuid
      # Wait until the executor start the job
      until: _job_uuid.stdout != "" and "null" not in _job_uuid.stdout
      retries: 60
      delay: 1

    - name: connect to console-stream
      command: |
        wsdump -r --eof-wait 5 -t '{"uuid":"{{ _job_uuid.stdout_lines[0] }}","logfile":"console.log"}' {{ zuul_ws_url }}/api/tenant/local/console-stream
      register: console_stream

    - name: show console stream
      debug:
        var: console_stream

    - name: fail if console stream does not contains expected job output
      when: "'Job console starting...' not in console_stream.stdout"
      # It seems like wsdump.py doesn't always stay connected for the whole job duration
      # when: "'Demo job is running' not in console_stream.stdout"
      fail:
        msg: "Task output is missing from: {{ console_stream.stdout }}"

    - name: Test the preview
      include_tasks: ./tasks/test_preview.yaml

    - name: Test the registry
      include_tasks: ./tasks/test_registry.yaml