diff --git a/serve-python/Dockerfile b/serve-python/Dockerfile index 13a1ec8..48992a5 100644 --- a/serve-python/Dockerfile +++ b/serve-python/Dockerfile @@ -12,15 +12,17 @@ RUN useradd -m -u 1000 $USER # Set working directory (this is where the code should go) WORKDIR $HOME -RUN /bin/bash -c "apt update" -RUN /bin/bash -c "apt install python3.7-dev -y" -RUN /bin/bash -c "apt install curl -y" -RUN /bin/bash -c "apt-get install python3-distutils -y" -RUN /bin/bash -c "curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py" -RUN /bin/bash -c "python3.7 get-pip.py" -RUN /bin/bash -c "apt install gcc -y" -RUN /bin/bash -c "update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1" -RUN /bin/bash -c "pip3 install --upgrade pip" +RUN apt update \ + && apt install python3.7-dev -y \ + && apt install curl -y \ + && apt-get install python3-distutils -y \ + && curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py \ + && python3.7 get-pip.py \ + && apt install gcc -y \ + && update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1 \ + && pip3 install --upgrade pip \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* # RUN /bin/bash -c "apt update" # RUN /bin/bash -c "curl https://dl.min.io/client/mc/release/linux-amd64/mc --output mc && chmod +x mc" COPY requirements.txt $HOME/requirements.txt @@ -36,4 +38,4 @@ RUN chmod +x start-script.sh \ ENV STACKN_MODEL_PATH=$HOME/models ENV PYTHONPATH=$HOME/models -CMD ./deploy.sh \ No newline at end of file +CMD ["./deploy.sh"] \ No newline at end of file diff --git a/serve-python/tests/test_api.py b/serve-python/tests/test_api.py index ad71990..7b9289c 100644 --- a/serve-python/tests/test_api.py +++ b/serve-python/tests/test_api.py @@ -41,29 +41,54 @@ def test_container_ports(): def test_container_access(): """Test of basic communication with the container returns status 200 (OK).""" - try: - url = _get_api_url(container) + "/health" - response = requests.get(url, timeout=TIMEOUT_CALL) - if response.status_code == 200: - assert True - except ConnectionError: - assert False + max_attempts = 100 + for attempt in range(1, max_attempts + 1): + try: + url = _get_api_url(container) + "/health" + response = requests.get(url, timeout=TIMEOUT_CALL) + if response.status_code == 200: + assert True + except: + pass + if attempt == max_attempts: + RuntimeError( + f"Python Deployment did not become ready after {max_attempts} attempts" + ) + assert False + + print( + f"Attempt {attempt} failed, waiting for 10 seconds before trying again..." + ) + time.sleep(10) def test_prediction(): """Verify that the model can be used for predictions.""" - # Set input string - example = "Jag är ett barn, och det här är mitt hem. Alltså är det ett barnhem!" - # msk_ind takes an index in order to mask (or hide) one of the word in the example sentence, which should then be predicted by the BERT trained model - msk_ind = 4 - url = _get_api_url(container) + "/predict/" - res = requests.post(url, json={"pred": example, "msk_ind": msk_ind}) - text_encoded = res.json().encode("latin1") - text_decoded = text_encoded.decode("unicode-escape") - print(json.loads(text_decoded)) - assert json.loads(text_decoded) == { - "result": ["barn", "hem", "hus", "spädbarn", "##hem"] - } + max_attempts = 100 + for attempt in range(1, max_attempts + 1): + # Set input string + example = "Jag är ett barn, och det här är mitt hem. Alltså är det ett barnhem!" + # msk_ind takes an index in order to mask (or hide) one of the word in the example sentence, which should then be predicted by the BERT trained model + msk_ind = 4 + url = _get_api_url(container) + "/predict/" + res = requests.post(url, json={"pred": example, "msk_ind": msk_ind}) + text_encoded = res.json().encode("latin1") + text_decoded = text_encoded.decode("unicode-escape") + print(json.loads(text_decoded)) + if res.status_code == 200: + assert json.loads(text_decoded) == { + "result": ["barn", "hem", "hus", "spädbarn", "##hem"] + } + if attempt == max_attempts: + RuntimeError( + f"Python Deployment did not become ready after {max_attempts} attempts" + ) + assert False + + print( + f"Attempt {attempt} failed, waiting for 10 seconds before trying again..." + ) + time.sleep(10) def test_shutdown():