add ut for pkg client factory (#6371)

Signed-off-by: danfengl <danfengl@vmware.com>
pull/6373/head
danfengliu 2023-06-09 09:59:19 +08:00 committed by GitHub
parent 5a4f2abd4f
commit 114193ae3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 4 deletions

View File

@ -42,5 +42,5 @@ fi
# but the user and group don't exist inside the container, when the code(https://github.com/kubernetes-sigs/controller-runtime/blob/v0.10.2/pkg/internal/testing/addr/manager.go#L44) # but the user and group don't exist inside the container, when the code(https://github.com/kubernetes-sigs/controller-runtime/blob/v0.10.2/pkg/internal/testing/addr/manager.go#L44)
# tries to get the cache directory, it gets the directory "/" and then get the permission error when trying to create directory under "/". # tries to get the cache directory, it gets the directory "/" and then get the permission error when trying to create directory under "/".
# Specifying the cache directory by environment variable "XDG_CACHE_HOME" to workaround it # Specifying the cache directory by environment variable "XDG_CACHE_HOME" to workaround it
XDG_CACHE_HOME=/tmp/ go test -installsuffix "static" -short -timeout 60s -coverprofile=coverage.out "${TARGETS[@]}" XDG_CACHE_HOME=/tmp/ go test -installsuffix "static" -short -timeout 120s -coverprofile=coverage.out "${TARGETS[@]}"
echo "Success!" echo "Success!"

View File

@ -16,11 +16,16 @@ limitations under the License.
package client package client
import ( import (
"context"
"fmt"
"os" "os"
"strings"
"testing" "testing"
"github.com/spf13/pflag" flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
) )
// TestFactory tests the client.Factory interface. // TestFactory tests the client.Factory interface.
@ -40,7 +45,7 @@ func TestFactory(t *testing.T) {
// Argument should change the namespace // Argument should change the namespace
f = NewFactory("velero", make(map[string]interface{})) f = NewFactory("velero", make(map[string]interface{}))
s := "flag-velero" s := "flag-velero"
flags := new(pflag.FlagSet) flags := new(flag.FlagSet)
f.BindFlags(flags) f.BindFlags(flags)
@ -51,11 +56,91 @@ func TestFactory(t *testing.T) {
// An argument overrides the env variable if both are set. // An argument overrides the env variable if both are set.
os.Setenv("VELERO_NAMESPACE", "env-velero") os.Setenv("VELERO_NAMESPACE", "env-velero")
f = NewFactory("velero", make(map[string]interface{})) f = NewFactory("velero", make(map[string]interface{}))
flags = new(pflag.FlagSet) flags = new(flag.FlagSet)
f.BindFlags(flags) f.BindFlags(flags)
flags.Parse([]string{"--namespace", s}) flags.Parse([]string{"--namespace", s})
assert.Equal(t, s, f.Namespace()) assert.Equal(t, s, f.Namespace())
os.Unsetenv("VELERO_NAMESPACE") os.Unsetenv("VELERO_NAMESPACE")
tests := []struct {
name string
kubeconfig string
kubecontext string
QPS float32
burst int
baseName string
expectedHost string
}{
{
name: "Test flag setting in factory ClientConfig (test data #1)",
kubeconfig: "kubeconfig",
kubecontext: "federal-context",
QPS: 1.0,
burst: 1,
baseName: "bn-velero-1",
expectedHost: "https://horse.org:4443",
},
{
name: "Test flag setting in factory ClientConfig (test data #2)",
kubeconfig: "kubeconfig",
kubecontext: "queen-anne-context",
QPS: 200.0,
burst: 20,
baseName: "bn-velero-2",
expectedHost: "https://pig.org:443",
},
}
baseName := "velero-bn"
config, err := LoadConfig()
assert.Equal(t, err, nil)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
f = NewFactory(baseName, config)
f.SetClientBurst(test.burst)
f.SetClientQPS(test.QPS)
f.SetBasename(test.baseName)
flags = new(flag.FlagSet)
f.BindFlags(flags)
flags.Parse([]string{"--kubeconfig", test.kubeconfig, "--kubecontext", test.kubecontext})
clientConfig, _ := f.ClientConfig()
assert.Equal(t, test.expectedHost, clientConfig.Host)
assert.Equal(t, test.QPS, clientConfig.QPS)
assert.Equal(t, test.burst, clientConfig.Burst)
strings.Contains(clientConfig.UserAgent, test.baseName)
client, _ := f.Client()
_, e := client.Discovery().ServerGroups()
assert.Contains(t, e.Error(), fmt.Sprintf("Get \"%s/api?timeout=", test.expectedHost))
assert.NotNil(t, client)
kubeClient, _ := f.KubeClient()
group := kubeClient.NodeV1().RESTClient().APIVersion().Group
assert.NotNil(t, kubeClient)
assert.Equal(t, "node.k8s.io", group)
namespace := "ns1"
dynamicClient, _ := f.DynamicClient()
resource := &schema.GroupVersionResource{
Group: "group_test",
Version: "verion_test",
}
list, e := dynamicClient.Resource(*resource).Namespace(namespace).List(
context.Background(),
metav1.ListOptions{
LabelSelector: "none",
},
)
assert.Contains(t, e.Error(), fmt.Sprintf("Get \"%s/apis/%s/%s/namespaces/%s", test.expectedHost, resource.Group, resource.Version, namespace))
assert.Nil(t, list)
assert.NotNil(t, dynamicClient)
kubebuilderClient, e := f.KubebuilderClient()
assert.Contains(t, e.Error(), fmt.Sprintf("Get \"%s/api?timeout=", test.expectedHost))
assert.Nil(t, kubebuilderClient)
})
}
} }