Skip to content

Commit

Permalink
Add manifest diff to push command
Browse files Browse the repository at this point in the history
- Displays the manifest diff from the new V3 Space Manifest Diff
endpoint

- An integration test will be written in the push suite in a later
commit but these unit tests cover every case for now

[#173478943]

Co-authored-by: Jenna Goldstrich <jgoldstrich@pivotal.io>
Co-authored-by: Teal Stannard <tstannard@pivotal.io>
Co-authored-by: Weyman Fung <weymanf@vmware.com>
Co-authored-by: Marc Paquette <mpaquette@pivotal.io>
  • Loading branch information
4 people committed Mar 17, 2021
1 parent 7744f93 commit 6ddc983
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
23 changes: 23 additions & 0 deletions command/v7/push_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/actor/v7pushaction"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/command"
Expand Down Expand Up @@ -111,6 +112,7 @@ type PushCommand struct {
CWD string
ManifestLocator ManifestLocator
ManifestParser ManifestParser
DiffDisplayer DiffDisplayer

stopStreamingFunc func()
}
Expand All @@ -132,6 +134,7 @@ func (cmd *PushCommand) Setup(config command.Config, ui command.UI) error {

cmd.ManifestLocator = manifestparser.NewLocator()
cmd.ManifestParser = manifestparser.ManifestParser{}
cmd.DiffDisplayer = shared.NewManifestDiffDisplayer(ui)

return err
}
Expand Down Expand Up @@ -182,10 +185,30 @@ func (cmd PushCommand) Execute(args []string) error {

hasManifest := transformedManifest.PathToManifest != ""

spaceGUID := cmd.Config.TargetedSpace().GUID
if hasManifest {
cmd.UI.DisplayText("Applying manifest file {{.Path}}...", map[string]interface{}{
"Path": transformedManifest.PathToManifest,
})

diff, warnings, err := cmd.Actor.DiffSpaceManifest(spaceGUID, transformedRawManifest)

cmd.UI.DisplayWarnings(warnings)
if err != nil {
if _, isUnexpectedError := err.(ccerror.V3UnexpectedResponseError); isUnexpectedError {
cmd.UI.DisplayWarning("Unable to generate diff. Continuing to apply manifest...")
} else {
return err
}
} else {
cmd.UI.DisplayNewline()
cmd.UI.DisplayText("Updating with these attributes...")

err = cmd.DiffDisplayer.DisplayDiff(transformedRawManifest, diff)
if err != nil {
return err
}
}
}

v7ActionWarnings, err := cmd.VersionActor.SetSpaceManifest(
Expand Down
82 changes: 82 additions & 0 deletions command/v7/push_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.cloudfoundry.org/cli/actor/sharedaction/sharedactionfakes"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/actor/v7pushaction"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/command/commandfakes"
"code.cloudfoundry.org/cli/command/flag"
Expand Down Expand Up @@ -84,6 +85,8 @@ var _ = Describe("push Command", func() {
fakeConfig *commandfakes.FakeConfig
fakeSharedActor *commandfakes.FakeSharedActor
fakeActor *v7fakes.FakePushActor
fakeDiffActor *v7fakes.FakeActor
fakeDiffDisplayer *v7fakes.FakeDiffDisplayer
fakeVersionActor *v7fakes.FakeV7ActorForPush
fakeProgressBar *v7fakes.FakeProgressBar
fakeLogCacheClient *sharedactionfakes.FakeLogCacheClient
Expand All @@ -106,6 +109,8 @@ var _ = Describe("push Command", func() {
fakeConfig = new(commandfakes.FakeConfig)
fakeSharedActor = new(commandfakes.FakeSharedActor)
fakeActor = new(v7fakes.FakePushActor)
fakeDiffActor = new(v7fakes.FakeActor)
fakeDiffDisplayer = new(v7fakes.FakeDiffDisplayer)
fakeVersionActor = new(v7fakes.FakeV7ActorForPush)
fakeProgressBar = new(v7fakes.FakeProgressBar)
fakeLogCacheClient = new(sharedactionfakes.FakeLogCacheClient)
Expand All @@ -127,6 +132,7 @@ var _ = Describe("push Command", func() {
SharedActor: fakeSharedActor,
UI: testUI,
Config: fakeConfig,
Actor: fakeDiffActor,
},
PushActor: fakeActor,
VersionActor: fakeVersionActor,
Expand All @@ -135,6 +141,7 @@ var _ = Describe("push Command", func() {
CWD: pwd,
ManifestLocator: fakeManifestLocator,
ManifestParser: fakeManifestParser,
DiffDisplayer: fakeDiffDisplayer,
}
})

Expand Down Expand Up @@ -324,6 +331,81 @@ var _ = Describe("push Command", func() {
Expect(actualManifestBytes).To(Equal([]byte("our-manifest")))
})

When("the manifest is successfully parsed", func() {
var expectedDiff resources.ManifestDiff

BeforeEach(func() {
fakeActor.HandleFlagOverridesReturns(
manifestparser.Manifest{
PathToManifest: "path/to/manifest",
},
nil,
)
expectedDiff = resources.ManifestDiff{
Diffs: []resources.Diff{
{Op: resources.AddOperation, Path: "/path/to/field", Value: "hello"},
},
}

fakeVersionActor.SetSpaceManifestReturns(
v7action.Warnings{"some-manifest-warning"},
nil,
)

fakeDiffActor.DiffSpaceManifestReturns(
expectedDiff,
nil,
nil,
)
})

It("shows the manifest diff and sets the manifest", func() {
Expect(executeErr).ToNot(HaveOccurred())
Expect(testUI.Out).To(Say("Applying manifest file %s...", ("path/to/manifest")))
Expect(testUI.Err).To(Say("some-manifest-warning"))

Expect(fakeDiffActor.DiffSpaceManifestCallCount()).To(Equal(1))
spaceGUID, manifestBytes := fakeDiffActor.DiffSpaceManifestArgsForCall(0)
Expect(spaceGUID).To(Equal("some-space-guid"))
Expect(manifestBytes).To(Equal([]byte("our-manifest")))

Expect(fakeDiffDisplayer.DisplayDiffCallCount()).To(Equal(1))
manifestBytes, diff := fakeDiffDisplayer.DisplayDiffArgsForCall(0)
Expect(manifestBytes).To(Equal([]byte("our-manifest")))
Expect(diff).To(Equal(expectedDiff))

Expect(fakeVersionActor.SetSpaceManifestCallCount()).To(Equal(1))
spaceGUIDArg, actualBytes := fakeVersionActor.SetSpaceManifestArgsForCall(0)
Expect(actualBytes).To(Equal([]byte("our-manifest")))
Expect(spaceGUIDArg).To(Equal("some-space-guid"))
})

When("the manifest diff fails", func() {
BeforeEach(func() {
fakeDiffActor.DiffSpaceManifestReturns(resources.ManifestDiff{}, v7action.Warnings{}, ccerror.V3UnexpectedResponseError{})
})

It("reports the 500, does not display the diff, but still applies the manifest", func() {
Expect(executeErr).ToNot(HaveOccurred())

Expect(testUI.Err).To(Say("Unable to generate diff. Continuing to apply manifest..."))
Expect(fakeDiffDisplayer.DisplayDiffCallCount()).To(Equal(0))
Expect(fakeVersionActor.SetSpaceManifestCallCount()).To(Equal(1))
})
})

When("displaying the manifest diff fails", func() {
BeforeEach(func() {
fakeDiffDisplayer.DisplayDiffReturns(errors.New("diff failed"))
})

It("returns the diff error", func() {
Expect(executeErr).To(MatchError("diff failed"))
Expect(fakeVersionActor.SetSpaceManifestCallCount()).To(Equal(0))
})
})
})

When("applying the manifest fails", func() {
BeforeEach(func() {
fakeVersionActor.SetSpaceManifestReturns(v7action.Warnings{"apply-manifest-warnings"}, errors.New("apply-manifest-error"))
Expand Down

0 comments on commit 6ddc983

Please sign in to comment.