2022-09-08 13:04:17 +00:00
|
|
|
(ns dda.c4k-website.website-test
|
|
|
|
(:require
|
|
|
|
#?(:clj [clojure.test :refer [deftest is are testing run-tests]]
|
|
|
|
:cljs [cljs.test :refer-macros [deftest is are testing run-tests]])
|
|
|
|
[clojure.spec.test.alpha :as st]
|
|
|
|
[dda.c4k-common.test-helper :as th]
|
|
|
|
[dda.c4k-common.base64 :as b64]
|
2022-10-14 11:41:21 +00:00
|
|
|
[dda.c4k-website.website :as cut]
|
|
|
|
[clojure.spec.alpha :as s]))
|
2022-09-08 13:04:17 +00:00
|
|
|
|
2022-10-13 14:39:55 +00:00
|
|
|
(st/instrument `cut/generate-http-ingress)
|
|
|
|
(st/instrument `cut/generate-https-ingress)
|
|
|
|
(st/instrument `cut/generate-nginx-configmap)
|
|
|
|
(st/instrument `cut/generate-nginx-deployment)
|
|
|
|
(st/instrument `cut/generate-nginx-service)
|
2022-09-21 12:40:34 +00:00
|
|
|
(st/instrument `cut/generate-website-content-volume)
|
2022-10-13 14:39:55 +00:00
|
|
|
(st/instrument `cut/generate-website-certificate)
|
|
|
|
(st/instrument `cut/generate-website-build-cron)
|
|
|
|
(st/instrument `cut/generate-website-build-deployment)
|
|
|
|
(st/instrument `cut/generate-website-build-secret)
|
2022-09-08 13:04:17 +00:00
|
|
|
|
2022-10-14 11:41:21 +00:00
|
|
|
(deftest should-be-valid-website-auth-spec
|
|
|
|
(is (true? (s/valid? cut/auth? {:auth
|
|
|
|
[{:unique-name "test.io"
|
|
|
|
:username "someuser"
|
|
|
|
:authtoken "abedjgbasdodj"}
|
|
|
|
{:unique-name "example.io"
|
|
|
|
:username "someuser"
|
|
|
|
:authtoken "abedjgbasdodj"}]}))))
|
|
|
|
|
|
|
|
(deftest should-be-valid-website-conf-spec
|
|
|
|
(is (true? (s/valid? cut/config? {:issuer "staging"
|
|
|
|
:websites
|
|
|
|
[{:unique-name "test.io" ;
|
|
|
|
:fqdns ["test.de" "test.org" "www.test.de" "www.test.org"]
|
|
|
|
:gitea-host "gitlab.de"
|
|
|
|
:gitea-repo "repo"
|
|
|
|
:branchname "main"}
|
|
|
|
{:unique-name "example.io"
|
|
|
|
:fqdns ["example.org", "www.example.com"]
|
|
|
|
:gitea-host "finegitehost.net"
|
|
|
|
:gitea-repo "repo"
|
|
|
|
:branchname "main"}]}))))
|
|
|
|
|
2022-10-21 13:36:31 +00:00
|
|
|
(deftest should-generate-nginx-configmap-website
|
2022-10-21 09:57:44 +00:00
|
|
|
(is (= "server {\n listen 80 default_server;\n listen [::]:80 default_server;\n listen 443 ssl;\n ssl_certificate /etc/certs/tls.crt;\n ssl_certificate_key /etc/certs/tls.key;\n server_name test.de www.test.de test-it.de www.test-it.de;\n add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';\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 # add_header Permissions-Policy \"permissions here\";\n root /var/www/html/website/;\n index index.html;\n location / {\n try_files $uri $uri/ /index.html =404;\n }\n}\n"
|
|
|
|
(:website.conf (:data (cut/generate-nginx-configmap {:unique-name "test.io",
|
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
2022-10-21 13:36:31 +00:00
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))))
|
|
|
|
(is (= "types {\n text/html html htm shtml;\n text/css css;\n text/xml xml rss;\n image/gif gif;\n image/jpeg jpeg jpg;\n application/x-javascript js;\n text/plain txt;\n text/x-component htc;\n text/mathml mml;\n image/png png;\n image/x-icon ico;\n image/x-jng jng;\n image/vnd.wap.wbmp wbmp;\n application/java-archive jar war ear;\n application/mac-binhex40 hqx;\n application/pdf pdf;\n application/x-cocoa cco;\n application/x-java-archive-diff jardiff;\n application/x-java-jnlp-file jnlp;\n application/x-makeself run;\n application/x-perl pl pm;\n application/x-pilot prc pdb;\n application/x-rar-compressed rar;\n application/x-redhat-package-manager rpm;\n application/x-sea sea;\n application/x-shockwave-flash swf;\n application/x-stuffit sit;\n application/x-tcl tcl tk;\n application/x-x509-ca-cert der pem crt;\n application/x-xpinstall xpi;\n application/zip zip;\n application/octet-stream deb;\n application/octet-stream bin exe dll;\n application/octet-stream dmg;\n application/octet-stream eot;\n application/octet-stream iso img;\n application/octet-stream msi msp msm;\n audio/mpeg mp3;\n audio/x-realaudio ra;\n video/mpeg mpeg mpg;\n video/quicktime mov;\n video/x-flv flv;\n video/x-msvideo avi;\n video/x-ms-wmv wmv;\n video/x-ms-asf asx asf;\n video/x-mng mng;\n}\n"
|
|
|
|
(:mime.types (:data (cut/generate-nginx-configmap {:unique-name "test.io",
|
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))))
|
|
|
|
(is (= "user nginx;\nworker_processes 3;\nerror_log /var/log/nginx/error.log;\npid /var/log/nginx/nginx.pid;\nworker_rlimit_nofile 8192;\nevents { \n worker_connections 4096; \n}\nhttp {\n include /etc/nginx/mime.types; \n default_type application/octet-stream;\n log_format main '$remote_addr - $remote_user [$time_local] $status'\n '\"$request\" $body_bytes_sent \"$http_referer\"'\n '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n access_log /var/log/nginx/access.log main;\n sendfile on;\n tcp_nopush on;\n keepalive_timeout 65;\n server_names_hash_bucket_size 128;\n include /etc/nginx/conf.d/website.conf;\n}\n"
|
|
|
|
(:nginx.conf (:data (cut/generate-nginx-configmap {:unique-name "test.io",
|
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))))
|
|
|
|
(is (= {:apiVersion "v1",
|
|
|
|
:kind "ConfigMap",
|
|
|
|
:metadata {:name "test-io-configmap", :namespace "default"}}
|
|
|
|
(dissoc (cut/generate-nginx-configmap {:unique-name "test.io",
|
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}) :data))))
|
2022-09-08 13:04:17 +00:00
|
|
|
|
2022-09-27 13:45:13 +00:00
|
|
|
(deftest should-generate-nginx-deployment
|
|
|
|
(is (= {:apiVersion "apps/v1",
|
|
|
|
:kind "Deployment",
|
2022-10-13 14:39:55 +00:00
|
|
|
:metadata {:name "test-io-deployment"},
|
2022-09-27 13:45:13 +00:00
|
|
|
:spec
|
|
|
|
{:replicas 1,
|
2022-10-13 14:39:55 +00:00
|
|
|
:selector {:matchLabels {:app "test-io-nginx"}},
|
2022-09-27 13:45:13 +00:00
|
|
|
:template
|
2022-10-13 14:39:55 +00:00
|
|
|
{:metadata {:labels {:app "test-io-nginx"}},
|
2022-09-27 13:45:13 +00:00
|
|
|
:spec
|
|
|
|
{:containers
|
2022-10-13 14:39:55 +00:00
|
|
|
[{:name "test-io-nginx",
|
2022-09-27 13:45:13 +00:00
|
|
|
:image "nginx:latest",
|
2022-09-30 15:54:26 +00:00
|
|
|
:imagePullPolicy "IfNotPresent",
|
|
|
|
:ports [{:containerPort 80}],
|
|
|
|
:volumeMounts
|
|
|
|
[{:mountPath "/etc/nginx", :readOnly true, :name "nginx-config-volume"}
|
|
|
|
{:mountPath "/var/log/nginx", :name "log"}
|
|
|
|
{:mountPath "/var/www/html/website", :name "website-content-volume", :readOnly true}
|
|
|
|
{:mountPath "/etc/certs", :name "website-cert", :readOnly true}]}],
|
|
|
|
:volumes
|
|
|
|
[{:name "nginx-config-volume",
|
|
|
|
:configMap
|
2022-10-13 14:39:55 +00:00
|
|
|
{:name "test-io-configmap",
|
2022-09-30 15:54:26 +00:00
|
|
|
:items
|
|
|
|
[{:key "nginx.conf", :path "nginx.conf"}
|
|
|
|
{:key "website.conf", :path "conf.d/website.conf"}
|
|
|
|
{:key "mime.types", :path "mime.types"}]}}
|
|
|
|
{:name "log", :emptyDir {}}
|
2022-10-13 14:39:55 +00:00
|
|
|
{:name "website-content-volume", :persistentVolumeClaim {:claimName "test-io-content-volume"}}
|
2022-09-30 15:54:26 +00:00
|
|
|
{:name "website-cert",
|
|
|
|
:secret
|
2022-10-13 14:39:55 +00:00
|
|
|
{:secretName "test-io-cert", :items [{:key "tls.crt", :path "tls.crt"} {:key "tls.key", :path "tls.key"}]}}]}}}}
|
2022-10-14 11:41:21 +00:00
|
|
|
(cut/generate-nginx-deployment {:unique-name "test.io",
|
2022-10-13 14:39:55 +00:00
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))
|
2022-09-30 15:54:26 +00:00
|
|
|
|
|
|
|
(deftest should-generate-nginx-service
|
2022-10-13 14:39:55 +00:00
|
|
|
(is (= {:name-c1 "test-io-service",
|
|
|
|
:name-c2 "test-org-service",
|
|
|
|
:app-c1 "test-io-nginx",
|
|
|
|
:app-c2 "test-org-nginx"}
|
2022-10-14 11:41:21 +00:00
|
|
|
(th/map-diff (cut/generate-nginx-service {:unique-name "test.io",
|
2022-10-13 14:39:55 +00:00
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})
|
2022-10-14 11:41:21 +00:00
|
|
|
(cut/generate-nginx-service {:unique-name "test.org",
|
2022-10-13 14:39:55 +00:00
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))))
|
2022-09-27 13:45:13 +00:00
|
|
|
|
2022-09-30 15:54:26 +00:00
|
|
|
(deftest should-generate-website-build-cron
|
2022-09-27 13:45:13 +00:00
|
|
|
(is (= {:apiVersion "batch/v1beta1",
|
|
|
|
:kind "CronJob",
|
2022-10-13 14:39:55 +00:00
|
|
|
:metadata {:name "test-io-build-cron", :labels {:app.kubernetes.part-of "website"}},
|
2022-09-27 13:45:13 +00:00
|
|
|
:spec
|
2022-09-28 12:32:41 +00:00
|
|
|
{:schedule "1,7,14,21,28,35,42,49,54,59 * * * *",
|
2022-09-27 13:45:13 +00:00
|
|
|
:successfulJobsHistoryLimit 1,
|
|
|
|
:failedJobsHistoryLimit 1,
|
|
|
|
:jobTemplate
|
|
|
|
{:spec
|
|
|
|
{:template
|
|
|
|
{:spec
|
|
|
|
{:containers
|
|
|
|
[{:image "domaindrivenarchitecture/c4k-website-build",
|
2022-10-13 14:39:55 +00:00
|
|
|
:name "test-io-build-app",
|
2022-09-27 13:45:13 +00:00
|
|
|
:imagePullPolicy "IfNotPresent",
|
2022-10-13 14:39:55 +00:00
|
|
|
:command ["/entrypoint.sh"],
|
|
|
|
:envFrom [{:secretRef {:name "test-io-secret"}}],
|
2022-09-27 13:45:13 +00:00
|
|
|
:volumeMounts [{:name "content-volume", :mountPath "/var/www/html/website"}]}],
|
2022-10-13 14:39:55 +00:00
|
|
|
:volumes [{:name "content-volume", :persistentVolumeClaim {:claimName "test-io-content-volume"}}],
|
2022-09-27 13:45:13 +00:00
|
|
|
:restartPolicy "OnFailure"}}}}}}
|
2022-10-14 11:41:21 +00:00
|
|
|
(cut/generate-website-build-cron {:unique-name "test.io",
|
2022-10-13 14:39:55 +00:00
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))
|
2022-09-27 13:45:13 +00:00
|
|
|
|
|
|
|
(deftest should-generate-website-build-deployment
|
|
|
|
(is (= {:apiVersion "apps/v1",
|
|
|
|
:kind "Deployment",
|
2022-10-13 14:39:55 +00:00
|
|
|
:metadata {:name "test-io-build-deployment"},
|
2022-09-27 13:45:13 +00:00
|
|
|
:spec
|
|
|
|
{:replicas 0,
|
2022-10-13 14:39:55 +00:00
|
|
|
:selector {:matchLabels {:app "test-io-builder"}},
|
2022-09-27 13:45:13 +00:00
|
|
|
:strategy {:type "Recreate"},
|
|
|
|
:template
|
|
|
|
{:metadata
|
2022-10-13 14:39:55 +00:00
|
|
|
{:labels {:app "test-io-builder", :app.kubernetes.io/name "test-io-builder", :app.kubernetes.io/part-of "website"}},
|
2022-09-27 13:45:13 +00:00
|
|
|
:spec
|
|
|
|
{:containers
|
|
|
|
[{:image "domaindrivenarchitecture/c4k-website-build",
|
2022-10-13 14:39:55 +00:00
|
|
|
:name "test-io-build-app",
|
2022-09-27 13:45:13 +00:00
|
|
|
:imagePullPolicy "IfNotPresent",
|
2022-10-13 14:39:55 +00:00
|
|
|
:command ["/entrypoint.sh"],
|
|
|
|
:envFrom [{:secretRef {:name "test-io-secret"}}],
|
2022-09-27 13:45:13 +00:00
|
|
|
:volumeMounts [{:name "content-volume", :mountPath "/var/www/html/website"}]}],
|
2022-10-13 14:39:55 +00:00
|
|
|
:volumes [{:name "content-volume", :persistentVolumeClaim {:claimName "test-io-content-volume"}}]}}}}
|
2022-10-14 11:41:21 +00:00
|
|
|
(cut/generate-website-build-deployment {:unique-name "test.io",
|
2022-10-13 14:39:55 +00:00
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]}))))
|
2022-09-27 13:45:13 +00:00
|
|
|
|
|
|
|
(deftest should-generate-website-build-secret
|
2022-10-13 14:39:55 +00:00
|
|
|
(is (= {:name-c1 "test-io-secret",
|
|
|
|
:name-c2 "test-org-secret",
|
|
|
|
:AUTHTOKEN-c1 (b64/encode "token1"),
|
2022-09-27 13:45:13 +00:00
|
|
|
:AUTHTOKEN-c2 (b64/encode "token2"),
|
2022-10-13 14:39:55 +00:00
|
|
|
:GITREPOURL-c1 (b64/encode "https://gitlab.org/api/v1/repos/dumpty/websitebau/archive/testname.zip"),
|
|
|
|
:GITREPOURL-c2 (b64/encode "https://github.com/api/v1/repos/humpty/websitedachs/archive/testname.zip")}
|
2022-10-14 11:41:21 +00:00
|
|
|
(th/map-diff (cut/generate-website-build-secret {:unique-name "test.io",
|
2022-10-13 14:39:55 +00:00
|
|
|
:authtoken "token1",
|
|
|
|
:gitea-host "gitlab.org",
|
|
|
|
:gitea-repo "websitebau",
|
|
|
|
:username "dumpty",
|
|
|
|
:branchname "testname"})
|
2022-10-14 11:41:21 +00:00
|
|
|
(cut/generate-website-build-secret {:unique-name "test.org",
|
2022-10-13 14:39:55 +00:00
|
|
|
:authtoken "token2",
|
|
|
|
:gitea-host "github.com",
|
|
|
|
:gitea-repo "websitedachs",
|
|
|
|
:username "humpty",
|
|
|
|
:branchname "testname"})))))
|
2022-09-27 13:45:13 +00:00
|
|
|
|
2022-09-21 12:40:34 +00:00
|
|
|
(deftest should-generate-website-content-volume
|
2022-10-13 14:39:55 +00:00
|
|
|
(is (= {:name-c1 "test-io-content-volume",
|
|
|
|
:name-c2 "test-org-content-volume",
|
|
|
|
:app-c1 "test-io-nginx",
|
|
|
|
:app-c2 "test-org-nginx"}
|
2022-10-14 11:41:21 +00:00
|
|
|
(th/map-diff (cut/generate-website-content-volume {:unique-name "test.io",
|
2022-10-13 14:39:55 +00:00
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})
|
2022-10-14 11:41:21 +00:00
|
|
|
(cut/generate-website-content-volume {:unique-name "test.org",
|
2022-10-13 14:39:55 +00:00
|
|
|
:gitea-host "gitea.evilorg",
|
|
|
|
:gitea-repo "none",
|
|
|
|
:branchname "mablain",
|
|
|
|
:fqdns ["test.de" "www.test.de" "test-it.de" "www.test-it.de"]})))))
|