From e85b25c743510b955e8a224d3dd12ed253d2f433 Mon Sep 17 00:00:00 2001 From: Marcela Campo Date: Thu, 5 Oct 2023 12:26:14 +0100 Subject: [PATCH] chore: refactor tests - Simplify tests while keeping all scenarios under test - Modify tests to have a consistent style - Move log lines tests to the logger tests instead of the upgrader tests [#185853346](https://www.pivotaltracker.com/story/show/185853346) --- internal/logger/logger.go | 24 ---- internal/logger/logger_test.go | 117 ++++++++++------ internal/upgrader/upgrader_test.go | 209 ++++++++++++++--------------- 3 files changed, 181 insertions(+), 169 deletions(-) diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 4899055..2b4a062 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -2,9 +2,7 @@ package logger import ( "fmt" - "strings" "sync" - "text/tabwriter" "time" "upgrade-all-services-cli-plugin/internal/ccapi" @@ -108,28 +106,6 @@ func (l *Logger) FinalTotals() { logRowFormatTotals(l) } -//lint:ignore U1000 Ignore unused function temporarily for better code review -func logOldFormatTotals(l *Logger) { - if len(l.failures) > 0 { - l.printf("failed to upgrade %d instances", len(l.failures)) - l.printf("") - - var sb strings.Builder - tw := tabwriter.NewWriter(&sb, 0, 0, 1, ' ', tabwriter.Debug) - fmt.Fprintln(tw, "Service Instance Name\tService Instance GUID\t Details") - fmt.Fprintln(tw, "---------------------\t---------------------\t -------") - - for _, failure := range l.failures { - fmt.Fprintf(tw, "%s\t %s\t %s\n", failure.instance.Name, failure.instance.GUID, failure.err) - } - tw.Flush() - - for _, line := range strings.Split(sb.String(), "\n") { - l.printf(line) - } - } -} - func logRowFormatTotals(l *Logger) { if len(l.failures) > 0 { l.printf("failed to upgrade %d instances", len(l.failures)) diff --git a/internal/logger/logger_test.go b/internal/logger/logger_test.go index 48f6b1a..b8158ee 100644 --- a/internal/logger/logger_test.go +++ b/internal/logger/logger_test.go @@ -43,38 +43,38 @@ var _ = Describe("Logger", func() { It("can log that it is skipping an instance", func() { result := captureStdout(func() { - l.SkippingInstance(fullInstance("my-instance", "fake-guid", true, "create", "failed")) + l.SkippingInstance(createFailedInstance()) }) - Expect(result).To(MatchRegexp(timestampRegexp + `: skipping instance: "my-instance" guid: "fake-guid" Upgrade Available: true Last Operation: Type: "create" State: "failed"\n`)) + Expect(result).To(MatchRegexp(timestampRegexp + `: skipping instance: "create-failed-instance" guid: "create-failed-instance-guid" Upgrade Available: true Last Operation: Type: "create" State: "failed"\n`)) }) It("can log the start of an upgrade", func() { result := captureStdout(func() { - l.UpgradeStarting(basicInstance("my-instance", "fake-guid")) + l.UpgradeStarting(upgradeableInstance(1)) }) - Expect(result).To(MatchRegexp(timestampRegexp + `: starting to upgrade instance: "my-instance" guid: "fake-guid"\n`)) + Expect(result).To(MatchRegexp(timestampRegexp + `: starting to upgrade instance: "my-service-instance-1" guid: "my-service-instance-guid-1"\n`)) }) It("can log the success of an upgrade", func() { result := captureStdout(func() { - l.UpgradeSucceeded(basicInstance("my-instance", "fake-guid"), time.Minute) + l.UpgradeSucceeded(upgradeableInstance(1), time.Minute) }) - Expect(result).To(MatchRegexp(timestampRegexp + `: finished upgrade of instance: "my-instance" guid: "fake-guid" successfully after 1m0s\n`)) + Expect(result).To(MatchRegexp(timestampRegexp + `: finished upgrade of instance: "my-service-instance-1" guid: "my-service-instance-guid-1" successfully after 1m0s\n`)) }) It("can log the failure of an upgrade", func() { result := captureStdout(func() { - l.UpgradeFailed(basicInstance("my-instance", "fake-guid"), time.Minute, fmt.Errorf("boom")) + l.UpgradeFailed(upgradeableInstance(1), time.Minute, fmt.Errorf("boom")) }) - Expect(result).To(MatchRegexp(timestampRegexp + `: upgrade of instance: "my-instance" guid: "fake-guid" failed after 1m0s: boom\n`)) + Expect(result).To(MatchRegexp(timestampRegexp + `: upgrade of instance: "my-service-instance-1" guid: "my-service-instance-guid-1" failed after 1m0s: boom\n`)) }) It("can log the final totals", func() { l.InitialTotals(10, 5) - l.UpgradeFailed(fullInstance("my-first-instance", "fake-guid-1", true, "fake-op-type1", "fake-op-state1"), time.Minute, fmt.Errorf("boom")) - l.UpgradeFailed(fullInstance("my-second-instance", "fake-guid-2", true, "fake-op-type2", "fake-op-state2"), time.Minute, fmt.Errorf("bang")) - l.UpgradeSucceeded(basicInstance("my-third-instance", "fake-guid-3"), time.Minute) - l.SkippingInstance(fullInstance("skipped", "skipped-guid", true, "create", "failed")) + l.UpgradeFailed(upgradeableInstance(1), time.Minute, fmt.Errorf("boom")) + l.UpgradeFailed(upgradeableInstance(2), time.Minute, fmt.Errorf("bang")) + l.UpgradeSucceeded(upToDateInstance(3), time.Minute) + l.SkippingInstance(createFailedInstance()) result := captureStdout(func() { l.FinalTotals() @@ -82,18 +82,38 @@ var _ = Describe("Logger", func() { Expect(result).To(MatchRegexp(`: skipped 1 instances\n`)) Expect(result).To(MatchRegexp(`: successfully upgraded 1 instances\n`)) Expect(result).To(MatchRegexp(`: failed to upgrade 2 instances\n`)) - Expect(result).To(MatchRegexp(`Service Instance Name: "my-first-instance"\s+`)) - Expect(result).To(MatchRegexp(`Service Instance GUID: "fake-guid-1"\s+`)) + Expect(result).To(MatchRegexp(`Service Instance Name: "my-service-instance-1"\s+`)) + Expect(result).To(MatchRegexp(`Service Instance GUID: "my-service-instance-guid-1"\s+`)) + Expect(result).To(MatchRegexp(`Service Version: "fake-version-1"\s+`)) + Expect(result).To(MatchRegexp(`Org Name: "fake-org-name-1"\s+`)) + Expect(result).To(MatchRegexp(`Org GUID: "fake-org-guid-1"\s+`)) + Expect(result).To(MatchRegexp(`Space Name: "fake-space-name-1"\s+`)) + Expect(result).To(MatchRegexp(`Space GUID: "fake-space-guid-1"\s+`)) + Expect(result).To(MatchRegexp(`Plan Name: "fake-plan-name-1"\s+`)) + Expect(result).To(MatchRegexp(`Plan GUID: "fake-plan-guid-1"\s+`)) + Expect(result).To(MatchRegexp(`Plan Version: "fake-plan-version-1"\s+`)) + Expect(result).To(MatchRegexp(`Service Offering Name: "fake-soffer-name-1"\s+`)) + Expect(result).To(MatchRegexp(`Service Offering GUID: "fake-soffer-guid-1"\s+`)) Expect(result).To(MatchRegexp(`Details: "boom"\n`)) - Expect(result).To(MatchRegexp(`Service Instance Name: "my-second-instance"\s+`)) - Expect(result).To(MatchRegexp(`Service Instance GUID: "fake-guid-2"\s+`)) + Expect(result).To(MatchRegexp(`Service Instance Name: "my-service-instance-2"\s+`)) + Expect(result).To(MatchRegexp(`Service Instance GUID: "my-service-instance-guid-2"\s+`)) + Expect(result).To(MatchRegexp(`Service Version: "fake-version-2"\s+`)) + Expect(result).To(MatchRegexp(`Org Name: "fake-org-name-2"\s+`)) + Expect(result).To(MatchRegexp(`Org GUID: "fake-org-guid-2"\s+`)) + Expect(result).To(MatchRegexp(`Space Name: "fake-space-name-2"\s+`)) + Expect(result).To(MatchRegexp(`Space GUID: "fake-space-guid-2"\s+`)) + Expect(result).To(MatchRegexp(`Plan Name: "fake-plan-name-2"\s+`)) + Expect(result).To(MatchRegexp(`Plan GUID: "fake-plan-guid-2"\s+`)) + Expect(result).To(MatchRegexp(`Plan Version: "fake-plan-version-2"\s+`)) + Expect(result).To(MatchRegexp(`Service Offering Name: "fake-soffer-name-2"\s+`)) + Expect(result).To(MatchRegexp(`Service Offering GUID: "fake-soffer-guid-2"\s+`)) Expect(result).To(MatchRegexp(`Details: "bang"\n`)) }) It("logs on a ticker", func() { l.InitialTotals(10, 5) - l.UpgradeSucceeded(basicInstance("fake-name", "fake-guid"), time.Minute) - l.UpgradeSucceeded(basicInstance("fake-name", "fake-guid"), time.Minute) + l.UpgradeSucceeded(upgradeableInstance(1), time.Minute) + l.UpgradeSucceeded(upgradeableInstance(1), time.Minute) result := captureStdout(func() { time.Sleep(150 * time.Millisecond) @@ -103,44 +123,63 @@ var _ = Describe("Logger", func() { }) }) -func basicInstance(name, guid string) ccapi.ServiceInstance { - return fullInstance(name, guid, false, "fake-op-type", "fake-op-state") +func createFailedInstance() ccapi.ServiceInstance { + return ccapi.ServiceInstance{ + Name: "create-failed-instance", + GUID: "create-failed-instance-guid", + UpgradeAvailable: true, + LastOperation: ccapi.LastOperation{ + Type: "create", + State: "failed", + }, + } +} + +func formatValue(stringID string, index int) string { + return fmt.Sprintf("%s-%d", stringID, index) +} +func upgradeableInstance(index int) ccapi.ServiceInstance { + return indexedInstance(index, true) +} + +func upToDateInstance(index int) ccapi.ServiceInstance { + return indexedInstance(index, false) } -func fullInstance(name, guid string, upgradeAvailable bool, lastOperationType, lastOperationState string) ccapi.ServiceInstance { +func indexedInstance(index int, upgradeAvailable bool) ccapi.ServiceInstance { return ccapi.ServiceInstance{ - Name: name, - GUID: guid, + Name: formatValue("my-service-instance", index), + GUID: formatValue("my-service-instance-guid", index), - PlanGUID: "fake-plan-guid", - SpaceGUID: "fake-space-guid", + PlanGUID: formatValue("fake-plan-guid", index), + SpaceGUID: formatValue("fake-space-guid", index), - MaintenanceInfoVersion: "fake-version", - PlanMaintenanceInfoVersion: "fake-plan-version", + MaintenanceInfoVersion: formatValue("fake-version", index), + PlanMaintenanceInfoVersion: formatValue("fake-plan-version", index), UpgradeAvailable: upgradeAvailable, LastOperation: ccapi.LastOperation{ - Type: lastOperationType, - State: lastOperationState, + Type: formatValue("last-operation-type", index), + State: formatValue("last-operation-state", index), }, Included: ccapi.EmbeddedInclude{ Plan: ccapi.IncludedPlan{ - Name: "fake-plan-name", - GUID: "fake-plan-guid", - ServiceOfferingGUID: "fake-soffer-guid", + Name: formatValue("fake-plan-name", index), + GUID: formatValue("fake-plan-guid", index), + ServiceOfferingGUID: formatValue("fake-soffer-guid", index), }, ServiceOffering: ccapi.ServiceOffering{ - Name: "fake-soffer-name", - GUID: "fake-soffer-guid", + Name: formatValue("fake-soffer-name", index), + GUID: formatValue("fake-soffer-guid", index), }, Space: ccapi.Space{ - Name: "fake-space-name", - GUID: "fake-space-guid", - OrganizationGUID: "fake-org-guid", + Name: formatValue("fake-space-name", index), + GUID: formatValue("fake-space-guid", index), + OrganizationGUID: formatValue("fake-org-guid", index), }, Organization: ccapi.Organization{ - Name: "fake-org-name", - GUID: "fake-org-guid", + Name: formatValue("fake-org-name", index), + GUID: formatValue("fake-org-guid", index), }, }, } diff --git a/internal/upgrader/upgrader_test.go b/internal/upgrader/upgrader_test.go index bcc02eb..690cb48 100644 --- a/internal/upgrader/upgrader_test.go +++ b/internal/upgrader/upgrader_test.go @@ -23,14 +23,16 @@ var _ = Describe("Upgrade", func() { ) var ( - fakeCFClient *upgraderfakes.FakeCFClient - fakePlan ccapi.Plan - fakeInstance1 ccapi.ServiceInstance - fakeInstance2 ccapi.ServiceInstance - fakeInstanceNoUpgrade ccapi.ServiceInstance - fakeInstanceCreateFailed ccapi.ServiceInstance - fakeServiceInstances []ccapi.ServiceInstance - fakeLog *upgraderfakes.FakeLogger + fakeCFClient *upgraderfakes.FakeCFClient + fakePlan ccapi.Plan + fakeInstance1 ccapi.ServiceInstance + fakeInstance2 ccapi.ServiceInstance + fakeInstanceNoUpgrade ccapi.ServiceInstance + fakeInstanceCreateFailed ccapi.ServiceInstance + fakeInstanceDestroyFailed ccapi.ServiceInstance + fakeServiceInstances []ccapi.ServiceInstance + fakeServiceInstancesNoUpgrade []ccapi.ServiceInstance + fakeLog *upgraderfakes.FakeLogger ) BeforeEach(func() { @@ -61,11 +63,19 @@ var _ = Describe("Upgrade", func() { PlanGUID: fakePlanGUID, UpgradeAvailable: true, } + fakeInstanceDestroyFailed = ccapi.ServiceInstance{ + Name: "fake-instance-destroy-failed", + GUID: "fake-instance-destroy-failed-GUID", + PlanGUID: fakePlanGUID, + UpgradeAvailable: true, + } fakeInstanceCreateFailed.LastOperation.Type = "create" fakeInstanceCreateFailed.LastOperation.State = "failed" - fakeServiceInstances = []ccapi.ServiceInstance{fakeInstance1, fakeInstance2, fakeInstanceNoUpgrade, fakeInstanceCreateFailed} - + fakeInstanceDestroyFailed.LastOperation.Type = "destroy" + fakeInstanceDestroyFailed.LastOperation.State = "failed" + fakeServiceInstances = []ccapi.ServiceInstance{fakeInstance1, fakeInstance2, fakeInstanceNoUpgrade, fakeInstanceCreateFailed, fakeInstanceDestroyFailed} + fakeServiceInstancesNoUpgrade = []ccapi.ServiceInstance{fakeInstanceNoUpgrade, fakeInstanceCreateFailed} fakeCFClient = &upgraderfakes.FakeCFClient{} fakeCFClient.GetServicePlansReturns([]ccapi.Plan{fakePlan}, nil) fakeCFClient.GetServiceInstancesReturns(fakeServiceInstances, nil) @@ -86,11 +96,12 @@ var _ = Describe("Upgrade", func() { Expect(fakeCFClient.GetServiceInstancesArgsForCall(0)).To(Equal([]string{fakePlanGUID})) By("calling upgrade on each upgradeable instance") - Expect(fakeCFClient.UpgradeServiceInstanceCallCount()).Should(Equal(2)) + Expect(fakeCFClient.UpgradeServiceInstanceCallCount()).Should(Equal(3)) instanceGUID1, _ := fakeCFClient.UpgradeServiceInstanceArgsForCall(0) instanceGUID2, _ := fakeCFClient.UpgradeServiceInstanceArgsForCall(1) - guids := []string{instanceGUID1, instanceGUID2} - Expect(guids).To(ConsistOf("fake-instance-guid-1", "fake-instance-guid-2")) + instanceGUID3, _ := fakeCFClient.UpgradeServiceInstanceArgsForCall(2) + guids := []string{instanceGUID1, instanceGUID2, instanceGUID3} + Expect(guids).To(ConsistOf("fake-instance-guid-1", "fake-instance-guid-2", "fake-instance-destroy-failed-GUID")) }) It("should pass the correct information to the logger", func() { @@ -99,8 +110,8 @@ var _ = Describe("Upgrade", func() { Expect(fakeLog.InitialTotalsCallCount()).To(Equal(1)) actualTotal, actualUpgradable := fakeLog.InitialTotalsArgsForCall(0) - Expect(actualTotal).To(Equal(4)) - Expect(actualUpgradable).To(Equal(2)) + Expect(actualTotal).To(Equal(5)) + Expect(actualUpgradable).To(Equal(3)) Expect(fakeLog.SkippingInstanceCallCount()).To(Equal(1)) instanceSkipped := fakeLog.SkippingInstanceArgsForCall(0) @@ -110,15 +121,18 @@ var _ = Describe("Upgrade", func() { Expect(instanceSkipped.LastOperation.Type).To(Equal("create")) Expect(instanceSkipped.LastOperation.State).To(Equal("failed")) - Expect(fakeLog.UpgradeStartingCallCount()).To(Equal(2)) + Expect(fakeLog.UpgradeStartingCallCount()).To(Equal(3)) instance1 := fakeLog.UpgradeStartingArgsForCall(0) Expect(instance1.Name).To(Equal("fake-instance-name-1")) Expect(instance1.GUID).To(Equal("fake-instance-guid-1")) instance2 := fakeLog.UpgradeStartingArgsForCall(1) Expect(instance2.Name).To(Equal("fake-instance-name-2")) Expect(instance2.GUID).To(Equal("fake-instance-guid-2")) + instance3 := fakeLog.UpgradeStartingArgsForCall(2) + Expect(instance3.Name).To(Equal("fake-instance-destroy-failed")) + Expect(instance3.GUID).To(Equal("fake-instance-destroy-failed-GUID")) - Expect(fakeLog.UpgradeSucceededCallCount()).To(Equal(2)) + Expect(fakeLog.UpgradeSucceededCallCount()).To(Equal(3)) Expect(fakeLog.UpgradeFailedCallCount()).To(Equal(0)) Expect(fakeLog.FinalTotalsCallCount()).To(Equal(1)) }) @@ -151,6 +165,66 @@ var _ = Describe("Upgrade", func() { }) }) + When("performing a CheckUpToDate", func() { + It("should print out service GUIDs and not attempt to upgrade", func() { + result := captureStdout(func() { + l := logger.New(100 * time.Millisecond) + defer l.Cleanup() + err := upgrader.Upgrade(fakeCFClient, fakeBrokerName, 5, false, true, l) + Expect(err).To(MatchError("check up-to-date failed: found 3 instances which are not up-to-date")) + }) + + By("getting the service plans") + Expect(fakeCFClient.GetServicePlansCallCount()).To(Equal(1)) + Expect(fakeCFClient.GetServicePlansArgsForCall(0)).To(Equal(fakeBrokerName)) + + By("getting the service instances") + Expect(fakeCFClient.GetServiceInstancesCallCount()).To(Equal(1)) + Expect(fakeCFClient.GetServiceInstancesArgsForCall(0)).To(Equal([]string{fakePlanGUID})) + + By("not calling upgrade") + Expect(fakeCFClient.UpgradeServiceInstanceCallCount()).Should(Equal(0)) + + By("printing the GUIDs") + Expect(result).To(ContainSubstring(fmt.Sprintf("discovering service instances for broker: %s", fakeBrokerName))) + Expect(result).To(ContainSubstring("the following service instances would be upgraded:")) + Expect(result).To(ContainSubstring(fmt.Sprintf(`Service Instance GUID: "%s"`, fakeInstance1.GUID))) + Expect(result).To(ContainSubstring(fmt.Sprintf(`Service Instance GUID: "%s"`, fakeInstance2.GUID))) + }) + + It("takes precedence over dry-run", func() { + upgradeErr := upgrader.Upgrade(fakeCFClient, fakeBrokerName, 1, true, true, fakeLog) + Expect(upgradeErr).To(MatchError("check up-to-date failed: found 3 instances which are not up-to-date")) + }) + + When("no service plans are available", func() { + It("returns error stating no plans available", func() { + fakeCFClient.GetServicePlansReturns([]ccapi.Plan{}, nil) + + err := upgrader.Upgrade(fakeCFClient, fakeBrokerName, 1, false, true, fakeLog) + Expect(err).To(MatchError(fmt.Sprintf("no service plans available for broker: %s", fakeBrokerName))) + }) + }) + + When("no service instances have pending upgrades", func() { + It("does not return an error", func() { + fakeCFClient.GetServiceInstancesReturns([]ccapi.ServiceInstance{}, nil) + + err := upgrader.Upgrade(fakeCFClient, fakeBrokerName, 1, false, true, fakeLog) + Expect(err).NotTo(HaveOccurred()) + }) + }) + + When("all instances are up to date", func() { + It("does not return an error", func() { + fakeCFClient.GetServiceInstancesReturns(fakeServiceInstancesNoUpgrade, nil) + + err := upgrader.Upgrade(fakeCFClient, fakeBrokerName, 1, false, true, fakeLog) + Expect(err).NotTo(HaveOccurred()) + }) + }) + }) + When("no service plans are available", func() { It("returns error stating no plans available", func() { fakeCFClient.GetServicePlansReturns([]ccapi.Plan{}, nil) @@ -166,11 +240,12 @@ var _ = Describe("Upgrade", func() { Expect(err).NotTo(HaveOccurred()) By("calling upgrade on each upgradeable instance") - Expect(fakeCFClient.UpgradeServiceInstanceCallCount()).Should(Equal(2)) + Expect(fakeCFClient.UpgradeServiceInstanceCallCount()).Should(Equal(3)) instanceGUID1, _ := fakeCFClient.UpgradeServiceInstanceArgsForCall(0) instanceGUID2, _ := fakeCFClient.UpgradeServiceInstanceArgsForCall(1) - guids := []string{instanceGUID1, instanceGUID2} - Expect(guids).To(ConsistOf("fake-instance-guid-1", "fake-instance-guid-2")) + instanceGUID3, _ := fakeCFClient.UpgradeServiceInstanceArgsForCall(2) + guids := []string{instanceGUID1, instanceGUID2, instanceGUID3} + Expect(guids).To(ConsistOf("fake-instance-guid-1", "fake-instance-guid-2", "fake-instance-destroy-failed-GUID")) }) }) @@ -208,6 +283,7 @@ var _ = Describe("Upgrade", func() { BeforeEach(func() { fakeCFClient.UpgradeServiceInstanceReturnsOnCall(0, nil) fakeCFClient.UpgradeServiceInstanceReturnsOnCall(1, fmt.Errorf("failed to upgrade instance")) + fakeCFClient.UpgradeServiceInstanceReturnsOnCall(2, nil) }) It("should pass the correct information to the logger", func() { @@ -216,104 +292,25 @@ var _ = Describe("Upgrade", func() { Expect(fakeLog.InitialTotalsCallCount()).To(Equal(1)) actualTotal, actualUpgradable := fakeLog.InitialTotalsArgsForCall(0) - Expect(actualTotal).To(Equal(4)) - Expect(actualUpgradable).To(Equal(2)) + Expect(actualTotal).To(Equal(5)) + Expect(actualUpgradable).To(Equal(3)) - Expect(fakeLog.UpgradeStartingCallCount()).To(Equal(2)) + Expect(fakeLog.UpgradeStartingCallCount()).To(Equal(3)) instance1 := fakeLog.UpgradeStartingArgsForCall(0) Expect(instance1.Name).To(Equal("fake-instance-name-1")) Expect(instance1.GUID).To(Equal("fake-instance-guid-1")) instance2 := fakeLog.UpgradeStartingArgsForCall(1) Expect(instance2.Name).To(Equal("fake-instance-name-2")) Expect(instance2.GUID).To(Equal("fake-instance-guid-2")) + instance3 := fakeLog.UpgradeStartingArgsForCall(2) + Expect(instance3.Name).To(Equal("fake-instance-destroy-failed")) + Expect(instance3.GUID).To(Equal("fake-instance-destroy-failed-GUID")) - Expect(fakeLog.UpgradeSucceededCallCount()).To(Equal(1)) + Expect(fakeLog.UpgradeSucceededCallCount()).To(Equal(2)) Expect(fakeLog.UpgradeFailedCallCount()).To(Equal(1)) Expect(fakeLog.FinalTotalsCallCount()).To(Equal(1)) }) }) - - Context("checkUpToDate: true", func() { - DescribeTable("expected behaviour of Upgrade when CheckUpToDate is enabled", - func(expectedErr error, expectedLog string, fakePlans []ccapi.Plan, fakeInstances []ccapi.ServiceInstance) { - passedDryRun := true - fakeCFClient.GetServicePlansReturns(fakePlans, nil) - fakeCFClient.GetServiceInstancesReturns(fakeInstances, nil) - - var upgradeErr error - upgradeLog := captureStdout(func() { - l := logger.New(100 * time.Millisecond) - defer l.Cleanup() - upgradeErr = upgrader.Upgrade(fakeCFClient, fakeBrokerName, 1, passedDryRun, true, l) - // Under no circumstances we want an actual upgrade to be scheduled - Expect(fakeCFClient.UpgradeServiceInstanceCallCount()).Should(Equal(0)) - }) - - // When `checkUpToDate: true` it takes precedence and the actual value of dryRun should be` irrelevant and lead to the exact same results - var upgradeErr2 error - upgradeLog2 := captureStdout(func() { - l := logger.New(100 * time.Millisecond) - defer l.Cleanup() - upgradeErr2 = upgrader.Upgrade(fakeCFClient, fakeBrokerName, 1, !passedDryRun, true, l) - // Under no circumstances we want an actual upgrade to be scheduled - Expect(fakeCFClient.UpgradeServiceInstanceCallCount()).Should(Equal(0)) - }) - - if expectedErr == nil { - Expect(upgradeErr).NotTo(HaveOccurred()) - Expect(upgradeErr2).NotTo(HaveOccurred()) - } else { - Expect(upgradeErr).To(Equal(expectedErr)) - Expect(upgradeErr2).To(Equal(expectedErr)) - } - if expectedLog == "" { - Expect(upgradeLog).To(Equal("")) - Expect(upgradeLog2).To(Equal("")) - } else { - Expect(upgradeLog).To(MatchRegexp(expectedLog)) - Expect(upgradeLog2).To(MatchRegexp(expectedLog)) - } - }, - Entry("no plans defined", - fmt.Errorf("no service plans available for broker: fake-broker-name"), "", - []ccapi.Plan{}, - []ccapi.ServiceInstance{}, - ), - Entry("no instances defined", - nil, "no instances available to upgrade", - []ccapi.Plan{fakePlan}, - []ccapi.ServiceInstance{}, - ), - Entry("all instances up to date", - nil, "no instances available to upgrade", - []ccapi.Plan{fakePlan}, - []ccapi.ServiceInstance{{UpgradeAvailable: false}}, - ), - Entry("upgradable instances", - fmt.Errorf("check up-to-date failed: found 1 instances which are not up-to-date"), - `(\s|.)*`, // we don't care about the logs in this test - []ccapi.Plan{fakePlan}, - []ccapi.ServiceInstance{{UpgradeAvailable: true}}, - ), - Entry("upgradable instances detailed logs", - fmt.Errorf("check up-to-date failed: found 1 instances which are not up-to-date"), - getExpectedLogForInstance(getFakeInstanceDetailed()), - []ccapi.Plan{{GUID: getFakeInstanceDetailed().PlanGUID, MaintenanceInfoVersion: getFakeInstanceDetailed().PlanMaintenanceInfoVersion}}, - []ccapi.ServiceInstance{getFakeInstanceDetailed()}, - ), - Entry("upgradeable instances whose creation failed", - nil, "no instances available to upgrade", - []ccapi.Plan{{GUID: fakeInstance1.PlanGUID}}, - []ccapi.ServiceInstance{{UpgradeAvailable: true, LastOperation: ccapi.LastOperation{Type: "create", State: "failed"}}}, - ), - Entry("upgradeable instances after a failed destroy attempt", - fmt.Errorf("check up-to-date failed: found 1 instances which are not up-to-date"), - `(\s|.)*`, // we don't care about the logs in this test, only that it raises the error above - []ccapi.Plan{{GUID: fakeInstance1.PlanGUID}}, - []ccapi.ServiceInstance{{UpgradeAvailable: true, LastOperation: ccapi.LastOperation{Type: "destroy", State: "failed"}}}, - ), - ) - }) }) var captureStdoutLock sync.Mutex