Skip to content

Commit

Permalink
fix: bscp get file命令下载指定文件失败 --bug=126653351
Browse files Browse the repository at this point in the history
  • Loading branch information
fireyun committed Jul 4, 2024
1 parent f323ef9 commit 7ee081e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
7 changes: 4 additions & 3 deletions bcs-services/bcs-bscp/pkg/tools/wildcard.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package tools

import (
"fmt"
"strings"

"github.com/gobwas/glob"
Expand All @@ -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]
Expand Down
78 changes: 78 additions & 0 deletions bcs-services/bcs-bscp/pkg/tools/wildcard_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 7ee081e

Please sign in to comment.