package couchctl import ( _ "embed" "sort" "testing" dsapi "edge-infra.dev/pkg/edge/datasync/apis/v1alpha1" "edge-infra.dev/pkg/lib/ini" "github.com/google/uuid" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" "sigs.k8s.io/yaml" ) var ( //go:embed testdata/cloud-configmap.yaml cloudCouchDBManifests []byte //go:embed testdata/store-configmap.yaml storeCouchDBManifests []byte //go:embed testdata/touchpoint1-configmap.yaml touchpoint1CouchDBManifests []byte ) func TestConfigmap(t *testing.T) { touchpointServer := dsapi.NewTouchpointCouchDBServer(uuid.NewString(), "1") for _, server := range []*dsapi.CouchDBServer{dsapi.NewAdminCouchDBServer(), dsapi.NewStoreCouchDBServer(), touchpointServer} { expected, err := loadConfigMap(*server) assert.NoError(t, err) if !server.IsCloud() { expected.Name = server.Name } generated, err := ConfigMap(*server) assert.NoError(t, err, "server type: %s, %s", server.Name, server.Type()) assertConfigMap(t, *server, expected, generated) } } func loadConfigMap(server dsapi.CouchDBServer) (*corev1.ConfigMap, error) { cm := &corev1.ConfigMap{} switch server.Type() { case dsapi.Cloud: return cm, yaml.Unmarshal(cloudCouchDBManifests, cm) case dsapi.Touchpoint: return cm, yaml.Unmarshal(touchpoint1CouchDBManifests, cm) default: return cm, yaml.Unmarshal(storeCouchDBManifests, cm) } } func assertConfigMap(t *testing.T, server dsapi.CouchDBServer, fromYaml, fromStruct *corev1.ConfigMap) { assert.Equal(t, fromYaml.TypeMeta, fromStruct.TypeMeta, "mismatch for [%s] configmap: %s manifests", server.Type(), fromYaml.Name) assert.Equal(t, fromYaml.ObjectMeta, fromStruct.ObjectMeta, "mismatch for [%s] configmap: %s manifests", server.Type(), fromYaml.Name) assert.Equal(t, fromYaml.Immutable, fromStruct.Immutable, "mismatch for [%s] configmap: %s manifests", server.Type(), fromYaml.Name) assert.Equal(t, len(fromYaml.Data), len(fromStruct.Data), "mismatch for [%s] configmap: %s manifests", server.Type(), fromYaml.Name) assert.Equal(t, len(fromYaml.BinaryData), len(fromStruct.BinaryData), "mismatch for %s configmap: [%s] manifests", server.Type(), fromYaml.Name) for key := range fromYaml.Data { if key == IniFileKey { assertINIFile(t, server, fromYaml.Name, []byte(fromYaml.Data[key]), []byte(fromStruct.Data[key])) continue } assert.Equal(t, fromYaml.Data[key], fromStruct.Data[key], "mismatch data for [%s] configmap: %s manifests", server.Type(), fromYaml.Name) } for key := range fromYaml.BinaryData { if key == IniFileKey { assertINIFile(t, server, fromYaml.Name, fromYaml.BinaryData[key], fromStruct.BinaryData[key]) continue } assert.Equal(t, fromYaml.BinaryData[key], fromStruct.BinaryData[key], "mismatch binary data for [%s] configmap: %s manifests", server.Type(), fromYaml.Name) } } func assertINIFile(t *testing.T, server dsapi.CouchDBServer, name string, fromYaml, fromStruct []byte) { yamlINIFile, err := ini.Load(fromYaml) assert.NoError(t, err) structINIFile, err := ini.Load(fromStruct) assert.NoError(t, err) yamlSections := yamlINIFile.SectionStrings() structSections := structINIFile.SectionStrings() sort.Strings(yamlSections) sort.Strings(structSections) assert.Equal(t, len(yamlSections), len(structSections), "mismatch ini for [%s] configmap: %s manifests", server.Type(), name) for _, section := range yamlSections { yamlSection := yamlINIFile.Section(section) structSection := structINIFile.Section(section) for _, key := range yamlSection.KeyStrings() { yamlVal := yamlSection.Key(key).String() structVal := structSection.Key(key).String() assert.Equal(t, yamlVal, structVal, "mismatch ini for [%s] configmap: %s manifests, section=%s, key=%s", server.Type(), name, section, key) } } }