fix unit test timeout

Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
hongming
2021-03-25 11:28:13 +08:00
parent e6bbff1111
commit 4663f70ec4
2 changed files with 61 additions and 127 deletions

2
go.mod
View File

@@ -765,4 +765,4 @@ replace (
sigs.k8s.io/yaml => sigs.k8s.io/yaml v1.2.0 sigs.k8s.io/yaml => sigs.k8s.io/yaml v1.2.0
sourcegraph.com/sourcegraph/appdash => sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 sourcegraph.com/sourcegraph/appdash => sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0
vbom.ml/util => vbom.ml/util v0.0.0-20160121211510-db5cfe13f5cc vbom.ml/util => vbom.ml/util v0.0.0-20160121211510-db5cfe13f5cc
) )

View File

@@ -17,35 +17,23 @@ limitations under the License.
package loginrecord package loginrecord
import ( import (
"context"
"fmt" "fmt"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels" fakek8s "k8s.io/client-go/kubernetes/fake"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/klogr" clienttesting "k8s.io/client-go/testing"
"kubesphere.io/kubesphere/pkg/apis" "kubesphere.io/kubesphere/pkg/apis"
iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2" iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned" fakeks "kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
"kubesphere.io/kubesphere/pkg/client/informers/externalversions" "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
"os" "math/rand"
"path/filepath"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer" "sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"testing" "testing"
"time" "time"
) )
var testEnv *envtest.Environment
var k8sManager ctrl.Manager
func TestLoginRecordController(t *testing.T) { func TestLoginRecordController(t *testing.T) {
RegisterFailHandler(Fail) RegisterFailHandler(Fail)
RunSpecsWithDefaultAndCustomReporters(t, RunSpecsWithDefaultAndCustomReporters(t,
@@ -53,70 +41,54 @@ func TestLoginRecordController(t *testing.T) {
[]Reporter{printer.NewlineReporter{}}) []Reporter{printer.NewlineReporter{}})
} }
var _ = BeforeSuite(func(done Done) { func newLoginRecord(username string) *iamv1alpha2.LoginRecord {
logf.SetLogger(klogr.New()) return &iamv1alpha2.LoginRecord{
ObjectMeta: metav1.ObjectMeta{
By("bootstrapping test environment") Name: fmt.Sprintf("%s-%d", username, rand.Intn(1000000)),
t := true Labels: map[string]string{
if os.Getenv("TEST_USE_EXISTING_CLUSTER") == "true" { iamv1alpha2.UserReferenceLabel: username,
testEnv = &envtest.Environment{ },
UseExistingCluster: &t, CreationTimestamp: metav1.Now(),
} },
} else { Spec: iamv1alpha2.LoginRecordSpec{
testEnv = &envtest.Environment{ Type: iamv1alpha2.Token,
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds")}, Provider: "",
AttachControlPlaneOutput: false, Success: true,
} Reason: iamv1alpha2.AuthenticatedSuccessfully,
SourceIP: "",
UserAgent: "",
},
} }
}
cfg, err := testEnv.Start() func newUser(username string) *iamv1alpha2.User {
Expect(err).ToNot(HaveOccurred()) return &iamv1alpha2.User{
Expect(cfg).ToNot(BeNil()) ObjectMeta: metav1.ObjectMeta{Name: username},
}
err = apis.AddToScheme(scheme.Scheme) }
Expect(err).NotTo(HaveOccurred())
k8sManager, err = ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
MetricsBindAddress: "0",
})
Expect(err).ToNot(HaveOccurred())
k8sClient, err := kubernetes.NewForConfig(cfg)
Expect(err).NotTo(HaveOccurred())
ksClient, err := kubesphere.NewForConfig(cfg)
Expect(err).NotTo(HaveOccurred())
ksInformers := externalversions.NewSharedInformerFactory(ksClient, time.Second*30)
Expect(err).NotTo(HaveOccurred())
loginRecordInformer := ksInformers.Iam().V1alpha2().LoginRecords()
userInformer := ksInformers.Iam().V1alpha2().Users()
loginRecordController := NewLoginRecordController(k8sClient, ksClient, loginRecordInformer, userInformer, time.Hour, 1)
err = k8sManager.Add(loginRecordController)
Expect(err).NotTo(HaveOccurred())
go func() {
stopChan := ctrl.SetupSignalHandler()
ksInformers.Start(stopChan)
err = k8sManager.Start(stopChan)
Expect(err).ToNot(HaveOccurred())
}()
close(done)
}, 60)
var _ = Describe("LoginRecord", func() { var _ = Describe("LoginRecord", func() {
const timeout = time.Second * 30 var k8sClient *fakek8s.Clientset
const interval = time.Second * 1 var ksClient *fakeks.Clientset
var user *iamv1alpha2.User
var loginRecord *iamv1alpha2.LoginRecord
var controller *loginRecordController
var informers externalversions.SharedInformerFactory
BeforeEach(func() { BeforeEach(func() {
admin := &iamv1alpha2.User{ user = newUser("admin")
ObjectMeta: metav1.ObjectMeta{Name: "admin"}, loginRecord = newLoginRecord(user.Name)
} k8sClient = fakek8s.NewSimpleClientset()
Expect(k8sManager.GetClient().Create(context.Background(), admin, &client.CreateOptions{})).Should(Succeed()) ksClient = fakeks.NewSimpleClientset(loginRecord, user)
informers = externalversions.NewSharedInformerFactory(ksClient, 0)
loginRecordInformer := informers.Iam().V1alpha2().LoginRecords()
userInformer := informers.Iam().V1alpha2().Users()
err := loginRecordInformer.Informer().GetIndexer().Add(loginRecord)
Expect(err).Should(BeNil())
err = userInformer.Informer().GetIndexer().Add(user)
Expect(err).Should(BeNil())
err = apis.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
controller = NewLoginRecordController(k8sClient, ksClient, loginRecordInformer, userInformer, time.Hour, 1)
}) })
// Add Tests for OpenAPI validation (or additonal CRD features) specified in // Add Tests for OpenAPI validation (or additonal CRD features) specified in
@@ -125,59 +97,21 @@ var _ = Describe("LoginRecord", func() {
// test Kubernetes API server, which isn't the goal here. // test Kubernetes API server, which isn't the goal here.
Context("LoginRecord Controller", func() { Context("LoginRecord Controller", func() {
It("Should create successfully", func() { It("Should create successfully", func() {
ctx := context.Background()
username := "admin"
loginRecord := &iamv1alpha2.LoginRecord{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-1", username),
Labels: map[string]string{
iamv1alpha2.UserReferenceLabel: username,
},
},
Spec: iamv1alpha2.LoginRecordSpec{
Type: iamv1alpha2.Token,
Provider: "",
Success: true,
Reason: iamv1alpha2.AuthenticatedSuccessfully,
SourceIP: "",
UserAgent: "",
},
}
By("Expecting to create login record successfully") By("Expecting to reconcile successfully")
Expect(k8sManager.GetClient().Create(ctx, loginRecord, &client.CreateOptions{})).Should(Succeed()) err := controller.reconcile(loginRecord.Name)
Expect(err).Should(BeNil())
expected := &iamv1alpha2.LoginRecord{} By("Expecting to update user last login time successfully")
Eventually(func() bool { err = controller.reconcile(loginRecord.Name)
err := k8sManager.GetClient().Get(ctx, types.NamespacedName{Name: loginRecord.Name}, expected) Expect(err).Should(BeNil())
fmt.Print(err) actions := ksClient.Actions()
return !expected.CreationTimestamp.IsZero() Expect(len(actions)).Should(Equal(1))
}, timeout, interval).Should(BeTrue()) newObject := user.DeepCopy()
newObject.Status.LastLoginTime = &loginRecord.CreationTimestamp
loginRecord.Name = fmt.Sprintf("%s-2", username) updateAction := clienttesting.NewUpdateAction(iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralUser), "", newObject)
loginRecord.ResourceVersion = "" updateAction.Subresource = "status"
By("Expecting to create login record successfully") Expect(actions[0]).Should(Equal(updateAction))
Expect(k8sManager.GetClient().Create(ctx, loginRecord, &client.CreateOptions{})).Should(Succeed())
Eventually(func() bool {
k8sManager.GetClient().Get(ctx, types.NamespacedName{Name: loginRecord.Name}, expected)
return !expected.CreationTimestamp.IsZero()
}, timeout, interval).Should(BeTrue())
By("Expecting to limit login record successfully")
Eventually(func() bool {
loginRecordList := &iamv1alpha2.LoginRecordList{}
selector := labels.SelectorFromSet(labels.Set{iamv1alpha2.UserReferenceLabel: username})
k8sManager.GetClient().List(ctx, loginRecordList, &client.ListOptions{LabelSelector: selector})
return len(loginRecordList.Items) == 1
}, timeout, interval).Should(BeTrue())
}) })
}) })
}) })
var _ = AfterSuite(func() {
By("tearing down the test environment")
gexec.KillAndWait(5 * time.Second)
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})