#!/usr/bin/bash -eu
 
POD_NAME="$JOB_BASE_NAME"
 
cleanup () {
  kubectl delete pod "$POD_NAME"
}
 
trap cleanup EXIT
 
aws eks --region eu-west-1 update-kubeconfig --name ci
 
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: ${POD_NAME}
spec:
  containers:
  - name: chrome
    image: selenium/node-chrome:3.141.59-20200525
    env:
    - name: HUB_HOST
      value: "localhost"
    resources:
      requests:
        memory: "2Gi"
        cpu: "1000m"
      limits:
        memory: "2Gi"
        cpu: "1000m"
  - name: hub
    image: selenium/hub:3.141.59-20200525
    resources:
      requests:
        memory: "512Mi"
        cpu: "250m"
      limits:
        memory: "512Mi"
        cpu: "250m"
EOF
 
echo 'Waiting until pods are ready'
 
until
  status=$(kubectl get pod "$POD_NAME" --output json | jq -r .status.phase)
  [[ "$status" == 'Running' ]]
do
  sleep 1
done
 
kubectl get pods
 
 
kubectl port-forward "$POD_NAME" 4444:4444 > /dev/null 2> /dev/null &
 
 
echo 'Waiting for the hub'
 
until curl -Lfs --output /dev/null http://localhost:4444/wd/hub; do
  sleep 1
done
 
 
python3 -m venv venv
 
source venv/bin/activate
 
pip3 install selenium
 
python3 <<"EOF"
 
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
 
 
options = webdriver.ChromeOptions()
options.add_argument(f"--browserName=chrome")
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
 
driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    desired_capabilities=options.to_capabilities())
 
driver.get('https://duckduckgo.com/')
 
e = WebDriverWait(driver, 10).until(lambda d: d.find_element_by_id('content_homepage'))
 
print(e.text)
 
EOF