Add tests

Fix configmap generation.
This commit is contained in:
erik 2022-09-21 15:55:42 +02:00
parent f0a69e7fda
commit bca85d1da4
4 changed files with 19 additions and 13 deletions

View file

@ -5,6 +5,7 @@
:url "https://www.apache.org/licenses/LICENSE-2.0.html"}
:dependencies [[org.clojure/clojure "1.10.3"]
[org.clojure/tools.reader "1.3.6"]
[org.clojure/math.numeric-tower "0.0.5"]
[org.domaindrivenarchitecture/c4k-common-clj "3.0.1"]
[hickory "0.7.1"]]
:target-path "target/%s/"

View file

@ -4,8 +4,7 @@
[dda.c4k-common.common :as cm]
[dda.c4k-website.website :as website]))
(defn k8s-objects [config]
(let [storage-class (if (contains? config :postgres-data-volume-path) :manual :local-path)]
(defn k8s-objects [config]
(cm/concat-vec
(map yaml/to-string
(filter #(not (nil? %))
@ -14,4 +13,4 @@
(website/generate-nginx-configmap config)
(website/generate-nginx-deployment)
(website/generate-nginx-service)
(website/generate-website-content-volume config)])))))
(website/generate-website-content-volume config)]))))

View file

@ -1,7 +1,7 @@
(ns dda.c4k-website.website
(:require
[clojure.spec.alpha :as s]
[clojure.math :as m]
[clojure.math.numeric-tower :as m]
[clojure.string :as st]
#?(:cljs [shadow.resource :as rc])
#?(:clj [orchestra.core :refer [defn-spec]]
@ -21,12 +21,16 @@
(s/def ::fqdn pred/fqdn-string?)
(s/def ::issuer pred/letsencrypt-issuer?)
(s/def ::volume-total-storage-size (partial pred/int-gt-n? 5))
(s/def ::mailer-user pred/bash-env-string?)
(s/def ::mailer-pw pred/bash-env-string?)
(def config-defaults {:issuer "staging"})
(def config? (s/keys :req-un [::fqdn]
:opt-un [::issuer]))
(def auth? (s/keys :opt-un [::mailer-user ::mailer-pw]))
(def vol? (s/keys :req-un [::volume-total-storage-size
::number-of-websites]))
@ -68,11 +72,13 @@
(defn-spec generate-nginx-configmap pred/map-or-seq?
[config config?]
(let [{:keys [fqdn]} config]
(->
(yaml/load-as-edn "website/nginx-configmap.yaml")
(cm/replace-all-matching-values-by-new-value "FQDN" (str fqdn ";"))
)))
(let [{:keys [fqdn]} config
configmap (yaml/load-as-edn "website/nginx-configmap.yaml")]
(->
configmap
(assoc-in [:data :website.conf] (st/replace (-> configmap :data :website.conf) #"FQDN" fqdn))
)
))
(defn-spec generate-nginx-deployment pred/map-or-seq?
[]

View file

@ -30,11 +30,11 @@
})))))
(deftest should-generate-nginx-configmap
(is (= {:server_name-c1 "test.de",
:server_name-c2 "test.com"}
(th/map-diff (cut/generate-appini-env {:fqdn "test.de"
(is (= {:website.conf-c1 "server {\n\n listen 80 default_server;\n listen [::]:80 default_server;\n\n listen 443 ssl;\n\n ssl_certificate /etc/certs/tls.crt;\n ssl_certificate_key /etc/certs/tls.key;\n\n server_name test.de\n\n # security headers\n add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';\n add_header Content-Security-Policy \"default-src 'self'; font-src *;img-src * data:; script-src *; style-src *\";\n add_header X-XSS-Protection \"1; mode=block\";\n add_header X-Frame-Options \"SAMEORIGIN\";\n add_header X-Content-Type-Options nosniff;\n add_header Referrer-Policy \"strict-origin\";\n # maybe need to add:\n # add_header Permissions-Policy \"permissions here\";\n\n root /var/www/html/website/;\n # root /usr/share/nginx/html/; # testing purposes\n\n index index.html;\n\n try_files $uri /index.html;\n\n}",
:website.conf-c2 "server {\n\n listen 80 default_server;\n listen [::]:80 default_server;\n\n listen 443 ssl;\n\n ssl_certificate /etc/certs/tls.crt;\n ssl_certificate_key /etc/certs/tls.key;\n\n server_name test.com\n\n # security headers\n add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';\n add_header Content-Security-Policy \"default-src 'self'; font-src *;img-src * data:; script-src *; style-src *\";\n add_header X-XSS-Protection \"1; mode=block\";\n add_header X-Frame-Options \"SAMEORIGIN\";\n add_header X-Content-Type-Options nosniff;\n add_header Referrer-Policy \"strict-origin\";\n # maybe need to add:\n # add_header Permissions-Policy \"permissions here\";\n\n root /var/www/html/website/;\n # root /usr/share/nginx/html/; # testing purposes\n\n index index.html;\n\n try_files $uri /index.html;\n\n}"}
(th/map-diff (cut/generate-nginx-configmap {:fqdn "test.de"
})
(cut/generate-appini-env {:fqdn "test.com"
(cut/generate-nginx-configmap {:fqdn "test.com"
})))))
(deftest should-generate-website-content-volume