github.com/erda-project/erda-infra@v1.0.9/base/servicehub/usage_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 "testing" 19 ) 20 21 func TestUsage(t *testing.T) { 22 provider1 := testDefine{ 23 "test1-provider", 24 &Spec{ 25 Services: []string{"test"}, 26 Description: "this is provider for test1", 27 ConfigFunc: func() interface{} { 28 return &struct { 29 Message string `file:"message" flag:"msg" default:"hi" desc:"message to show" env:"TEST_MESSAGE"` 30 }{} 31 }, 32 Creator: func() Provider { 33 return &struct{}{} 34 }, 35 }, 36 } 37 provider2 := testDefine{ 38 "test2-provider", 39 &Spec{ 40 Services: []string{"test"}, 41 Description: "this is provider for test2", 42 ConfigFunc: func() interface{} { 43 return &struct { 44 Name string `file:"name" flag:"name" default:"test" desc:"description for test" env:"TEST_NAME"` 45 }{} 46 }, 47 Creator: func() Provider { 48 return &struct{}{} 49 }, 50 }, 51 } 52 53 type args struct { 54 names []string 55 providers []testDefine 56 } 57 tests := []struct { 58 name string 59 args args 60 want string 61 }{ 62 { 63 name: "test1", 64 args: args{ 65 names: []string{"test1-provider"}, 66 providers: []testDefine{provider1}, 67 }, 68 want: `Service Providers: 69 test1-provider 70 this is provider for test1 71 file:"message" flag:"msg" env:"TEST_MESSAGE" default:"hi" , message to show 72 `, 73 }, 74 { 75 name: "test2", 76 args: args{ 77 names: []string{"test2-provider"}, 78 providers: []testDefine{provider2}, 79 }, 80 want: `Service Providers: 81 test2-provider 82 this is provider for test2 83 file:"name" flag:"name" env:"TEST_NAME" default:"test" , description for test 84 `, 85 }, 86 { 87 name: "all providers", 88 args: args{ 89 names: []string{"test1-provider", "test2-provider"}, 90 providers: []testDefine{ 91 provider1, provider2, 92 }, 93 }, 94 want: `Service Providers: 95 test1-provider 96 this is provider for test1 97 file:"message" flag:"msg" env:"TEST_MESSAGE" default:"hi" , message to show 98 test2-provider 99 this is provider for test2 100 file:"name" flag:"name" env:"TEST_NAME" default:"test" , description for test 101 `, 102 }, 103 } 104 for _, tt := range tests { 105 t.Run(tt.name, func(t *testing.T) { 106 for _, p := range tt.args.providers { 107 Register(p.name, p.spec) 108 } 109 if got := Usage(tt.args.names...); got != tt.want { 110 t.Errorf("Usage() = %v, want %v", got, tt.want) 111 } 112 for _, p := range tt.args.providers { 113 delete(serviceProviders, p.name) 114 } 115 }) 116 } 117 }