diff --git a/k8s/apis/k8s/v1alpha1/webhook_suite_test.go b/k8s/apis/k8s/v1alpha1/webhook_test.go similarity index 78% rename from k8s/apis/k8s/v1alpha1/webhook_suite_test.go rename to k8s/apis/k8s/v1alpha1/webhook_test.go index af8e64ee..cd9bb277 100644 --- a/k8s/apis/k8s/v1alpha1/webhook_suite_test.go +++ b/k8s/apis/k8s/v1alpha1/webhook_test.go @@ -16,13 +16,15 @@ import ( "context" "crypto/tls" "fmt" + "io" "net" "path/filepath" "testing" "time" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "github.com/avast/retry-go" + "github.com/stretchr/testify/assert" + "github.com/v6d-io/v6d/k8s/pkg/log" admissionv1beta1 "k8s.io/api/admission/v1beta1" //+kubebuilder:scaffold:imports @@ -47,45 +49,39 @@ var ( cancel context.CancelFunc ) -func TestAPIs(t *testing.T) { - RegisterFailHandler(Fail) - - RunSpecs(t, - "Webhook Suite") -} - -var _ = BeforeSuite(func() { +func Test_webhook(t *testing.T) { + var GinkgoWriter io.Writer logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) ctx, cancel = context.WithCancel(context.TODO()) - By("bootstrapping test environment") + log.Info("Bootstrapping test environment") testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")}, ErrorIfCRDPathMissing: false, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "..", "config", "webhook")}, + Paths: []string{filepath.Join("..", "..", "..", "config", "webhook", "manifests.yaml")}, }, } var err error // cfg is defined in this file globally. cfg, err = testEnv.Start() - Expect(err).NotTo(HaveOccurred()) - Expect(cfg).NotTo(BeNil()) + assert.NoError(t, err) + assert.NotNil(t, cfg) scheme := runtime.NewScheme() err = AddToScheme(scheme) - Expect(err).NotTo(HaveOccurred()) + assert.NoError(t, err) err = admissionv1beta1.AddToScheme(scheme) - Expect(err).NotTo(HaveOccurred()) + assert.NoError(t, err) //+kubebuilder:scaffold:scheme k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) - Expect(err).NotTo(HaveOccurred()) - Expect(k8sClient).NotTo(BeNil()) + assert.NoError(t, err) + assert.NotNil(t, k8sClient) // start webhook server using Manager webhookInstallOptions := &testEnv.WebhookInstallOptions @@ -97,26 +93,25 @@ var _ = BeforeSuite(func() { LeaderElection: false, MetricsBindAddress: "0", }) - Expect(err).NotTo(HaveOccurred()) + assert.NoError(t, err) err = (&Operation{}).SetupWebhookWithManager(mgr) - Expect(err).NotTo(HaveOccurred()) + assert.NoError(t, err) err = (&Sidecar{}).SetupWebhookWithManager(mgr) - Expect(err).NotTo(HaveOccurred()) + assert.NoError(t, err) err = (&Backup{}).SetupWebhookWithManager(mgr) - Expect(err).NotTo(HaveOccurred()) + assert.NoError(t, err) err = (&Recover{}).SetupWebhookWithManager(mgr) - Expect(err).NotTo(HaveOccurred()) + assert.NoError(t, err) //+kubebuilder:scaffold:webhook go func() { - defer GinkgoRecover() err = mgr.Start(ctx) - Expect(err).NotTo(HaveOccurred()) + assert.NoError(t, err) }() // wait for the webhook server to get ready @@ -126,7 +121,7 @@ var _ = BeforeSuite(func() { webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort, ) - Eventually(func() error { + err = retry.Do(func() error { conn, err := tls.DialWithDialer( dialer, "tcp", @@ -138,12 +133,12 @@ var _ = BeforeSuite(func() { } conn.Close() return nil - }).Should(Succeed()) -}, 60) + }) + assert.NoError(t, err) -var _ = AfterSuite(func() { cancel() - By("tearing down the test environment") - err := testEnv.Stop() - Expect(err).NotTo(HaveOccurred()) -}) + time.Sleep(1 * time.Second) + log.Info("tearing down the test environment") + err = testEnv.Stop() + assert.NoError(t, err) +} diff --git a/k8s/cmd/commands/client/ls_metadatas.go b/k8s/cmd/commands/client/ls_metadatas.go index e19b42ad..6c45af16 100644 --- a/k8s/cmd/commands/client/ls_metadatas.go +++ b/k8s/cmd/commands/client/ls_metadatas.go @@ -16,8 +16,6 @@ limitations under the License. package client import ( - "os" - "github.com/spf13/cobra" "github.com/v6d-io/v6d/k8s/cmd/commands/flags" "github.com/v6d-io/v6d/k8s/cmd/commands/util" @@ -89,10 +87,3 @@ func init() { flags.ApplyLsOpts(lsMetadatas) flags.ApplyOutputOpts(lsMetadatas) } - -func DisableStdout() *os.File { - stdout := os.Stdout - os.Stdout, _ = os.Open(os.DevNull) - - return stdout -} diff --git a/k8s/cmd/commands/deploy/deploy_recover_job_test.go b/k8s/cmd/commands/deploy/deploy_recover_job_test.go index 65adf9e4..c72183dc 100644 --- a/k8s/cmd/commands/deploy/deploy_recover_job_test.go +++ b/k8s/cmd/commands/deploy/deploy_recover_job_test.go @@ -17,7 +17,6 @@ package deploy import ( "context" - "fmt" "reflect" "testing" @@ -314,7 +313,6 @@ func Test_getRecoverObjectsFromTemplate_third(t *testing.T) { } for i := range got { if !reflect.DeepEqual(*got[i], *(tt.want)[i]) { - fmt.Println(*got[i]) t.Errorf("getRecoverObjectsFromTemplate() = %+v, want %+v", got, tt.want) } } diff --git a/k8s/cmd/commands/sidecar/inject_test.go b/k8s/cmd/commands/sidecar/inject_test.go index 03ea0439..6f3ba094 100644 --- a/k8s/cmd/commands/sidecar/inject_test.go +++ b/k8s/cmd/commands/sidecar/inject_test.go @@ -503,8 +503,6 @@ spec: return } if !reflect.DeepEqual(got, tt.want) { - fmt.Println(got) - fmt.Println(tt.want) t.Errorf("GetWorkloadObj() = %v, want %v", got, tt.want) } }) diff --git a/k8s/cmd/commands/util/format.go b/k8s/cmd/commands/util/format.go index 17f2a423..ac0ba586 100644 --- a/k8s/cmd/commands/util/format.go +++ b/k8s/cmd/commands/util/format.go @@ -29,10 +29,6 @@ func Examples(examples string) string { return formatter{examples}.trim().tab().indent().string } -func Docs(yamls string) string { - return formatter{yamls}.doc().string -} - type formatter struct { string } diff --git a/k8s/cmd/commands/util/usage/md_docs.go b/k8s/cmd/commands/util/usage/md_docs.go index 886c0837..63baa7c4 100644 --- a/k8s/cmd/commands/util/usage/md_docs.go +++ b/k8s/cmd/commands/util/usage/md_docs.go @@ -66,11 +66,6 @@ func printOptions(buf *bytes.Buffer, titleLevel string, cmd *cobra.Command, name return nil } -// GenMarkdown creates markdown output. -func GenMarkdown(cmd *cobra.Command, w io.Writer) error { - return GenMarkdownCustom(cmd, w, func(s string) string { return s }) -} - // GenMarkdownCustom creates custom markdown output. func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error { cmd.InitDefaultHelpCmd() diff --git a/k8s/controllers/k8s/suite_test.go b/k8s/controllers/k8s/controller_test.go similarity index 76% rename from k8s/controllers/k8s/suite_test.go rename to k8s/controllers/k8s/controller_test.go index 57f8c18f..5a9423ba 100644 --- a/k8s/controllers/k8s/suite_test.go +++ b/k8s/controllers/k8s/controller_test.go @@ -13,11 +13,12 @@ limitations under the License. package k8s import ( + "io" "path/filepath" "testing" + "time" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "github.com/stretchr/testify/assert" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" @@ -27,6 +28,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" k8sv1alpha1 "github.com/v6d-io/v6d/k8s/apis/k8s/v1alpha1" + "github.com/v6d-io/v6d/k8s/pkg/log" //+kubebuilder:scaffold:imports ) @@ -37,16 +39,11 @@ var cfg *rest.Config var k8sClient client.Client var testEnv *envtest.Environment -func TestAPIs(t *testing.T) { - RegisterFailHandler(Fail) - - RunSpecs(t, "Controller Suite") -} - -var _ = BeforeSuite(func() { +func Test_controller(t *testing.T) { + var GinkgoWriter io.Writer logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - By("bootstrapping test environment") + log.Info("Bootstrapping test environment") testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, ErrorIfCRDPathMissing: true, @@ -55,22 +52,20 @@ var _ = BeforeSuite(func() { var err error // cfg is defined in this file globally. cfg, err = testEnv.Start() - Expect(err).NotTo(HaveOccurred()) - Expect(cfg).NotTo(BeNil()) + assert.NoError(t, err) + assert.NotNil(t, cfg) err = k8sv1alpha1.AddToScheme(scheme.Scheme) - Expect(err).NotTo(HaveOccurred()) + assert.NoError(t, err) //+kubebuilder:scaffold:scheme k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) - Expect(err).NotTo(HaveOccurred()) - Expect(k8sClient).NotTo(BeNil()) + assert.NoError(t, err) + assert.NotNil(t, k8sClient) -}, 60) - -var _ = AfterSuite(func() { - By("tearing down the test environment") - err := testEnv.Stop() - Expect(err).NotTo(HaveOccurred()) -}) + time.Sleep(1 * time.Second) + log.Info("tearing down the test environment") + err = testEnv.Stop() + assert.NoError(t, err) +} diff --git a/k8s/go.mod b/k8s/go.mod index 8074bdd0..01935d1d 100644 --- a/k8s/go.mod +++ b/k8s/go.mod @@ -10,8 +10,6 @@ require ( github.com/ghodss/yaml v1.0.0 github.com/go-logr/logr v1.2.3 github.com/olekukonko/tablewriter v0.0.4 - github.com/onsi/ginkgo v1.16.5 - github.com/onsi/gomega v1.20.1 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.4.0 github.com/spf13/pflag v1.0.5 @@ -85,7 +83,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/nxadm/tail v1.4.8 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/selinux v1.10.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect @@ -129,7 +126,6 @@ require ( google.golang.org/protobuf v1.28.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect - gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/apiserver v0.25.10 // indirect