# syntax=docker/dockerfile:experimental
FROM docker.hops.works/hopsworks/hwutils:1.7

# The base image runs as the hopsworks user; the tests need root to
# install packages and trust the cluster certificate (update-ca-certificates)
USER root

# unbuffer for pytest output streaming, and a headless JDK 17
# for the maven builds in the java tests (the base image only ships a JRE 8,
# and hopsworks-api's java build targets 17 since the Spark 4.1 upgrade)
RUN apt-get update && apt-get install -y --no-install-recommends \
    expect-dev \
    openjdk-17-jdk-headless \
    && rm -rf /var/lib/apt/lists/*

ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
ENV PATH=$JAVA_HOME/bin:$PATH

# Maven from the binary tarball; apt maven would pull in openjdk-11
# as a second JDK
ARG MAVEN_VERSION=3.6.3
RUN curl -fL "https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" \
    -o /tmp/maven.tar.gz && \
    tar -xzf /tmp/maven.tar.gz -C /opt && \
    rm /tmp/maven.tar.gz && \
    ln -sf /opt/apache-maven-${MAVEN_VERSION}/bin/mvn /usr/local/bin/mvn

# Standalone CPython 3.14 (ships with pip); extracts to /srv/hops/python
ARG PYTHON_URL_X86_64=https://github.com/astral-sh/python-build-standalone/releases/download/20260610/cpython-3.14.6%2B20260610-x86_64-unknown-linux-gnu-install_only.tar.gz
ARG PYTHON_URL_ARM64=https://github.com/astral-sh/python-build-standalone/releases/download/20260610/cpython-3.14.6%2B20260610-aarch64-unknown-linux-gnu-install_only.tar.gz

RUN ARCH=$(uname -m) && \
    case "$ARCH" in \
        *x86_64) PYTHON_URL=$PYTHON_URL_X86_64 ;; \
        *aarch64) PYTHON_URL=$PYTHON_URL_ARM64 ;; \
        *) echo "Unsupported architecture: $ARCH"; exit 1 ;; \
    esac && \
    curl -fL "$PYTHON_URL" -o /tmp/python.tar.gz && \
    mkdir -p /srv/hops && \
    tar -xzf /tmp/python.tar.gz -C /srv/hops && \
    rm /tmp/python.tar.gz && \
    chgrp -R 0 /srv/hops && chmod -R g=u /srv/hops

# oc, for the OpenShift runs: create_project_namespace_okd.sh drives it, and
# kubectl has no equivalent of `oc adm policy add-scc-to-user`.
# The rhel8 build, not the default one: the default needs glibc 2.34 and this
# base is Ubuntu 20.04 on 2.31. rhel8 targets 2.28 and runs here.
# stable-4.21, not stable: the cluster is OKD 4.21, and a `stable` that rolls on
# to 4.22 has already been seen dropping the rhel8 build this needs.
ARG OC_URL_X86_64=https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/stable-4.21/openshift-client-linux-amd64-rhel8.tar.gz
ARG OC_URL_ARM64=https://mirror.openshift.com/pub/openshift-v4/arm64/clients/ocp/stable-4.21/openshift-client-linux-arm64-rhel8.tar.gz

RUN ARCH=$(uname -m) && \
    case "$ARCH" in \
        *x86_64) OC_URL=$OC_URL_X86_64 ;; \
        *aarch64) OC_URL=$OC_URL_ARM64 ;; \
        *) echo "Unsupported architecture: $ARCH"; exit 1 ;; \
    esac && \
    curl -fL "$OC_URL" | tar -xz -C /usr/local/bin oc && \
    oc version --client

ENV PATH=/srv/hops/python/bin:$PATH

RUN python3 --version && pip3 --version && mvn --version

RUN install -d -m 777 /tests
COPY --chmod=777 requirements.txt /tests/requirements.txt
WORKDIR /tests
# The job pip-installs into /srv/hops/python as an arbitrary UID in group 0. Set group
# perms in the RUN that writes the files: a later chgrp/chmod layer duplicates them.
RUN --mount=type=secret,id=gitconfig,target=/etc/gitconfig \
    pip3 install -r requirements.txt --no-cache-dir && \
    find /srv/hops \( ! -group 0 \
        -o -perm -u=r ! -perm -g=r -o -perm -u=w ! -perm -g=w -o -perm -u=x ! -perm -g=x \) \
        -exec chgrp 0 {} + -exec chmod g=u {} +

COPY --chmod=777 . /tests

# Set environment variables
ENV HOPSWORKS_PORT=28181
ENV HOPSWORKS_DOMAIN=hopsworks.glassfish.service.consul
ENV MYSQL_HOST=mysql.service.consul
ENV INSTALL_REQUIREMENTS=False
ENV PYTHONPATH=${home_dir}/tests:$PYTHONPATH
ENV IS_KUBE=True
ENV HOPSWORKS_TEST_USER_PASSWORD="Test1234!"
# This removes warnings on self signed certificate
ENV TRUST_CERTIFICATE=True
# Set the default command
ENTRYPOINT ["/bin/bash"]
CMD ["run_tests_on_kube.sh"]
