From a6159d10b51145837a116ee2ec56c60b454279c7 Mon Sep 17 00:00:00 2001 From: Ted Teng Date: Thu, 13 Oct 2022 14:49:14 +0800 Subject: [PATCH] unit test --- pkg/cmd/info/info_suite_test.go | 28 ++++++++ pkg/cmd/info/info_test.go | 116 ++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 pkg/cmd/info/info_suite_test.go create mode 100644 pkg/cmd/info/info_test.go diff --git a/pkg/cmd/info/info_suite_test.go b/pkg/cmd/info/info_suite_test.go new file mode 100644 index 00000000..08b3da6f --- /dev/null +++ b/pkg/cmd/info/info_suite_test.go @@ -0,0 +1,28 @@ +/* +SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Gardener contributors + +SPDX-License-Identifier: Apache-2.0 +*/ + +package info_test + +import ( + "testing" + + gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1" + operationsv1alpha1 "github.com/gardener/gardener/pkg/apis/operations/v1alpha1" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/client-go/kubernetes/scheme" +) + +func init() { + utilruntime.Must(gardencorev1beta1.AddToScheme(scheme.Scheme)) + utilruntime.Must(operationsv1alpha1.AddToScheme(scheme.Scheme)) +} + +func TestCommand(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Info Command Test Suite") +} diff --git a/pkg/cmd/info/info_test.go b/pkg/cmd/info/info_test.go new file mode 100644 index 00000000..1679a12e --- /dev/null +++ b/pkg/cmd/info/info_test.go @@ -0,0 +1,116 @@ +/* +SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Gardener contributors +SPDX-License-Identifier: Apache-2.0 +*/ + +package info_test + +import ( + "fmt" + + internalfake "github.com/gardener/gardenctl-v2/internal/fake" + "github.com/gardener/gardenctl-v2/internal/util" + cmdinfo "github.com/gardener/gardenctl-v2/pkg/cmd/info" + "github.com/gardener/gardenctl-v2/pkg/config" + "github.com/gardener/gardenctl-v2/pkg/target" + targetmocks "github.com/gardener/gardenctl-v2/pkg/target/mocks" + + gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1" + "github.com/golang/mock/gomock" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/pointer" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +var _ = Describe("Info Command", func() { + const ( + gardenName = "mygarden" + ) + + var ( + streams util.IOStreams + out *util.SafeBytesBuffer + factory *internalfake.Factory + targetProvider *internalfake.TargetProvider + ctrl *gomock.Controller + clientProvider *targetmocks.MockClientProvider + cfg *config.Config + gardenClient client.Client + testShootUnscheduled *gardencorev1beta1.Shoot + testShootAws *gardencorev1beta1.Shoot + testShootGCP *gardencorev1beta1.Shoot + ) + + BeforeEach(func() { + cfg = &config.Config{ + LinkKubeconfig: pointer.Bool(false), + Gardens: []config.Garden{{ + Name: gardenName, + }}, + } + + seedAws := "aws" + seedGcp := "gcp" + brokenShoot := "brokenShoot" + + testShootUnscheduled = &gardencorev1beta1.Shoot{ + ObjectMeta: metav1.ObjectMeta{Name: brokenShoot}, + Spec: gardencorev1beta1.ShootSpec{ + SeedName: nil, + }, + } + + testShootAws = &gardencorev1beta1.Shoot{ + ObjectMeta: metav1.ObjectMeta{Name: seedAws}, + Spec: gardencorev1beta1.ShootSpec{ + SeedName: &seedAws, + }, + } + + testShootGCP = &gardencorev1beta1.Shoot{ + ObjectMeta: metav1.ObjectMeta{Name: seedGcp}, + Spec: gardencorev1beta1.ShootSpec{ + SeedName: &seedGcp, + }, + } + + streams, _, out, _ = util.NewTestIOStreams() + + ctrl = gomock.NewController(GinkgoT()) + + clientProvider = targetmocks.NewMockClientProvider(ctrl) + targetProvider = internalfake.NewFakeTargetProvider(target.NewTarget(gardenName, "", "", "")) + + factory = internalfake.NewFakeFactory(cfg, nil, clientProvider, targetProvider) + }) + + JustBeforeEach(func() { + clientConfig, err := cfg.ClientConfig(gardenName) + Expect(err).ToNot(HaveOccurred()) + clientProvider.EXPECT().FromClientConfig(gomock.Eq(clientConfig)).Return(gardenClient, nil).AnyTimes() + }) + + AfterEach(func() { + ctrl.Finish() + }) + + Describe("RunE", func() { + BeforeEach(func() { + gardenClient = internalfake.NewClientWithObjects( + testShootUnscheduled, + testShootAws, + testShootGCP, + ) + }) + + It("should print info information", func() { + o := cmdinfo.NewInfoOptions(streams) + cmd := cmdinfo.NewCmdInfo(factory, o) + + Expect(cmd.RunE(cmd, nil)).To(Succeed()) + Expect(out.String()).To(Equal(fmt.Sprintf("Garden: mygarden\nSeed Total Active Hibernated Allocatable Capacity\n---- ----- ------ ---------- ----------- --------\n---- ----- ------ ---------- ----------- --------\nTOTAL 3 2 0 - -\nUnscheduled 1\nUnscheduled List brokenShoot\n\n"))) + }) + }) +})