@@ -101,6 +101,127 @@ func TestPullRefMetricsSucceed(t *testing.T) {
101101 assert .Equal (t , status , metrics [status .Key ()])
102102}
103103
104+ func TestPullRefMetricsUpdatingPipeline (t * testing.T ) {
105+ // given
106+ ctx , c , mux , srv := newTestController (config.Config {})
107+ defer srv .Close ()
108+ apiPipeline := `{
109+ "id":1,
110+ "created_at":"2016-08-11T11:27:00.085Z",
111+ "started_at":"2016-08-11T11:28:00.085Z",
112+ "duration":300,
113+ "queued_duration":60,
114+ "status":"running",
115+ "coverage":"30.2",
116+ "source":"pipeline"
117+ }`
118+
119+ mux .HandleFunc ("/api/v4/projects/foo/pipelines" ,
120+ func (w http.ResponseWriter , r * http.Request ) {
121+ assert .Equal (t , "bar" , r .URL .Query ().Get ("ref" ))
122+ fmt .Fprint (w , `[{"id":1}]` )
123+ })
124+
125+ mux .HandleFunc ("/api/v4/projects/foo/pipelines/1" ,
126+ func (w http.ResponseWriter , r * http.Request ) {
127+ fmt .Fprint (w , apiPipeline )
128+ })
129+
130+ mux .HandleFunc ("/api/v4/projects/foo/pipelines/1/variables" ,
131+ func (w http.ResponseWriter , r * http.Request ) {
132+ assert .Equal (t , "GET" , r .Method )
133+ fmt .Fprint (w , `[{"key":"foo","value":"bar"}]` )
134+ })
135+
136+ p := schemas .NewProject ("foo" )
137+ p .Pull .Pipeline .Variables .Enabled = true
138+
139+ labels := map [string ]string {
140+ "kind" : string (schemas .RefKindBranch ),
141+ "project" : "foo" ,
142+ "ref" : "bar" ,
143+ "topics" : "" ,
144+ "variables" : "foo:bar" ,
145+ "source" : "pipeline" ,
146+ }
147+
148+ // when
149+ assert .NoError (t , c .PullRefMetrics (
150+ ctx ,
151+ schemas .NewRef (
152+ p ,
153+ schemas .RefKindBranch ,
154+ "bar" ,
155+ )))
156+
157+ metrics , _ := c .Store .Metrics (ctx )
158+
159+ // then
160+ runID := schemas.Metric {
161+ Kind : schemas .MetricKindID ,
162+ Labels : labels ,
163+ Value : 1 ,
164+ }
165+ assert .Equal (t , runID , metrics [runID .Key ()])
166+
167+ labels ["status" ] = "running"
168+ status := schemas.Metric {
169+ Kind : schemas .MetricKindStatus ,
170+ Labels : labels ,
171+ Value : 1 ,
172+ }
173+ assert .Equal (t , status , metrics [status .Key ()])
174+
175+ // given again
176+ apiPipeline = `{
177+ "id":1,
178+ "created_at":"2016-08-11T11:27:00.085Z",
179+ "started_at":"2016-08-11T11:28:00.085Z",
180+ "duration":300,
181+ "queued_duration":60,
182+ "status":"failed",
183+ "coverage":"30.2",
184+ "source":"pipeline"
185+ }`
186+
187+ labels = map [string ]string {
188+ "kind" : string (schemas .RefKindBranch ),
189+ "project" : "foo" ,
190+ "ref" : "bar" ,
191+ "topics" : "" ,
192+ "variables" : "foo:bar" ,
193+ "source" : "pipeline" ,
194+ }
195+
196+ // when again
197+ assert .NoError (t , c .PullRefMetrics (
198+ ctx ,
199+ schemas .NewRef (
200+ p ,
201+ schemas .RefKindBranch ,
202+ "bar" ,
203+ )))
204+
205+ metrics , _ = c .Store .Metrics (ctx )
206+
207+ // then again
208+ runID = schemas.Metric {
209+ Kind : schemas .MetricKindID ,
210+ Labels : labels ,
211+ Value : 1 ,
212+ }
213+ assert .Equal (t , runID , metrics [runID .Key ()])
214+
215+ labels ["status" ] = "failed"
216+ status = schemas.Metric {
217+ Kind : schemas .MetricKindStatus ,
218+ Labels : labels ,
219+ Value : 1 ,
220+ }
221+ assert .Equal (t , status , metrics [status .Key ()])
222+
223+ }
224+
104225func TestPullRefTestReportMetrics (t * testing.T ) {
105226 ctx , c , mux , srv := newTestController (config.Config {})
106227 defer srv .Close ()
0 commit comments