github.com/oam-dev/kubevela@v1.9.11/pkg/policy/replication_test.go (about) 1 /* 2 Copyright 2021 The KubeVela 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 policy 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 "k8s.io/apimachinery/pkg/runtime" 25 26 "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 27 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1" 28 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 29 "github.com/oam-dev/kubevela/pkg/oam/util" 30 ) 31 32 func TestReplicateComponents(t *testing.T) { 33 comp1 := common.ApplicationComponent{Name: "comp1"} 34 comp2 := common.ApplicationComponent{Name: "comp2"} 35 baseComponents := []common.ApplicationComponent{ 36 comp1, 37 comp2, 38 } 39 testCases := map[string]struct { 40 Components []common.ApplicationComponent 41 Selectors []string 42 Output []common.ApplicationComponent 43 WantErr error 44 }{ 45 "nil selector, don't replicate": { 46 Components: baseComponents, 47 Selectors: nil, 48 Output: nil, 49 WantErr: fmt.Errorf("no component selected to replicate"), 50 }, 51 "select all, replicate all": { 52 Components: baseComponents, 53 Selectors: []string{"comp1", "comp2"}, 54 Output: baseComponents, 55 }, 56 "replicate part": { 57 Components: baseComponents, 58 Selectors: []string{"comp1"}, 59 Output: []common.ApplicationComponent{comp1}, 60 }, 61 "part invalid selector": { 62 Components: baseComponents, 63 Selectors: []string{"comp1", "comp3"}, 64 Output: []common.ApplicationComponent{comp1}, 65 }, 66 "no component selected": { 67 Components: baseComponents, 68 Selectors: []string{"comp3"}, 69 Output: []common.ApplicationComponent{}, 70 WantErr: fmt.Errorf("no component selected for replicate"), 71 }, 72 } 73 for name, tc := range testCases { 74 t.Run(name, func(t *testing.T) { 75 result, err := selectReplicateComponents(tc.Components, tc.Selectors) 76 if tc.WantErr != nil { 77 assert.Error(t, err) 78 } else { 79 assert.NoError(t, err) 80 assert.Equal(t, len(result), len(tc.Output)) 81 assert.Equal(t, result, tc.Output) 82 } 83 }) 84 } 85 } 86 87 func TestGetReplicationComponents(t *testing.T) { 88 baseComps := []common.ApplicationComponent{ 89 {Name: "comp1"}, 90 {Name: "comp2"}, 91 } 92 PolicyName := "test-policy" 93 testCases := map[string]struct { 94 Policies []v1beta1.AppPolicy 95 Components []common.ApplicationComponent 96 WantErr error 97 WantComps []common.ApplicationComponent 98 }{ 99 "no replication policy, all components remain unchanged": { 100 Policies: []v1beta1.AppPolicy{ 101 { 102 Name: PolicyName, 103 Type: "foo", 104 Properties: nil, 105 }, 106 }, 107 Components: baseComps, 108 WantComps: baseComps, 109 }, 110 "one replication policy, replicate those components": { 111 Policies: []v1beta1.AppPolicy{ 112 { 113 Name: PolicyName, 114 Type: "replication", 115 Properties: util.Object2RawExtension(v1alpha1.ReplicationPolicySpec{ 116 Keys: []string{"replica-1", "replica-2"}, 117 Selector: []string{"comp1"}, 118 }), 119 }, 120 }, 121 Components: baseComps, 122 WantComps: []common.ApplicationComponent{ 123 {Name: "comp2"}, 124 {Name: "comp1", ReplicaKey: "replica-1"}, 125 {Name: "comp1", ReplicaKey: "replica-2"}, 126 }, 127 }, 128 "replicate non-exist component": { 129 Policies: []v1beta1.AppPolicy{ 130 { 131 Name: PolicyName, 132 Type: "replication", 133 Properties: util.Object2RawExtension(v1alpha1.ReplicationPolicySpec{ 134 Keys: []string{"replica-1", "replica-2"}, 135 Selector: []string{"comp-non-exist"}, 136 }), 137 }, 138 }, 139 Components: baseComps, 140 WantErr: fmt.Errorf("failed to apply replicate policy %s", PolicyName), 141 }, 142 "invalid-override-policy": { 143 Policies: []v1beta1.AppPolicy{ 144 { 145 Name: PolicyName, 146 Type: "replication", 147 Properties: &runtime.RawExtension{Raw: []byte(`{bad value}`)}, 148 }, 149 }, 150 Components: baseComps, 151 WantErr: fmt.Errorf("failed to parse replicate policy %s", PolicyName), 152 }, 153 } 154 for name, tc := range testCases { 155 t.Run(name, func(t *testing.T) { 156 comps, err := ReplicateComponents(tc.Policies, tc.Components) 157 if tc.WantErr != nil { 158 assert.Error(t, err) 159 assert.Contains(t, err.Error(), tc.WantErr.Error()) 160 } else { 161 assert.NoError(t, err) 162 assert.Equal(t, comps, tc.WantComps) 163 } 164 }) 165 } 166 }