github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/utils/cputil/provider_name_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 cputil 16 17 import ( 18 "testing" 19 ) 20 21 func TestGetScenarioAndCompNameFromProviderKey(t *testing.T) { 22 type args struct { 23 providerKey string 24 } 25 tests := []struct { 26 name string 27 args args 28 wantScenario string 29 wantCompName string 30 wantInstanceName string 31 haveErr bool 32 }{ 33 { 34 name: "issue-manage.content", 35 args: args{ 36 providerKey: "component-protocol.components.issue-manage.content", 37 }, 38 wantScenario: "issue-manage", 39 wantCompName: "content", 40 wantInstanceName: "content", 41 haveErr: false, 42 }, 43 { 44 name: "issue-manage.content@content2", 45 args: args{ 46 providerKey: "component-protocol.components.issue-manage.content@content2", 47 }, 48 wantScenario: "issue-manage", 49 wantCompName: "content", 50 wantInstanceName: "content2", 51 haveErr: false, 52 }, 53 { 54 name: "missing compName", 55 args: args{ 56 providerKey: "component-protocol.components.issue-manage", 57 }, 58 wantScenario: "", 59 wantCompName: "", 60 wantInstanceName: "", 61 haveErr: true, 62 }, 63 { 64 name: "invalid prefix", 65 args: args{ 66 providerKey: "xxx.components.issue-manage", 67 }, 68 wantScenario: "", 69 wantCompName: "", 70 wantInstanceName: "", 71 haveErr: true, 72 }, 73 { 74 name: "valid default namespace key", 75 args: args{ 76 providerKey: componentProviderDefaultNamespacePrefix + "filter", 77 }, 78 wantScenario: "", 79 wantCompName: "filter", 80 wantInstanceName: "filter", 81 haveErr: false, 82 }, 83 { 84 name: "valid default namespace key with label", 85 args: args{ 86 providerKey: componentProviderDefaultNamespacePrefix + "filter@filter2", 87 }, 88 wantScenario: "", 89 wantCompName: "filter", 90 wantInstanceName: "filter2", 91 haveErr: false, 92 }, 93 } 94 for _, tt := range tests { 95 t.Run(tt.name, func(t *testing.T) { 96 gotScenario, gotCompName, gotInstanceName, gotErr := GetScenarioAndCompNameFromProviderKey(tt.args.providerKey) 97 if gotScenario != tt.wantScenario { 98 t.Errorf("MustGetScenarioAndCompNameFromProviderKey() gotScenario = %v, want %v", gotScenario, tt.wantScenario) 99 } 100 if gotCompName != tt.wantCompName { 101 t.Errorf("MustGetScenarioAndCompNameFromProviderKey() gotCompName = %v, want %v", gotCompName, tt.wantCompName) 102 } 103 if gotInstanceName != tt.wantInstanceName { 104 t.Errorf("MustGetScenarioAndInstanceNameFromProviderKey() gotInstanceName = %v, want %v", gotInstanceName, tt.wantInstanceName) 105 } 106 if (tt.haveErr && gotErr == nil) || (!tt.haveErr && gotErr != nil) { 107 t.Errorf("MustGetScenarioAndCompNameFromProviderKey() getErr = %v, haveErr %v", gotErr, tt.haveErr) 108 } 109 }) 110 } 111 }