1
1
package e2e_test
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"io"
@@ -187,6 +188,70 @@ var _ = Describe("API", Ordered, func() {
187
188
})
188
189
})
189
190
191
+ Describe ("Provider clear endpoint" , func () {
192
+ BeforeEach (func () {
193
+ clk .SetTime (now )
194
+ })
195
+
196
+ Context ("when the provider is unknown" , func () {
197
+ It ("should return a 404 response" , func () {
198
+ resp , _ := apiCall (srv , providerClearReq ("unknown" , "unknown" , "unknown" ))
199
+ Expect (resp .StatusCode ).To (Equal (http .StatusNotFound ))
200
+ })
201
+ })
202
+
203
+ Context ("when the provider is known" , func () {
204
+ Context ("when there is existing state" , func () {
205
+ var clearResp * http.Response
206
+ var clearRespBody string
207
+ checkStateAndExpectEmptyPayload := func (resp * http.Response , respBody string ) {
208
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
209
+ expectedPayload := fmt .Sprintf (`{
210
+ "last_updated_at": "%s",
211
+ "acquired": null,
212
+ "known": {}
213
+ }` , clk .Now ().Format (time .RFC3339 ))
214
+ Expect (respBody ).To (MatchJSON (expectedPayload ))
215
+ }
216
+
217
+ JustBeforeEach (func () {
218
+ clearResp , clearRespBody = apiCall (srv , providerClearReq (owner , repo , baseRef ))
219
+ // should always succeed
220
+ Expect (clearResp .StatusCode ).To (Equal (http .StatusOK ))
221
+ })
222
+
223
+ BeforeEach (func () {
224
+ providerState , opts := generateProviderState (now , owner , repo , baseRef , map [int ]lease.Status {
225
+ 1 : lease .StatusPending ,
226
+ 2 : lease .StatusAcquired ,
227
+ }, pointer .Int (2 ))
228
+ storage .PrefillStorage (storageDir , providerState )
229
+ currentTime := opts .LastUpdatedAt
230
+ currentTime = currentTime .Add (time .Second )
231
+ clk .SetTime (currentTime )
232
+ })
233
+ It ("should return an empty list of requests" , func () {
234
+ checkStateAndExpectEmptyPayload (clearResp , clearRespBody )
235
+ })
236
+ It ("should empty the local (in-memory) state" , func () {
237
+ // try to get the new state again, should be empty
238
+ providerDetailsResp , providerDetailsRespBody := apiCall (srv , providerDetailsReq (owner , repo , baseRef ))
239
+ checkStateAndExpectEmptyPayload (providerDetailsResp , providerDetailsRespBody )
240
+ })
241
+ It ("should empty the persisted (storage) state" , func () {
242
+ provider , err := srv .GetOrchestrator ().Get (owner , repo , baseRef )
243
+ Expect (err ).To (BeNil ())
244
+ // fore hydration again
245
+ err = provider .HydrateFromState (context .Background ())
246
+ Expect (err ).To (BeNil ())
247
+
248
+ providerDetailsResp , providerDetailsRespBody := apiCall (srv , providerDetailsReq (owner , repo , baseRef ))
249
+ checkStateAndExpectEmptyPayload (providerDetailsResp , providerDetailsRespBody )
250
+ })
251
+ })
252
+ })
253
+ })
254
+
190
255
Describe ("Acquire endpoint" , func () {
191
256
BeforeEach (func () {
192
257
clk .SetTime (now )
@@ -583,113 +648,113 @@ var _ = Describe("API", Ordered, func() {
583
648
})
584
649
})
585
650
})
586
- })
587
651
588
- Context ("maximum request reached, Success build" , func () {
589
- BeforeEach (func () {
590
- clk .SetTime (now )
591
- })
652
+ Context ("maximum request reached, Success build" , func () {
653
+ BeforeEach (func () {
654
+ clk .SetTime (now )
655
+ })
592
656
593
- It ("should complete the flow successfully" , func () {
594
- max := configHelper .DefaultConfigRepoExpectedRequestCount
595
- for i := 1 ; i <= max - 1 ; i ++ {
596
- By (fmt .Sprintf ("test acquire, request %d => should be pending" , i ), func () {
597
- resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (i ), i ))
598
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
599
- Expect (body ).To (MatchJSON (fmt .Sprintf (`{
657
+ It ("should complete the flow successfully" , func () {
658
+ max := configHelper .DefaultConfigRepoExpectedRequestCount
659
+ for i := 1 ; i <= max - 1 ; i ++ {
660
+ By (fmt .Sprintf ("test acquire, request %d => should be pending" , i ), func () {
661
+ resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (i ), i ))
662
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
663
+ Expect (body ).To (MatchJSON (fmt .Sprintf (`{
600
664
"head_sha": "xxx-%d",
601
665
"priority": %d,
602
666
"status": "pending"
603
667
}` , i , i )))
604
- })
605
- }
606
- By (fmt .Sprintf ("test acquire, request %d => should be acquired" , max ), func () {
607
- resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max ), max ))
608
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
609
- Expect (body ).To (MatchJSON (fmt .Sprintf (`{
668
+ })
669
+ }
670
+ By (fmt .Sprintf ("test acquire, request %d => should be acquired" , max ), func () {
671
+ resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max ), max ))
672
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
673
+ Expect (body ).To (MatchJSON (fmt .Sprintf (`{
610
674
"head_sha": "xxx-%d",
611
675
"priority": %d,
612
676
"status": "acquired"
613
677
}` , max , max )))
614
- })
615
- By (fmt .Sprintf ("test release (success), request %d => should be completed" , max ), func () {
616
- resp , body := apiCall (srv , releaseReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max ), max , lease .StatusSuccess ))
617
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
618
- Expect (body ).To (MatchJSON (fmt .Sprintf (`{
678
+ })
679
+ By (fmt .Sprintf ("test release (success), request %d => should be completed" , max ), func () {
680
+ resp , body := apiCall (srv , releaseReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max ), max , lease .StatusSuccess ))
681
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
682
+ Expect (body ).To (MatchJSON (fmt .Sprintf (`{
619
683
"head_sha": "xxx-%d",
620
684
"priority": %d,
621
685
"status": "completed"
622
686
}` , max , max )))
623
- })
624
- for i := 1 ; i <= max - 1 ; i ++ {
625
- By (fmt .Sprintf ("test acquire, request %d => should be completed" , i ), func () {
626
- resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (i ), i ))
627
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
628
- Expect (body ).To (MatchJSON (fmt .Sprintf (`{
687
+ })
688
+ for i := 1 ; i <= max - 1 ; i ++ {
689
+ By (fmt .Sprintf ("test acquire, request %d => should be completed" , i ), func () {
690
+ resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (i ), i ))
691
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
692
+ Expect (body ).To (MatchJSON (fmt .Sprintf (`{
629
693
"head_sha": "xxx-%d",
630
694
"priority": %d,
631
695
"status": "completed"
632
696
}` , i , i )))
633
- })
634
- }
697
+ })
698
+ }
699
+ })
635
700
})
636
- })
637
701
638
- Context ("maximum request reached, Failed build" , func () {
639
- BeforeEach (func () {
640
- clk .SetTime (now )
641
- })
702
+ Context ("maximum request reached, Failed build" , func () {
703
+ BeforeEach (func () {
704
+ clk .SetTime (now )
705
+ })
642
706
643
- It ("should complete the flow successfully" , func () {
644
- max := configHelper .DefaultConfigRepoExpectedRequestCount
645
- for i := 1 ; i <= max - 1 ; i ++ {
646
- By (fmt .Sprintf ("test acquire, request %d => should be pending" , i ), func () {
647
- resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (i ), i ))
648
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
649
- Expect (body ).To (MatchJSON (fmt .Sprintf (`{
707
+ It ("should complete the flow successfully" , func () {
708
+ max := configHelper .DefaultConfigRepoExpectedRequestCount
709
+ for i := 1 ; i <= max - 1 ; i ++ {
710
+ By (fmt .Sprintf ("test acquire, request %d => should be pending" , i ), func () {
711
+ resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (i ), i ))
712
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
713
+ Expect (body ).To (MatchJSON (fmt .Sprintf (`{
650
714
"head_sha": "xxx-%d",
651
715
"priority": %d,
652
716
"status": "pending"
653
717
}` , i , i )))
654
- })
655
- }
656
- By (fmt .Sprintf ("test acquire, request %d => should be acquired" , max ), func () {
657
- resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max ), max ))
658
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
659
- Expect (body ).To (MatchJSON (fmt .Sprintf (`{
718
+ })
719
+ }
720
+ By (fmt .Sprintf ("test acquire, request %d => should be acquired" , max ), func () {
721
+ resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max ), max ))
722
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
723
+ Expect (body ).To (MatchJSON (fmt .Sprintf (`{
660
724
"head_sha": "xxx-%d",
661
725
"priority": %d,
662
726
"status": "acquired"
663
727
}` , max , max )))
664
- })
665
- By (fmt .Sprintf ("test release (failure), request %d => should be failure" , max ), func () {
666
- resp , body := apiCall (srv , releaseReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max ), max , lease .StatusFailure ))
667
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
668
- Expect (body ).To (MatchJSON (fmt .Sprintf (`{
728
+ })
729
+ By (fmt .Sprintf ("test release (failure), request %d => should be failure" , max ), func () {
730
+ resp , body := apiCall (srv , releaseReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max ), max , lease .StatusFailure ))
731
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
732
+ Expect (body ).To (MatchJSON (fmt .Sprintf (`{
669
733
"head_sha": "xxx-%d",
670
734
"priority": %d,
671
735
"status": "failure"
672
736
}` , max , max )))
673
- })
674
- for i := 1 ; i <= max - 2 ; i ++ {
675
- By (fmt .Sprintf ("test acquire, request %d => should be pending" , i ), func () {
676
- resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (i ), i ))
677
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
678
- Expect (body ).To (MatchJSON (fmt .Sprintf (`{
737
+ })
738
+ for i := 1 ; i <= max - 2 ; i ++ {
739
+ By (fmt .Sprintf ("test acquire, request %d => should be pending" , i ), func () {
740
+ resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (i ), i ))
741
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
742
+ Expect (body ).To (MatchJSON (fmt .Sprintf (`{
679
743
"head_sha": "xxx-%d",
680
744
"priority": %d,
681
745
"status": "pending"
682
746
}` , i , i )))
683
- })
684
- }
685
- By (fmt .Sprintf ("test acquire, request %d => should be acquired" , max - 1 ), func () {
686
- resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max - 1 ), max - 1 ))
687
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
688
- Expect (body ).To (MatchJSON (fmt .Sprintf (`{
747
+ })
748
+ }
749
+ By (fmt .Sprintf ("test acquire, request %d => should be acquired" , max - 1 ), func () {
750
+ resp , body := apiCall (srv , acquireReq (owner , repo , baseRef , "xxx-" + strconv .Itoa (max - 1 ), max - 1 ))
751
+ Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
752
+ Expect (body ).To (MatchJSON (fmt .Sprintf (`{
689
753
"head_sha": "xxx-%d",
690
754
"priority": %d,
691
755
"status": "acquired"
692
756
}` , max - 1 , max - 1 )))
757
+ })
693
758
})
694
759
})
695
760
})
@@ -713,6 +778,15 @@ func providerDetailsReq(owner string, repo string, baseRef string) *http.Request
713
778
)
714
779
}
715
780
781
+ // providerClearReq returns a pre-configured request for the "DELETE /:owner/:repo/:baseRef" endpoint
782
+ func providerClearReq (owner string , repo string , baseRef string ) * http.Request {
783
+ return httptest .NewRequest (
784
+ "DELETE" ,
785
+ fmt .Sprintf ("/%s/%s/%s" , owner , repo , baseRef ),
786
+ nil ,
787
+ )
788
+ }
789
+
716
790
// acquireReq returns a pre-configured request for the "POST /:owner/:repo/:baseRef/acquire" endpoint
717
791
func acquireReq (owner string , repo string , baseRef string , headSha string , priority int ) * http.Request {
718
792
req := httptest .NewRequest (
0 commit comments