github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_apply_manifest_command_test.go (about) 1 package v3_test 2 3 import ( 4 "errors" 5 "regexp" 6 7 "code.cloudfoundry.org/cli/actor/actionerror" 8 "code.cloudfoundry.org/cli/actor/v3action" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 "code.cloudfoundry.org/cli/command/flag" 12 "code.cloudfoundry.org/cli/command/translatableerror" 13 "code.cloudfoundry.org/cli/command/v3" 14 "code.cloudfoundry.org/cli/command/v3/v3fakes" 15 "code.cloudfoundry.org/cli/util/configv3" 16 "code.cloudfoundry.org/cli/util/ui" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gbytes" 20 ) 21 22 var _ = Describe("v3-apply-manifest Command", func() { 23 var ( 24 cmd v3.V3ApplyManifestCommand 25 testUI *ui.UI 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 fakeActor *v3fakes.FakeV3ApplyManifestActor 29 fakeParser *v3fakes.FakeManifestParser 30 binaryName string 31 executeErr error 32 ) 33 34 BeforeEach(func() { 35 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v3fakes.FakeV3ApplyManifestActor) 39 fakeParser = new(v3fakes.FakeManifestParser) 40 41 binaryName = "faceman" 42 fakeConfig.BinaryNameReturns(binaryName) 43 44 cmd = v3.V3ApplyManifestCommand{ 45 UI: testUI, 46 Config: fakeConfig, 47 SharedActor: fakeSharedActor, 48 Actor: fakeActor, 49 Parser: fakeParser, 50 } 51 52 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionV3) 53 }) 54 55 JustBeforeEach(func() { 56 executeErr = cmd.Execute(nil) 57 }) 58 59 Context("when the API version is below the minimum", func() { 60 BeforeEach(func() { 61 fakeActor.CloudControllerAPIVersionReturns("0.0.0") 62 }) 63 64 It("returns a MinimumAPIVersionNotMetError", func() { 65 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 66 CurrentVersion: "0.0.0", 67 MinimumVersion: ccversion.MinVersionV3, 68 })) 69 }) 70 71 It("displays the experimental warning", func() { 72 Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice")) 73 }) 74 }) 75 76 Context("when checking target fails", func() { 77 BeforeEach(func() { 78 fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}) 79 }) 80 81 It("returns an error", func() { 82 Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})) 83 84 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 85 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 86 Expect(checkTargetedOrg).To(BeTrue()) 87 Expect(checkTargetedSpace).To(BeTrue()) 88 }) 89 }) 90 91 Context("when the user is not logged in", func() { 92 var expectedErr error 93 94 BeforeEach(func() { 95 expectedErr = errors.New("some current user error") 96 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 97 }) 98 99 It("return an error", func() { 100 Expect(executeErr).To(Equal(expectedErr)) 101 }) 102 }) 103 104 Context("when the user is logged in", func() { 105 var ( 106 providedPath string 107 ) 108 109 BeforeEach(func() { 110 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 111 Name: "some-org", 112 }) 113 fakeConfig.TargetedSpaceReturns(configv3.Space{ 114 Name: "some-space", 115 GUID: "some-space-guid", 116 }) 117 fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil) 118 119 providedPath = "some-manifest-path" 120 cmd.PathToManifest = flag.PathWithExistenceCheck(providedPath) 121 }) 122 123 Context("when the parse is successful", func() { 124 BeforeEach(func() { 125 fakeActor.ApplyApplicationManifestReturns( 126 v3action.Warnings{"some-manifest-warning"}, 127 nil, 128 ) 129 }) 130 131 It("displays the success text", func() { 132 Expect(executeErr).ToNot(HaveOccurred()) 133 Expect(testUI.Out).To(Say("Applying manifest %s in org some-org / space some-space as steve...", regexp.QuoteMeta(providedPath))) 134 Expect(testUI.Err).To(Say("some-manifest-warning")) 135 Expect(testUI.Out).To(Say("OK")) 136 137 Expect(fakeParser.ParseCallCount()).To(Equal(1)) 138 Expect(fakeParser.ParseArgsForCall(0)).To(Equal(providedPath)) 139 140 Expect(fakeActor.ApplyApplicationManifestCallCount()).To(Equal(1)) 141 parserArg, spaceGUIDArg := fakeActor.ApplyApplicationManifestArgsForCall(0) 142 Expect(parserArg).To(Equal(fakeParser)) 143 Expect(spaceGUIDArg).To(Equal("some-space-guid")) 144 }) 145 }) 146 147 Context("when the parse errors", func() { 148 var expectedErr error 149 150 BeforeEach(func() { 151 expectedErr = errors.New("oooooh nooooos") 152 fakeParser.ParseReturns(expectedErr) 153 }) 154 155 It("returns back the parse error", func() { 156 Expect(executeErr).To(MatchError(expectedErr)) 157 158 Expect(fakeActor.ApplyApplicationManifestCallCount()).To(Equal(0)) 159 }) 160 }) 161 }) 162 })