version: "3"
services:
  kube-api:
    image: rancher/k3s:${K3S_VERSION}
    command: server --disable-agent
    container_name: k3s-api
    volumes:
      - ./tmp-rancher:/.kube
      - ./tmp-rancher:/.rancher
      - ./tmp-rancher/etc:/etc/rancher/k3s
    expose:
      - 6443
    user: "${USERID}"
    environment:
      - USER=nobody
      - HOME=/
  e2e-test:
    build: .
    container_name: e2e-test
    links:
      - "kube-api:kube-api"
    depends_on:
      - kube-api
    volumes:
      - ./tmp-rancher:/tmp/rancher
      - ..:/work
    working_dir: /work
    environment:
      - HOME=/
    user: "${USERID}"
    command:
      - bash
      - -c
      - |
        echo "INFO: Starting tests: unit, lint ..."
        make -C tests test-srcs
        export KUBECONFIG=/tmp/kubeconfig
        echo "INFO: Waiting for kube-api to be available ..."
        until kubectl get nodes; do
          sleep 1
          # Found that k3s releases create k3s.yaml under diff paths,
          # redirecting stderr just to avoid red-herrings errors
          sed -e s/localhost/kube-api/ -e s/127.0.0.1/kube-api/ \
            /tmp/rancher/k3s.yaml /tmp/rancher/etc/k3s.yaml \
            > $$KUBECONFIG 2>/dev/null
        done
        echo "INFO: Starting tests: validate ..."
        set -x
        make -C tests test-kube
