@@ -12,6 +12,8 @@ import (
12
12
"github.com/gruntwork-io/go-commons/git"
13
13
"github.com/gruntwork-io/go-commons/logging"
14
14
"github.com/gruntwork-io/terratest/modules/environment"
15
+ ttlogger "github.com/gruntwork-io/terratest/modules/logger"
16
+ "github.com/gruntwork-io/terratest/modules/shell"
15
17
"github.com/stretchr/testify/assert"
16
18
"github.com/stretchr/testify/require"
17
19
)
29
31
// All these tests are also run in serial to avoid race conditions on the git config file.
30
32
31
33
func TestHTTPSAuth (t * testing.T ) {
34
+ defer cleanupGitConfig (t )
35
+
32
36
currentDir , err := os .Getwd ()
33
37
require .NoError (t , err )
34
38
require .Equal (t , "/workspace/go-commons/git/test" , currentDir )
@@ -43,7 +47,109 @@ func TestHTTPSAuth(t *testing.T) {
43
47
assert .True (t , files .IsDir (filepath .Join (tmpDir , "modules/lambda" )))
44
48
}
45
49
50
+ func TestHTTPSAuthWithPath (t * testing.T ) {
51
+ defer cleanupGitConfig (t )
52
+
53
+ currentDir , err := os .Getwd ()
54
+ require .NoError (t , err )
55
+ require .Equal (t , "/workspace/go-commons/git/test" , currentDir )
56
+
57
+ environment .RequireEnvVar (t , gitPATEnvName )
58
+ gitPAT := os .Getenv (gitPATEnvName )
59
+
60
+ lambdaGitURL := "https://github.com/gruntwork-io/terraform-aws-lambda.git"
61
+ lambdaOpts := git.CacheCredentialOptions {
62
+ Host : lambdaGitURL ,
63
+ DefaultUsername : "git" ,
64
+ IncludeHTTPPath : true ,
65
+ SocketPath : "" ,
66
+ Timeout : 3600 ,
67
+ }
68
+ require .NoError (
69
+ t ,
70
+ git .ConfigureCacheCredentialsHelper (logger , lambdaOpts ),
71
+ )
72
+ require .NoError (
73
+ t ,
74
+ git .StoreCacheCredentials (logger , "git" , gitPAT , "github.com" , "gruntwork-io/terraform-aws-lambda.git" , "" ),
75
+ )
76
+
77
+ tmpDir , err := ioutil .TempDir ("" , "git-test" )
78
+ require .NoError (t , err )
79
+ require .NoError (t , git .Clone (logger , lambdaGitURL , tmpDir ))
80
+ assert .True (t , files .IsDir (filepath .Join (tmpDir , "modules/lambda" )))
81
+ }
82
+
83
+ func TestHTTPSAuthMixed (t * testing.T ) {
84
+ defer cleanupGitConfig (t )
85
+
86
+ currentDir , err := os .Getwd ()
87
+ require .NoError (t , err )
88
+ require .Equal (t , "/workspace/go-commons/git/test" , currentDir )
89
+
90
+ // Make sure the directory for git credential sockets exist
91
+ socketPath := "/tmp/git-credential-sockets"
92
+ require .NoError (t , os .MkdirAll (socketPath , 0o700 ))
93
+ lambdaSocketPath := filepath .Join (socketPath , "lambda" )
94
+ githubSocketPath := filepath .Join (socketPath , "github" )
95
+
96
+ environment .RequireEnvVar (t , gitPATEnvName )
97
+ gitPAT := os .Getenv (gitPATEnvName )
98
+
99
+ lambdaGitURL := "https://github.com/gruntwork-io/terraform-aws-lambda.git"
100
+ lambdaOpts := git.CacheCredentialOptions {
101
+ Host : lambdaGitURL ,
102
+ DefaultUsername : "git" ,
103
+ IncludeHTTPPath : true ,
104
+ SocketPath : lambdaSocketPath ,
105
+ Timeout : 3600 ,
106
+ }
107
+ require .NoError (
108
+ t ,
109
+ git .ConfigureCacheCredentialsHelper (logger , lambdaOpts ),
110
+ )
111
+ require .NoError (
112
+ t ,
113
+ git .StoreCacheCredentials (logger , "git" , gitPAT , "github.com" , "gruntwork-io/terraform-aws-lambda.git" , lambdaSocketPath ),
114
+ )
115
+
116
+ githubOpts := git.CacheCredentialOptions {
117
+ Host : "https://github.com" ,
118
+ DefaultUsername : "git" ,
119
+ IncludeHTTPPath : false ,
120
+ SocketPath : githubSocketPath ,
121
+ Timeout : 3600 ,
122
+ }
123
+ require .NoError (
124
+ t ,
125
+ git .ConfigureCacheCredentialsHelper (logger , githubOpts ),
126
+ )
127
+ require .NoError (
128
+ t ,
129
+ git .StoreCacheCredentials (logger , "git" , "wrong-pat" , "github.com" , "" , githubSocketPath ),
130
+ )
131
+
132
+ tmpDir , err := ioutil .TempDir ("" , "git-test" )
133
+ require .NoError (t , err )
134
+ lambdaDir := filepath .Join (tmpDir , "terraform-aws-lambda" )
135
+ ciDir := filepath .Join (tmpDir , "terraform-aws-ci" )
136
+ require .NoError (t , os .Mkdir (lambdaDir , 0o755 ))
137
+ require .NoError (t , os .Mkdir (ciDir , 0o755 ))
138
+
139
+ require .NoError (
140
+ t ,
141
+ git .Clone (logger , lambdaGitURL , lambdaDir ),
142
+ )
143
+ require .Error (
144
+ t ,
145
+ git .Clone (logger , "https://github.com/gruntwork-io/terraform-aws-ci.git" , ciDir ),
146
+ )
147
+
148
+ }
149
+
46
150
func TestForceHTTPS (t * testing.T ) {
151
+ defer cleanupGitConfig (t )
152
+
47
153
currentDir , err := os .Getwd ()
48
154
require .NoError (t , err )
49
155
require .Equal (t , "/workspace/go-commons/git/test" , currentDir )
@@ -58,3 +164,18 @@ func TestForceHTTPS(t *testing.T) {
58
164
require .NoError (t , git .Clone (logger , "git@github.com:gruntwork-io/terraform-aws-lambda.git" , tmpDir ))
59
165
assert .True (t , files .IsDir (filepath .Join (tmpDir , "modules/lambda" )))
60
166
}
167
+
168
+ // cleanupGitConfig will reset the git credential cache and git config
169
+ func cleanupGitConfig (t * testing.T ) {
170
+ data , err := ioutil .ReadFile ("/root/.gitconfig" )
171
+ require .NoError (t , err )
172
+ ttlogger .Logf (t , string (data ))
173
+
174
+ require .NoError (t , os .Remove ("/root/.gitconfig" ))
175
+
176
+ cmd := shell.Command {
177
+ Command : "git" ,
178
+ Args : []string {"credential-cache" , "exit" },
179
+ }
180
+ shell .RunCommand (t , cmd )
181
+ }
0 commit comments