github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v7/apply_manifest_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  	"regexp"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/actor/v7action"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	"code.cloudfoundry.org/cli/command/translatableerror"
    12  	. "code.cloudfoundry.org/cli/command/v7"
    13  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/manifestparser"
    16  	"code.cloudfoundry.org/cli/util/ui"
    17  	"github.com/cloudfoundry/bosh-cli/director/template"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	. "github.com/onsi/gomega/gbytes"
    21  )
    22  
    23  var _ = Describe("apply-manifest Command", func() {
    24  	var (
    25  		cmd             ApplyManifestCommand
    26  		testUI          *ui.UI
    27  		fakeConfig      *commandfakes.FakeConfig
    28  		fakeSharedActor *commandfakes.FakeSharedActor
    29  		fakeActor       *v7fakes.FakeApplyManifestActor
    30  		fakeParser      *v7fakes.FakeManifestParser
    31  		fakeLocator     *v7fakes.FakeManifestLocator
    32  		binaryName      string
    33  		executeErr      error
    34  	)
    35  
    36  	BeforeEach(func() {
    37  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    38  		fakeConfig = new(commandfakes.FakeConfig)
    39  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    40  		fakeActor = new(v7fakes.FakeApplyManifestActor)
    41  		fakeParser = new(v7fakes.FakeManifestParser)
    42  		fakeLocator = new(v7fakes.FakeManifestLocator)
    43  
    44  		binaryName = "faceman"
    45  		fakeConfig.BinaryNameReturns(binaryName)
    46  
    47  		cmd = ApplyManifestCommand{
    48  			UI:              testUI,
    49  			Config:          fakeConfig,
    50  			SharedActor:     fakeSharedActor,
    51  			Actor:           fakeActor,
    52  			ManifestParser:  fakeParser,
    53  			ManifestLocator: fakeLocator,
    54  			CWD:             "fake-directory",
    55  		}
    56  	})
    57  
    58  	JustBeforeEach(func() {
    59  		executeErr = cmd.Execute(nil)
    60  	})
    61  
    62  	When("checking target fails", func() {
    63  		BeforeEach(func() {
    64  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    65  		})
    66  
    67  		It("returns an error", func() {
    68  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    69  
    70  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    71  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    72  			Expect(checkTargetedOrg).To(BeTrue())
    73  			Expect(checkTargetedSpace).To(BeTrue())
    74  		})
    75  	})
    76  
    77  	When("the user is not logged in", func() {
    78  		var expectedErr error
    79  
    80  		BeforeEach(func() {
    81  			expectedErr = errors.New("some current user error")
    82  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    83  		})
    84  
    85  		It("return an error", func() {
    86  			Expect(executeErr).To(Equal(expectedErr))
    87  		})
    88  	})
    89  
    90  	When("the user is logged in", func() {
    91  		var (
    92  			providedPath string
    93  		)
    94  
    95  		BeforeEach(func() {
    96  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    97  				Name: "some-org",
    98  			})
    99  			fakeConfig.TargetedSpaceReturns(configv3.Space{
   100  				Name: "some-space",
   101  				GUID: "some-space-guid",
   102  			})
   103  			fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
   104  		})
   105  
   106  		When("the manifest location is specified with `-f`", func() {
   107  			BeforeEach(func() {
   108  				providedPath = "some-manifest-path"
   109  				cmd.PathToManifest = flag.ManifestPathWithExistenceCheck(providedPath)
   110  			})
   111  
   112  			It("tries locate the manifest file at the given path", func() {
   113  				Expect(fakeLocator.PathCallCount()).To(Equal(1))
   114  				Expect(fakeLocator.PathArgsForCall(0)).To(Equal(providedPath))
   115  			})
   116  		})
   117  
   118  		When("the manifest location is not specified with `-f`", func() {
   119  			When("looking for the manifest file errors", func() {
   120  				BeforeEach(func() {
   121  					fakeLocator.PathReturns("", false, errors.New("some-error"))
   122  				})
   123  
   124  				It("returns the error", func() {
   125  					Expect(fakeLocator.PathCallCount()).To(Equal(1))
   126  					Expect(fakeLocator.PathArgsForCall(0)).To(Equal(cmd.CWD))
   127  					Expect(executeErr).To(MatchError("some-error"))
   128  				})
   129  			})
   130  
   131  			When("the manifest file does not exist in the current directory", func() {
   132  				BeforeEach(func() {
   133  					fakeLocator.PathReturns("", false, nil)
   134  				})
   135  
   136  				It("returns a descriptive error", func() {
   137  					Expect(executeErr).To(MatchError(translatableerror.ManifestFileNotFoundInDirectoryError{
   138  						PathToManifest: cmd.CWD,
   139  					}))
   140  				})
   141  			})
   142  
   143  			When("the manifest file exists in the current directory", func() {
   144  				var resolvedPath = "/fake/manifest.yml"
   145  
   146  				BeforeEach(func() {
   147  					cmd.PathsToVarsFiles = []flag.PathWithExistenceCheck{"vars.yml"}
   148  					cmd.Vars = []template.VarKV{{Name: "o", Value: "nice"}}
   149  					fakeLocator.PathReturns(resolvedPath, true, nil)
   150  				})
   151  
   152  				When("the manifest is successfully parsed", func() {
   153  					BeforeEach(func() {
   154  						fakeActor.SetSpaceManifestReturns(
   155  							v7action.Warnings{"some-manifest-warning"},
   156  							nil,
   157  						)
   158  						fakeParser.MarshalManifestReturns([]byte("manifesto"), nil)
   159  					})
   160  
   161  					It("displays the success text", func() {
   162  						Expect(executeErr).ToNot(HaveOccurred())
   163  						Expect(testUI.Out).To(Say("Applying manifest %s in org some-org / space some-space as steve...", regexp.QuoteMeta(resolvedPath)))
   164  						Expect(testUI.Err).To(Say("some-manifest-warning"))
   165  						Expect(testUI.Out).To(Say("OK"))
   166  
   167  						Expect(fakeParser.InterpolateAndParseCallCount()).To(Equal(1))
   168  						path, varsFiles, vars := fakeParser.InterpolateAndParseArgsForCall(0)
   169  						Expect(path).To(Equal(resolvedPath))
   170  						Expect(varsFiles).To(Equal([]string{"vars.yml"}))
   171  						Expect(vars).To(Equal([]template.VarKV{{Name: "o", Value: "nice"}}))
   172  
   173  						Expect(fakeActor.SetSpaceManifestCallCount()).To(Equal(1))
   174  						spaceGUIDArg, actualBytes := fakeActor.SetSpaceManifestArgsForCall(0)
   175  						Expect(actualBytes).To(Equal([]byte("manifesto")))
   176  						Expect(spaceGUIDArg).To(Equal("some-space-guid"))
   177  					})
   178  				})
   179  
   180  				When("the manifest is unparseable", func() {
   181  					var expectedErr error
   182  
   183  					BeforeEach(func() {
   184  						expectedErr = errors.New("oooooh nooooos")
   185  						fakeParser.InterpolateAndParseReturns(manifestparser.Manifest{}, expectedErr)
   186  					})
   187  
   188  					It("returns back the parse error", func() {
   189  						Expect(executeErr).To(MatchError(expectedErr))
   190  
   191  						Expect(fakeActor.SetSpaceManifestCallCount()).To(Equal(0))
   192  					})
   193  				})
   194  			})
   195  		})
   196  	})
   197  })