github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/apply_manifest_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 "regexp" 6 7 "gopkg.in/yaml.v2" 8 9 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror" 10 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action" 11 "github.com/LukasHeimann/cloudfoundrycli/v8/api/cloudcontroller/ccerror" 12 "github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes" 13 "github.com/LukasHeimann/cloudfoundrycli/v8/command/flag" 14 "github.com/LukasHeimann/cloudfoundrycli/v8/command/translatableerror" 15 . "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 16 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 17 "github.com/LukasHeimann/cloudfoundrycli/v8/resources" 18 "github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3" 19 "github.com/LukasHeimann/cloudfoundrycli/v8/util/manifestparser" 20 "github.com/LukasHeimann/cloudfoundrycli/v8/util/ui" 21 "github.com/cloudfoundry/bosh-cli/director/template" 22 . "github.com/onsi/ginkgo" 23 . "github.com/onsi/gomega" 24 . "github.com/onsi/gomega/gbytes" 25 ) 26 27 var _ = Describe("apply-manifest Command", func() { 28 var ( 29 cmd ApplyManifestCommand 30 testUI *ui.UI 31 fakeConfig *commandfakes.FakeConfig 32 fakeSharedActor *commandfakes.FakeSharedActor 33 fakeActor *v7fakes.FakeActor 34 fakeParser *v7fakes.FakeManifestParser 35 fakeLocator *v7fakes.FakeManifestLocator 36 fakeDiffDisplayer *v7fakes.FakeDiffDisplayer 37 binaryName string 38 executeErr error 39 ) 40 41 BeforeEach(func() { 42 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 43 fakeConfig = new(commandfakes.FakeConfig) 44 fakeSharedActor = new(commandfakes.FakeSharedActor) 45 fakeActor = new(v7fakes.FakeActor) 46 fakeParser = new(v7fakes.FakeManifestParser) 47 fakeLocator = new(v7fakes.FakeManifestLocator) 48 fakeDiffDisplayer = new(v7fakes.FakeDiffDisplayer) 49 50 binaryName = "faceman" 51 fakeConfig.BinaryNameReturns(binaryName) 52 53 cmd = ApplyManifestCommand{ 54 BaseCommand: BaseCommand{ 55 UI: testUI, 56 Config: fakeConfig, 57 SharedActor: fakeSharedActor, 58 Actor: fakeActor, 59 }, 60 ManifestParser: fakeParser, 61 ManifestLocator: fakeLocator, 62 DiffDisplayer: fakeDiffDisplayer, 63 CWD: "fake-directory", 64 } 65 }) 66 67 JustBeforeEach(func() { 68 executeErr = cmd.Execute(nil) 69 }) 70 71 When("checking target fails", func() { 72 BeforeEach(func() { 73 fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}) 74 }) 75 76 It("returns an error", func() { 77 Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})) 78 79 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 80 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 81 Expect(checkTargetedOrg).To(BeTrue()) 82 Expect(checkTargetedSpace).To(BeTrue()) 83 }) 84 }) 85 86 When("the user is not logged in", func() { 87 var expectedErr error 88 89 BeforeEach(func() { 90 expectedErr = errors.New("some current user error") 91 fakeActor.GetCurrentUserReturns(configv3.User{}, expectedErr) 92 }) 93 94 It("return an error", func() { 95 Expect(executeErr).To(Equal(expectedErr)) 96 }) 97 }) 98 99 When("the user is logged in", func() { 100 var ( 101 providedPath string 102 ) 103 104 BeforeEach(func() { 105 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 106 Name: "some-org", 107 }) 108 fakeConfig.TargetedSpaceReturns(configv3.Space{ 109 Name: "some-space", 110 GUID: "some-space-guid", 111 }) 112 fakeActor.GetCurrentUserReturns(configv3.User{Name: "steve"}, nil) 113 }) 114 115 When("the manifest location is specified with `-f`", func() { 116 BeforeEach(func() { 117 providedPath = "some-manifest-path" 118 cmd.PathToManifest = flag.ManifestPathWithExistenceCheck(providedPath) 119 }) 120 121 It("tries locate the manifest file at the given path", func() { 122 Expect(fakeLocator.PathCallCount()).To(Equal(1)) 123 Expect(fakeLocator.PathArgsForCall(0)).To(Equal(providedPath)) 124 }) 125 }) 126 127 When("the manifest location is not specified with `-f`", func() { 128 When("looking for the manifest file errors", func() { 129 BeforeEach(func() { 130 fakeLocator.PathReturns("", false, errors.New("some-error")) 131 }) 132 133 It("returns the error", func() { 134 Expect(fakeLocator.PathCallCount()).To(Equal(1)) 135 Expect(fakeLocator.PathArgsForCall(0)).To(Equal(cmd.CWD)) 136 Expect(executeErr).To(MatchError("some-error")) 137 }) 138 }) 139 140 When("the manifest file does not exist in the current directory", func() { 141 BeforeEach(func() { 142 fakeLocator.PathReturns("", false, nil) 143 }) 144 145 It("returns a descriptive error", func() { 146 Expect(executeErr).To(MatchError(translatableerror.ManifestFileNotFoundInDirectoryError{ 147 PathToManifest: cmd.CWD, 148 })) 149 }) 150 }) 151 152 When("the manifest file exists in the current directory", func() { 153 var resolvedPath = "/fake/manifest.yml" 154 155 BeforeEach(func() { 156 cmd.PathsToVarsFiles = []flag.PathWithExistenceCheck{"vars.yml"} 157 cmd.Vars = []template.VarKV{{Name: "o", Value: "nice"}} 158 fakeLocator.PathReturns(resolvedPath, true, nil) 159 }) 160 161 When("the manifest is successfully parsed", func() { 162 var expectedDiff resources.ManifestDiff 163 164 BeforeEach(func() { 165 expectedDiff = resources.ManifestDiff{ 166 Diffs: []resources.Diff{ 167 {Op: resources.AddOperation, Path: "/path/to/field", Value: "hello"}, 168 }, 169 } 170 171 fakeActor.SetSpaceManifestReturns( 172 v7action.Warnings{"some-manifest-warning"}, 173 nil, 174 ) 175 fakeParser.InterpolateManifestReturns([]byte("interpolated!"), nil) 176 fakeParser.MarshalManifestReturns([]byte("manifesto"), nil) 177 fakeActor.DiffSpaceManifestReturns(expectedDiff, nil, nil) 178 }) 179 180 It("displays the success text", func() { 181 Expect(executeErr).ToNot(HaveOccurred()) 182 Expect(testUI.Out).To(Say("Applying manifest %s in org some-org / space some-space as steve...", regexp.QuoteMeta(resolvedPath))) 183 Expect(testUI.Err).To(Say("some-manifest-warning")) 184 Expect(testUI.Out).To(Say("OK")) 185 186 Expect(fakeParser.InterpolateManifestCallCount()).To(Equal(1)) 187 path, varsFiles, vars := fakeParser.InterpolateManifestArgsForCall(0) 188 Expect(path).To(Equal(resolvedPath)) 189 Expect(varsFiles).To(Equal([]string{"vars.yml"})) 190 Expect(vars).To(Equal([]template.VarKV{{Name: "o", Value: "nice"}})) 191 192 Expect(fakeParser.ParseManifestCallCount()).To(Equal(1)) 193 path, rawManifest := fakeParser.ParseManifestArgsForCall(0) 194 Expect(path).To(Equal(resolvedPath)) 195 Expect(rawManifest).To(Equal([]byte("interpolated!"))) 196 197 Expect(fakeActor.DiffSpaceManifestCallCount()).To(Equal(1)) 198 spaceGUID, manifestBytes := fakeActor.DiffSpaceManifestArgsForCall(0) 199 Expect(spaceGUID).To(Equal("some-space-guid")) 200 Expect(manifestBytes).To(Equal([]byte("manifesto"))) 201 202 Expect(fakeDiffDisplayer.DisplayDiffCallCount()).To(Equal(1)) 203 manifestBytes, diff := fakeDiffDisplayer.DisplayDiffArgsForCall(0) 204 Expect(manifestBytes).To(Equal([]byte("manifesto"))) 205 Expect(diff).To(Equal(expectedDiff)) 206 207 Expect(fakeActor.SetSpaceManifestCallCount()).To(Equal(1)) 208 spaceGUIDArg, actualBytes := fakeActor.SetSpaceManifestArgsForCall(0) 209 Expect(actualBytes).To(Equal([]byte("manifesto"))) 210 Expect(spaceGUIDArg).To(Equal("some-space-guid")) 211 }) 212 }) 213 214 When("the manifest is unparseable", func() { 215 BeforeEach(func() { 216 fakeParser.ParseManifestReturns(manifestparser.Manifest{}, &yaml.TypeError{ 217 Errors: []string{"oooooh nooooos"}, 218 }) 219 }) 220 221 It("returns back the parse error", func() { 222 Expect(executeErr).To(MatchError(errors.New("Unable to apply manifest because its format is invalid."))) 223 224 Expect(fakeActor.SetSpaceManifestCallCount()).To(Equal(0)) 225 }) 226 }) 227 228 When("retrieving the manifest diff 500s", func() { 229 BeforeEach(func() { 230 fakeActor.DiffSpaceManifestReturns(resources.ManifestDiff{}, v7action.Warnings{}, ccerror.V3UnexpectedResponseError{}) 231 }) 232 233 It("reports the 500, does not display the diff, but still applies the manifest", func() { 234 Expect(executeErr).ToNot(HaveOccurred()) 235 236 Expect(testUI.Err).To(Say("Unable to generate diff. Continuing to apply manifest...")) 237 Expect(fakeDiffDisplayer.DisplayDiffCallCount()).To(Equal(0)) 238 Expect(fakeActor.SetSpaceManifestCallCount()).To(Equal(1)) 239 }) 240 }) 241 242 When("displaying the manifest diff fails", func() { 243 BeforeEach(func() { 244 fakeDiffDisplayer.DisplayDiffReturns(errors.New("diff failed")) 245 }) 246 247 It("returns the diff error", func() { 248 Expect(executeErr).To(MatchError("diff failed")) 249 Expect(fakeActor.SetSpaceManifestCallCount()).To(Equal(0)) 250 }) 251 }) 252 }) 253 }) 254 }) 255 })