github.com/redhat-appstudio/release-service@v0.0.0-20240507143925-083712697924/syncer/syncer_test.go (about) 1 /* 2 Copyright 2022. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package syncer 18 19 import ( 20 "context" 21 . "github.com/onsi/ginkgo/v2" 22 . "github.com/onsi/gomega" 23 . "github.com/onsi/gomega/gstruct" 24 applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1" 25 v12 "k8s.io/api/core/v1" 26 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/types" 28 "reflect" 29 ctrl "sigs.k8s.io/controller-runtime" 30 ) 31 32 type key int 33 34 const ( 35 syncerContextKey key = iota 36 ) 37 38 var _ = Describe("Syncer", Ordered, func() { 39 const targetNamespace string = "syncer" 40 41 snapshot := &applicationapiv1alpha1.Snapshot{ 42 ObjectMeta: v1.ObjectMeta{ 43 Annotations: map[string]string{ 44 "foo": "bar", 45 }, 46 GenerateName: "snapshot-", 47 Labels: map[string]string{ 48 "foo": "bar", 49 }, 50 Namespace: "default", 51 }, 52 Spec: applicationapiv1alpha1.SnapshotSpec{ 53 Application: "app", 54 Components: []applicationapiv1alpha1.SnapshotComponent{ 55 { 56 Name: "foo", 57 ContainerImage: "quay.io/foo", 58 }, 59 }, 60 }, 61 } 62 63 BeforeAll(func() { 64 namespace := &v12.Namespace{ 65 ObjectMeta: v1.ObjectMeta{ 66 Name: targetNamespace, 67 }, 68 } 69 Expect(k8sClient.Create(ctx, namespace)).To(Succeed()) 70 Expect(k8sClient.Create(ctx, snapshot)).To(Succeed()) 71 }) 72 73 AfterAll(func() { 74 Expect(k8sClient.Delete(ctx, snapshot)).To(Succeed()) 75 }) 76 77 It("can create a new Syncer without specifying a context", func() { 78 syncer := NewSyncer(k8sClient, &ctrl.Log) 79 Expect(reflect.TypeOf(syncer)).To(Equal(reflect.TypeOf(&Syncer{}))) 80 }) 81 82 It("can create a new Syncer specifying a context", func() { 83 syncer := NewSyncerWithContext(k8sClient, &ctrl.Log, context.TODO()) 84 Expect(reflect.TypeOf(syncer)).To(Equal(reflect.TypeOf(&Syncer{}))) 85 }) 86 87 It("can modify the context being used", func() { 88 syncerContext := context.WithValue(context.TODO(), syncerContextKey, "bar") 89 syncer := NewSyncerWithContext(k8sClient, &ctrl.Log, syncerContext) 90 Expect(syncer.ctx.Value(syncerContextKey)).To(Equal("bar")) 91 syncer.SetContext(context.TODO()) 92 Expect(syncer.ctx.Value(syncerContextKey)).To(BeNil()) 93 }) 94 95 It("can sync an snapshot into a given namespace", func() { 96 syncer := NewSyncer(k8sClient, &ctrl.Log) 97 98 Expect(syncer.SyncSnapshot(snapshot, targetNamespace)).To(Succeed()) 99 100 syncedSnapshot := &applicationapiv1alpha1.Snapshot{} 101 Expect(k8sClient.Get(ctx, types.NamespacedName{ 102 Name: snapshot.Name, 103 Namespace: targetNamespace, 104 }, syncedSnapshot)).To(Succeed()) 105 Expect(*syncedSnapshot).To(MatchFields(IgnoreExtras, Fields{ 106 "ObjectMeta": MatchFields(IgnoreExtras, Fields{ 107 "Annotations": HaveLen(len(snapshot.Annotations)), 108 "Labels": HaveLen(len(snapshot.Labels)), 109 }), 110 "Spec": MatchFields(IgnoreExtras, Fields{ 111 "Application": Equal(snapshot.Spec.Application), 112 "Components": HaveLen(len(snapshot.Spec.Components)), 113 }), 114 })) 115 116 Expect(k8sClient.Delete(ctx, syncedSnapshot)).To(Succeed()) 117 }) 118 })