github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v7/create_app_manifest_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"regexp"
     9  
    10  	"code.cloudfoundry.org/cli/actor/actionerror"
    11  	"code.cloudfoundry.org/cli/actor/v7action"
    12  	"code.cloudfoundry.org/cli/command/commandfakes"
    13  	"code.cloudfoundry.org/cli/command/flag"
    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/ui"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	. "github.com/onsi/gomega/gbytes"
    21  )
    22  
    23  var _ = Describe("create-app-manifest Command", func() {
    24  	var (
    25  		cmd             CreateAppManifestCommand
    26  		testUI          *ui.UI
    27  		fakeConfig      *commandfakes.FakeConfig
    28  		fakeSharedActor *commandfakes.FakeSharedActor
    29  		fakeActor       *v7fakes.FakeCreateAppManifestActor
    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(v7fakes.FakeCreateAppManifestActor)
    39  
    40  		cmd = CreateAppManifestCommand{
    41  			UI:          testUI,
    42  			Config:      fakeConfig,
    43  			SharedActor: fakeSharedActor,
    44  			Actor:       fakeActor,
    45  		}
    46  
    47  		cmd.RequiredArgs.AppName = "some-app"
    48  
    49  		binaryName = "faceman"
    50  		fakeConfig.BinaryNameReturns(binaryName)
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	When("checking target fails", func() {
    58  		BeforeEach(func() {
    59  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    60  		})
    61  
    62  		It("returns an error if the check fails", func() {
    63  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "faceman"}))
    64  
    65  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    66  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    67  			Expect(checkTargetedOrg).To(BeTrue())
    68  			Expect(checkTargetedSpace).To(BeTrue())
    69  		})
    70  	})
    71  
    72  	When("the user is logged in, and org and space are targeted", func() {
    73  		BeforeEach(func() {
    74  			fakeConfig.HasTargetedOrganizationReturns(true)
    75  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    76  			fakeConfig.HasTargetedSpaceReturns(true)
    77  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    78  				GUID: "some-space-guid",
    79  				Name: "some-space"})
    80  			fakeConfig.CurrentUserReturns(
    81  				configv3.User{Name: "some-user"},
    82  				nil)
    83  		})
    84  
    85  		When("creating the manifest errors", func() {
    86  			BeforeEach(func() {
    87  				fakeActor.GetRawApplicationManifestByNameAndSpaceReturns(nil, v7action.Warnings{"some-warning"}, errors.New("some-error"))
    88  			})
    89  
    90  			It("returns the error, prints warnings", func() {
    91  				Expect(testUI.Out).To(Say("Creating an app manifest from current settings of app some-app in org some-org / space some-space as some-user..."))
    92  				Expect(testUI.Err).To(Say("some-warning"))
    93  				Expect(executeErr).To(MatchError("some-error"))
    94  			})
    95  		})
    96  
    97  		When("creating the manifest succeeds", func() {
    98  			var tempDir string
    99  			var yamlContents string
   100  			var pathToYAMLFile string
   101  
   102  			BeforeEach(func() {
   103  				var err error
   104  				tempDir, err = ioutil.TempDir("", "create-app-manifest-unit")
   105  				Expect(err).ToNot(HaveOccurred())
   106  				cmd.PWD = tempDir
   107  
   108  				yamlContents = `---\n- banana`
   109  				fakeActor.GetRawApplicationManifestByNameAndSpaceReturns([]byte(yamlContents), v7action.Warnings{"some-warning"}, nil)
   110  				pathToYAMLFile = filepath.Join(tempDir, "some-app_manifest.yml")
   111  			})
   112  
   113  			AfterEach(func() {
   114  				Expect(os.RemoveAll(tempDir)).ToNot(HaveOccurred())
   115  			})
   116  
   117  			It("creates application manifest in current directry as <app-name>-manifest.yml", func() {
   118  				Expect(executeErr).ToNot(HaveOccurred())
   119  
   120  				Expect(fakeActor.GetRawApplicationManifestByNameAndSpaceCallCount()).To(Equal(1))
   121  				appArg, spaceArg := fakeActor.GetRawApplicationManifestByNameAndSpaceArgsForCall(0)
   122  				Expect(appArg).To(Equal("some-app"))
   123  				Expect(spaceArg).To(Equal("some-space-guid"))
   124  
   125  				fileContents, err := ioutil.ReadFile(pathToYAMLFile)
   126  				Expect(err).ToNot(HaveOccurred())
   127  				Expect(string(fileContents)).To(Equal(yamlContents))
   128  			})
   129  
   130  			It("displays the file it created and returns no errors", func() {
   131  				Expect(testUI.Out).To(Say("Creating an app manifest from current settings of app some-app in org some-org / space some-space as some-user..."))
   132  				Expect(testUI.Err).To(Say("some-warning"))
   133  				Expect(testUI.Out).To(Say("Manifest file created successfully at %s", regexp.QuoteMeta(filepath.Join(tempDir, "some-app_manifest.yml"))))
   134  				Expect(testUI.Out).To(Say("OK"))
   135  				Expect(executeErr).ToNot(HaveOccurred())
   136  			})
   137  
   138  			When("a filepath is provided", func() {
   139  				var flagPath string
   140  
   141  				BeforeEach(func() {
   142  					flagPath = filepath.Join(tempDir, "my-special-manifest.yml")
   143  					cmd.FilePath = flag.Path(flagPath)
   144  				})
   145  
   146  				It("creates application manifest at the specified location", func() {
   147  					Expect(testUI.Out).To(Say("Creating an app manifest from current settings of app some-app in org some-org / space some-space as some-user..."))
   148  					Expect(testUI.Err).To(Say("some-warning"))
   149  					Expect(testUI.Out).To(Say("Manifest file created successfully at %s", regexp.QuoteMeta(flagPath)))
   150  					Expect(testUI.Out).To(Say("OK"))
   151  					Expect(executeErr).ToNot(HaveOccurred())
   152  
   153  					Expect(fakeActor.GetRawApplicationManifestByNameAndSpaceCallCount()).To(Equal(1))
   154  					appArg, spaceArg := fakeActor.GetRawApplicationManifestByNameAndSpaceArgsForCall(0)
   155  					Expect(appArg).To(Equal("some-app"))
   156  					Expect(spaceArg).To(Equal("some-space-guid"))
   157  
   158  					fileContents, err := ioutil.ReadFile(flagPath)
   159  					Expect(err).ToNot(HaveOccurred())
   160  					Expect(string(fileContents)).To(Equal(yamlContents))
   161  				})
   162  			})
   163  		})
   164  
   165  		When("writing the file errors", func() {
   166  			var yamlContents string
   167  			BeforeEach(func() {
   168  				cmd.PWD = filepath.Join("should", "be", "unwritable")
   169  
   170  				yamlContents = `---\n- banana`
   171  				fakeActor.GetRawApplicationManifestByNameAndSpaceReturns([]byte(yamlContents), v7action.Warnings{"some-warning"}, nil)
   172  			})
   173  
   174  			It("returns a 'ManifestCreationError' error", func() {
   175  				Expect(executeErr.Error()).To(ContainSubstring("Error creating manifest file:"))
   176  			})
   177  		})
   178  	})
   179  })