From 7ee081e4355d4c3f155f97a212075a12622d9f4c Mon Sep 17 00:00:00 2001 From: johnypeng Date: Thu, 4 Jul 2024 14:15:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20bscp=20get=20file=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=8C=87=E5=AE=9A=E6=96=87=E4=BB=B6=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=20--bug=3D126653351?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bcs-services/bcs-bscp/pkg/tools/wildcard.go | 7 +- .../bcs-bscp/pkg/tools/wildcard_test.go | 78 +++++++++++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 bcs-services/bcs-bscp/pkg/tools/wildcard_test.go diff --git a/bcs-services/bcs-bscp/pkg/tools/wildcard.go b/bcs-services/bcs-bscp/pkg/tools/wildcard.go index 5b0ef58ed2..541e135a03 100644 --- a/bcs-services/bcs-bscp/pkg/tools/wildcard.go +++ b/bcs-services/bcs-bscp/pkg/tools/wildcard.go @@ -13,6 +13,7 @@ package tools import ( + "fmt" "strings" "github.com/gobwas/glob" @@ -32,9 +33,9 @@ func MatchConfigItem(scope, path, name string) (bool, error) { // MatchAppConfigItem checks if the scope string matches the app and config item. func MatchAppConfigItem(scope, app, path, name string) (bool, error) { - arr := strings.Split(scope, "/") - if len(arr) < 2 { - return false, nil + arr := strings.SplitN(scope, "/", 2) + if len(arr) != 2 { + return false, fmt.Errorf("invalid scope %s for app %s, it can't be splited into 2 substrings by /", scope, app) } appPattern := arr[0] ciPattern := arr[1] diff --git a/bcs-services/bcs-bscp/pkg/tools/wildcard_test.go b/bcs-services/bcs-bscp/pkg/tools/wildcard_test.go new file mode 100644 index 0000000000..f34c0973bd --- /dev/null +++ b/bcs-services/bcs-bscp/pkg/tools/wildcard_test.go @@ -0,0 +1,78 @@ +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tools + +import ( + "testing" +) + +func TestMatchConfigItem(t *testing.T) { + tests := []struct { + scope string + path string + name string + want bool + }{ + {"foo/*", "foo", "bar", true}, + {"foo/*", "foo", "baz", true}, + {"foo/*", "bar", "baz", false}, + {"foo/bar", "foo", "bar", true}, + {"foo/bar", "foo", "baz", false}, + {"*/bar", "foo", "bar", true}, + {"*/foo/bar", "bar", "foo/bar", true}, + {"*/**", "bar", "foo/bar", true}, + {"**", "bar", "foo/bar", true}, + } + + for _, tt := range tests { + got, err := MatchConfigItem(tt.scope, tt.path, tt.name) + if err != nil { + t.Errorf("MatchConfigItem() error = %v", err) + continue + } + if got != tt.want { + t.Errorf("MatchConfigItem() = %v, want %v, tt:%+v", got, tt.want, tt) + } + } +} + +func TestMatchAppConfigItem(t *testing.T) { + tests := []struct { + scope string + app string + path string + name string + want bool + }{ + {"app1/*/bar", "app1", "foo", "bar", true}, + {"app1/*/baz", "app1", "foo", "bar", false}, + {"app1/foo/*", "app1", "foo", "bar", true}, + {"app2/foo/*", "app1", "foo", "bar", false}, + {"app1/*/bar", "app2", "foo", "bar", false}, + {"app1/*/foo/bar", "app1", "bar", "foo/bar", true}, + {"app1/foo/*/bar", "app1", "foo", "bar/bar", true}, + {"app1/foo/**", "app1", "foo", "bar/bar", true}, + {"app1/**", "app1", "foo", "bar/bar", true}, + } + + for _, tt := range tests { + got, err := MatchAppConfigItem(tt.scope, tt.app, tt.path, tt.name) + if err != nil { + t.Errorf("MatchAppConfigItem() error = %v", err) + continue + } + if got != tt.want { + t.Errorf("MatchAppConfigItem() = %v, want %v, tt:%+v", got, tt.want, tt) + } + } +}