...

Source file src/edge-infra.dev/pkg/edge/datasync/controllers/couchctl/configmap_test.go

Documentation: edge-infra.dev/pkg/edge/datasync/controllers/couchctl

     1  package couchctl
     2  
     3  import (
     4  	_ "embed"
     5  	"sort"
     6  	"testing"
     7  
     8  	dsapi "edge-infra.dev/pkg/edge/datasync/apis/v1alpha1"
     9  	"edge-infra.dev/pkg/lib/ini"
    10  
    11  	"github.com/google/uuid"
    12  	"github.com/stretchr/testify/assert"
    13  
    14  	corev1 "k8s.io/api/core/v1"
    15  	"sigs.k8s.io/yaml"
    16  )
    17  
    18  var (
    19  
    20  	//go:embed testdata/cloud-configmap.yaml
    21  	cloudCouchDBManifests []byte
    22  
    23  	//go:embed testdata/store-configmap.yaml
    24  	storeCouchDBManifests []byte
    25  
    26  	//go:embed testdata/touchpoint1-configmap.yaml
    27  	touchpoint1CouchDBManifests []byte
    28  )
    29  
    30  func TestConfigmap(t *testing.T) {
    31  	touchpointServer := dsapi.NewTouchpointCouchDBServer(uuid.NewString(), "1")
    32  	for _, server := range []*dsapi.CouchDBServer{dsapi.NewAdminCouchDBServer(), dsapi.NewStoreCouchDBServer(), touchpointServer} {
    33  		expected, err := loadConfigMap(*server)
    34  		assert.NoError(t, err)
    35  		if !server.IsCloud() {
    36  			expected.Name = server.Name
    37  		}
    38  
    39  		generated, err := ConfigMap(*server)
    40  		assert.NoError(t, err, "server type: %s, %s", server.Name, server.Type())
    41  
    42  		assertConfigMap(t, *server, expected, generated)
    43  	}
    44  }
    45  
    46  func loadConfigMap(server dsapi.CouchDBServer) (*corev1.ConfigMap, error) {
    47  	cm := &corev1.ConfigMap{}
    48  	switch server.Type() {
    49  	case dsapi.Cloud:
    50  		return cm, yaml.Unmarshal(cloudCouchDBManifests, cm)
    51  	case dsapi.Touchpoint:
    52  		return cm, yaml.Unmarshal(touchpoint1CouchDBManifests, cm)
    53  	default:
    54  		return cm, yaml.Unmarshal(storeCouchDBManifests, cm)
    55  	}
    56  }
    57  
    58  func assertConfigMap(t *testing.T, server dsapi.CouchDBServer, fromYaml, fromStruct *corev1.ConfigMap) {
    59  	assert.Equal(t, fromYaml.TypeMeta, fromStruct.TypeMeta, "mismatch for [%s] configmap: %s manifests", server.Type(), fromYaml.Name)
    60  	assert.Equal(t, fromYaml.ObjectMeta, fromStruct.ObjectMeta, "mismatch for [%s] configmap: %s manifests", server.Type(), fromYaml.Name)
    61  	assert.Equal(t, fromYaml.Immutable, fromStruct.Immutable, "mismatch for [%s] configmap: %s manifests", server.Type(), fromYaml.Name)
    62  	assert.Equal(t, len(fromYaml.Data), len(fromStruct.Data), "mismatch for [%s] configmap: %s manifests", server.Type(), fromYaml.Name)
    63  	assert.Equal(t, len(fromYaml.BinaryData), len(fromStruct.BinaryData), "mismatch for %s configmap: [%s] manifests", server.Type(), fromYaml.Name)
    64  	for key := range fromYaml.Data {
    65  		if key == IniFileKey {
    66  			assertINIFile(t, server, fromYaml.Name, []byte(fromYaml.Data[key]), []byte(fromStruct.Data[key]))
    67  			continue
    68  		}
    69  		assert.Equal(t, fromYaml.Data[key], fromStruct.Data[key], "mismatch data for [%s] configmap: %s manifests", server.Type(), fromYaml.Name)
    70  	}
    71  	for key := range fromYaml.BinaryData {
    72  		if key == IniFileKey {
    73  			assertINIFile(t, server, fromYaml.Name, fromYaml.BinaryData[key], fromStruct.BinaryData[key])
    74  			continue
    75  		}
    76  		assert.Equal(t, fromYaml.BinaryData[key], fromStruct.BinaryData[key], "mismatch binary data for [%s] configmap: %s manifests", server.Type(), fromYaml.Name)
    77  	}
    78  }
    79  
    80  func assertINIFile(t *testing.T, server dsapi.CouchDBServer, name string, fromYaml, fromStruct []byte) {
    81  	yamlINIFile, err := ini.Load(fromYaml)
    82  	assert.NoError(t, err)
    83  
    84  	structINIFile, err := ini.Load(fromStruct)
    85  	assert.NoError(t, err)
    86  
    87  	yamlSections := yamlINIFile.SectionStrings()
    88  	structSections := structINIFile.SectionStrings()
    89  	sort.Strings(yamlSections)
    90  	sort.Strings(structSections)
    91  
    92  	assert.Equal(t, len(yamlSections), len(structSections), "mismatch ini for [%s] configmap: %s manifests", server.Type(), name)
    93  
    94  	for _, section := range yamlSections {
    95  		yamlSection := yamlINIFile.Section(section)
    96  		structSection := structINIFile.Section(section)
    97  		for _, key := range yamlSection.KeyStrings() {
    98  			yamlVal := yamlSection.Key(key).String()
    99  			structVal := structSection.Key(key).String()
   100  			assert.Equal(t, yamlVal, structVal, "mismatch ini for [%s] configmap: %s manifests, section=%s, key=%s", server.Type(), name, section, key)
   101  		}
   102  	}
   103  }
   104  

View as plain text