---
# xos tosca e2e test

- job-template:
    id: 'xos-tosca-e2e-test'
    name: 'verify_{project}_e2e-test'

    description: |
      Created by {id} job-template from ci-management/jjb/xos-tosca-e2e.yaml

    triggers:
      - cord-infra-gerrit-trigger-patchset:
          gerrit-server-name: '{gerrit-server-name}'
          project-regexp: '^{project}$'
          branch-regexp: '{branch-regexp}'
          dependency-jobs: '{dependency-jobs}'
          file-include-regexp: '{all-files-regexp}'

    properties:
      - cord-infra-properties:
          build-days-to-keep: '{build-days-to-keep}'
          artifact-num-to-keep: '{artifact-num-to-keep}'

    wrappers:
      - lf-infra-wrappers:
          build-timeout: '20'
          jenkins-ssh-credential: '{jenkins-ssh-credential}'

    scm:
      - cord-infra-gerrit-repo-scm:
          manifest-url: '{gerrit-server-url}/{cord-repo-manifest}'
          branch: '$GERRIT_BRANCH'
          destination-dir: 'cord'

    node: 'ubuntu16.04-basebuild-4c-8g'
    project-type: freestyle
    concurrent: true

    builders:
      - cord-infra-gerrit-repo-patch:
          destination-dir: 'cord'
          project: '$GERRIT_PROJECT'
          change-number: '$GERRIT_CHANGE_NUMBER'
          patchset-number: '$GERRIT_PATCHSET_NUMBER'
      - shell: |
          #!/usr/bin/env bash
          set -ex -o pipefail

          export XOS_DIR=$WORKSPACE/cord/orchestration/xos
          export TOSCA_DIR=$WORKSPACE/cord/orchestration/xos-tosca

           #install minikube+helm
           export MINIKUBE_WANTUPDATENOTIFICATION=false
           export MINIKUBE_WANTREPORTERRORPROMPT=false
           export CHANGE_MINIKUBE_NONE_USER=true
           export MINIKUBE_HOME=$HOME
           mkdir -p $HOME/.kube || true
           touch $HOME/.kube/config
           export KUBECONFIG=$HOME/.kube/config
           sudo -E /usr/bin/minikube start --vm-driver=none
           sleep 300

           helm init
           sleep 60
           helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com/

           # build tosca container
           cd $TOSCA_DIR
           make build

           #deploy xos core
           cd $WORKSPACE/cord/helm-charts
           helm dep update xos-core
           helm install --set images.xos_tosca.tag=candidate -f examples/imagePullPolicy-IfNotPresent.yaml -n xos-core xos-core

           #wait for xos-core and models to be loaded
           timeout 300 bash -c "until http -a admin@opencord.org:letmein GET http://127.0.0.1:30001/xosapi/v1/core/sites |jq '.items[0].name'|grep -q mysite; do echo 'Waiting for API To be up'; sleep 10; done"

           #test apis
           curl -Is -u admin@opencord.org:letmein GET http://127.0.0.1:30001/xosapi/v1/core/sites | grep -q '200 OK'
           curl -Is -u wronguser@opencord.org:wrongpass GET http://127.0.0.1:30001/xosapi/v1/core/sites | grep '403 Forbidden'
           curl -Is -u admin@opencord.org:letmein GET http://127.0.0.1:30001/xosapi/v1/utility/auth_noop | grep '405 Method Not Allowed'
           curl -Is -u wronguser@opencord.org:wrongpass GET http://127.0.0.1:30001/xosapi/v1/utility/auth_noop | grep '405 Method Not Allowed'
           curl -Is -u wronguser@opencord.org:wrongpass GET http://127.0.0.1:30001/xosapi/v1/utility/noop | grep '405 Method Not Allowed'
           curl -Is GET http://127.0.0.1:30001/xosapi/v1/utility/noop | grep '405 Method Not Allowed'
