github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/organization/organization_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package organization
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	"fmt"
    27  	"os"
    28  
    29  	"k8s.io/cli-runtime/pkg/genericiooptions"
    30  )
    31  
    32  type MockOrganization struct {
    33  	genericiooptions.IOStreams
    34  }
    35  
    36  func (m *MockOrganization) getOrganization(name string) (*OrgItem, error) {
    37  	return &OrgItem{
    38  		ID:          "test",
    39  		Name:        "test",
    40  		Role:        "test",
    41  		Description: "test",
    42  		DisplayName: "test",
    43  		CreatedAt:   "test",
    44  	}, nil
    45  }
    46  
    47  func (m *MockOrganization) GetOrganizations() (*Organizations, error) {
    48  	return &Organizations{
    49  		Items: []OrgItem{
    50  			{
    51  				ID:          "test",
    52  				Name:        "test",
    53  				Role:        "test",
    54  				Description: "test",
    55  				DisplayName: "test",
    56  				CreatedAt:   "test",
    57  			},
    58  		},
    59  	}, nil
    60  }
    61  
    62  func (m *MockOrganization) switchOrganization(name string) (string, error) {
    63  	fmt.Printf("switch to %s\n", name)
    64  	return "", nil
    65  }
    66  
    67  func (m *MockOrganization) getCurrentOrganization() (string, error) {
    68  	fmt.Printf("get current organization\n")
    69  	return "", nil
    70  }
    71  
    72  func (m *MockOrganization) addOrganization(body []byte) error {
    73  	fmt.Printf("add organization %s\n", string(body))
    74  	return nil
    75  }
    76  
    77  func (m *MockOrganization) deleteOrganization(name string) error {
    78  	fmt.Printf("delete organization %s\n", name)
    79  	return nil
    80  }
    81  
    82  func (m *MockOrganization) IsValidOrganization(name string) (bool, error) {
    83  	fmt.Printf("check organization %s\n", name)
    84  	return true, nil
    85  }
    86  
    87  var _ = Describe("Test Organization", func() {
    88  	var (
    89  		streams genericiooptions.IOStreams
    90  		o       *OrganizationOption
    91  	)
    92  	BeforeEach(func() {
    93  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    94  		o = &OrganizationOption{IOStreams: streams, Organization: &MockOrganization{}}
    95  		os.Setenv("TEST_ENV", "true")
    96  	})
    97  
    98  	AfterEach(func() {
    99  		defer os.Unsetenv("TEST_ENV")
   100  	})
   101  
   102  	Context("test organization", func() {
   103  		args := []string{"test", "test", "test"}
   104  
   105  		It("test organization list ", func() {
   106  			cmd := newOrgListCmd(streams)
   107  			Expect(o.complete(args)).Should(Succeed())
   108  			Expect(o.validate(cmd)).Should(Succeed())
   109  			Expect(o.runList()).Should(Succeed())
   110  		})
   111  
   112  		It("test organization switch ", func() {
   113  			cmd := newOrgListCmd(streams)
   114  			Expect(o.complete(args)).Should(Succeed())
   115  			Expect(o.validate(cmd)).Should(Succeed())
   116  			Expect(o.runSwitch()).Should(Succeed())
   117  		})
   118  
   119  		It("test organization current ", func() {
   120  			cmd := newOrgListCmd(streams)
   121  			Expect(o.complete(args)).Should(Succeed())
   122  			Expect(o.validate(cmd)).Should(Succeed())
   123  			Expect(o.runCurrent()).Should(Succeed())
   124  		})
   125  
   126  		It("test organization describe ", func() {
   127  			cmd := newOrgListCmd(streams)
   128  			Expect(o.complete(args)).Should(Succeed())
   129  			Expect(o.validate(cmd)).Should(Succeed())
   130  			Expect(o.runDescribe()).Should(Succeed())
   131  		})
   132  	})
   133  })