зеркало из https://github.com/Azure/ARO-RP.git
Test hive create namespace (#2327)
This commit is contained in:
Родитель
ca87817010
Коммит
6dcf13b024
|
@ -13,8 +13,11 @@ import (
|
|||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
kruntime "k8s.io/apimachinery/pkg/runtime"
|
||||
kubernetesfake "k8s.io/client-go/kubernetes/fake"
|
||||
|
||||
"github.com/Azure/ARO-RP/pkg/util/cmp"
|
||||
"github.com/Azure/ARO-RP/pkg/util/uuid"
|
||||
"github.com/Azure/ARO-RP/pkg/util/uuid/fake"
|
||||
)
|
||||
|
||||
func TestIsClusterDeploymentReady(t *testing.T) {
|
||||
|
@ -158,3 +161,63 @@ func TestResetCorrelationData(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateNamespace(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
nsNames []string
|
||||
useFakeGenerator bool
|
||||
shouldFail bool
|
||||
}{
|
||||
{
|
||||
name: "not conflict names and real generator",
|
||||
nsNames: []string{"namespace1", "namespace2"},
|
||||
useFakeGenerator: false,
|
||||
shouldFail: false,
|
||||
},
|
||||
{
|
||||
name: "conflict names and real generator",
|
||||
nsNames: []string{"namespace", "namespace"},
|
||||
useFakeGenerator: false,
|
||||
shouldFail: false,
|
||||
},
|
||||
{
|
||||
name: "not conflict names and fake generator",
|
||||
nsNames: []string{"namespace1", "namespace2"},
|
||||
useFakeGenerator: true,
|
||||
shouldFail: false,
|
||||
},
|
||||
{
|
||||
name: "conflict names and fake generator",
|
||||
nsNames: []string{"namespace", "namespace"},
|
||||
useFakeGenerator: true,
|
||||
shouldFail: true,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fakeClientset := kubernetesfake.NewSimpleClientset()
|
||||
c := clusterManager{
|
||||
kubernetescli: fakeClientset,
|
||||
}
|
||||
|
||||
if tc.useFakeGenerator {
|
||||
uuid.DefaultGenerator = fake.NewGenerator(tc.nsNames)
|
||||
}
|
||||
|
||||
ns, err := c.CreateNamespace(context.Background())
|
||||
if err != nil && !tc.shouldFail {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
res, err := fakeClientset.CoreV1().Namespaces().Get(context.Background(), ns.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(ns, res) {
|
||||
t.Errorf("results are not equal: \n wanted: %+v \n got %+v", ns, res)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package fake
|
||||
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the Apache License 2.0.
|
||||
|
||||
import (
|
||||
"github.com/Azure/ARO-RP/pkg/util/uuid"
|
||||
)
|
||||
|
||||
type fakeGenerator struct {
|
||||
words []string
|
||||
currentPos int
|
||||
}
|
||||
|
||||
func NewGenerator(predefinedWords []string) uuid.Generator {
|
||||
return &fakeGenerator{
|
||||
words: predefinedWords,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fakeGenerator) movePos() {
|
||||
f.currentPos++
|
||||
}
|
||||
|
||||
func (f fakeGenerator) Generate() string {
|
||||
defer f.movePos()
|
||||
if len(f.words) < f.currentPos {
|
||||
return ""
|
||||
}
|
||||
return f.words[f.currentPos]
|
||||
}
|
Загрузка…
Ссылка в новой задаче