Skip to content

Commit 18dec9b

Browse files
committed
feat: unit tests
1 parent 960ad66 commit 18dec9b

File tree

5 files changed

+76
-12
lines changed

5 files changed

+76
-12
lines changed

internal/flux_deployer/git_credentials.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ import (
1414
)
1515

1616
const (
17-
username = "username"
18-
password = "password"
19-
token = "token"
20-
identity = "identity"
21-
knownHosts = "known_hosts"
17+
Username = "username"
18+
Password = "password"
19+
Token = "token"
20+
Identity = "identity"
21+
KnownHosts = "known_hosts"
2222
)
2323

2424
// CreateGitCredentialsSecret creates or updates a Secret with name "git" in the fluxcd namespace.
25-
// The secret contains git credentials for flux sync, read from the file d.gitCredentials.
25+
// The secret contains git credentials for the flux sync, read from the file gitCredentialsPath.
2626
// The file should contain a YAML of a map[string]string, whose keys are described
2727
// in https://fluxcd.io/flux/components/source/gitrepositories/#secret-reference, e.g. username and password.
28-
func CreateGitCredentialsSecret(ctx context.Context, log *logrus.Logger, gitCredentialsPath string, secretName, secretNamespace string, platformClient client.Client) error {
28+
func CreateGitCredentialsSecret(ctx context.Context, log *logrus.Logger, gitCredentialsPath, secretName, secretNamespace string, platformClient client.Client) error {
29+
2930
log.Infof("Creating/updating git credentials secret %s/%s", secretNamespace, secretName)
3031

3132
gitCredentialsData := map[string][]byte{}
@@ -44,12 +45,12 @@ func CreateGitCredentialsSecret(ctx context.Context, log *logrus.Logger, gitCred
4445

4546
if config.Authentication.BasicAuth != nil {
4647
log.Debug("Using basic auth credentials for git operations")
47-
gitCredentialsData[username] = []byte(config.Authentication.BasicAuth.Username)
48-
gitCredentialsData[password] = []byte(config.Authentication.BasicAuth.Password)
48+
gitCredentialsData[Username] = []byte(config.Authentication.BasicAuth.Username)
49+
gitCredentialsData[Password] = []byte(config.Authentication.BasicAuth.Password)
4950
}
5051
if config.Authentication.BearerToken != nil {
5152
log.Debug("Using bearer token for git operations")
52-
gitCredentialsData[token] = []byte(config.Authentication.BearerToken.Token)
53+
gitCredentialsData[Token] = []byte(config.Authentication.BearerToken.Token)
5354
}
5455
if config.Authentication.SSHPrivateKey != nil {
5556
log.Debug("Using ssh private key for git operations")
@@ -58,7 +59,7 @@ func CreateGitCredentialsSecret(ctx context.Context, log *logrus.Logger, gitCred
5859
return err
5960
}
6061

61-
gitCredentialsData[identity] = privateKey
62+
gitCredentialsData[Identity] = privateKey
6263

6364
if config.Authentication.SSHPrivateKey.KnownHosts != "" {
6465
knownHostsPath := config.Authentication.SSHPrivateKey.KnownHosts
@@ -69,7 +70,7 @@ func CreateGitCredentialsSecret(ctx context.Context, log *logrus.Logger, gitCred
6970
if err != nil {
7071
return fmt.Errorf("error reading known hosts file %s: %w", knownHostsPath, err)
7172
}
72-
gitCredentialsData[knownHosts] = knownHostsContent
73+
gitCredentialsData[KnownHosts] = knownHostsContent
7374
}
7475
}
7576
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package flux_deployer_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
corev1 "k8s.io/api/core/v1"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
9+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
10+
11+
"github.com/openmcp-project/bootstrapper/internal/flux_deployer"
12+
logging "github.com/openmcp-project/bootstrapper/internal/log"
13+
)
14+
15+
func TestCreateGitCredentialsSecret(t *testing.T) {
16+
platformClient := fake.NewClientBuilder().Build()
17+
18+
testCases := []struct {
19+
desc string
20+
gitConfigPath string
21+
secretName string
22+
expectedData map[string][]byte
23+
}{
24+
{
25+
desc: "Git secret with basic auth credentials",
26+
gitConfigPath: "./testdata/02/git-config-basic.yaml",
27+
secretName: "test-secret-basic",
28+
expectedData: map[string][]byte{
29+
flux_deployer.Username: []byte("test-user"),
30+
flux_deployer.Password: []byte("test-pass"),
31+
},
32+
},
33+
{
34+
desc: "Git secret with basic ssh credentials",
35+
gitConfigPath: "./testdata/02/git-config-ssh.yaml",
36+
secretName: "test-secret-ssh",
37+
expectedData: map[string][]byte{
38+
flux_deployer.Identity: []byte("test-key"),
39+
flux_deployer.KnownHosts: []byte("test-known-hosts"),
40+
},
41+
},
42+
}
43+
44+
for _, tc := range testCases {
45+
t.Run(tc.desc, func(t *testing.T) {
46+
err := flux_deployer.CreateGitCredentialsSecret(t.Context(), logging.GetLogger(), tc.gitConfigPath, tc.secretName, flux_deployer.FluxSystemNamespace, platformClient)
47+
assert.NoError(t, err, "Error creating git credentials secret")
48+
secret := &corev1.Secret{}
49+
err = platformClient.Get(t.Context(), client.ObjectKey{Name: tc.secretName, Namespace: flux_deployer.FluxSystemNamespace}, secret)
50+
assert.NoError(t, err, "Error getting git credentials secret")
51+
assert.Equal(t, tc.expectedData, secret.Data, "Git credentials secret data do not match expected data")
52+
})
53+
}
54+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
auth:
2+
basic:
3+
username: test-user
4+
password: test-pass
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
auth:
2+
sshPrivateKey:
3+
privateKey: dGVzdC1rZXk=
4+
knownHosts: ./testdata/02/known_hosts
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test-known-hosts

0 commit comments

Comments
 (0)