github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/application/env_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/applications/applicationsfakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/errors"
     8  	"code.cloudfoundry.org/cli/cf/models"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    14  
    15  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("env command", func() {
    21  	var (
    22  		ui                  *testterm.FakeUI
    23  		app                 models.Application
    24  		appRepo             *applicationsfakes.FakeRepository
    25  		configRepo          coreconfig.Repository
    26  		requirementsFactory *requirementsfakes.FakeFactory
    27  		deps                commandregistry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.UI = ui
    32  		deps.Config = configRepo
    33  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    34  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("env").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		ui = &testterm.FakeUI{}
    39  
    40  		app = models.Application{}
    41  		app.Name = "my-app"
    42  		appRepo = new(applicationsfakes.FakeRepository)
    43  		appRepo.ReadReturns(app, nil)
    44  
    45  		configRepo = testconfig.NewRepositoryWithDefaults()
    46  		requirementsFactory = new(requirementsfakes.FakeFactory)
    47  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    48  		requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    49  	})
    50  
    51  	runCommand := func(args ...string) bool {
    52  		return testcmd.RunCLICommand("env", args, requirementsFactory, updateCommandDependency, false, ui)
    53  	}
    54  
    55  	Describe("Requirements", func() {
    56  		It("fails when the user is not logged in", func() {
    57  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    58  			Expect(runCommand("my-app")).To(BeFalse())
    59  		})
    60  
    61  		It("fails if a space is not targeted", func() {
    62  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    63  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
    64  			Expect(runCommand("my-app")).To(BeFalse())
    65  		})
    66  	})
    67  
    68  	It("fails with usage when no app name is given", func() {
    69  		passed := runCommand()
    70  
    71  		Expect(ui.Outputs()).To(ContainSubstrings(
    72  			[]string{"Incorrect Usage", "Requires", "argument"},
    73  		))
    74  		Expect(passed).To(BeFalse())
    75  	})
    76  
    77  	It("fails with usage when the app cannot be found", func() {
    78  		appRepo.ReadReturns(models.Application{}, errors.NewModelNotFoundError("app", "hocus-pocus"))
    79  		runCommand("hocus-pocus")
    80  
    81  		Expect(ui.Outputs()).To(ContainSubstrings(
    82  			[]string{"FAILED"},
    83  			[]string{"not found"},
    84  		))
    85  	})
    86  
    87  	Context("when the app has at least one env var", func() {
    88  		BeforeEach(func() {
    89  			app = models.Application{}
    90  			app.Name = "my-app"
    91  			app.GUID = "the-app-guid"
    92  
    93  			appRepo.ReadReturns(app, nil)
    94  			appRepo.ReadEnvReturns(&models.Environment{
    95  				Environment: map[string]interface{}{
    96  					"my-key":     "my-value",
    97  					"my-key2":    "my-value2",
    98  					"first-key":  0,
    99  					"first-bool": false,
   100  				},
   101  				System: map[string]interface{}{
   102  					"VCAP_SERVICES": map[string]interface{}{
   103  						"pump-yer-brakes": "drive-slow",
   104  					},
   105  				},
   106  				Application: map[string]interface{}{
   107  					"VCAP_APPLICATION": map[string]interface{}{
   108  						"dis-be-an-app-field": "wit-an-app-value",
   109  						"app-key-1":           0,
   110  						"app-key-2":           false,
   111  					},
   112  				},
   113  			}, nil)
   114  		})
   115  
   116  		It("lists those environment variables, in sorted order for provided services", func() {
   117  			runCommand("my-app")
   118  			Expect(appRepo.ReadEnvArgsForCall(0)).To(Equal("the-app-guid"))
   119  			Expect(ui.Outputs()).To(ContainSubstrings(
   120  				[]string{"Getting env variables for app", "my-app", "my-org", "my-space", "my-user"},
   121  				[]string{"OK"},
   122  				[]string{"System-Provided:"},
   123  				[]string{"VCAP_SERVICES", ":", "{"},
   124  				[]string{"pump-yer-brakes", ":", "drive-slow"},
   125  				[]string{"}"},
   126  				[]string{"User-Provided:"},
   127  				[]string{"first-bool", "false"},
   128  				[]string{"first-key", "0"},
   129  				[]string{"my-key", "my-value"},
   130  				[]string{"my-key2", "my-value2"},
   131  			))
   132  		})
   133  		It("displays the application env info under the System env column", func() {
   134  			runCommand("my-app")
   135  			Expect(ui.Outputs()).To(ContainSubstrings(
   136  				[]string{"Getting env variables for app", "my-app", "my-org", "my-space", "my-user"},
   137  				[]string{"OK"},
   138  				[]string{"System-Provided:"},
   139  				[]string{"VCAP_SERVICES", ":", "{"},
   140  				[]string{"pump-yer-brakes", ":", "drive-slow"},
   141  				[]string{"}"},
   142  				[]string{"VCAP_APPLICATION", ":", "{"},
   143  				[]string{"dis-be-an-app-field", ":", "wit-an-app-value"},
   144  				[]string{"app-key-1", ":", "0"},
   145  				[]string{"app-key-2", ":", "false"},
   146  				[]string{"}"},
   147  			))
   148  		})
   149  	})
   150  
   151  	Context("when the app has no user-defined environment variables", func() {
   152  		It("shows an empty message", func() {
   153  			appRepo.ReadEnvReturns(&models.Environment{}, nil)
   154  			runCommand("my-app")
   155  
   156  			Expect(ui.Outputs()).To(ContainSubstrings(
   157  				[]string{"Getting env variables for app", "my-app"},
   158  				[]string{"OK"},
   159  				[]string{"No", "system-provided", "env variables", "have been set"},
   160  				[]string{"No", "env variables", "have been set"},
   161  			))
   162  		})
   163  	})
   164  
   165  	Context("when the app has no environment variables", func() {
   166  		It("informs the user that each group is empty", func() {
   167  			appRepo.ReadEnvReturns(&models.Environment{}, nil)
   168  
   169  			runCommand("my-app")
   170  			Expect(ui.Outputs()).To(ContainSubstrings([]string{"No system-provided env variables have been set"}))
   171  			Expect(ui.Outputs()).To(ContainSubstrings([]string{"No user-defined env variables have been set"}))
   172  			Expect(ui.Outputs()).To(ContainSubstrings([]string{"No running env variables have been set"}))
   173  			Expect(ui.Outputs()).To(ContainSubstrings([]string{"No staging env variables have been set"}))
   174  		})
   175  	})
   176  
   177  	Context("when the app has at least one running and staging environment variable", func() {
   178  		BeforeEach(func() {
   179  			app = models.Application{}
   180  			app.Name = "my-app"
   181  			app.GUID = "the-app-guid"
   182  
   183  			appRepo.ReadReturns(app, nil)
   184  			appRepo.ReadEnvReturns(&models.Environment{
   185  				Running: map[string]interface{}{
   186  					"running-key-1": "running-value-1",
   187  					"running-key-2": "running-value-2",
   188  					"running":       true,
   189  					"number":        37,
   190  				},
   191  				Staging: map[string]interface{}{
   192  					"staging-key-1": "staging-value-1",
   193  					"staging-key-2": "staging-value-2",
   194  					"staging":       false,
   195  					"number":        42,
   196  				},
   197  			}, nil)
   198  		})
   199  
   200  		It("lists the environment variables", func() {
   201  			runCommand("my-app")
   202  			Expect(appRepo.ReadEnvArgsForCall(0)).To(Equal("the-app-guid"))
   203  			Expect(ui.Outputs()).To(ContainSubstrings(
   204  				[]string{"Getting env variables for app", "my-app", "my-org", "my-space", "my-user"},
   205  				[]string{"OK"},
   206  				[]string{"Running Environment Variable Groups:"},
   207  				[]string{"running-key-1", ":", "running-value-1"},
   208  				[]string{"running-key-2", ":", "running-value-2"},
   209  				[]string{"running", ":", "true"},
   210  				[]string{"number", ":", "37"},
   211  				[]string{"Staging Environment Variable Groups:"},
   212  				[]string{"staging-key-1", ":", "staging-value-1"},
   213  				[]string{"staging-key-2", ":", "staging-value-2"},
   214  				[]string{"staging", ":", "false"},
   215  				[]string{"number", ":", "42"},
   216  			))
   217  		})
   218  	})
   219  
   220  	Context("when reading the environment variables returns an error", func() {
   221  		It("tells you about that error", func() {
   222  			appRepo.ReadEnvReturns(nil, errors.New("BOO YOU CANT DO THAT; GO HOME; you're drunk"))
   223  			runCommand("whatever")
   224  			Expect(ui.Outputs()).To(ContainSubstrings([]string{"you're drunk"}))
   225  		})
   226  	})
   227  })