Skip to content

Commit 041bb23

Browse files
authored
Merge pull request #16 from fluxcd/internal-tests
internal: Helm and Git authentication helper tests
2 parents 0e82a43 + 582dfb2 commit 041bb23

File tree

3 files changed

+273
-2
lines changed

3 files changed

+273
-2
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+
}

internal/helm/getter.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ func ClientOptionsFromSecret(secret corev1.Secret) ([]getter.Option, func(), err
1616
if err != nil {
1717
return opts, nil, err
1818
}
19-
opts = append(opts, basicAuth)
19+
if basicAuth != nil {
20+
opts = append(opts, basicAuth)
21+
}
2022
tlsClientConfig, cleanup, err := TLSClientConfigFromSecret(secret)
2123
if err != nil {
2224
return opts, nil, err
2325
}
24-
opts = append(opts, tlsClientConfig)
26+
if tlsClientConfig != nil {
27+
opts = append(opts, tlsClientConfig)
28+
}
2529
return opts, cleanup, nil
2630
}
2731

internal/helm/getter_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package helm
2+
3+
import (
4+
"testing"
5+
6+
corev1 "k8s.io/api/core/v1"
7+
)
8+
9+
var (
10+
basicAuthSecretFixture = corev1.Secret{
11+
Data: map[string][]byte{
12+
"username": []byte("user"),
13+
"password": []byte("password"),
14+
},
15+
}
16+
tlsSecretFixture = corev1.Secret{
17+
Data: map[string][]byte{
18+
"certFile": []byte(`fixture`),
19+
"keyFile": []byte(`fixture`),
20+
"caFile": []byte(`fixture`),
21+
},
22+
}
23+
)
24+
25+
func TestClientOptionsFromSecret(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
secrets []corev1.Secret
29+
}{
30+
{"basic auth", []corev1.Secret{basicAuthSecretFixture}},
31+
{"TLS", []corev1.Secret{tlsSecretFixture}},
32+
{"basic auth and TLS", []corev1.Secret{basicAuthSecretFixture, tlsSecretFixture}},
33+
{"empty", []corev1.Secret{}},
34+
}
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
secret := corev1.Secret{Data: map[string][]byte{}}
38+
for _, s := range tt.secrets {
39+
for k, v := range s.Data {
40+
secret.Data[k] = v
41+
}
42+
}
43+
got, cleanup, err := ClientOptionsFromSecret(secret)
44+
if cleanup != nil {
45+
defer cleanup()
46+
}
47+
if err != nil {
48+
t.Errorf("ClientOptionsFromSecret() error = %v", err)
49+
return
50+
}
51+
if len(got) != len(tt.secrets) {
52+
t.Errorf("ClientOptionsFromSecret() options = %v, expected = %v", got, len(tt.secrets))
53+
}
54+
})
55+
}
56+
}
57+
58+
func TestBasicAuthFromSecret(t *testing.T) {
59+
tests := []struct {
60+
name string
61+
secret corev1.Secret
62+
modify func(secret *corev1.Secret)
63+
wantErr bool
64+
wantNil bool
65+
}{
66+
{"username and password", basicAuthSecretFixture, nil,false, false},
67+
{"without username", basicAuthSecretFixture, func(s *corev1.Secret) { delete(s.Data, "username") }, true, true},
68+
{"without password", basicAuthSecretFixture, func(s *corev1.Secret) { delete(s.Data, "password") }, true, true},
69+
{"empty", corev1.Secret{}, nil, false, true},
70+
}
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
secret := tt.secret.DeepCopy()
74+
if tt.modify != nil {
75+
tt.modify(secret)
76+
}
77+
got, err := BasicAuthFromSecret(*secret)
78+
if (err != nil) != tt.wantErr {
79+
t.Errorf("BasicAuthFromSecret() error = %v, wantErr %v", err, tt.wantErr)
80+
return
81+
}
82+
if tt.wantNil && got != nil {
83+
t.Error("BasicAuthFromSecret() != nil")
84+
return
85+
}
86+
})
87+
}
88+
}
89+
90+
func TestTLSClientConfigFromSecret(t *testing.T) {
91+
tests := []struct {
92+
name string
93+
secret corev1.Secret
94+
modify func(secret *corev1.Secret)
95+
wantErr bool
96+
wantNil bool
97+
}{
98+
{"certFile, keyFile and caFile", tlsSecretFixture, nil,false, false},
99+
{"without certFile", tlsSecretFixture, func(s *corev1.Secret) { delete(s.Data, "certFile") }, true, true},
100+
{"without keyFile", tlsSecretFixture, func(s *corev1.Secret) { delete(s.Data, "keyFile") }, true, true},
101+
{"without caFile", tlsSecretFixture, func(s *corev1.Secret) { delete(s.Data, "caFile") }, true, true},
102+
{"empty", corev1.Secret{}, nil, false, true},
103+
}
104+
for _, tt := range tests {
105+
t.Run(tt.name, func(t *testing.T) {
106+
secret := tt.secret.DeepCopy()
107+
if tt.modify != nil {
108+
tt.modify(secret)
109+
}
110+
got, cleanup, err := TLSClientConfigFromSecret(*secret)
111+
if cleanup != nil {
112+
defer cleanup()
113+
}
114+
if (err != nil) != tt.wantErr {
115+
t.Errorf("TLSClientConfigFromSecret() error = %v, wantErr %v", err, tt.wantErr)
116+
return
117+
}
118+
if tt.wantNil && got != nil {
119+
t.Error("TLSClientConfigFromSecret() != nil")
120+
return
121+
}
122+
})
123+
}
124+
}

0 commit comments

Comments
 (0)