This repository was archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathend_to_end_test.clj
163 lines (133 loc) · 6.6 KB
/
end_to_end_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
(ns lambdacd-git.end-to-end-test
(:require [clojure.test :refer :all]
[lambdacd-git.core :as core]
[lambdacd.core :as lambdacd]
[lambdacd.state.core :as lambdacd-state]
[lambdacd-git.example.simple-pipeline :as simple-pipeline]
[lambdacd.steps.manualtrigger :as manualtrigger]
[lambdacd.steps.shell :as shell]
[lambdacd-git.test-utils :as test-utils]
[lambdacd.steps.control-flow :refer [either with-workspace]]
[lambdacd.steps.manualtrigger :refer [wait-for-manual-trigger]]
[lambdacd-git.ssh :as ssh]
[lambdacd.execution.core :as execution-core])
(:import (org.eclipse.jgit.transport UsernamePasswordCredentialsProvider SshSessionFactory)))
(defn match-all-refs [_]
true)
(defn wait-for-git [args ctx]
(core/wait-for-git ctx (:repo-uri (:config ctx))
:ref match-all-refs
:ms-between-polls (* 1 1000)))
(defn clone [args ctx]
(core/clone ctx (:repo-uri (:config ctx)) (:revision args) (:cwd args)))
(defn ls [args ctx]
(shell/bash ctx (:cwd args) "ls"))
(defn create-new-tag [args ctx]
(core/tag-version ctx (:cwd args) (:repo-uri (:config ctx)) "HEAD" (str (System/currentTimeMillis))))
(def pipeline-structure
`((either
wait-for-manual-trigger
wait-for-git)
(with-workspace
clone
core/list-changes
ls
create-new-tag)))
; ======================================================================================================================
(defn trigger-id [ctx build-number step-id]
(let [step-result (lambdacd-state/get-step-result ctx build-number step-id)]
(:trigger-id step-result)))
(defn- trigger-manually-internal [pipeline]
(let [ctx (:context pipeline)]
(test-utils/while-with-timeout 10000 (nil? (trigger-id ctx 1 [1 1]))
(Thread/sleep 1000))
(manualtrigger/post-id ctx (trigger-id ctx 1 [1 1]) {})))
(defn- init-state [& {:keys [config pipeline-structure]}]
(atom {:config config
:pipeline (lambdacd/assemble-pipeline pipeline-structure config)
:pipeline-structure pipeline-structure}))
(defn- start-pipeline [state]
(let [pipeline-structure (:pipeline-structure @state)
ctx (:context (:pipeline @state))]
(swap! state #(assoc % :future-pipeline-result
(future
(execution-core/run-pipeline pipeline-structure ctx)))))
state)
(defn- trigger-manually [state]
(trigger-manually-internal (:pipeline @state))
state)
(defn pipeline-result [state]
(deref (:future-pipeline-result @state) 60000 :timeout))
(defn wait-for-completion [state]
(pipeline-result state)
state)
(defmacro expect-status [state status]
; macro is inlined, therefore convinces cursive to mark the call location of the check as failed, not the implementation
`(let [pipeline-result# (pipeline-result ~state)]
(is (= ~status (:status pipeline-result#)) (str "No success in " pipeline-result#))
~state))
(defmacro expect-success [state]
`(expect-status ~state :success))
(defmacro expect-failure [state]
`(expect-status ~state :failure))
(defn- init-ssh [state k v]
(let [existing-session-factory (SshSessionFactory/getInstance)]
(swap! state #(assoc % :existing-session-factory existing-session-factory))
(core/init-ssh! k v)
state))
(defn- reset-init-ssh [state]
(let [existing-session-factory (:existing-session-factory @state)]
(SshSessionFactory/setInstance existing-session-factory)
(swap! state #(dissoc % :existing-session-factory))
(reset! ssh/init-ssh-called? false)
state))
; ======================================================================================================================
(deftest example-pipeline-test
(testing "the example-pipeline"
(-> (init-state :config {:home-dir (test-utils/create-temp-dir)}
:pipeline-structure simple-pipeline/pipeline-structure)
(start-pipeline)
(trigger-manually)
(wait-for-completion)
(expect-success))))
(deftest ^:e2e-with-auth end-to-end-with-auth-test
(testing "a complete pipeline with all features against private repositories using https and ssh"
(doseq [repo-config [{:repo-uri (or (System/getenv "LAMBDACD_GIT_TESTREPO_SSH") "git@gitlab.com:flosell-test/testrepo.git")
:git {:ssh {:strict-host-key-checking "no"
:identity-file "~/.ssh/id_rsa_gitlab-test"
:use-agent false}}}
{:repo-uri (or (System/getenv "LAMBDACD_GIT_TESTREPO_HTTPS") "https://gitlab.com/flosell-test/testrepo.git")
:git {:credentials-provider (UsernamePasswordCredentialsProvider. (System/getenv "LAMBDACD_GIT_TESTREPO_USERNAME")
(System/getenv "LAMBDACD_GIT_TESTREPO_PASSWORD"))}}]]
(testing (:repo-uri repo-config)
(-> (init-state :config (assoc repo-config :home-dir (test-utils/create-temp-dir))
:pipeline-structure pipeline-structure)
(start-pipeline)
(trigger-manually)
(wait-for-completion)
(expect-success)
(start-pipeline)
(wait-for-completion)
(expect-success))))))
(deftest ^:e2e-with-auth backwards-compatibility-test
(testing "that git operations on ssh fail if init-ssh was called and ssh config was also given"
(-> (init-state :config {:repo-uri (or (System/getenv "LAMBDACD_GIT_TESTREPO_SSH") "git@gitlab.com:flosell-test/testrepo.git")
:git {:ssh {:strict-host-key-checking "yes"}}
:home-dir (test-utils/create-temp-dir)}
:pipeline-structure pipeline-structure)
(init-ssh :strict-host-key-checking "no")
(start-pipeline)
(trigger-manually)
(wait-for-completion)
(expect-failure)
(reset-init-ssh)))
(testing "that git operations on ssh don't fail if no ssh config was given"
(-> (init-state :config {:repo-uri (or (System/getenv "LAMBDACD_GIT_TESTREPO_SSH") "git@gitlab.com:flosell-test/testrepo.git")
:home-dir (test-utils/create-temp-dir)}
:pipeline-structure pipeline-structure)
(init-ssh :strict-host-key-checking "no")
(start-pipeline)
(trigger-manually)
(wait-for-completion)
(expect-success)
(reset-init-ssh))))