github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/rollback_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/api/cloudcontroller/ccv3/constant"
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
    11  	v7 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
    12  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/shared/sharedfakes"
    13  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
    14  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
    15  	"github.com/LukasHeimann/cloudfoundrycli/v8/types"
    16  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
    17  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	. "github.com/onsi/gomega/gbytes"
    21  )
    22  
    23  var _ = Describe("rollback Command", func() {
    24  	var (
    25  		app             string
    26  		binaryName      string
    27  		executeErr      error
    28  		fakeActor       *v7fakes.FakeActor
    29  		fakeConfig      *commandfakes.FakeConfig
    30  		fakeSharedActor *commandfakes.FakeSharedActor
    31  		input           *Buffer
    32  		testUI          *ui.UI
    33  
    34  		fakeAppStager *sharedfakes.FakeAppStager
    35  		cmd           v7.RollbackCommand
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		app = "some-app"
    40  		binaryName = "faceman"
    41  		fakeActor = new(v7fakes.FakeActor)
    42  		fakeAppStager = new(sharedfakes.FakeAppStager)
    43  		fakeConfig = new(commandfakes.FakeConfig)
    44  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    45  		input = NewBuffer()
    46  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    47  
    48  		revisions := []resources.Revision{
    49  			resources.Revision{Version: 2},
    50  			resources.Revision{Version: 1},
    51  		}
    52  
    53  		fakeActor.GetRevisionsByApplicationNameAndSpaceReturns(
    54  			revisions, v7action.Warnings{"warning-2"}, nil,
    55  		)
    56  
    57  		fakeConfig.BinaryNameReturns(binaryName)
    58  		fakeActor.GetCurrentUserReturns(configv3.User{Name: "steve"}, nil)
    59  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    60  			Name: "some-org",
    61  			GUID: "some-org-guid",
    62  		})
    63  
    64  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    65  			Name: "some-space",
    66  			GUID: "some-space-guid",
    67  		})
    68  
    69  		cmd = v7.RollbackCommand{
    70  			RequiredArgs: flag.AppName{AppName: app},
    71  			BaseCommand: v7.BaseCommand{
    72  				UI:          testUI,
    73  				Config:      fakeConfig,
    74  				Actor:       fakeActor,
    75  				SharedActor: fakeSharedActor,
    76  			},
    77  			Stager: fakeAppStager,
    78  		}
    79  	})
    80  
    81  	JustBeforeEach(func() {
    82  		executeErr = cmd.Execute(nil)
    83  	})
    84  
    85  	It("displays the experimental warning", func() {
    86  		Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    87  	})
    88  
    89  	When("checking target fails", func() {
    90  		BeforeEach(func() {
    91  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    92  		})
    93  
    94  		It("returns an error", func() {
    95  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    96  
    97  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    98  
    99  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   100  			Expect(checkTargetedOrg).To(BeTrue())
   101  			Expect(checkTargetedSpace).To(BeTrue())
   102  		})
   103  	})
   104  
   105  	When("the user is not logged in", func() {
   106  		var expectedErr error
   107  
   108  		BeforeEach(func() {
   109  			expectedErr = errors.New("some current user error")
   110  			fakeActor.GetCurrentUserReturns(configv3.User{}, expectedErr)
   111  		})
   112  
   113  		It("returns an error", func() {
   114  			Expect(executeErr).To(Equal(expectedErr))
   115  		})
   116  	})
   117  
   118  	When("failing to retrieve the app", func() {
   119  		BeforeEach(func() {
   120  			fakeActor.GetApplicationByNameAndSpaceReturns(resources.Application{}, v7action.Warnings{"warning-1", "warning-2"}, errors.New("oh no"))
   121  		})
   122  
   123  		It("returns an error and all warnings", func() {
   124  			Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1), "GetApplicationByNameAndSpace call count")
   125  			Expect(executeErr).To(MatchError("oh no"))
   126  
   127  			Expect(testUI.Err).To(Say("warning-1"))
   128  			Expect(testUI.Err).To(Say("warning-2"))
   129  		})
   130  	})
   131  
   132  	When("there is a failure fetching the revision", func() {
   133  		BeforeEach(func() {
   134  			fakeActor.GetRevisionByApplicationAndVersionReturns(resources.Revision{}, v7action.Warnings{"warning-1", "warning-2"}, errors.New("oh no"))
   135  		})
   136  
   137  		It("returns an error and all warnings", func() {
   138  			Expect(fakeActor.GetRevisionByApplicationAndVersionCallCount()).To(Equal(1), "GetRevisionByApplicationAndVersion call count")
   139  			Expect(executeErr).To(MatchError("oh no"))
   140  
   141  			Expect(testUI.Err).To(Say("warning-1"))
   142  			Expect(testUI.Err).To(Say("warning-2"))
   143  		})
   144  	})
   145  
   146  	When("the first revision is set as the rollback target", func() {
   147  		BeforeEach(func() {
   148  			cmd.Version = flag.Revision{NullInt: types.NullInt{Value: 1, IsSet: true}}
   149  		})
   150  
   151  		When("the app has at least one revision", func() {
   152  			BeforeEach(func() {
   153  				fakeActor.GetApplicationByNameAndSpaceReturns(
   154  					resources.Application{GUID: "123"},
   155  					v7action.Warnings{"app-warning-1"},
   156  					nil,
   157  				)
   158  
   159  				fakeActor.GetRevisionByApplicationAndVersionReturns(
   160  					resources.Revision{Version: 1, GUID: "some-1-guid"},
   161  					v7action.Warnings{"revision-warning-3"},
   162  					nil,
   163  				)
   164  
   165  				fakeAppStager.StartAppReturns(
   166  					nil,
   167  				)
   168  			})
   169  
   170  			It("fetches the app and revision revision", func() {
   171  				Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1), "GetApplicationByNameAndSpace call count")
   172  				appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   173  				Expect(appName).To(Equal(app))
   174  				Expect(spaceGUID).To(Equal("some-space-guid"))
   175  
   176  				Expect(fakeActor.GetRevisionByApplicationAndVersionCallCount()).To(Equal(1), "GetRevisionByApplicationAndVersion call count")
   177  				appGUID, version := fakeActor.GetRevisionByApplicationAndVersionArgsForCall(0)
   178  				Expect(appGUID).To(Equal("123"))
   179  				Expect(spaceGUID).To(Equal("some-space-guid"))
   180  				Expect(version).To(Equal(1))
   181  			})
   182  
   183  			When("the user passes the force flag", func() {
   184  				BeforeEach(func() {
   185  					cmd.Force = true
   186  				})
   187  
   188  				It("skips the prompt and executes the rollback", func() {
   189  					Expect(fakeAppStager.StartAppCallCount()).To(Equal(1), "GetStartApp call count")
   190  
   191  					application, revisionGUID, _, _, _, _, appAction := fakeAppStager.StartAppArgsForCall(0)
   192  					Expect(application.GUID).To(Equal("123"))
   193  					Expect(revisionGUID).To(Equal("some-1-guid"))
   194  					Expect(appAction).To(Equal(constant.ApplicationRollingBack))
   195  
   196  					Expect(testUI.Out).ToNot(Say("Rolling '%s' back to revision '1' will create a new revision. The new revision '3' will use the settings from revision '1'.", app))
   197  					Expect(testUI.Out).ToNot(Say("Are you sure you want to continue?"))
   198  
   199  					Expect(testUI.Out).To(Say("Rolling back to revision 1 for app some-app in org some-org / space some-space as steve..."))
   200  
   201  					Expect(testUI.Err).To(Say("warning-1"))
   202  					Expect(testUI.Err).To(Say("warning-2"))
   203  					Expect(testUI.Err).To(Say("warning-3"))
   204  
   205  					Expect(testUI.Out).To(Say("OK"))
   206  				})
   207  			})
   208  
   209  			When("user says yes to prompt", func() {
   210  				BeforeEach(func() {
   211  					_, err := input.Write([]byte("y\n"))
   212  					Expect(err).NotTo(HaveOccurred())
   213  				})
   214  
   215  				It("successfully executes the command and outputs warnings", func() {
   216  					Expect(fakeAppStager.StartAppCallCount()).To(Equal(1), "GetStartApp call count")
   217  
   218  					application, revisionGUID, _, _, _, _, appAction := fakeAppStager.StartAppArgsForCall(0)
   219  					Expect(application.GUID).To(Equal("123"))
   220  					Expect(revisionGUID).To(Equal("some-1-guid"))
   221  					Expect(appAction).To(Equal(constant.ApplicationRollingBack))
   222  
   223  					Expect(testUI.Out).To(Say("Rolling '%s' back to revision '1' will create a new revision. The new revision will use the settings from revision '1'.", app))
   224  					Expect(testUI.Out).To(Say("Are you sure you want to continue?"))
   225  					Expect(testUI.Out).To(Say("Rolling back to revision 1 for app some-app in org some-org / space some-space as steve..."))
   226  
   227  					Expect(testUI.Err).To(Say("warning-1"))
   228  					Expect(testUI.Err).To(Say("warning-2"))
   229  					Expect(testUI.Err).To(Say("warning-3"))
   230  
   231  					Expect(testUI.Out).To(Say("OK"))
   232  				})
   233  			})
   234  
   235  			When("user says no to prompt", func() {
   236  				BeforeEach(func() {
   237  					_, err := input.Write([]byte("n\n"))
   238  					Expect(err).NotTo(HaveOccurred())
   239  				})
   240  
   241  				It("does not execute the command and outputs warnings", func() {
   242  					Expect(fakeAppStager.StartAppCallCount()).To(Equal(0), "GetStartApp call count")
   243  
   244  					Expect(testUI.Out).To(Say("Rolling '%s' back to revision '1' will create a new revision. The new revision will use the settings from revision '1'.", app))
   245  					Expect(testUI.Out).To(Say("App '%s' has not been rolled back to revision '1'.", app))
   246  
   247  					Expect(testUI.Err).To(Say("app-warning-1"))
   248  					Expect(testUI.Err).To(Say("revision-warning-3"))
   249  				})
   250  			})
   251  
   252  			When("the user chooses the default", func() {
   253  				BeforeEach(func() {
   254  					_, err := input.Write([]byte("\n"))
   255  					Expect(err).NotTo(HaveOccurred())
   256  				})
   257  
   258  				It("cancels the rollback", func() {
   259  					Expect(executeErr).NotTo(HaveOccurred())
   260  
   261  					Expect(fakeAppStager.StartAppCallCount()).To(Equal(0), "GetStartApp call count")
   262  
   263  					Expect(testUI.Out).To(Say("App '%s' has not been rolled back to revision '1'.", app))
   264  				})
   265  			})
   266  		})
   267  	})
   268  })