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