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