Files
kubevela/pkg/utils/config/project_test.go
Zheng Xi Zhou 58edb344f6 Feat: check whether a project matched a config's project (#3725)
* Feat: check whether a project matched a config's project

If the config project is not nil, it's matched whether the project
matched the target project.
If the config project is nil, the target project matched the config.

Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com>

* fix CI

Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com>

* fix CI

Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com>

* change imports order

Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com>

* change imported libraries order again

Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com>

* fix CI

Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com>
2022-04-22 17:53:41 +08:00

98 lines
1.9 KiB
Go

/*
Copyright 2022 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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 config
import (
"testing"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/oam-dev/kubevela/apis/types"
)
var ResponseString = "Hello HTTP Get."
func TestMatchProject(t *testing.T) {
s := runtime.NewScheme()
corev1.AddToScheme(s)
secret1 := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "s1",
Namespace: types.DefaultKubeVelaNS,
Labels: map[string]string{
types.LabelConfigProject: "p1",
},
},
}
secret2 := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "s2",
Namespace: types.DefaultKubeVelaNS,
Labels: map[string]string{
types.LabelConfigProject: "",
},
},
}
type args struct {
secret *corev1.Secret
project string
}
type want struct {
matched bool
}
testcases := []struct {
name string
args args
want want
}{
{
name: "matched",
args: args{
project: "p99",
secret: secret1,
},
want: want{
matched: false,
},
},
{
name: "not matched",
args: args{
project: "p99",
secret: secret2,
},
want: want{
matched: true,
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
got := ProjectMatched(tc.args.secret, tc.args.project)
assert.Equal(t, tc.want.matched, got)
})
}
}