Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
Implement new interface to query state (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
flosell committed Sep 18, 2016
1 parent 819e3c7 commit 104b6c8
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/clj/lambdacd/internal/execution.clj
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
(fn [args ctx]
(let [state (legacy-pipeline-state/get-all (:pipeline-state-component ctx))
original-build-result (get state retriggered-build-number)
original-step-result (get original-build-result (:step-id ctx))]
original-step-result (state/get-step-result ctx retriggered-build-number (:step-id ctx))]
(publish-child-step-results ctx retriggered-build-number original-build-result)
(assoc original-step-result
:retrigger-mock-for-build-number retriggered-build-number))))
Expand Down
43 changes: 41 additions & 2 deletions src/clj/lambdacd/state/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,60 @@
(:require [lambdacd.state.protocols :as protocols]
[lambdacd.internal.pipeline-state :as legacy-pipeline-state]))

(defn- all-build-numbers-from-legacy [component]
(->> (legacy-pipeline-state/get-all component)
(keys)
(sort)))

(defn- get-build-from-legacy [component build-number]
{:step-results (-> (legacy-pipeline-state/get-all component)
(get build-number))})

; -------------------------------------------------------------------------

(defn state-component [ctx]
(:pipeline-state-component ctx))

; -------------------------------------------------------------------------
(defn consume-step-result-update
"Update a step-result in the state"
[ctx build-number step-id step-result]
(let [component (state-component ctx)]
(if (satisfies? lambdacd.state.protocols/StepResultUpdateConsumer component)
(if (satisfies? protocols/StepResultUpdateConsumer component)
(protocols/consume-step-result-update component build-number step-id step-result)
(legacy-pipeline-state/update component build-number step-id step-result))))

(defn next-build-number
"Returns the build number for the next build"
[ctx]
(let [component (state-component ctx)]
(if (satisfies? lambdacd.state.protocols/BuildNumberSource component)
(if (satisfies? protocols/NextBuildNumberSource component)
(protocols/next-build-number component)
(legacy-pipeline-state/next-build-number component))))

(defn all-build-numbers
"Returns all existing build numbers"
[ctx]
(let [component (state-component ctx)]
(if (satisfies? protocols/QueryAllBuildNumbersSource component)
(protocols/all-build-numbers component)
(all-build-numbers-from-legacy component))))

(defn get-build
"Returns all information for a given build as a map with :step-results and TODO more"
[ctx build-number]
(let [component (state-component ctx)]
(if (satisfies? protocols/QueryBuildSource component)
(protocols/get-build component build-number)
(get-build-from-legacy component build-number))))

(defn get-step-results
"Returns a map from step-ids to step-results"
[ctx build-number]
(:step-results (get-build ctx build-number)))

(defn get-step-result
"Returns a map containing the result of one step"
[ctx build-number step-id]
(get (get-step-results ctx build-number)
step-id))
11 changes: 10 additions & 1 deletion src/clj/lambdacd/state/protocols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
(consume-step-result-update [self build-number step-id step-result]
"Tells the component to update the result of a particular step. Is called on every update so it needs to handle lots of requests"))

(defprotocol BuildNumberSource
(defprotocol NextBuildNumberSource
"Components implementing this protocol provide the LambdaCD execution engine with new build numbers"
(next-build-number [self]
"Returns the build number for the next build. Must be comparable and greater than all existing build numbers"))

(defprotocol QueryAllBuildNumbersSource
"Components implementing this protocol can supply a list of all build numbers present in the datastore"
(all-build-numbers [self]
"Returns a sorted list of build numbers present in the datastore"))

(defprotocol QueryBuildSource
"Components implementing this protocol can supply information on one build"
(get-build [self build-number]
"Returns build information as a map with :step-results, and TODO"))
1 change: 0 additions & 1 deletion src/clj/lambdacd/ui/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@
(POST "/dynamic/:id" {{id :id} :params data :json-params} (do
(manualtrigger/post-id ctx id (w/keywordize-keys data))
(util/json {:status :success})))))))

44 changes: 41 additions & 3 deletions test/clj/lambdacd/state/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,57 @@
(let [component (mock state-protocols/StepResultUpdateConsumer)]
(s/consume-step-result-update (some-ctx-with :pipeline-state-component component)
some-build-number some-step-id some-step-result)
(is (received? component state-protocols/consume-step-result-update [some-build-number some-step-id some-step-result]))))
(is (received? component state-protocols/consume-step-result-update [some-build-number some-step-id some-step-result]))))
(testing "that calls to a legacy PipelineStateComponent be mapped to that method"
(let [component (mock legacy-pipeline-state/PipelineStateComponent)]
(s/consume-step-result-update (some-ctx-with :pipeline-state-component component)
some-build-number some-step-id some-step-result)
(is (received? component legacy-pipeline-state/update [some-build-number some-step-id some-step-result])))))
(is (received? component legacy-pipeline-state/update [some-build-number some-step-id some-step-result])))))

(deftest next-build-number-test
(testing "that calls to a BuildNumberSource will just pass through"
(let [component (mock state-protocols/BuildNumberSource)]
(let [component (mock state-protocols/NextBuildNumberSource)]
(s/next-build-number (some-ctx-with :pipeline-state-component component))
(is (received? component state-protocols/next-build-number []))))
(testing "that calls to a legacy PipelineStateComponent be mapped to that method"
(let [component (mock legacy-pipeline-state/PipelineStateComponent)]
(s/next-build-number (some-ctx-with :pipeline-state-component component))
(is (received? component legacy-pipeline-state/next-build-number [])))))

(deftest all-build-numbers-test
(testing "that calls to QueryAllBuildNumbersSource will just pass through"
(let [component (mock state-protocols/QueryAllBuildNumbersSource)]
(s/all-build-numbers (some-ctx-with :pipeline-state-component component))
(is (received? component state-protocols/all-build-numbers []))))
(testing "compatibility with PipelineStateComponent"
(testing "an empty state"
(let [component (mock legacy-pipeline-state/PipelineStateComponent {:get-all {}})]
(is (= [] (s/all-build-numbers (some-ctx-with :pipeline-state-component component))))))
(testing "state with builds returns sorted list"
(let [component (mock legacy-pipeline-state/PipelineStateComponent {:get-all {5 {}
1 {}
2 {}}})]
(is (= [1 2 5] (s/all-build-numbers (some-ctx-with :pipeline-state-component component))))))))

(deftest get-build-test
(testing "that calls to QueryBuildSource will just pass through"
(let [component (mock state-protocols/QueryBuildSource {:get-build {:some :state}})]
(is (= {:some :state} (s/get-build (some-ctx-with :pipeline-state-component component) 1)))
(is (received? component state-protocols/get-build [1]))))
(testing "compatibility with PipelineStateComponent"
(testing "an empty state"
(let [component (mock legacy-pipeline-state/PipelineStateComponent {:get-all {}})]
(is (= {:step-results nil} (s/get-build (some-ctx-with :pipeline-state-component component) 1)))))
(testing "state with builds returns sorted list"
(let [component (mock legacy-pipeline-state/PipelineStateComponent {:get-all {1 {:some :build}}})]
(is (= {:step-results {:some :build}} (s/get-build (some-ctx-with :pipeline-state-component component) 1)))))))

(deftest get-step-results
(testing "that it returns step-results"
(with-redefs [s/get-build (constantly {:step-results {:step :results}})]
(is (= {:step :results} (s/get-step-results nil nil))))))

(deftest step-result-test
(testing "that we can get a simple step-result"
(with-redefs [s/get-build (constantly {:step-results {[2 1] {:step :result}}})]
(is (= {:step :result} (s/get-step-result nil 1 [2 1]))))))

0 comments on commit 104b6c8

Please sign in to comment.