refine group e2e test

Signed-off-by: Roland.Ma <rolandma@yunify.com>
This commit is contained in:
Roland.Ma
2021-03-15 02:54:31 +00:00
parent eaf937b15f
commit 8a1c453412
15 changed files with 540 additions and 33 deletions

View File

@@ -19,6 +19,7 @@ package framework
import (
"context"
"fmt"
"time"
"github.com/onsi/ginkgo" //nolint:stylecheck
"github.com/onsi/gomega"
@@ -29,11 +30,21 @@ import (
"k8s.io/client-go/rest"
"kubesphere.io/client-go/client"
"kubesphere.io/client-go/client/generic"
"kubesphere.io/client-go/restclient"
"kubesphere.io/kubesphere/pkg/apis"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/test/e2e/framework/workspace"
)
const (
// Using the same interval as integration should be fine given the
// minimal load that the apiserver is likely to be under.
PollInterval = 50 * time.Millisecond
// How long to try single API calls (like 'get' or 'list'). Used to prevent
// transient failures from failing tests.
DefaultSingleCallTimeout = 30 * time.Second
)
type Framework struct {
BaseName string
Workspace string
@@ -45,6 +56,7 @@ type Framework struct {
// that the implementation can vary without affecting tests.
type KubeSphereFramework interface {
GenericClient(userAgent string) client.Client
RestClient(userAgent string) *restclient.RestClient
KubeSphereSystemNamespace() string
// Name of the workspace for the current test to target
@@ -54,6 +66,8 @@ type KubeSphereFramework interface {
CreateNamespace(name string) string
// Get Names of the namespaces for the current test to target
GetNamespaceNames() []string
GetScheme() *runtime.Scheme
}
func NewKubeSphereFramework(baseName string) KubeSphereFramework {
@@ -65,6 +79,7 @@ func NewKubeSphereFramework(baseName string) KubeSphereFramework {
if err := scheme.AddToScheme(sch); err != nil {
Failf("unable add Kubernetes APIs to scheme: %v", err)
}
f := &Framework{
BaseName: baseName,
Scheme: sch,
@@ -97,7 +112,7 @@ func CreateTestWorkSpace(client client.Client, baseName string) string {
wspt.GenerateName = fmt.Sprintf("e2e-tests-%v-", baseName)
wspt, err := workspace.CreateWorkspace(client, wspt)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Created test namespace %s", wspt.Name))
ginkgo.By(fmt.Sprintf("Created test workspace %s", wspt.Name))
return wspt.Name
}
@@ -106,6 +121,7 @@ func (f *Framework) GetNamespaceNames() []string {
}
func (f *Framework) CreateNamespace(name string) string {
name = fmt.Sprintf("%s-%s", f.Workspace, name)
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
@@ -133,19 +149,32 @@ func (f *Framework) KubeSphereSystemNamespace() string {
func (f *Framework) GenericClient(userAgent string) client.Client {
ctx := TestContext
config := &rest.Config{
Host: "127.0.0.1:9090",
Username: "admin",
Password: "P@88w0rd",
}
if ctx.Host != "" {
config = &rest.Config{
Host: ctx.Host,
Username: ctx.Username,
Password: ctx.Password,
}
Host: ctx.Host,
Username: ctx.Username,
Password: ctx.Password,
}
rest.AddUserAgent(config, userAgent)
return generic.NewForConfigOrDie(config, client.Options{Scheme: f.Scheme})
}
func (f *Framework) RestClient(userAgent string) *restclient.RestClient {
ctx := TestContext
config := &rest.Config{
Host: ctx.Host,
Username: ctx.Username,
Password: ctx.Password,
}
c, err := restclient.NewForConfig(config)
if err != nil {
panic(err)
}
return c
}
func (f *Framework) GetScheme() *runtime.Scheme {
return f.Scheme
}

View File

@@ -41,7 +41,7 @@ func NewUser(name, globelRole string) *iamv1alpha2.User {
},
},
Spec: iamv1alpha2.UserSpec{
Email: fmt.Sprint("%v@kubesphere.io", name),
Email: fmt.Sprintf("%s@kubesphere.io", name),
EncryptedPassword: "P@88w0rd",
},
}

View File

@@ -0,0 +1,70 @@
/*
Copyright 2021 KubeSphere 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 iam
import (
"context"
"fmt"
"golang.org/x/oauth2"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"kubesphere.io/client-go/client"
"kubesphere.io/client-go/client/generic"
"kubesphere.io/client-go/restclient"
"kubesphere.io/kubesphere/test/e2e/framework"
)
//NewClient creates a new client with user authencation
func NewClient(s *runtime.Scheme, user, passsword string) (client.Client, error) {
ctx := framework.TestContext
token, err := getToken(ctx.Host, user, passsword)
if err != nil {
return nil, err
}
config := &rest.Config{
Host: ctx.Host,
BearerToken: token.AccessToken,
}
return generic.New(config, client.Options{Scheme: s})
}
func NewRestClient(user, passsword string) (*restclient.RestClient, error) {
ctx := framework.TestContext
token, err := getToken(ctx.Host, user, passsword)
if err != nil {
return nil, err
}
config := &rest.Config{
Host: ctx.Host,
BearerToken: token.AccessToken,
}
return restclient.NewForConfig(config)
}
func getToken(host, user, password string) (*oauth2.Token, error) {
config := &oauth2.Config{
Endpoint: oauth2.Endpoint{
TokenURL: fmt.Sprintf("%s/oauth/token", host),
AuthStyle: oauth2.AuthStyleInParams,
},
}
return config.PasswordCredentialsToken(context.TODO(), user, password)
}

View File

@@ -0,0 +1,35 @@
/*
Copyright 2021 KubeSphere 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 resource
import (
"context"
v1 "k8s.io/api/core/v1"
"kubesphere.io/client-go/client"
)
// ListPods gets the Pods by namespace. If the returned error is nil, the returned PodList is valid.
func ListPods(c client.Client, namespace string) (*v1.PodList, error) {
pods := &v1.PodList{}
err := c.List(context.TODO(), pods, &client.ListOptions{Namespace: namespace})
if err != nil {
return nil, err
}
return pods, nil
}

View File

@@ -19,6 +19,8 @@ package framework
import (
"flag"
"os"
"kubesphere.io/kubesphere/test/e2e/constant"
)
type TestContextType struct {
@@ -41,7 +43,22 @@ func registerFlags(t *TestContextType) {
var TestContext *TestContextType = &TestContextType{}
func setDefaultValue(t *TestContextType) {
if t.Host == "" {
t.Host = constant.LocalAPIServer
}
if t.Username == "" {
t.Username = constant.DefaultAdminUser
}
if t.Password == "" {
t.Password = constant.DefaultPassword
}
}
func ParseFlags() {
registerFlags(TestContext)
flag.Parse()
setDefaultValue(TestContext)
}