Skip to content

Commit 594cfd0

Browse files
committed
internal/git: add tests for auth method helpers
1 parent f8e0685 commit 594cfd0

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

internal/git/transport_test.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package git
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/go-git/go-git/v5/plumbing/transport"
8+
"github.com/go-git/go-git/v5/plumbing/transport/http"
9+
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
10+
corev1 "k8s.io/api/core/v1"
11+
)
12+
13+
const (
14+
// secretKeyFixture is a randomly generated password less
15+
// 512bit RSA private key.
16+
secretKeyFixture string = `-----BEGIN RSA PRIVATE KEY-----
17+
MIICXAIBAAKBgQCrakELAKxozvwJijQEggYlTvS1QTZx1DaBwOhW/4kRSuR21plu
18+
xuQeyuUiztoWeb9jgW7wjzG4j1PIJjdbsgjPIcIZ4PBY7JeEW+QRopfwuN8MHXNp
19+
uTLgIHbkmhoOg5qBEcjzO/lEOOPpV0EmbObgqv3+wRmLJrgfzWl/cTtRewIDAQAB
20+
AoGAawKFImpEN5Xn78iwWpQVZBsbV0AjzgHuGSiloxIZrorzf2DPHkHZzYNaclVx
21+
/o/4tBTsfg7WumH3qr541qyZJDgU7iRMABwmx0v1vm2wQiX7NJzLzH2E9vlMC3mw
22+
d8S99g9EqRuNH98XX8su34B9WGRPqiKvEm0RW8Hideo2/KkCQQDbs6rHcriKQyPB
23+
paidHZAfguu0eVbyHT2EgLgRboWE+tEAqFEW2ycqNL3VPz9fRvwexbB6rpOcPpQJ
24+
DEL4XB2XAkEAx7xJz8YlCQ2H38xggK8R8EUXF9Zhb0fqMJHMNmao1HCHVMtbsa8I
25+
jR2EGyQ4CaIqNG5tdWukXQSJrPYDRWNvvQJAZX3rP7XUYDLB2twvN12HzbbKMhX3
26+
v2MYnxRjc9INpi/Dyzz2MMvOnOW+aDuOh/If2AtVCmeJUx1pf4CFk3viQwJBAKyC
27+
t824+evjv+NQBlme3AOF6PgxtV4D4wWoJ5Uk/dTejER0j/Hbl6sqPxuiILRRV9qJ
28+
Ngkgu4mLjc3RfenEhJECQAx8zjWUE6kHHPGAd9DfiAIQ4bChqnyS0Nwb9+Gd4hSE
29+
P0Ah10mHiK/M0o3T8Eanwum0gbQHPnOwqZgsPkwXRqQ=
30+
-----END RSA PRIVATE KEY-----`
31+
32+
// knownHostsFixture is known_hosts fixture in the expected
33+
// format.
34+
knownHostsFixture string = `github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==`
35+
)
36+
37+
var (
38+
basicAuthSecretFixture = corev1.Secret{
39+
Data: map[string][]byte{
40+
"username": []byte("git"),
41+
"password": []byte("password"),
42+
},
43+
}
44+
privateKeySecretFixture = corev1.Secret{
45+
Data: map[string][]byte{
46+
"identity": []byte(secretKeyFixture),
47+
"known_hosts": []byte(knownHostsFixture),
48+
},
49+
}
50+
)
51+
52+
func TestAuthMethodFromSecret(t *testing.T) {
53+
tests := []struct {
54+
name string
55+
url string
56+
secret corev1.Secret
57+
want transport.AuthMethod
58+
wantErr bool
59+
}{
60+
{"HTTP", "http://git.example.com/org/repo.git", basicAuthSecretFixture, &http.BasicAuth{}, false},
61+
{"HTTPS", "https://git.example.com/org/repo.git", basicAuthSecretFixture, &http.BasicAuth{}, false},
62+
{"SSH", "ssh://git.example.com:2222/org/repo.git", privateKeySecretFixture, &ssh.PublicKeys{}, false},
63+
{"unsupported", "protocol://git.example.com/org/repo.git", corev1.Secret{}, nil, false},
64+
}
65+
for _, tt := range tests {
66+
t.Run(tt.name, func(t *testing.T) {
67+
got, cleanup, err := AuthMethodFromSecret(tt.url, tt.secret)
68+
if cleanup != nil {
69+
defer cleanup()
70+
}
71+
if (err != nil) != tt.wantErr {
72+
t.Errorf("AuthMethodFromSecret() error = %v, wantErr %v", err, tt.wantErr)
73+
return
74+
}
75+
if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
76+
t.Errorf("AuthMethodFromSecret() got = %v, want %v", got, tt.want)
77+
}
78+
})
79+
}
80+
}
81+
82+
func TestBasicAuthFromSecret(t *testing.T) {
83+
tests := []struct {
84+
name string
85+
secret corev1.Secret
86+
modify func(secret *corev1.Secret)
87+
want *http.BasicAuth
88+
wantErr bool
89+
}{
90+
{"username and password", basicAuthSecretFixture, nil, &http.BasicAuth{Username: "git", Password: "password"}, false},
91+
{"without username", basicAuthSecretFixture, func(s *corev1.Secret) { delete(s.Data, "username")}, nil, true},
92+
{"without password", basicAuthSecretFixture, func(s *corev1.Secret) { delete(s.Data, "password")}, nil, true},
93+
{"empty", corev1.Secret{}, nil, nil, true},
94+
}
95+
for _, tt := range tests {
96+
t.Run(tt.name, func(t *testing.T) {
97+
secret := tt.secret.DeepCopy()
98+
if tt.modify != nil {
99+
tt.modify(secret)
100+
}
101+
got, err := BasicAuthFromSecret(*secret)
102+
if (err != nil) != tt.wantErr {
103+
t.Errorf("BasicAuthFromSecret() error = %v, wantErr %v", err, tt.wantErr)
104+
return
105+
}
106+
if !reflect.DeepEqual(got, tt.want) {
107+
t.Errorf("BasicAuthFromSecret() got = %v, want %v", got, tt.want)
108+
}
109+
})
110+
}
111+
}
112+
113+
func TestPublicKeysFromSecret(t *testing.T) {
114+
tests := []struct {
115+
name string
116+
secret corev1.Secret
117+
modify func(secret *corev1.Secret)
118+
wantErr bool
119+
}{
120+
{"private key and known_hosts", privateKeySecretFixture, nil, false},
121+
{"missing private key", privateKeySecretFixture, func(s *corev1.Secret) { delete(s.Data, "identity") }, true},
122+
{"invalid private key", privateKeySecretFixture, func(s *corev1.Secret) { s.Data["identity"] = []byte(`-----BEGIN RSA PRIVATE KEY-----`) }, true},
123+
{"missing known_hosts", privateKeySecretFixture, func(s *corev1.Secret) { delete(s.Data, "known_hosts") }, true},
124+
{"invalid known_hosts", privateKeySecretFixture, func(s *corev1.Secret) { s.Data["known_hosts"] = []byte(`invalid`) }, true},
125+
{"empty", corev1.Secret{}, nil, true},
126+
}
127+
for _, tt := range tests {
128+
t.Run(tt.name, func(t *testing.T) {
129+
secret := tt.secret.DeepCopy()
130+
if tt.modify != nil {
131+
tt.modify(secret)
132+
}
133+
_, cleanup, err := PublicKeysFromSecret(*secret)
134+
if cleanup != nil {
135+
defer cleanup()
136+
}
137+
if (err != nil) != tt.wantErr {
138+
t.Errorf("PublicKeysFromSecret() error = %v, wantErr %v", err, tt.wantErr)
139+
return
140+
}
141+
})
142+
}
143+
}

0 commit comments

Comments
 (0)