github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/securitygroup/bind_running_security_group_test.go (about)

     1  package securitygroup_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/securitygroups/defaults/running/runningfakes"
     7  	"code.cloudfoundry.org/cli/cf/api/securitygroups/securitygroupsfakes"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    15  
    16  	"code.cloudfoundry.org/cli/cf/commandregistry"
    17  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("bind-running-security-group command", func() {
    23  	var (
    24  		ui                           *testterm.FakeUI
    25  		configRepo                   coreconfig.Repository
    26  		requirementsFactory          *requirementsfakes.FakeFactory
    27  		fakeSecurityGroupRepo        *securitygroupsfakes.FakeSecurityGroupRepo
    28  		fakeRunningSecurityGroupRepo *runningfakes.FakeSecurityGroupsRepo
    29  		deps                         commandregistry.Dependency
    30  	)
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.UI = ui
    34  		deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(fakeSecurityGroupRepo)
    35  		deps.RepoLocator = deps.RepoLocator.SetRunningSecurityGroupRepository(fakeRunningSecurityGroupRepo)
    36  		deps.Config = configRepo
    37  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("bind-running-security-group").SetDependency(deps, pluginCall))
    38  	}
    39  
    40  	BeforeEach(func() {
    41  		ui = &testterm.FakeUI{}
    42  		configRepo = testconfig.NewRepositoryWithDefaults()
    43  		requirementsFactory = new(requirementsfakes.FakeFactory)
    44  		fakeSecurityGroupRepo = new(securitygroupsfakes.FakeSecurityGroupRepo)
    45  		fakeRunningSecurityGroupRepo = new(runningfakes.FakeSecurityGroupsRepo)
    46  	})
    47  
    48  	runCommand := func(args ...string) bool {
    49  		return testcmd.RunCLICommand("bind-running-security-group", args, requirementsFactory, updateCommandDependency, false, ui)
    50  	}
    51  
    52  	Describe("requirements", func() {
    53  		It("fails when the user is not logged in", func() {
    54  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    55  			Expect(runCommand("name")).To(BeFalse())
    56  		})
    57  
    58  		It("fails with usage when a name is not provided", func() {
    59  			runCommand()
    60  			Expect(ui.Outputs()).To(ContainSubstrings(
    61  				[]string{"Incorrect Usage", "Requires", "argument"},
    62  			))
    63  		})
    64  	})
    65  
    66  	Context("when the user is logged in and provides the name of a group", func() {
    67  		BeforeEach(func() {
    68  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    69  			group := models.SecurityGroup{}
    70  			group.GUID = "being-a-guid"
    71  			group.Name = "security-group-name"
    72  			fakeSecurityGroupRepo.ReadReturns(group, nil)
    73  		})
    74  
    75  		JustBeforeEach(func() {
    76  			runCommand("security-group-name")
    77  		})
    78  
    79  		It("Describes what it is doing to the user", func() {
    80  			Expect(ui.Outputs()).To(ContainSubstrings(
    81  				[]string{"Binding", "security-group-name", "as", "my-user"},
    82  				[]string{"OK"},
    83  				[]string{"TIP: Changes will not apply to existing running applications until they are restarted."},
    84  			))
    85  		})
    86  
    87  		It("binds the group to the running group set", func() {
    88  			Expect(fakeSecurityGroupRepo.ReadArgsForCall(0)).To(Equal("security-group-name"))
    89  			Expect(fakeRunningSecurityGroupRepo.BindToRunningSetArgsForCall(0)).To(Equal("being-a-guid"))
    90  		})
    91  
    92  		Context("when binding the security group to the running set fails", func() {
    93  			BeforeEach(func() {
    94  				fakeRunningSecurityGroupRepo.BindToRunningSetReturns(errors.New("WOAH. I know kung fu"))
    95  			})
    96  
    97  			It("fails and describes the failure to the user", func() {
    98  				Expect(ui.Outputs()).To(ContainSubstrings(
    99  					[]string{"FAILED"},
   100  					[]string{"WOAH. I know kung fu"},
   101  				))
   102  			})
   103  		})
   104  
   105  		Context("when the security group with the given name cannot be found", func() {
   106  			BeforeEach(func() {
   107  				fakeSecurityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.New("Crème insufficiently brûlée'd"))
   108  			})
   109  
   110  			It("fails and tells the user that the security group does not exist", func() {
   111  				Expect(fakeRunningSecurityGroupRepo.BindToRunningSetCallCount()).To(Equal(0))
   112  				Expect(ui.Outputs()).To(ContainSubstrings(
   113  					[]string{"FAILED"},
   114  				))
   115  			})
   116  		})
   117  	})
   118  })