Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tatlat committed Dec 1, 2023
1 parent 72075ba commit d04fdf0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
25 changes: 19 additions & 6 deletions pkg/curatedpackages/packagecontrollerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,17 @@ func (pc *PackageControllerClient) Enable(ctx context.Context) error {

// UpdateSecrets is used to update the registry-mirror-cred secret used by the packages controller.
func (pc *PackageControllerClient) UpdateSecrets(ctx context.Context, client client.Client, cluster *anywherev1.Cluster) error {
secretName := "registry-mirror-cred"
secretKey := types.NamespacedName{
Namespace: constants.EksaPackagesName,
Name: "registry-mirror-cred",
Name: secretName,
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: constants.EksaPackagesName,
},
}
secret := &corev1.Secret{}
credErr := client.Get(ctx, secretKey, secret)
err := fillRegistrySecret(cluster.Name, cluster.Spec.RegistryMirrorConfiguration, secret)
if err != nil {
Expand All @@ -272,13 +278,20 @@ func (pc *PackageControllerClient) UpdateSecrets(ctx context.Context, client cli
func fillRegistrySecret(clusterName string, registry *anywherev1.RegistryMirrorConfiguration, secret *corev1.Secret) error {
caDataName := clusterName + "_ca.crt"
insecureDataName := clusterName + "_insecure"
configName := "config.json"
if secret.Data == nil {
secret.Data = make(map[string][]byte)
}
secret.Data[caDataName] = []byte(registry.CACertContent)
secret.Data[insecureDataName] = []byte(strconv.FormatBool(registry.InsecureSkipVerify))

dconfig := &dockerConfig{Auths: make(map[string]*dockerAuth)}
err := json.Unmarshal(secret.Data["config.json"], dconfig)
if err != nil {
return err
configData, ok := secret.Data[configName]
if ok {
err := json.Unmarshal(configData, dconfig)
if err != nil {
return err
}
}
username, password, err := config.ReadCredentials()
if err != nil {
Expand All @@ -295,7 +308,7 @@ func fillRegistrySecret(clusterName string, registry *anywherev1.RegistryMirrorC
if err != nil {
return err
}

Check warning on line 310 in pkg/curatedpackages/packagecontrollerclient.go

View check run for this annotation

Codecov / codecov/patch

pkg/curatedpackages/packagecontrollerclient.go#L309-L310

Added lines #L309 - L310 were not covered by tests
secret.Data["config.json"] = configJSON
secret.Data[configName] = configJSON
return nil
}

Expand Down
68 changes: 68 additions & 0 deletions pkg/curatedpackages/packagecontrollerclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,74 @@ func TestCreateHelmOverrideValuesYamlFail(t *testing.T) {
}
}

func TestUpdateSecrets(t *testing.T) {
tests := []struct {
name string
unsetEnv bool
secret *corev1.Secret
error bool
}{
{
name: "secret_not_found",
secret: nil,
},
{
name: "secret_found",
secret: &corev1.Secret{},
},
{
name: "unmarshal_error",
secret: &corev1.Secret{
Data: map[string][]byte{
"config.json": nil,
},
},
error: true,
},
{
name: "no_cred_env",
unsetEnv: true,
error: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
ctrl := gomock.NewController(t)
k := mocks.NewMockKubectlRunner(ctrl)
cm := mocks.NewMockChartManager(ctrl)
kubeConfig := "kubeconfig.kubeconfig"
cluster := newReconcileTestCluster()
cluster.Spec.RegistryMirrorConfiguration = test.RegistryMirrorInsecureSkipVerifyEnabledAndCACert()
objs := []runtime.Object{cluster}
if tt.secret != nil {
tt.secret.Name = "registry-mirror-cred"
tt.secret.Namespace = constants.EksaPackagesName
objs = append(objs, tt.secret)
}
client := fake.NewClientBuilder().WithRuntimeObjects(objs...).Build()
pcc := curatedpackages.NewPackageControllerClient(cm, k, "test", kubeConfig, nil, nil)

os.Setenv(constants.RegistryUsername, "test")
os.Setenv(constants.RegistryPassword, "test")

if tt.unsetEnv {
os.Unsetenv(constants.RegistryUsername)
os.Unsetenv(constants.RegistryPassword)
}

err := pcc.UpdateSecrets(ctx, client, cluster)
if !tt.error {
g.Expect(err).To(BeNil())
} else {
g.Expect(err).ToNot(BeNil())
}
})
}
}

func TestCreateHelmOverrideValuesYamlFailWithNoWriter(t *testing.T) {
for _, tt := range newPackageControllerTests(t) {
tt.command = curatedpackages.NewPackageControllerClient(
Expand Down

0 comments on commit d04fdf0

Please sign in to comment.