---
# Copyright 2020 VEXXHOST, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- hosts: all
  pre_tasks:
    - name: Deploy a hpa for test
      shell: |
        cat <<EOF | kubectl apply -f-
        apiVersion: autoscaling/v1
        kind: HorizontalPodAutoscaler
        metadata:
          name: test
          namespace: kube-system
        spec:
          scaleTargetRef:
            apiVersion: apps/v1
            kind: Deployment
            name: test
          minReplicas: 1
          maxReplicas: 3
          targetCPUUtilizationPercentage: 90
    - name: Deploy cert-manager
      shell: |
        kubectl apply --validate=false -f https://github.com/jetstack/cert-manager/releases/download/v0.15.2/cert-manager.yaml
    - name: Create a configmap for test
      shell: |
        cat <<EOF | kubectl apply -f-
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        data:
          key: value
  roles:
    - name: helm-template
      helm_release_name: rbac
      helm_chart: charts/rbac
      helm_wait_for_pods: false
      helm_values_file: charts/rbac/test-values.yaml
    - name: wait-for-pods
      vars:
        wait_for_pods_namespace: kube-system

  tasks:
    - import_tasks: tasks/setup-test-context.yaml

    # List and get nodes
    - name: Ensure listing nodes works
      shell: kubectl --context=test get nodes
    - name: Ensure getting a node works
      shell: kubectl --context=test get $(kubectl get nodes -oname | head -1)

    # List and get applications
    - name: Ensure listing applications works
      shell: kubectl --context=test get {{ item }}
      loop:
        - daemonsets
        - deployments
        - replicasets
        - statefulsets
    # TODO: Get applications

    # List and get pods
    - name: Ensure listing pods works
      shell: kubectl --context=test -n kube-system get pods
    - name: Ensure getting a pod works
      shell: kubectl --context=test -n kube-system get $(kubectl -n kube-system get pods -oname | head -1)

    # View logs for pods
    - name: Ensure getting logs for a pod works
      shell: kubectl --context=test -n kube-system logs $(kubectl -n kube-system get pods -oname | head -1)

    # List and get hpas
    - name: Ensure listing hpas works
      shell: kubectl --context=test -n kube-system get hpa
    - name: Ensure getting a hpa works
      shell: kubectl --context=test -n kube-system get hpa test

    # List and get pvcs
    - name: Ensure listing pvcs
      shell: kubectl --context=test -n kube-system get pvc

    # List and get pvs
    - name: Ensure listing pvs
      shell: kubectl --context=test get pv

    # List and get storageclasses
    - name: Ensure listing sc
      shell: kubectl --context=test get sc

    # List and get metrics for nodes
    - name: Ensure listing resources of nodes
      command: kubectl --context=test top nodes
      register: _nodes_metrics
      until: _nodes_metrics is success
      retries: 60
      delay: 5
    - name: Ensure getting resources of a node
      shell: kubectl --context=test top node $(kubectl get nodes --no-headers=true -o=custom-columns='name:metadata.name' | head -1)
      register: _node_metrics
      until: _node_metrics is success
      retries: 60
      delay: 5

    # List and get metrics for pods
    - name: Ensure listing resources for pods in a namespace
      shell: kubectl --context=test -n kube-system top pods
      register: _pods_metrics
      until: _pods_metrics is success
      retries: 60
      delay: 5
    - name: Ensure getting resources for a pod
      shell: kubectl --context=test -n kube-system top pod $(kubectl -n kube-system --no-headers=true get pods -o=custom-columns='name:metadata.name' | head -1)
      register: _pod_metrics
      until: _pod_metrics is success
      retries: 60
      delay: 5

    # List and get namespaces
    - name: Ensure listing namespaces works
      shell: kubectl --context=test get namespace
    - name: Ensure getting a namespace works
      shell: kubectl --context=test get namespace default

    # List and get certificates, orders, and challenges
    - name: Ensure listing certificates works
      shell: kubectl --context=test get {{ item }}
      loop:
        - certificates
        - orders
        - challenges

    # List and get configmap
    - name: Ensure listing configmaps works
      shell: kubectl --context=test get configmaps
    - name: Ensure getting a configmap works
      shell: kubectl --context=test get configmap test

    # List and get service
    - name: Ensure listing services works
      shell: kubectl --context=test get services
    - name: Ensure getting a configmap works
      shell: kubectl --context=test get service kubernetes