package cushion import ( "context" "testing" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" dsapi "edge-infra.dev/pkg/edge/datasync/apis/v1alpha1" "edge-infra.dev/pkg/edge/datasync/couchdb" ) type namePair struct { DBName string K8sDBName string } var ( namePairs = []namePair{ {"db_1", "db-1"}, {"DB-2", "db-2"}, } ) func TestResourceMigration(t *testing.T) { ctx := context.Background() scheme := runtime.NewScheme() utilruntime.Must(corev1.AddToScheme(scheme)) utilruntime.Must(dsapi.AddToScheme(scheme)) cl := fake.NewClientBuilder(). WithScheme(scheme). WithRuntimeObjects(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: cushionNamespace}}). Build() for _, np := range namePairs { nn := types.NamespacedName{Name: np.K8sDBName, Namespace: cushionNamespace} k8sDB := newCouchDBDatabase(nn, normalizeDBName(np.DBName), couchdb.Namespace) k8sDB.SetLabels(map[string]string{}) // old k8sDB has no labels err := cl.Create(ctx, k8sDB) if err != nil { t.Errorf("Error creating k8s db %s: %v", k8sDB.Name, err) } } k8sDBs := &dsapi.CouchDBDatabaseList{} err := cl.List(ctx, k8sDBs, &client.ListOptions{Namespace: cushionNamespace}) if err != nil { t.Errorf("Error listing k8s dbs: %v", err) } err = migrateCushionK8sDBs(ctx, cl) if err != nil { t.Errorf("Error migrating cushion k8s dbs: %v", err) } for _, np := range namePairs { k8sDB := &dsapi.CouchDBDatabase{} nn := types.NamespacedName{Name: K8sDBName(np.DBName), Namespace: cushionNamespace} err = cl.Get(ctx, nn, k8sDB) if err != nil { t.Errorf("Error getting k8s db %s: %s, %v", k8sDB.Name, nn, err) } if _, ok := k8sDB.Labels[componentLabel]; !ok { t.Errorf("Component label not found on k8s db %s", k8sDB.Name) } } }