github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/v3_apply_manifest_command_test.go (about)

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