Skip to content

Commit bca8987

Browse files
author
Moritz Clasmeier
committed
Tests
1 parent 743c58c commit bca8987

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package deployer
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/stackrox/roxie/internal/dockerauth"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
v1 "k8s.io/api/core/v1"
12+
)
13+
14+
func TestInjectRegistryCredentialsIntoSecret(t *testing.T) {
15+
const (
16+
registryUsername = "user"
17+
registryPassword = "pass"
18+
)
19+
20+
makeSecret := func(credentials map[string]map[string]string) *v1.Secret {
21+
data, err := json.Marshal(map[string]any{
22+
"auths": credentials,
23+
})
24+
require.NoError(t, err)
25+
return &v1.Secret{Data: map[string][]byte{dockerConfigJsonKey: data}}
26+
}
27+
28+
encodeCredentials := func(username, password string) string {
29+
return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
30+
}
31+
32+
tests := []struct {
33+
name string
34+
secret *v1.Secret
35+
expectModified bool
36+
expectError bool
37+
expectCredentials map[string]map[string]string
38+
}{
39+
{
40+
name: "injects into empty auths",
41+
secret: makeSecret(nil),
42+
expectModified: true,
43+
expectCredentials: map[string]map[string]string{
44+
registryForDownstreamImages: {
45+
"auth": encodeCredentials(registryUsername, registryPassword),
46+
},
47+
},
48+
},
49+
{
50+
name: "preserves existing entries",
51+
secret: makeSecret(map[string]map[string]string{
52+
"registry.example.com": {
53+
"auth": encodeCredentials("other", "secret"),
54+
},
55+
}),
56+
expectModified: true,
57+
expectCredentials: map[string]map[string]string{
58+
"registry.example.com": {
59+
"auth": encodeCredentials("other", "secret"),
60+
},
61+
registryForDownstreamImages: {
62+
"auth": encodeCredentials(registryUsername, registryPassword),
63+
},
64+
},
65+
},
66+
{
67+
name: "skips if already present",
68+
secret: makeSecret(map[string]map[string]string{
69+
registryForDownstreamImages: {
70+
"auth": encodeCredentials("existing", "existing"),
71+
},
72+
}),
73+
expectModified: false,
74+
expectCredentials: map[string]map[string]string{
75+
registryForDownstreamImages: {
76+
"auth": encodeCredentials("existing", "existing"),
77+
},
78+
},
79+
},
80+
{
81+
name: "handles nil secret data",
82+
secret: &v1.Secret{},
83+
expectModified: true,
84+
expectCredentials: map[string]map[string]string{
85+
registryForDownstreamImages: {
86+
"auth": encodeCredentials(registryUsername, registryPassword),
87+
},
88+
},
89+
},
90+
{
91+
name: "returns error on invalid JSON",
92+
secret: &v1.Secret{Data: map[string][]byte{dockerConfigJsonKey: []byte("not json")}},
93+
expectError: true,
94+
},
95+
}
96+
97+
for _, tt := range tests {
98+
t.Run(tt.name, func(t *testing.T) {
99+
creds := dockerauth.Credentials{Username: registryUsername, Password: registryPassword}
100+
modified, err := injectRegistryCredentialsIntoSecret(creds, tt.secret)
101+
if tt.expectError {
102+
assert.Error(t, err)
103+
return
104+
}
105+
require.NoError(t, err)
106+
assert.Equal(t, tt.expectModified, modified)
107+
108+
var cfg dockerConfigJSON
109+
require.NoError(t, json.Unmarshal(tt.secret.Data[dockerConfigJsonKey], &cfg))
110+
111+
assert.Equal(t, len(tt.expectCredentials), len(cfg.Auths), "credential length mismatch")
112+
113+
for regName, regCredentials := range tt.expectCredentials {
114+
assert.Equal(t, regCredentials["auth"], cfg.Auths[regName].Auth, "credentials mismatch for registry %s", regName)
115+
}
116+
})
117+
}
118+
}

0 commit comments

Comments
 (0)