github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/application/env_test.go (about)

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