<commit_before>;;
;;       Copyright (c) Two Sigma Investments, LLC.
;;       All Rights Reserved
;;
;;       THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
;;       Two Sigma Investments, LLC.
;;
;;       The copyright notice above does not evidence any
;;       actual or intended publication of such source code.
;;
(ns waiter.deployment-errors-test
  (:require [clojure.string :as str]
            [clojure.test :refer :all]
            [clojure.tools.logging :as log]
            [waiter.client-tools :refer :all]
            [waiter.settings :as settings]))

(deftest ^:parallel ^:integration-fast test-invalid-health-check-response
  (testing-using-waiter-url
    (let [headers {:x-waiter-name (rand-name)
                   ; health check endpoint always returns status 402
                   :x-waiter-health-check-url "/bad-status?status=402"
                   :x-waiter-queue-timeout 600000}
          {:keys [headers body] :as response} (make-request-with-debug-info headers #(make-kitchen-request waiter-url %))
          service-id (get headers "x-waiter-service-id")]
      (is (not (nil? service-id)))
      (assert-response-status response 503)
      (is (str/starts-with? body (str "Deployment error: " (-> (waiter-settings waiter-url) :messages :invalid-health-check-response))))
      (delete-service waiter-url service-id))))

(deftest ^:parallel ^:integration-fast test-cannot-connect
  (testing-using-waiter-url
    (let [headers {:x-waiter-name (rand-name)
                   ; nothing to connect to
                   :x-waiter-cmd "sleep 3600"
                   :x-waiter-queue-timeout 600000}
          {:keys [headers body] :as response} (make-request-with-debug-info headers #(make-shell-request waiter-url %))
          service-id (get headers "x-waiter-service-id")]
      (is (not (nil? service-id)))
      (assert-response-status response 503)
      (is (str/starts-with? body (str "Deployment error: " (-> (waiter-settings waiter-url) :messages :cannot-connect))))
      (delete-service waiter-url service-id))))

(deftest ^:parallel ^:integration-fast test-health-check-timed-out
  (testing-using-waiter-url
    (let [headers {:x-waiter-name (rand-name)
                   ; health check endpoint sleeps for 300000 ms (= 5 minutes)
                   :x-waiter-health-check-url "/sleep?sleep-ms=300000&status=400"
                   :x-waiter-queue-timeout 600000}
          {:keys [headers body] :as response} (make-request-with-debug-info headers #(make-kitchen-request waiter-url %))
          service-id (get headers "x-waiter-service-id")]
      (is (not (nil? service-id)))
      (assert-response-status response 503)
      (is (str/starts-with? body (str "Deployment error: " (-> (waiter-settings waiter-url) :messages :health-check-timed-out))))
      (delete-service waiter-url service-id))))

(deftest ^:parallel ^:integration-fast test-health-check-requires-authentication
  (testing-using-waiter-url
    (let [headers {:x-waiter-name (rand-name)
                   :x-waiter-health-check-url "/bad-status?status=401"}<commit_msg>:integration-fast -> :integration-slow for certain deployment error tests (#123)<commit_after>;;
;;       Copyright (c) Two Sigma Investments, LLC.
;;       All Rights Reserved
;;
;;       THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
;;       Two Sigma Investments, LLC.
;;
;;       The copyright notice above does not evidence any
;;       actual or intended publication of such source code.
;;
(ns waiter.deployment-errors-test
  (:require [clojure.string :as str]
            [clojure.test :refer :all]
            [clojure.tools.logging :as log]
            [waiter.client-tools :refer :all]
            [waiter.settings :as settings]))

(deftest ^:parallel ^:integration-slow test-invalid-health-check-response
  (testing-using-waiter-url
    (let [headers {:x-waiter-name (rand-name)
                   ; health check endpoint always returns status 402
                   :x-waiter-health-check-url "/bad-status?status=402"
                   :x-waiter-queue-timeout 600000}
          {:keys [headers body] :as response} (make-request-with-debug-info headers #(make-kitchen-request waiter-url %))
          service-id (get headers "x-waiter-service-id")]
      (is (not (nil? service-id)))
      (assert-response-status response 503)
      (is (str/starts-with? body (str "Deployment error: " (-> (waiter-settings waiter-url) :messages :invalid-health-check-response))))
      (delete-service waiter-url service-id))))

(deftest ^:parallel ^:integration-slow test-cannot-connect
  (testing-using-waiter-url
    (let [headers {:x-waiter-name (rand-name)
                   ; nothing to connect to
                   :x-waiter-cmd "sleep 3600"
                   :x-waiter-queue-timeout 600000}
          {:keys [headers body] :as response} (make-request-with-debug-info headers #(make-shell-request waiter-url %))
          service-id (get headers "x-waiter-service-id")]
      (is (not (nil? service-id)))
      (assert-response-status response 503)
      (is (str/starts-with? body (str "Deployment error: " (-> (waiter-settings waiter-url) :messages :cannot-connect))))
      (delete-service waiter-url service-id))))

(deftest ^:parallel ^:integration-slow test-health-check-timed-out
  (testing-using-waiter-url
    (let [headers {:x-waiter-name (rand-name)
                   ; health check endpoint sleeps for 300000 ms (= 5 minutes)
                   :x-waiter-health-check-url "/sleep?sleep-ms=300000&status=400"
                   :x-waiter-queue-timeout 600000}
          {:keys [headers body] :as response} (make-request-with-debug-info headers #(make-kitchen-request waiter-url %))
          service-id (get headers "x-waiter-service-id")]
      (is (not (nil? service-id)))
      (assert-response-status response 503)
      (is (str/starts-with? body (str "Deployment error: " (-> (waiter-settings waiter-url) :messages :health-check-timed-out))))
      (delete-service waiter-url service-id))))

(deftest ^:parallel ^:integration-fast test-health-check-requires-authentication
  (testing-using-waiter-url
    (let [headers {:x-waiter-name (rand-name)
                   :x-waiter-health-check-url "/bad-status?status=401"}
{'fraction_non_alphanumeric': 0.13996464727623334, 'fraction_numerical': 0.018479832878033103, 'mean_word_length': 2.6828402366863906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '525759', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1874, 'n_words': 456}