1
1
package gitproviders
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"fmt"
7
+ "github.com/stretchr/testify/assert"
8
+ "io"
9
+ "io/ioutil"
6
10
"math/rand"
7
11
"net/http"
8
12
"net/url"
9
13
"os"
10
14
"strings"
15
+ "sync"
11
16
"testing"
12
17
"time"
13
18
14
- "github.com/stretchr/testify/assert"
15
-
16
19
"github.com/fluxcd/go-git-providers/gitlab"
17
20
"github.com/weaveworks/weave-gitops/pkg/utils"
18
21
34
37
GitlabUserTestName = "bot"
35
38
)
36
39
40
+ type customTransport struct {
41
+ transport http.RoundTripper
42
+ mux * sync.Mutex
43
+ }
44
+
45
+ func getBodyFromReaderWithoutConsuming (r * io.ReadCloser ) string {
46
+ body , _ := ioutil .ReadAll (* r )
47
+ (* r ).Close ()
48
+ * r = ioutil .NopCloser (bytes .NewBuffer (body ))
49
+ return string (body )
50
+ }
51
+
52
+ const (
53
+ ConnectionResetByPeer = "connection reset by peer"
54
+ ProjectStillBeingDeleted = "The project is still being deleted"
55
+ )
56
+
57
+ func (t * customTransport ) RoundTrip (req * http.Request ) (* http.Response , error ) {
58
+ t .mux .Lock ()
59
+ defer t .mux .Unlock ()
60
+
61
+ var resp * http.Response
62
+ var err error
63
+ var responseBody string
64
+ var requestBody string
65
+ retryCount := 15
66
+ for retryCount != 0 {
67
+ responseBody = ""
68
+ requestBody = ""
69
+ if req != nil && req .Body != nil {
70
+ requestBody = getBodyFromReaderWithoutConsuming (& req .Body )
71
+ }
72
+ resp , err = t .transport .RoundTrip (req )
73
+ if resp != nil && resp .Body != nil {
74
+ responseBody = getBodyFromReaderWithoutConsuming (& resp .Body )
75
+ }
76
+ if (err != nil && (strings .Contains (err .Error (), ConnectionResetByPeer ))) ||
77
+ strings .Contains (string (responseBody ), ProjectStillBeingDeleted ) {
78
+ time .Sleep (4 * time .Second )
79
+ if req != nil && req .Body != nil {
80
+ req .Body = ioutil .NopCloser (strings .NewReader (requestBody ))
81
+ }
82
+ retryCount --
83
+ continue
84
+ }
85
+ break
86
+ }
87
+
88
+ return resp , err
89
+ }
90
+
37
91
func SetRecorder (recorder * recorder.Recorder ) gitprovider.ChainableRoundTripperFunc {
38
92
return func (transport http.RoundTripper ) http.RoundTripper {
39
93
recorder .SetTransport (transport )
@@ -143,17 +197,23 @@ func TestMain(m *testing.M) {
143
197
144
198
accounts := getAccounts ()
145
199
200
+ t := customTransport {}
201
+
146
202
var err error
147
203
cacheGithubRecorder , err := NewRecorder ("github" , accounts )
148
204
if err != nil {
149
205
panic (err )
150
206
}
151
207
208
+ cacheGithubRecorder .SetTransport (& t )
209
+
152
210
cacheGitlabRecorder , err := NewRecorder ("gitlab" , accounts )
153
211
if err != nil {
154
212
panic (err )
155
213
}
156
214
215
+ cacheGitlabRecorder .SetTransport (& t )
216
+
157
217
githubTestClient , err = newGithubTestClient (SetRecorder (cacheGithubRecorder ))
158
218
if err != nil {
159
219
panic (err )
@@ -507,27 +567,25 @@ var _ = Describe("Test deploy keys creation", func() {
507
567
508
568
deployKey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBmym4XOiTj4rY3AcJKoJ8QupfgpFWtgNzDxzL0TrzfnurUQm+snozKLHGtOtS7PjMQsMaW9phyhhXv2KxadVI1uweFkC1TK4rPNWrqYX2g0JLXEScvaafSiv+SqozWLN/zhQ0e0jrtrYphtkd+H72RYsdq3mngY4WPJXM7z+HSjHSKilxj7XsxENt0dxT08LArxDC4OQXv9EYFgCyZ7SuLPBgA9160Co46Jm27enB/oBPx5zWd1MlkI+RtUi+XV2pLMzIpvYi2r2iWwOfDqE0N2cfpD0bY7cIOlv0iS7v6Qkmf7pBD+tRGTIZFcD5tGmZl1DOaeCZZ/VAN66aX+rN"
509
569
510
- // Uncomment these when the recording is fixed
511
-
512
- // exists, err := DeployKeyExists(accounts.GithubUserName, repoName)
513
- // Expect(err).ShouldNot(HaveOccurred())
514
- // Expect(exists).To(BeFalse())
570
+ exists , err := DeployKeyExists (accounts .GithubUserName , repoName )
571
+ Expect (err ).ShouldNot (HaveOccurred ())
572
+ Expect (exists ).To (BeFalse ())
515
573
516
574
stdout := utils .CaptureStdout (func () {
517
575
err = UploadDeployKey (accounts .GithubUserName , repoName , []byte (deployKey ))
518
576
Expect (err ).ShouldNot (HaveOccurred ())
519
577
})
520
578
Expect (stdout ).To (Equal ("uploading deploy key\n " ))
521
579
522
- // exists, err = DeployKeyExists(accounts.GithubUserName, repoName)
523
- // Expect(err).ShouldNot(HaveOccurred())
524
- // Expect(exists).To(BeTrue())
580
+ exists , err = DeployKeyExists (accounts .GithubUserName , repoName )
581
+ Expect (err ).ShouldNot (HaveOccurred ())
582
+ Expect (exists ).To (BeTrue ())
525
583
526
584
stdout = utils .CaptureStdout (func () {
527
585
err = UploadDeployKey (accounts .GithubUserName , repoName , []byte (deployKey ))
528
- Expect (err ).ShouldNot (HaveOccurred ())
586
+ Expect (err ).Should (HaveOccurred ())
529
587
})
530
- Expect (stdout ).To (Equal (fmt . Sprintf ( "deploy key weave-gitops- deploy-key already exists for repo %s \n ", repoName ) ))
588
+ Expect (stdout ).To (Equal ("uploading deploy key \n " ))
531
589
532
590
ctx := context .Background ()
533
591
user , err := githubTestClient .UserRepositories ().Get (ctx , userRepoRef )
@@ -541,6 +599,8 @@ var _ = Describe("Test deploy keys creation", func() {
541
599
542
600
accounts := getAccounts ()
543
601
602
+ SetGithubProvider (githubTestClient )
603
+
544
604
repoName := "test-deploy-key-org-repo"
545
605
orgRepoRef := NewOrgRepositoryRef (github .DefaultDomain , accounts .GithubOrgName , repoName )
546
606
repoInfo := NewRepositoryInfo ("test user repository" , gitprovider .RepositoryVisibilityPrivate )
@@ -551,31 +611,31 @@ var _ = Describe("Test deploy keys creation", func() {
551
611
err := CreateOrgRepository (githubTestClient , orgRepoRef , repoInfo , opts )
552
612
Expect (err ).ShouldNot (HaveOccurred ())
553
613
err = utils .WaitUntil (os .Stdout , time .Second , time .Second * 30 , func () error {
554
- return GetUserRepo (githubTestClient , accounts .GithubOrgName , repoName )
614
+ return GetOrgRepo (githubTestClient , accounts .GithubOrgName , repoName )
555
615
})
556
616
Expect (err ).ShouldNot (HaveOccurred ())
557
617
558
- deployKey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBmym4XOiTj4rY3AcJKoJ8QupfgpFWtgNzDxzL0TrzfnurUQm+snozKLHGtOtS7PjMQsMaW9phyhhXv2KxadVI1uweFkC1TK4rPNWrqYX2g0JLXEScvaafSiv+SqozWLN/zhQ0e0jrtrYphtkd+H72RYsdq3mngY4WPJXM7z+HSjHSKilxj7XsxENt0dxT08LArxDC4OQXv9EYFgCyZ7SuLPBgA9160Co46Jm27enB/oBPx5zWd1MlkI+RtUi+XV2pLMzIpvYi2r2iWwOfDqE0N2cfpD0bY7cIOlv0iS7v6Qkmf7pBD+tRGTIZFcD5tGmZl1DOaeCZZ/VAN66aX+rN "
618
+ deployKey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDorjCI1Ai7xhZx4e2dYImHbjzbEc0gH1mjnkcb3Tqc5Zs/tQVxo282YIMeXq8IABt2AcwTzDHAviajbPqC05GNRwCmEFrYOnYKhMrdrKtYuCtmEhgnhPQlItXJlF00XwHfYetjfIzFSk8vdLJcwmGp6PPemDW2Xv6CPBAN23OGqTbYYsFuO7+hdU3CgGcR9WPDdzN7/4q1aq4Tk7qhNl5Yxw1DQ0OVgiAQnBJHeViOar14Dw1olhtzL2s88e/TE9t47p9iLXFXwN4irER25A4NUa7DYGpNfUEGQdlf1k81ctegQeA8fOZ4uT4zYSja7mG6QYRgPwN4ZB8ywTcHeON6EzWucSWKM4TcJgASmvJtJn5RifbuzMJTtqpCtIFmpo5/ItQFKYjI18Omqh0ZJe/P9YtYtM+Ac3FIOC0yKU7Ozsx/N7wq3uSIOTv8KCxkEgq2fBi9gF/+kE0BGSVao0RfY/fAUjS/ScuNvo30+MrW+8NmWeWRdhMJkJ25kLGuWBE= "
559
619
560
- // exists, err := DeployKeyExists(accounts.GithubUserName , repoName)
561
- // Expect(err).ShouldNot(HaveOccurred())
562
- // Expect(exists).To(BeFalse())
620
+ exists , err := DeployKeyExists (accounts .GithubOrgName , repoName )
621
+ Expect (err ).ShouldNot (HaveOccurred ())
622
+ Expect (exists ).To (BeFalse ())
563
623
564
624
stdout := utils .CaptureStdout (func () {
565
625
err = UploadDeployKey (accounts .GithubOrgName , repoName , []byte (deployKey ))
566
626
Expect (err ).ShouldNot (HaveOccurred ())
567
627
})
568
628
Expect (stdout ).To (Equal ("uploading deploy key\n " ))
569
629
570
- // exists, err = DeployKeyExists(accounts.GithubUserName , repoName)
571
- // Expect(err).ShouldNot(HaveOccurred())
572
- // Expect(exists).To(BeTrue())
630
+ exists , err = DeployKeyExists (accounts .GithubOrgName , repoName )
631
+ Expect (err ).ShouldNot (HaveOccurred ())
632
+ Expect (exists ).To (BeTrue ())
573
633
574
634
stdout = utils .CaptureStdout (func () {
575
635
err = UploadDeployKey (accounts .GithubOrgName , repoName , []byte (deployKey ))
576
- Expect (err ).ShouldNot (HaveOccurred ())
636
+ Expect (err ).Should (HaveOccurred ())
577
637
})
578
- Expect (stdout ).To (Equal (fmt . Sprintf ( "deploy key weave-gitops- deploy-key already exists for repo %s \n ", repoName ) ))
638
+ Expect (stdout ).To (Equal ("uploading deploy key \n " ))
579
639
580
640
ctx := context .Background ()
581
641
user , err := githubTestClient .OrgRepositories ().Get (ctx , orgRepoRef )
0 commit comments