github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/security_group_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/ccv3fakes"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     9  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    10  	"code.cloudfoundry.org/cli/resources"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("SecurityGroup", func() {
    17  	var (
    18  		client    *Client
    19  		requester *ccv3fakes.FakeRequester
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		requester = new(ccv3fakes.FakeRequester)
    24  		client, _ = NewFakeRequesterTestClient(requester)
    25  	})
    26  
    27  	Describe("CreateSecurityGroup", func() {
    28  		var (
    29  			securityGroupName    string
    30  			securityGroupParams  resources.SecurityGroup
    31  			createdSecurityGroup resources.SecurityGroup
    32  			warnings             Warnings
    33  			executeErr           error
    34  		)
    35  
    36  		BeforeEach(func() {
    37  			securityGroupName = "some-group-name"
    38  			requester.MakeRequestCalls(func(requestParams RequestParams) (JobURL, Warnings, error) {
    39  				requestParams.ResponseBody.(*resources.SecurityGroup).GUID = "some-guid"
    40  				return "", Warnings{"some-warning"}, errors.New("some-error")
    41  			})
    42  			securityGroupParams = resources.SecurityGroup{
    43  				Name: securityGroupName,
    44  				Rules: []resources.Rule{
    45  					{
    46  						Protocol:    "tcp",
    47  						Destination: "10.0.10.0/24",
    48  					},
    49  				},
    50  			}
    51  		})
    52  
    53  		JustBeforeEach(func() {
    54  			createdSecurityGroup, warnings, executeErr = client.CreateSecurityGroup(securityGroupParams)
    55  		})
    56  
    57  		It("makes the correct request", func() {
    58  			Expect(requester.MakeRequestCallCount()).To(Equal(1))
    59  			actualParams := requester.MakeRequestArgsForCall(0)
    60  			Expect(actualParams.RequestName).To(Equal(internal.PostSecurityGroupRequest))
    61  			Expect(actualParams.RequestBody).To(Equal(securityGroupParams))
    62  			Expect(actualParams.ResponseBody).To(HaveTypeOf(&resources.SecurityGroup{}))
    63  		})
    64  
    65  		It("returns the given role and all warnings", func() {
    66  			Expect(createdSecurityGroup).To(Equal(resources.SecurityGroup{GUID: "some-guid"}))
    67  			Expect(warnings).To(ConsistOf("some-warning"))
    68  			Expect(executeErr).To(MatchError("some-error"))
    69  		})
    70  	})
    71  
    72  	Describe("GetSecurityGroups", func() {
    73  		var (
    74  			returnedSecurityGroups []resources.SecurityGroup
    75  			query                  = Query{}
    76  			warnings               Warnings
    77  			executeErr             error
    78  		)
    79  
    80  		BeforeEach(func() {
    81  			requester.MakeListRequestCalls(func(requestParams RequestParams) (IncludedResources, Warnings, error) {
    82  				err := requestParams.AppendToList(resources.SecurityGroup{Name: "security-group-name-1", GUID: "security-group-guid-1"})
    83  				Expect(err).NotTo(HaveOccurred())
    84  				return IncludedResources{}, Warnings{"some-warning"}, errors.New("some-error")
    85  			})
    86  		})
    87  
    88  		JustBeforeEach(func() {
    89  			returnedSecurityGroups, warnings, executeErr = client.GetSecurityGroups(query)
    90  		})
    91  
    92  		It("makes the correct request", func() {
    93  			Expect(requester.MakeListRequestCallCount()).To(Equal(1))
    94  			params := requester.MakeListRequestArgsForCall(0)
    95  
    96  			Expect(params.RequestName).To(Equal(internal.GetSecurityGroupsRequest))
    97  			Expect(params.Query).To(Equal([]Query{query}))
    98  			Expect(params.ResponseBody).To(Equal(resources.SecurityGroup{}))
    99  		})
   100  
   101  		It("returns the resources and all warnings", func() {
   102  			Expect(warnings).To(ConsistOf("some-warning"))
   103  			Expect(executeErr).To(MatchError("some-error"))
   104  			Expect(returnedSecurityGroups).To(Equal([]resources.SecurityGroup{{
   105  				GUID: "security-group-guid-1",
   106  				Name: "security-group-name-1",
   107  			}}))
   108  		})
   109  	})
   110  
   111  	Describe("GetRunningSecurityGroups", func() {
   112  		var (
   113  			spaceGUID              = "some-space-guid"
   114  			returnedSecurityGroups []resources.SecurityGroup
   115  			query                  Query
   116  			warnings               Warnings
   117  			executeErr             error
   118  		)
   119  
   120  		BeforeEach(func() {
   121  			requester.MakeListRequestCalls(func(requestParams RequestParams) (IncludedResources, Warnings, error) {
   122  				err := requestParams.AppendToList(resources.SecurityGroup{Name: "security-group-name-1", GUID: "security-group-guid-1"})
   123  				Expect(err).NotTo(HaveOccurred())
   124  				return IncludedResources{}, Warnings{"some-warning"}, errors.New("some-error")
   125  			})
   126  		})
   127  
   128  		JustBeforeEach(func() {
   129  			returnedSecurityGroups, warnings, executeErr = client.GetRunningSecurityGroups(spaceGUID, query)
   130  		})
   131  
   132  		It("makes the correct request", func() {
   133  			Expect(requester.MakeListRequestCallCount()).To(Equal(1))
   134  			params := requester.MakeListRequestArgsForCall(0)
   135  
   136  			Expect(params.RequestName).To(Equal(internal.GetSpaceRunningSecurityGroupsRequest))
   137  			Expect(params.URIParams).To(Equal(internal.Params{"space_guid": spaceGUID}))
   138  			Expect(params.Query).To(Equal([]Query{query}))
   139  			Expect(params.ResponseBody).To(Equal(resources.SecurityGroup{}))
   140  		})
   141  
   142  		It("returns the resources and all warnings", func() {
   143  			Expect(executeErr).To(MatchError("some-error"))
   144  			Expect(warnings).To(Equal(Warnings{"some-warning"}))
   145  			Expect(returnedSecurityGroups).To(Equal([]resources.SecurityGroup{{
   146  				GUID: "security-group-guid-1",
   147  				Name: "security-group-name-1",
   148  			}}))
   149  		})
   150  	})
   151  
   152  	Describe("GetStagingSecurityGroups", func() {
   153  		var (
   154  			spaceGUID              = "some-space-guid"
   155  			returnedSecurityGroups []resources.SecurityGroup
   156  			query                  Query
   157  			warnings               Warnings
   158  			executeErr             error
   159  		)
   160  
   161  		BeforeEach(func() {
   162  			requester.MakeListRequestCalls(func(requestParams RequestParams) (IncludedResources, Warnings, error) {
   163  				err := requestParams.AppendToList(resources.SecurityGroup{Name: "security-group-name-1", GUID: "security-group-guid-1"})
   164  				Expect(err).NotTo(HaveOccurred())
   165  				return IncludedResources{}, Warnings{"some-warning"}, errors.New("some-error")
   166  			})
   167  		})
   168  
   169  		JustBeforeEach(func() {
   170  			returnedSecurityGroups, warnings, executeErr = client.GetStagingSecurityGroups(spaceGUID, query)
   171  		})
   172  
   173  		It("makes the correct request", func() {
   174  			Expect(requester.MakeListRequestCallCount()).To(Equal(1))
   175  			params := requester.MakeListRequestArgsForCall(0)
   176  
   177  			Expect(params.RequestName).To(Equal(internal.GetSpaceStagingSecurityGroupsRequest))
   178  			Expect(params.URIParams).To(Equal(internal.Params{"space_guid": spaceGUID}))
   179  			Expect(params.Query).To(Equal([]Query{query}))
   180  			Expect(params.ResponseBody).To(Equal(resources.SecurityGroup{}))
   181  		})
   182  
   183  		It("returns the resources and all warnings", func() {
   184  			Expect(executeErr).To(MatchError("some-error"))
   185  			Expect(warnings).To(Equal(Warnings{"some-warning"}))
   186  			Expect(returnedSecurityGroups).To(Equal([]resources.SecurityGroup{{
   187  				GUID: "security-group-guid-1",
   188  				Name: "security-group-name-1",
   189  			}}))
   190  		})
   191  	})
   192  
   193  	Describe("UnbindSecurityGroupRunningSpace", func() {
   194  		var (
   195  			spaceGUID         = "some-space-guid"
   196  			securityGroupGUID = "some-security-group-guid"
   197  			warnings          Warnings
   198  			executeErr        error
   199  		)
   200  
   201  		BeforeEach(func() {
   202  			requester.MakeRequestReturns(JobURL(""), Warnings{"some-warning"}, errors.New("some-error"))
   203  		})
   204  
   205  		JustBeforeEach(func() {
   206  			warnings, executeErr = client.UnbindSecurityGroupRunningSpace(securityGroupGUID, spaceGUID)
   207  		})
   208  
   209  		It("makes the correct request", func() {
   210  			Expect(requester.MakeRequestCallCount()).To(Equal(1))
   211  			params := requester.MakeRequestArgsForCall(0)
   212  
   213  			Expect(params.RequestName).To(Equal(internal.DeleteSecurityGroupRunningSpaceRequest))
   214  			Expect(params.URIParams).To(Equal(internal.Params{
   215  				"security_group_guid": securityGroupGUID, "space_guid": spaceGUID,
   216  			}))
   217  		})
   218  
   219  		It("returns the resources and all warnings", func() {
   220  			Expect(executeErr).To(MatchError("some-error"))
   221  			Expect(warnings).To(Equal(Warnings{"some-warning"}))
   222  		})
   223  	})
   224  
   225  	Describe("UnbindSecurityGroupStagingSpace", func() {
   226  		var (
   227  			spaceGUID         = "some-space-guid"
   228  			securityGroupGUID = "some-security-group-guid"
   229  			warnings          Warnings
   230  			executeErr        error
   231  		)
   232  
   233  		BeforeEach(func() {
   234  			requester.MakeRequestReturns(JobURL(""), Warnings{"some-warning"}, errors.New("some-error"))
   235  		})
   236  
   237  		JustBeforeEach(func() {
   238  			warnings, executeErr = client.UnbindSecurityGroupStagingSpace(securityGroupGUID, spaceGUID)
   239  		})
   240  
   241  		It("makes the correct request", func() {
   242  			Expect(requester.MakeRequestCallCount()).To(Equal(1))
   243  			params := requester.MakeRequestArgsForCall(0)
   244  
   245  			Expect(params.RequestName).To(Equal(internal.DeleteSecurityGroupStagingSpaceRequest))
   246  			Expect(params.URIParams).To(Equal(internal.Params{
   247  				"security_group_guid": securityGroupGUID, "space_guid": spaceGUID,
   248  			}))
   249  		})
   250  
   251  		It("returns the resources and all warnings", func() {
   252  			Expect(executeErr).To(MatchError("some-error"))
   253  			Expect(warnings).To(Equal(Warnings{"some-warning"}))
   254  		})
   255  	})
   256  
   257  	Describe("UpdateSecurityGroupRunningSpace", func() {
   258  		var (
   259  			spaceGUIDs        = []string{"some-space-guid", "other-space-guid"}
   260  			securityGroupGUID = "some-security-group-guid"
   261  			warnings          Warnings
   262  			executeErr        error
   263  		)
   264  
   265  		BeforeEach(func() {
   266  			requester.MakeRequestReturns(JobURL(""), Warnings{"some-warning"}, errors.New("some-error"))
   267  		})
   268  
   269  		JustBeforeEach(func() {
   270  			warnings, executeErr = client.UpdateSecurityGroupRunningSpace(securityGroupGUID, spaceGUIDs)
   271  		})
   272  
   273  		It("makes the correct request", func() {
   274  			Expect(requester.MakeRequestCallCount()).To(Equal(1))
   275  			params := requester.MakeRequestArgsForCall(0)
   276  
   277  			Expect(params.RequestName).To(Equal(internal.PostSecurityGroupRunningSpaceRequest))
   278  			Expect(params.URIParams).To(Equal(internal.Params{"security_group_guid": securityGroupGUID}))
   279  			Expect(params.RequestBody).To(Equal(resources.RelationshipList{
   280  				GUIDs: spaceGUIDs,
   281  			}))
   282  		})
   283  
   284  		It("returns the resources and all warnings", func() {
   285  			Expect(executeErr).To(MatchError("some-error"))
   286  			Expect(warnings).To(Equal(Warnings{"some-warning"}))
   287  		})
   288  	})
   289  
   290  	Describe("UpdateSecurityGroupStagingSpace", func() {
   291  		var (
   292  			spaceGUIDs        = []string{"some-space-guid", "other-space-guid"}
   293  			securityGroupGUID = "some-security-group-guid"
   294  			warnings          Warnings
   295  			executeErr        error
   296  		)
   297  
   298  		BeforeEach(func() {
   299  			requester.MakeRequestReturns(JobURL(""), Warnings{"some-warning"}, errors.New("some-error"))
   300  		})
   301  
   302  		JustBeforeEach(func() {
   303  			warnings, executeErr = client.UpdateSecurityGroupStagingSpace(securityGroupGUID, spaceGUIDs)
   304  		})
   305  
   306  		It("makes the correct request", func() {
   307  			Expect(requester.MakeRequestCallCount()).To(Equal(1))
   308  			params := requester.MakeRequestArgsForCall(0)
   309  
   310  			Expect(params.RequestName).To(Equal(internal.PostSecurityGroupStagingSpaceRequest))
   311  			Expect(params.URIParams).To(Equal(internal.Params{"security_group_guid": securityGroupGUID}))
   312  			Expect(params.RequestBody).To(Equal(resources.RelationshipList{
   313  				GUIDs: spaceGUIDs,
   314  			}))
   315  		})
   316  
   317  		It("returns the resources and all warnings", func() {
   318  			Expect(executeErr).To(MatchError("some-error"))
   319  			Expect(warnings).To(Equal(Warnings{"some-warning"}))
   320  		})
   321  	})
   322  
   323  	Describe("UpdateSecurityGroup", func() {
   324  		var (
   325  			securityGroup         resources.SecurityGroup
   326  			returnedSecurityGroup resources.SecurityGroup
   327  			warnings              Warnings
   328  			executeErr            error
   329  			trueValue             = true
   330  		)
   331  
   332  		BeforeEach(func() {
   333  			requester.MakeRequestCalls(func(requestParams RequestParams) (JobURL, Warnings, error) {
   334  				requestParams.ResponseBody.(*resources.SecurityGroup).GUID = "returned-group-guid"
   335  				return "", Warnings{"some-warning"}, errors.New("some-error")
   336  			})
   337  
   338  			securityGroup = resources.SecurityGroup{
   339  				Name:                   "some-security-group-name",
   340  				GUID:                   "some-security-group-guid",
   341  				StagingGloballyEnabled: &trueValue,
   342  			}
   343  		})
   344  
   345  		JustBeforeEach(func() {
   346  			returnedSecurityGroup, warnings, executeErr = client.UpdateSecurityGroup(securityGroup)
   347  		})
   348  
   349  		It("makes the correct request", func() {
   350  			Expect(requester.MakeRequestCallCount()).To(Equal(1))
   351  			params := requester.MakeRequestArgsForCall(0)
   352  
   353  			Expect(params.RequestName).To(Equal(internal.PatchSecurityGroupRequest))
   354  			Expect(params.URIParams).To(Equal(internal.Params{"security_group_guid": securityGroup.GUID}))
   355  			Expect(params.RequestBody).To(Equal(resources.SecurityGroup{
   356  				StagingGloballyEnabled: &trueValue,
   357  			}))
   358  		})
   359  
   360  		It("returns the resource and all warnings", func() {
   361  			Expect(returnedSecurityGroup).To(Equal(resources.SecurityGroup{
   362  				GUID: "returned-group-guid",
   363  			}))
   364  			Expect(executeErr).To(MatchError("some-error"))
   365  			Expect(warnings).To(Equal(Warnings{"some-warning"}))
   366  		})
   367  	})
   368  
   369  	Describe("DeleteSecurityGroup", func() {
   370  		var (
   371  			securityGroupGUID = "some-security-group-guid"
   372  			jobURL            JobURL
   373  			warnings          Warnings
   374  			executeErr        error
   375  		)
   376  
   377  		BeforeEach(func() {
   378  			requester.MakeRequestReturns(JobURL("some-job-url"), Warnings{"some-warning"}, errors.New("some-error"))
   379  		})
   380  
   381  		JustBeforeEach(func() {
   382  			jobURL, warnings, executeErr = client.DeleteSecurityGroup(securityGroupGUID)
   383  		})
   384  
   385  		It("makes the correct request", func() {
   386  			Expect(requester.MakeRequestCallCount()).To(Equal(1))
   387  			params := requester.MakeRequestArgsForCall(0)
   388  
   389  			Expect(params.RequestName).To(Equal(internal.DeleteSecurityGroupRequest))
   390  			Expect(params.URIParams).To(Equal(internal.Params{"security_group_guid": securityGroupGUID}))
   391  		})
   392  
   393  		It("returns all warnings and a job url", func() {
   394  			Expect(executeErr).To(MatchError("some-error"))
   395  			Expect(warnings).To(Equal(Warnings{"some-warning"}))
   396  			Expect(jobURL).To(Equal(JobURL("some-job-url")))
   397  		})
   398  	})
   399  })