github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/revisions_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  	"strconv"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	. "code.cloudfoundry.org/cli/actor/v7action"
     9  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    12  	"code.cloudfoundry.org/cli/resources"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Revisions Actions", func() {
    18  	Describe("GetRevisionsByApplicationNameAndSpace", func() {
    19  		var (
    20  			actor                     *Actor
    21  			fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    22  			fakeConfig                *v7actionfakes.FakeConfig
    23  			appName                   string
    24  			spaceGUID                 string
    25  			fetchedRevisions          []resources.Revision
    26  			executeErr                error
    27  			warnings                  Warnings
    28  		)
    29  
    30  		BeforeEach(func() {
    31  			fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    32  			fakeConfig = new(v7actionfakes.FakeConfig)
    33  			actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, nil, nil, nil)
    34  			appName = "some-app"
    35  			spaceGUID = "space-guid"
    36  			fakeConfig.APIVersionReturns("3.86.0")
    37  		})
    38  
    39  		JustBeforeEach(func() {
    40  			fetchedRevisions, warnings, executeErr = actor.GetRevisionsByApplicationNameAndSpace(appName, spaceGUID)
    41  		})
    42  
    43  		When("finding the app fails", func() {
    44  			BeforeEach(func() {
    45  				fakeCloudControllerClient.GetApplicationsReturns(
    46  					nil,
    47  					ccv3.Warnings{"get-application-warning"},
    48  					errors.New("get-application-error"),
    49  				)
    50  			})
    51  
    52  			It("returns an executeError", func() {
    53  				Expect(executeErr).To(MatchError("get-application-error"))
    54  				Expect(warnings).To(ConsistOf("get-application-warning"))
    55  			})
    56  		})
    57  
    58  		When("finding the app succeeds", func() {
    59  			BeforeEach(func() {
    60  				fakeCloudControllerClient.GetApplicationsReturns(
    61  					[]resources.Application{{Name: "some-app", GUID: "some-app-guid"}},
    62  					ccv3.Warnings{"get-application-warning"},
    63  					nil,
    64  				)
    65  			})
    66  
    67  			When("getting the app revisions fails", func() {
    68  				BeforeEach(func() {
    69  					fakeCloudControllerClient.GetApplicationRevisionsReturns(
    70  						[]resources.Revision{},
    71  						ccv3.Warnings{"get-application-revisisions-warning"},
    72  						errors.New("get-application-revisions-error"),
    73  					)
    74  				})
    75  
    76  				It("returns an executeError", func() {
    77  					Expect(executeErr).To(MatchError("get-application-revisions-error"))
    78  					Expect(warnings).To(ConsistOf("get-application-warning", "get-application-revisisions-warning"))
    79  				})
    80  			})
    81  
    82  			When("getting the app revisions succeeds", func() {
    83  				BeforeEach(func() {
    84  					fakeCloudControllerClient.GetApplicationRevisionsReturns(
    85  						[]resources.Revision{
    86  							{GUID: "3", Deployable: true, Droplet: resources.Droplet{GUID: "droplet-guid-3"}},
    87  							{GUID: "2", Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-2"}},
    88  							{GUID: "1", Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-1"}},
    89  						},
    90  						ccv3.Warnings{"get-application-revisisions-warning"},
    91  						nil,
    92  					)
    93  				})
    94  
    95  				It("makes the API call to get the app revisions and returns all warnings and revisions in descending order", func() {
    96  					Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1), "GetApplications call count")
    97  					Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf(
    98  						ccv3.Query{Key: ccv3.NameFilter, Values: []string{appName}},
    99  						ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}},
   100  					))
   101  
   102  					Expect(fakeCloudControllerClient.GetApplicationRevisionsCallCount()).To(Equal(1), "GetApplicationRevisions call count")
   103  
   104  					appGuidArg, queryArg := fakeCloudControllerClient.GetApplicationRevisionsArgsForCall(0)
   105  					Expect(appGuidArg).To(Equal("some-app-guid"))
   106  					Expect(queryArg).To(Equal([]ccv3.Query{{Key: ccv3.OrderBy, Values: []string{"-created_at"}}}))
   107  
   108  					Expect(fakeConfig.APIVersionCallCount()).To(Equal(1), "APIVersion call count")
   109  
   110  					Expect(fetchedRevisions).To(Equal(
   111  						[]resources.Revision{
   112  							{GUID: "3", Deployable: true, Droplet: resources.Droplet{GUID: "droplet-guid-3"}},
   113  							{GUID: "2", Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-2"}},
   114  							{GUID: "1", Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-1"}},
   115  						}))
   116  					Expect(executeErr).ToNot(HaveOccurred())
   117  					Expect(warnings).To(ConsistOf("get-application-warning", "get-application-revisisions-warning"))
   118  				})
   119  
   120  				When("minimum supported version for CAPI returning deployable field is not met (< 3.86.0)", func() {
   121  					BeforeEach(func() {
   122  						fakeConfig.APIVersionReturns("3.85.0")
   123  
   124  						fakeCloudControllerClient.GetApplicationRevisionsReturns(
   125  							[]resources.Revision{
   126  								{GUID: "3", Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-3"}},
   127  								{GUID: "2", Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-2"}},
   128  								{GUID: "1", Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-1"}},
   129  							},
   130  							ccv3.Warnings{"get-application-revisisions-warning"},
   131  							nil,
   132  						)
   133  
   134  						fakeCloudControllerClient.GetDropletsReturns(
   135  							[]resources.Droplet{
   136  								{GUID: "droplet-guid-2", State: constant.DropletExpired},
   137  								{GUID: "droplet-guid-3", State: constant.DropletStaged},
   138  							},
   139  							nil,
   140  							nil,
   141  						)
   142  					})
   143  
   144  					It("fetches the deployments", func() {
   145  						Expect(fakeConfig.APIVersionCallCount()).To(Equal(1), "APIVersion call count")
   146  						Expect(fakeCloudControllerClient.GetDropletsCallCount()).To(Equal(1), "GetDroplets call count")
   147  						Expect(fakeCloudControllerClient.GetDropletsArgsForCall(0)).To(Equal(
   148  							[]ccv3.Query{{Key: ccv3.AppGUIDFilter, Values: []string{"some-app-guid"}}},
   149  						))
   150  						Expect(fetchedRevisions).To(Equal(
   151  							[]resources.Revision{
   152  								{GUID: "3", Deployable: true, Droplet: resources.Droplet{GUID: "droplet-guid-3"}},
   153  								{GUID: "2", Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-2"}},
   154  								{GUID: "1", Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-1"}},
   155  							}))
   156  						Expect(executeErr).ToNot(HaveOccurred())
   157  					})
   158  
   159  					When("fetching droplets fails", func() {
   160  						BeforeEach(func() {
   161  							fakeCloudControllerClient.GetDropletsReturns(
   162  								[]resources.Droplet{},
   163  								ccv3.Warnings{"get-droplets-warning"},
   164  								errors.New("get-droplets-error"),
   165  							)
   166  						})
   167  
   168  						It("returns the fetching droplets error", func() {
   169  							Expect(executeErr).To(MatchError("get-droplets-error"))
   170  							Expect(warnings).To(ConsistOf("get-application-warning", "get-application-revisisions-warning", "get-droplets-warning"))
   171  						})
   172  					})
   173  				})
   174  			})
   175  		})
   176  	})
   177  
   178  	Describe("GetRevisionByApplicationAndVersion", func() {
   179  		var (
   180  			actor                     *Actor
   181  			fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
   182  			fakeConfig                *v7actionfakes.FakeConfig
   183  			appGUID                   string
   184  			revisionVersion           int
   185  			executeErr                error
   186  			warnings                  Warnings
   187  			revision                  resources.Revision
   188  		)
   189  
   190  		BeforeEach(func() {
   191  			fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
   192  			fakeConfig = new(v7actionfakes.FakeConfig)
   193  			actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, nil, nil, nil)
   194  			appGUID = "some-app-guid"
   195  			revisionVersion = 1
   196  			fakeConfig.APIVersionReturns("3.86.0")
   197  		})
   198  
   199  		JustBeforeEach(func() {
   200  			revision, warnings, executeErr = actor.GetRevisionByApplicationAndVersion(appGUID, revisionVersion)
   201  		})
   202  
   203  		When("finding the revision succeeds", func() {
   204  			BeforeEach(func() {
   205  				fakeCloudControllerClient.GetApplicationRevisionsReturns(
   206  					[]resources.Revision{
   207  						{GUID: "revision-guid-1", Version: 1, Deployable: true},
   208  					},
   209  					ccv3.Warnings{"get-revisions-warning-1"},
   210  					nil,
   211  				)
   212  			})
   213  
   214  			It("returns the revision", func() {
   215  				expectedQuery := []ccv3.Query{
   216  					{Key: ccv3.VersionsFilter, Values: []string{strconv.Itoa(revisionVersion)}},
   217  					{Key: ccv3.PerPage, Values: []string{"2"}},
   218  					{Key: ccv3.Page, Values: []string{"1"}},
   219  				}
   220  				Expect(fakeCloudControllerClient.GetApplicationRevisionsCallCount()).To(Equal(1), "GetApplicationRevisions call count")
   221  				appGuid, query := fakeCloudControllerClient.GetApplicationRevisionsArgsForCall(0)
   222  				Expect(appGuid).To(Equal("some-app-guid"))
   223  				Expect(query).To(ConsistOf(expectedQuery))
   224  
   225  				Expect(fakeConfig.APIVersionCallCount()).To(Equal(1), "APIVersion call count")
   226  				Expect(fakeCloudControllerClient.GetDropletsCallCount()).To(Equal(0), "GetDroplets call count")
   227  
   228  				Expect(revision.Version).To(Equal(1))
   229  				Expect(revision.GUID).To(Equal("revision-guid-1"))
   230  				Expect(executeErr).ToNot(HaveOccurred())
   231  				Expect(warnings).To(ConsistOf("get-revisions-warning-1"))
   232  			})
   233  
   234  			When("minimum supported version for CAPI returning deployable field is not met (< 3.86.0)", func() {
   235  				BeforeEach(func() {
   236  					fakeConfig.APIVersionReturns("3.85.0")
   237  
   238  					fakeCloudControllerClient.GetApplicationRevisionsReturns(
   239  						[]resources.Revision{
   240  							{GUID: "revision-guid-1", Version: 1, Deployable: false, Droplet: resources.Droplet{GUID: "droplet-guid-1"}},
   241  						},
   242  						ccv3.Warnings{"get-revisions-warning-1"},
   243  						nil,
   244  					)
   245  
   246  					fakeCloudControllerClient.GetDropletsReturns(
   247  						[]resources.Droplet{
   248  							{GUID: "droplet-guid-1", State: constant.DropletStaged},
   249  						},
   250  						nil,
   251  						nil,
   252  					)
   253  				})
   254  
   255  				It("fills in deployable based on droplet status", func() {
   256  					expectedQuery := []ccv3.Query{
   257  						{Key: ccv3.VersionsFilter, Values: []string{strconv.Itoa(revisionVersion)}},
   258  						{Key: ccv3.PerPage, Values: []string{"2"}},
   259  						{Key: ccv3.Page, Values: []string{"1"}},
   260  					}
   261  					Expect(fakeCloudControllerClient.GetApplicationRevisionsCallCount()).To(Equal(1), "GetApplicationRevisions call count")
   262  					appGuid, query := fakeCloudControllerClient.GetApplicationRevisionsArgsForCall(0)
   263  					Expect(appGuid).To(Equal("some-app-guid"))
   264  					Expect(query).To(ConsistOf(expectedQuery))
   265  
   266  					Expect(fakeConfig.APIVersionCallCount()).To(Equal(1), "APIVersion call count")
   267  					Expect(fakeCloudControllerClient.GetDropletsCallCount()).To(Equal(1), "GetDroplets call count")
   268  					Expect(fakeCloudControllerClient.GetDropletsArgsForCall(0)).To(Equal(
   269  						[]ccv3.Query{{Key: ccv3.AppGUIDFilter, Values: []string{"some-app-guid"}}},
   270  					))
   271  
   272  					Expect(revision.Version).To(Equal(1))
   273  					Expect(revision.GUID).To(Equal("revision-guid-1"))
   274  					Expect(revision.Deployable).To(Equal(true))
   275  					Expect(executeErr).ToNot(HaveOccurred())
   276  					Expect(warnings).To(ConsistOf("get-revisions-warning-1"))
   277  				})
   278  
   279  				When("fetching droplets fails", func() {
   280  					BeforeEach(func() {
   281  						fakeCloudControllerClient.GetDropletsReturns(
   282  							[]resources.Droplet{},
   283  							ccv3.Warnings{"get-droplets-warning"},
   284  							errors.New("get-droplets-error"),
   285  						)
   286  					})
   287  
   288  					It("returns the fetching droplets error", func() {
   289  						Expect(executeErr).To(MatchError("get-droplets-error"))
   290  						Expect(warnings).To(ConsistOf("get-revisions-warning-1", "get-droplets-warning"))
   291  					})
   292  				})
   293  			})
   294  		})
   295  
   296  		When("no matching revision found", func() {
   297  
   298  			BeforeEach(func() {
   299  				fakeCloudControllerClient.GetApplicationRevisionsReturns(
   300  					[]resources.Revision{},
   301  					ccv3.Warnings{"get-revisions-warning-1"},
   302  					nil,
   303  				)
   304  			})
   305  			It("returns an error", func() {
   306  				Expect(executeErr).To(MatchError(actionerror.RevisionNotFoundError{Version: 1}))
   307  				Expect(warnings).To(ConsistOf("get-revisions-warning-1"))
   308  			})
   309  		})
   310  		When("more than one revision found", func() {
   311  
   312  			BeforeEach(func() {
   313  				fakeCloudControllerClient.GetApplicationRevisionsReturns(
   314  					[]resources.Revision{
   315  						{GUID: "revision-guid-1", Version: 1},
   316  						{GUID: "revision-guid-2", Version: 22},
   317  					},
   318  					ccv3.Warnings{"get-revisions-warning-1"},
   319  					nil,
   320  				)
   321  			})
   322  			It("returns an error", func() {
   323  				Expect(executeErr).To(MatchError(actionerror.RevisionAmbiguousError{Version: 1}))
   324  				Expect(warnings).To(ConsistOf("get-revisions-warning-1"))
   325  			})
   326  		})
   327  		When("finding the revision fails", func() {
   328  			BeforeEach(func() {
   329  				fakeCloudControllerClient.GetApplicationRevisionsReturns(nil, ccv3.Warnings{"get-application-warning"}, errors.New("get-application-error"))
   330  			})
   331  
   332  			It("returns an error", func() {
   333  				Expect(executeErr).To(MatchError("get-application-error"))
   334  				Expect(warnings).To(ConsistOf("get-application-warning"))
   335  			})
   336  		})
   337  	})
   338  
   339  	Describe("GetApplicationRevisionsDeployed", func() {
   340  		var (
   341  			actor                     *Actor
   342  			fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
   343  			appGUID                   string
   344  			fetchedRevisions          []resources.Revision
   345  			executeErr                error
   346  			warnings                  Warnings
   347  		)
   348  
   349  		BeforeEach(func() {
   350  			fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
   351  			actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
   352  			appGUID = "some-app-guid"
   353  		})
   354  
   355  		JustBeforeEach(func() {
   356  			fetchedRevisions, warnings, executeErr = actor.GetApplicationRevisionsDeployed(appGUID)
   357  		})
   358  
   359  		When("getting the app deployed revisions fails", func() {
   360  			BeforeEach(func() {
   361  				fakeCloudControllerClient.GetApplicationRevisionsDeployedReturns([]resources.Revision{}, ccv3.Warnings{"some-revisions-warnings"}, errors.New("some-revisions-executeError"))
   362  			})
   363  
   364  			It("returns an executeError", func() {
   365  				Expect(executeErr).To(MatchError("some-revisions-executeError"))
   366  				Expect(warnings).To(ConsistOf("some-revisions-warnings"))
   367  			})
   368  		})
   369  
   370  		When("getting the app revisions succeeds", func() {
   371  			BeforeEach(func() {
   372  				fakeCloudControllerClient.GetApplicationRevisionsDeployedReturns(
   373  					[]resources.Revision{
   374  						{GUID: "3"},
   375  						{GUID: "2"},
   376  						{GUID: "1"},
   377  					},
   378  					ccv3.Warnings{"some-revisions-warnings"},
   379  					nil,
   380  				)
   381  			})
   382  
   383  			It("makes the API call to get the app revisions and returns all warnings and revisions in descending order", func() {
   384  				Expect(fakeCloudControllerClient.GetApplicationRevisionsDeployedCallCount()).To(Equal(1))
   385  
   386  				appGuidArg := fakeCloudControllerClient.GetApplicationRevisionsDeployedArgsForCall(0)
   387  				Expect(appGuidArg).To(Equal("some-app-guid"))
   388  
   389  				Expect(fetchedRevisions).To(Equal(
   390  					[]resources.Revision{
   391  						{GUID: "3"},
   392  						{GUID: "2"},
   393  						{GUID: "1"},
   394  					}))
   395  				Expect(executeErr).ToNot(HaveOccurred())
   396  				Expect(warnings).To(ConsistOf("some-revisions-warnings"))
   397  			})
   398  		})
   399  	})
   400  })