github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/actor/v2action/space_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Space", func() {
    16  	Describe("SpaceNotFoundError#Error", func() {
    17  		Context("when the name is specified", func() {
    18  			It("returns an error message with the name of the missing space", func() {
    19  				err := actionerror.SpaceNotFoundError{
    20  					Name: "some-space",
    21  				}
    22  				Expect(err.Error()).To(Equal("Space 'some-space' not found."))
    23  			})
    24  		})
    25  
    26  		Context("when the name is not specified, but the GUID is specified", func() {
    27  			It("returns an error message with the GUID of the missing space", func() {
    28  				err := actionerror.SpaceNotFoundError{
    29  					GUID: "some-space-guid",
    30  				}
    31  				Expect(err.Error()).To(Equal("Space with GUID 'some-space-guid' not found."))
    32  			})
    33  		})
    34  
    35  		Context("when neither the name nor the GUID is specified", func() {
    36  			It("returns a generic error message for the missing space", func() {
    37  				err := actionerror.SpaceNotFoundError{}
    38  				Expect(err.Error()).To(Equal("Space '' not found."))
    39  			})
    40  		})
    41  	})
    42  
    43  	Describe("Actions", func() {
    44  		var (
    45  			actor                     *Actor
    46  			fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    47  		)
    48  
    49  		BeforeEach(func() {
    50  			fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    51  			actor = NewActor(fakeCloudControllerClient, nil, nil)
    52  		})
    53  
    54  		Describe("DeleteSpaceByNameAndOrganizationName", func() {
    55  			var (
    56  				warnings Warnings
    57  				err      error
    58  			)
    59  
    60  			JustBeforeEach(func() {
    61  				warnings, err = actor.DeleteSpaceByNameAndOrganizationName("some-space", "some-org")
    62  			})
    63  
    64  			Context("when the org is not found", func() {
    65  				BeforeEach(func() {
    66  					fakeCloudControllerClient.GetOrganizationsReturns(
    67  						[]ccv2.Organization{},
    68  						ccv2.Warnings{
    69  							"warning-1",
    70  							"warning-2",
    71  						},
    72  						nil,
    73  					)
    74  				})
    75  
    76  				It("returns an OrganizationNotFoundError", func() {
    77  					Expect(err).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
    78  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    79  				})
    80  			})
    81  
    82  			Context("when the org is found", func() {
    83  				BeforeEach(func() {
    84  					fakeCloudControllerClient.GetOrganizationsReturns(
    85  						[]ccv2.Organization{{Name: "some-org", GUID: "some-org-guid"}},
    86  						ccv2.Warnings{"warning-1", "warning-2"},
    87  						nil,
    88  					)
    89  				})
    90  
    91  				Context("when the space is not found", func() {
    92  					BeforeEach(func() {
    93  						fakeCloudControllerClient.GetSpacesReturns(
    94  							[]ccv2.Space{},
    95  							ccv2.Warnings{"warning-3", "warning-4"},
    96  							nil,
    97  						)
    98  					})
    99  
   100  					It("returns an SpaceNotFoundError", func() {
   101  						Expect(err).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"}))
   102  						Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4"))
   103  					})
   104  				})
   105  
   106  				Context("when the space is found", func() {
   107  					BeforeEach(func() {
   108  						fakeCloudControllerClient.GetSpacesReturns(
   109  							[]ccv2.Space{{GUID: "some-space-guid"}},
   110  							ccv2.Warnings{"warning-3", "warning-4"},
   111  							nil,
   112  						)
   113  					})
   114  
   115  					Context("when the delete returns an error", func() {
   116  						var expectedErr error
   117  
   118  						BeforeEach(func() {
   119  							expectedErr = errors.New("some delete space error")
   120  							fakeCloudControllerClient.DeleteSpaceJobReturns(
   121  								ccv2.Job{},
   122  								ccv2.Warnings{"warning-5", "warning-6"},
   123  								expectedErr,
   124  							)
   125  						})
   126  
   127  						It("returns the error", func() {
   128  							Expect(err).To(Equal(expectedErr))
   129  							Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6"))
   130  						})
   131  					})
   132  
   133  					Context("when the delete returns a job", func() {
   134  						BeforeEach(func() {
   135  							fakeCloudControllerClient.DeleteSpaceJobReturns(
   136  								ccv2.Job{GUID: "some-job-guid"},
   137  								ccv2.Warnings{"warning-5", "warning-6"},
   138  								nil,
   139  							)
   140  						})
   141  
   142  						Context("when polling errors", func() {
   143  							var expectedErr error
   144  
   145  							BeforeEach(func() {
   146  								expectedErr = errors.New("Never expected, by anyone")
   147  								fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"warning-7", "warning-8"}, expectedErr)
   148  							})
   149  
   150  							It("returns the error", func() {
   151  								Expect(err).To(Equal(expectedErr))
   152  								Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6", "warning-7", "warning-8"))
   153  							})
   154  						})
   155  
   156  						Context("when the job is successful", func() {
   157  							BeforeEach(func() {
   158  								fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"warning-7", "warning-8"}, nil)
   159  							})
   160  
   161  							It("returns warnings and no error", func() {
   162  								Expect(err).ToNot(HaveOccurred())
   163  								Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6", "warning-7", "warning-8"))
   164  
   165  								Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   166  								Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(Equal([]ccv2.Filter{{
   167  									Type:     constant.NameFilter,
   168  									Operator: constant.EqualOperator,
   169  									Values:   []string{"some-org"},
   170  								}}))
   171  
   172  								Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   173  								Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal([]ccv2.Filter{{
   174  									Type:     constant.NameFilter,
   175  									Operator: constant.EqualOperator,
   176  									Values:   []string{"some-space"},
   177  								},
   178  									{
   179  										Type:     constant.OrganizationGUIDFilter,
   180  										Operator: constant.EqualOperator,
   181  										Values:   []string{"some-org-guid"},
   182  									},
   183  								}))
   184  
   185  								Expect(fakeCloudControllerClient.DeleteSpaceJobCallCount()).To(Equal(1))
   186  								Expect(fakeCloudControllerClient.DeleteSpaceJobArgsForCall(0)).To(Equal("some-space-guid"))
   187  
   188  								Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   189  								Expect(fakeCloudControllerClient.PollJobArgsForCall(0)).To(Equal(ccv2.Job{GUID: "some-job-guid"}))
   190  							})
   191  						})
   192  					})
   193  				})
   194  			})
   195  		})
   196  
   197  		Describe("GetOrganizationSpaces", func() {
   198  			Context("when there are spaces in the org", func() {
   199  				BeforeEach(func() {
   200  					fakeCloudControllerClient.GetSpacesReturns(
   201  						[]ccv2.Space{
   202  							{
   203  								GUID:                     "space-1-guid",
   204  								Name:                     "space-1",
   205  								AllowSSH:                 true,
   206  								SpaceQuotaDefinitionGUID: "some-space-quota-guid",
   207  							},
   208  							{
   209  								GUID:     "space-2-guid",
   210  								Name:     "space-2",
   211  								AllowSSH: false,
   212  							},
   213  						},
   214  						ccv2.Warnings{"warning-1", "warning-2"},
   215  						nil)
   216  				})
   217  
   218  				It("returns all spaces and all warnings", func() {
   219  					spaces, warnings, err := actor.GetOrganizationSpaces("some-org-guid")
   220  
   221  					Expect(err).ToNot(HaveOccurred())
   222  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   223  					Expect(spaces).To(Equal(
   224  						[]Space{
   225  							{
   226  								GUID:                     "space-1-guid",
   227  								Name:                     "space-1",
   228  								AllowSSH:                 true,
   229  								SpaceQuotaDefinitionGUID: "some-space-quota-guid",
   230  							},
   231  							{
   232  								GUID:     "space-2-guid",
   233  								Name:     "space-2",
   234  								AllowSSH: false,
   235  							},
   236  						}))
   237  
   238  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   239  					Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal(
   240  						[]ccv2.Filter{
   241  							{
   242  								Type:     constant.OrganizationGUIDFilter,
   243  								Operator: constant.EqualOperator,
   244  								Values:   []string{"some-org-guid"},
   245  							},
   246  						}))
   247  				})
   248  			})
   249  
   250  			Context("when an error is encountered", func() {
   251  				var returnedErr error
   252  
   253  				BeforeEach(func() {
   254  					returnedErr = errors.New("cc-get-spaces-error")
   255  					fakeCloudControllerClient.GetSpacesReturns(
   256  						[]ccv2.Space{},
   257  						ccv2.Warnings{"warning-1", "warning-2"},
   258  						returnedErr,
   259  					)
   260  				})
   261  
   262  				It("returns the error and all warnings", func() {
   263  					_, warnings, err := actor.GetOrganizationSpaces("some-org-guid")
   264  
   265  					Expect(err).To(MatchError(returnedErr))
   266  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   267  				})
   268  			})
   269  		})
   270  
   271  		Describe("GetSpaceByOrganizationAndName", func() {
   272  			Context("when the space exists", func() {
   273  				BeforeEach(func() {
   274  					fakeCloudControllerClient.GetSpacesReturns(
   275  						[]ccv2.Space{
   276  							{
   277  								GUID:                     "some-space-guid",
   278  								Name:                     "some-space",
   279  								AllowSSH:                 true,
   280  								SpaceQuotaDefinitionGUID: "some-space-quota-guid",
   281  							},
   282  						},
   283  						ccv2.Warnings{"warning-1", "warning-2"},
   284  						nil)
   285  				})
   286  
   287  				It("returns the space and all warnings", func() {
   288  					space, warnings, err := actor.GetSpaceByOrganizationAndName("some-org-guid", "some-space")
   289  
   290  					Expect(err).ToNot(HaveOccurred())
   291  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   292  					Expect(space).To(Equal(Space{
   293  						GUID:                     "some-space-guid",
   294  						Name:                     "some-space",
   295  						AllowSSH:                 true,
   296  						SpaceQuotaDefinitionGUID: "some-space-quota-guid",
   297  					}))
   298  
   299  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   300  					Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(ConsistOf(
   301  						[]ccv2.Filter{
   302  							{
   303  								Type:     constant.OrganizationGUIDFilter,
   304  								Operator: constant.EqualOperator,
   305  								Values:   []string{"some-org-guid"},
   306  							},
   307  							{
   308  								Type:     constant.NameFilter,
   309  								Operator: constant.EqualOperator,
   310  								Values:   []string{"some-space"},
   311  							},
   312  						}))
   313  				})
   314  			})
   315  
   316  			Context("when an error is encountered", func() {
   317  				var returnedErr error
   318  
   319  				BeforeEach(func() {
   320  					returnedErr = errors.New("cc-get-spaces-error")
   321  					fakeCloudControllerClient.GetSpacesReturns(
   322  						[]ccv2.Space{},
   323  						ccv2.Warnings{"warning-1", "warning-2"},
   324  						returnedErr,
   325  					)
   326  				})
   327  
   328  				It("return the error and all warnings", func() {
   329  					_, warnings, err := actor.GetSpaceByOrganizationAndName("some-org-guid", "some-space")
   330  
   331  					Expect(err).To(MatchError(returnedErr))
   332  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   333  				})
   334  			})
   335  
   336  			Context("when the space does not exist", func() {
   337  				BeforeEach(func() {
   338  					fakeCloudControllerClient.GetSpacesReturns(
   339  						[]ccv2.Space{},
   340  						nil,
   341  						nil,
   342  					)
   343  				})
   344  
   345  				It("returns SpaceNotFoundError", func() {
   346  					_, _, err := actor.GetSpaceByOrganizationAndName("some-org-guid", "some-space")
   347  
   348  					Expect(err).To(MatchError(actionerror.SpaceNotFoundError{
   349  						Name: "some-space",
   350  					}))
   351  				})
   352  			})
   353  
   354  			Context("when multiple spaces exists", func() {
   355  				BeforeEach(func() {
   356  					fakeCloudControllerClient.GetSpacesReturns(
   357  						[]ccv2.Space{
   358  							{
   359  								GUID:     "some-space-guid",
   360  								Name:     "some-space",
   361  								AllowSSH: true,
   362  							},
   363  							{
   364  								GUID:     "another-space-guid",
   365  								Name:     "another-space",
   366  								AllowSSH: true,
   367  							},
   368  						},
   369  						nil,
   370  						nil,
   371  					)
   372  				})
   373  
   374  				It("returns MultipleSpacesFoundError", func() {
   375  					_, _, err := actor.GetSpaceByOrganizationAndName("some-org-guid", "some-space")
   376  
   377  					Expect(err).To(MatchError(actionerror.MultipleSpacesFoundError{
   378  						OrgGUID: "some-org-guid",
   379  						Name:    "some-space",
   380  					}))
   381  				})
   382  			})
   383  		})
   384  	})
   385  })