github.com/erda-project/erda-infra@v1.0.9/base/servicehub/default_provider_define_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package servicehub 16 17 import ( 18 "reflect" 19 "testing" 20 ) 21 22 type testSpecDefine struct { 23 services []string 24 types []reflect.Type 25 dependencies []string 26 summary string 27 description string 28 } 29 30 func (d *testSpecDefine) Services() []string { return d.services } 31 func (d *testSpecDefine) Types() []reflect.Type { return d.types } 32 func (d *testSpecDefine) Summary() string { return d.summary } 33 func (d *testSpecDefine) Description() string { return d.description } 34 func (d *testSpecDefine) Dependencies(*Hub) []string { return d.dependencies } 35 36 func Test_specDefine_Services(t *testing.T) { 37 tests := []struct { 38 name string 39 spec *Spec 40 want []string 41 }{ 42 { 43 name: "empty", 44 spec: &Spec{}, 45 want: nil, 46 }, 47 { 48 name: "direct", 49 spec: &Spec{Services: []string{"test-service"}}, 50 want: []string{"test-service"}, 51 }, 52 { 53 name: "override", 54 spec: &Spec{ 55 Define: &testSpecDefine{services: []string{"test-service-2"}}, 56 }, 57 want: []string{"test-service-2"}, 58 }, 59 } 60 for _, tt := range tests { 61 t.Run(tt.name, func(t *testing.T) { 62 d := &specDefine{s: tt.spec} 63 if got := d.Services(); !reflect.DeepEqual(got, tt.want) { 64 t.Errorf("specDefine.Services() = %v, want %v", got, tt.want) 65 } 66 }) 67 } 68 } 69 70 func Test_specDefine_Types(t *testing.T) { 71 testType := reflect.TypeOf((*testSpecDefine)(nil)) 72 tests := []struct { 73 name string 74 spec *Spec 75 want []reflect.Type 76 }{ 77 { 78 name: "empty", 79 spec: &Spec{}, 80 want: nil, 81 }, 82 { 83 name: "direct", 84 spec: &Spec{Types: []reflect.Type{testType}}, 85 want: []reflect.Type{testType}, 86 }, 87 { 88 name: "override", 89 spec: &Spec{ 90 Define: &testSpecDefine{types: []reflect.Type{testType}}, 91 }, 92 want: []reflect.Type{testType}, 93 }, 94 } 95 for _, tt := range tests { 96 t.Run(tt.name, func(t *testing.T) { 97 d := &specDefine{s: tt.spec} 98 if got := d.Types(); !reflect.DeepEqual(got, tt.want) { 99 t.Errorf("specDefine.Types() = %v, want %v", got, tt.want) 100 } 101 }) 102 } 103 } 104 105 func Test_specDefine_Dependencies(t *testing.T) { 106 hub := &Hub{servicesMap: make(map[string][]*providerContext)} 107 hub.servicesMap["test-service"] = []*providerContext{nil} 108 hub.servicesMap["test-service-1"] = []*providerContext{nil} 109 tests := []struct { 110 name string 111 spec *Spec 112 hub *Hub 113 want []string 114 }{ 115 { 116 name: "empty", 117 spec: &Spec{}, 118 hub: hub, 119 want: nil, 120 }, 121 { 122 name: "dependencies", 123 spec: &Spec{Dependencies: []string{"test-service"}}, 124 hub: hub, 125 want: []string{"test-service"}, 126 }, 127 { 128 name: "optional dependencies", 129 spec: &Spec{ 130 OptionalDependencies: []string{"test-service", "test-service-3"}, 131 }, 132 hub: hub, 133 want: []string{"test-service"}, 134 }, 135 { 136 name: "merge dependencies", 137 spec: &Spec{ 138 Dependencies: []string{"test-service"}, 139 OptionalDependencies: []string{"test-service-1", "test-service-2"}, 140 }, 141 hub: hub, 142 want: []string{"test-service", "test-service-1"}, 143 }, 144 { 145 name: "override", 146 spec: &Spec{ 147 Define: &testSpecDefine{dependencies: []string{"test-service-override"}}, 148 }, 149 hub: hub, 150 want: []string{"test-service-override"}, 151 }, 152 } 153 for _, tt := range tests { 154 t.Run(tt.name, func(t *testing.T) { 155 d := &specDefine{s: tt.spec} 156 if got := d.Dependencies(tt.hub); !reflect.DeepEqual(got, tt.want) { 157 t.Errorf("specDefine.Dependencies() = %v, want %v", got, tt.want) 158 } 159 }) 160 } 161 } 162 163 func Test_specDefine_Summary(t *testing.T) { 164 tests := []struct { 165 name string 166 spec *Spec 167 want string 168 }{ 169 { 170 name: "empty", 171 spec: &Spec{}, 172 want: "", 173 }, 174 { 175 name: "direct", 176 spec: &Spec{ 177 Summary: "test-summary", 178 }, 179 want: "test-summary", 180 }, 181 { 182 name: "override", 183 spec: &Spec{ 184 Define: &testSpecDefine{summary: "test-summary-override"}, 185 }, 186 want: "test-summary-override", 187 }, 188 } 189 for _, tt := range tests { 190 t.Run(tt.name, func(t *testing.T) { 191 d := &specDefine{ 192 s: tt.spec, 193 } 194 if got := d.Summary(); got != tt.want { 195 t.Errorf("specDefine.Summary() = %v, want %v", got, tt.want) 196 } 197 }) 198 } 199 } 200 201 func Test_specDefine_Description(t *testing.T) { 202 tests := []struct { 203 name string 204 spec *Spec 205 want string 206 }{ 207 { 208 name: "empty", 209 spec: &Spec{}, 210 want: "", 211 }, 212 { 213 name: "direct", 214 spec: &Spec{ 215 Description: "test-description", 216 }, 217 want: "test-description", 218 }, 219 { 220 name: "override", 221 spec: &Spec{ 222 Define: &testSpecDefine{description: "test-description-override"}, 223 }, 224 want: "test-description-override", 225 }, 226 } 227 for _, tt := range tests { 228 t.Run(tt.name, func(t *testing.T) { 229 d := &specDefine{ 230 s: tt.spec, 231 } 232 if got := d.Description(); got != tt.want { 233 t.Errorf("specDefine.Description() = %v, want %v", got, tt.want) 234 } 235 }) 236 } 237 }