Files
open-cluster-management/pkg/addon/templateagent/values_test.go
Nitish Chauhan 9dd935b525 adding contextual logging in addon pkg (#255)
Signed-off-by: ntishchauhan0022 <nitishchauhan0022@gmail.com>
2023-08-24 04:29:59 -04:00

103 lines
2.9 KiB
Go

package templateagent
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2/ktesting"
"open-cluster-management.io/addon-framework/pkg/addonfactory"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterv1 "open-cluster-management.io/api/cluster/v1"
)
func TestGetAddOnRegistriesPrivateValuesFromClusterAnnotation(t *testing.T) {
cases := []struct {
name string
cluster *clusterv1.ManagedCluster
expectedValues addonfactory.Values
expectedError string
}{
{
name: "no values",
cluster: &clusterv1.ManagedCluster{},
expectedValues: addonfactory.Values{},
},
{
name: "not expected annotation",
cluster: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
clusterv1.ClusterImageRegistriesAnnotationKey: `{"registries-test":[{"mirror-test":"quay.io/ocm","source-test":"quay-test.io/ocm"}]}`,
},
},
},
expectedValues: addonfactory.Values{},
},
{
name: "annotation invalid",
cluster: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
clusterv1.ClusterImageRegistriesAnnotationKey: `{"registries":`,
},
},
},
expectedValues: addonfactory.Values{},
expectedError: "unexpected end of JSON input",
},
{
name: "override registries",
cluster: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
clusterv1.ClusterImageRegistriesAnnotationKey: `{"registries":[{"mirror":"quay.io/ocm","source":"quay-test.io/ocm"}]}`,
},
},
},
expectedValues: addonfactory.Values{
RegistriesPrivateValueKey: []addonapiv1alpha1.ImageMirror{
{
Source: "quay-test.io/ocm",
Mirror: "quay.io/ocm",
},
},
},
},
{
name: "override image",
cluster: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
clusterv1.ClusterImageRegistriesAnnotationKey: `{"registries":[{"mirror":"quay.io/ocm/test","source":"quay.io/open-cluster-management/test"}]}`,
},
},
},
expectedValues: addonfactory.Values{
RegistriesPrivateValueKey: []addonapiv1alpha1.ImageMirror{
{
Source: "quay.io/open-cluster-management/test",
Mirror: "quay.io/ocm/test",
},
},
},
},
}
for _, c := range cases {
logger, _ := ktesting.NewTestContext(t)
t.Run(c.name, func(t *testing.T) {
values, err := GetAddOnRegistriesPrivateValuesFromClusterAnnotation(logger, c.cluster, nil)
if err != nil || len(c.expectedError) > 0 {
assert.ErrorContains(t, err, c.expectedError, "expected error: %v, got: %v", c.expectedError, err)
}
if !reflect.DeepEqual(values, c.expectedValues) {
t.Errorf("expected values: %v, got: %v", c.expectedValues, values)
}
})
}
}