github.com/argoproj/argo-events@v1.9.1/sensors/policy/resource-labels_test.go (about) 1 /* 2 Copyright 2018 BlackRock, Inc. 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 policy 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/apimachinery/pkg/runtime/schema" 28 "k8s.io/apimachinery/pkg/util/wait" 29 "k8s.io/client-go/dynamic/fake" 30 31 "github.com/argoproj/argo-events/pkg/apis/common" 32 "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" 33 ) 34 35 func newUnstructured(apiVersion, kind, namespace, name string) *unstructured.Unstructured { 36 return &unstructured.Unstructured{ 37 Object: map[string]interface{}{ 38 "apiVersion": apiVersion, 39 "kind": kind, 40 "metadata": map[string]interface{}{ 41 "namespace": namespace, 42 "name": name, 43 "labels": map[string]interface{}{ 44 "name": name, 45 }, 46 }, 47 }, 48 } 49 } 50 51 func TestResourceLabels_ApplyPolicy(t *testing.T) { 52 uObj := newUnstructured("apps/v1", "Deployment", "fake", "test") 53 runtimeScheme := runtime.NewScheme() 54 client := fake.NewSimpleDynamicClient(runtimeScheme, uObj) 55 artifact := common.NewResource(uObj) 56 jitter := common.NewAmount("0.5") 57 factor := common.NewAmount("2") 58 duration := common.FromString("1s") 59 trigger := &v1alpha1.Trigger{ 60 Template: &v1alpha1.TriggerTemplate{ 61 Name: "fake-trigger", 62 K8s: &v1alpha1.StandardK8STrigger{ 63 Source: &v1alpha1.ArtifactLocation{ 64 Resource: &artifact, 65 }, 66 }, 67 }, 68 Policy: &v1alpha1.TriggerPolicy{ 69 K8s: &v1alpha1.K8SResourcePolicy{ 70 ErrorOnBackoffTimeout: true, 71 Labels: map[string]string{ 72 "complete": "true", 73 }, 74 Backoff: &common.Backoff{ 75 Steps: 2, 76 Duration: &duration, 77 Factor: &factor, 78 Jitter: &jitter, 79 }, 80 }, 81 }, 82 } 83 84 namespacableClient := client.Resource(schema.GroupVersionResource{ 85 Resource: "deployments", 86 Version: "v1", 87 Group: "apps", 88 }) 89 90 ctx := context.TODO() 91 92 tests := []struct { 93 name string 94 updateFunc func(deployment *unstructured.Unstructured) (*unstructured.Unstructured, error) 95 testFunc func(err error) 96 }{ 97 { 98 name: "success", 99 updateFunc: func(deployment *unstructured.Unstructured) (*unstructured.Unstructured, error) { 100 labels := deployment.GetLabels() 101 if labels == nil { 102 labels = map[string]string{} 103 } 104 labels["complete"] = "true" 105 deployment.SetLabels(labels) 106 return namespacableClient.Namespace("fake").Update(ctx, deployment, metav1.UpdateOptions{}) 107 }, 108 testFunc: func(err error) { 109 assert.Nil(t, err) 110 }, 111 }, 112 { 113 name: "failure", 114 updateFunc: func(deployment *unstructured.Unstructured) (i *unstructured.Unstructured, e error) { 115 labels := deployment.GetLabels() 116 if labels == nil { 117 labels = map[string]string{} 118 } 119 labels["complete"] = "false" 120 deployment.SetLabels(labels) 121 return namespacableClient.Namespace("fake").Update(ctx, deployment, metav1.UpdateOptions{}) 122 }, 123 testFunc: func(err error) { 124 assert.NotNil(t, err) 125 assert.Equal(t, wait.ErrWaitTimeout.Error(), err.Error()) 126 }, 127 }, 128 } 129 130 resourceLabelsPolicy := &ResourceLabels{ 131 Obj: uObj, 132 Trigger: trigger, 133 Client: namespacableClient, 134 } 135 136 for _, test := range tests { 137 t.Run(test.name, func(t *testing.T) { 138 var err error 139 uObj, err = test.updateFunc(uObj) 140 assert.Nil(t, err) 141 err = resourceLabelsPolicy.ApplyPolicy(ctx) 142 test.testFunc(err) 143 }) 144 } 145 }