...

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

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

     1  package couchctl
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"testing"
     8  
     9  	corev1 "k8s.io/api/core/v1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"k8s.io/apimachinery/pkg/runtime"
    12  
    13  	"github.com/google/uuid"
    14  	"github.com/stretchr/testify/assert"
    15  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    16  )
    17  
    18  func Test_oldPVCsSuffixes(t *testing.T) {
    19  	ctx := context.Background()
    20  
    21  	tests := []struct {
    22  		name        string
    23  		description string
    24  		result      map[string]string
    25  		err         error
    26  	}{
    27  		{
    28  			name:        "database-storage-data-sync-couchdb-0",
    29  			description: "Old control plane no lane",
    30  			result:      map[string]string{"node-0": "0"},
    31  		},
    32  		{
    33  			name:        "database-storage-data-sync-couchdb-1-0",
    34  			description: "Old touchpoint lane 1",
    35  			result:      map[string]string{"node-1": "1"},
    36  		},
    37  		{
    38  			name:        "database-storage-data-sync-couchdb-a424450319062fa778f6-0",
    39  			description: "Old touchpoint no lane",
    40  			result:      map[string]string{"node-2": "a424450319062fa778f6"},
    41  		},
    42  		{
    43  			name:        "database-storage-couchdb-xqw0z42tplcr-0",
    44  			description: "New Name",
    45  			result:      map[string]string{"node-3": ""},
    46  		},
    47  	}
    48  
    49  	var pvcs []string
    50  	for _, tc := range tests {
    51  		pvcs = append(pvcs, tc.name)
    52  	}
    53  
    54  	cl := fake.NewFakeClient(fakeObjects(pvcs...)...)
    55  	for _, tc := range tests {
    56  		t.Run(tc.name, func(t *testing.T) {
    57  			r, err := oldPVCsSuffixes(ctx, cl)
    58  			assert.True(t, errors.Is(err, tc.err), "%s: pvc error = %v, wantErr %v", tc.description, err, tc.err)
    59  			m := r["data-sync-couchdb"]
    60  			for k, v := range tc.result {
    61  				assert.Equal(t, v, m[k], tc.description)
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func fakeObjects(pvcs ...string) []runtime.Object {
    68  	var objs []runtime.Object
    69  	for i, pvc := range pvcs {
    70  		uid := uuid.NewString()
    71  		pv := fmt.Sprintf("pvc-%s", uid)
    72  		objs = append(objs, &corev1.PersistentVolumeClaim{
    73  			TypeMeta: metav1.TypeMeta{
    74  				Kind:       "PersistentVolumeClaim",
    75  				APIVersion: "v1",
    76  			},
    77  			ObjectMeta: metav1.ObjectMeta{
    78  				Name:      pvc,
    79  				Namespace: "data-sync-couchdb",
    80  			},
    81  			Spec: corev1.PersistentVolumeClaimSpec{
    82  				VolumeName: pv,
    83  			},
    84  		})
    85  		objs = append(objs, &corev1.PersistentVolume{
    86  			TypeMeta: metav1.TypeMeta{
    87  				Kind:       "PersistentVolume",
    88  				APIVersion: "v1",
    89  			},
    90  			ObjectMeta: metav1.ObjectMeta{
    91  				Name: pv,
    92  			},
    93  			Spec: corev1.PersistentVolumeSpec{
    94  				NodeAffinity: &corev1.VolumeNodeAffinity{
    95  					Required: &corev1.NodeSelector{
    96  						NodeSelectorTerms: []corev1.NodeSelectorTerm{
    97  							{
    98  								MatchExpressions: []corev1.NodeSelectorRequirement{
    99  									{
   100  										Key:      "kubernetes.io/hostname",
   101  										Operator: corev1.NodeSelectorOpIn,
   102  										Values:   []string{fmt.Sprintf("node-%d", i)},
   103  									},
   104  								},
   105  							},
   106  						},
   107  					},
   108  				},
   109  			},
   110  		})
   111  	}
   112  	return objs
   113  }
   114  

View as plain text