sigs.k8s.io/external-dns@v0.14.1/plan/policy_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 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 plan 18 19 import ( 20 "reflect" 21 "testing" 22 23 "sigs.k8s.io/external-dns/endpoint" 24 ) 25 26 // TestApply tests that applying a policy results in the correct set of changes. 27 func TestApply(t *testing.T) { 28 // empty list of records 29 empty := []*endpoint.Endpoint{} 30 // a simple entry 31 fooV1 := []*endpoint.Endpoint{{DNSName: "foo", Targets: endpoint.Targets{"v1"}}} 32 // the same entry but with different target 33 fooV2 := []*endpoint.Endpoint{{DNSName: "foo", Targets: endpoint.Targets{"v2"}}} 34 // another two simple entries 35 bar := []*endpoint.Endpoint{{DNSName: "bar", Targets: endpoint.Targets{"v1"}}} 36 baz := []*endpoint.Endpoint{{DNSName: "baz", Targets: endpoint.Targets{"v1"}}} 37 38 for _, tc := range []struct { 39 policy Policy 40 changes *Changes 41 expected *Changes 42 }{ 43 { 44 // SyncPolicy doesn't modify the set of changes. 45 &SyncPolicy{}, 46 &Changes{Create: baz, UpdateOld: fooV1, UpdateNew: fooV2, Delete: bar}, 47 &Changes{Create: baz, UpdateOld: fooV1, UpdateNew: fooV2, Delete: bar}, 48 }, 49 { 50 // UpsertOnlyPolicy clears the list of deletions. 51 &UpsertOnlyPolicy{}, 52 &Changes{Create: baz, UpdateOld: fooV1, UpdateNew: fooV2, Delete: bar}, 53 &Changes{Create: baz, UpdateOld: fooV1, UpdateNew: fooV2, Delete: empty}, 54 }, 55 { 56 // CreateOnlyPolicy clears the list of updates and deletions. 57 &CreateOnlyPolicy{}, 58 &Changes{Create: baz, UpdateOld: fooV1, UpdateNew: fooV2, Delete: bar}, 59 &Changes{Create: baz, UpdateOld: empty, UpdateNew: empty, Delete: empty}, 60 }, 61 } { 62 // apply policy 63 changes := tc.policy.Apply(tc.changes) 64 65 // validate changes after applying policy 66 validateEntries(t, changes.Create, tc.expected.Create) 67 validateEntries(t, changes.UpdateOld, tc.expected.UpdateOld) 68 validateEntries(t, changes.UpdateNew, tc.expected.UpdateNew) 69 validateEntries(t, changes.Delete, tc.expected.Delete) 70 } 71 } 72 73 // TestPolicies tests that policies are correctly registered. 74 func TestPolicies(t *testing.T) { 75 validatePolicy(t, Policies["sync"], &SyncPolicy{}) 76 validatePolicy(t, Policies["upsert-only"], &UpsertOnlyPolicy{}) 77 validatePolicy(t, Policies["create-only"], &CreateOnlyPolicy{}) 78 } 79 80 // validatePolicy validates that a given policy is of the given type. 81 func validatePolicy(t *testing.T, policy, expected Policy) { 82 policyType := reflect.TypeOf(policy).String() 83 expectedType := reflect.TypeOf(expected).String() 84 85 if policyType != expectedType { 86 t.Errorf("expected %q to match %q", policyType, expectedType) 87 } 88 }