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

     1  package application_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  	"code.cloudfoundry.org/cli/cf/requirements"
     9  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    10  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    11  	"code.cloudfoundry.org/cli/plugin/models"
    12  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  
    18  	"os"
    19  
    20  	"code.cloudfoundry.org/cli/cf/commands/application"
    21  	"code.cloudfoundry.org/cli/cf/flags"
    22  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    23  )
    24  
    25  var _ = Describe("list-apps command", func() {
    26  	var (
    27  		ui                  *testterm.FakeUI
    28  		configRepo          coreconfig.Repository
    29  		appSummaryRepo      *apifakes.OldFakeAppSummaryRepo
    30  		requirementsFactory *requirementsfakes.FakeFactory
    31  		deps                commandregistry.Dependency
    32  	)
    33  
    34  	updateCommandDependency := func(pluginCall bool) {
    35  		deps.UI = ui
    36  		deps.Config = configRepo
    37  		deps.RepoLocator = deps.RepoLocator.SetAppSummaryRepository(appSummaryRepo)
    38  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("apps").SetDependency(deps, pluginCall))
    39  	}
    40  
    41  	BeforeEach(func() {
    42  		ui = &testterm.FakeUI{}
    43  		appSummaryRepo = new(apifakes.OldFakeAppSummaryRepo)
    44  		configRepo = testconfig.NewRepositoryWithDefaults()
    45  		requirementsFactory = new(requirementsfakes.FakeFactory)
    46  
    47  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    48  		requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    49  
    50  		app1Routes := []models.RouteSummary{
    51  			{
    52  				Host: "app1",
    53  				Domain: models.DomainFields{
    54  					Name:                   "cfapps.io",
    55  					Shared:                 true,
    56  					OwningOrganizationGUID: "org-123",
    57  					GUID: "domain-guid",
    58  				},
    59  			},
    60  			{
    61  				Host: "app1",
    62  				Domain: models.DomainFields{
    63  					Name: "example.com",
    64  				},
    65  			}}
    66  
    67  		app2Routes := []models.RouteSummary{
    68  			{
    69  				Host:   "app2",
    70  				Domain: models.DomainFields{Name: "cfapps.io"},
    71  			}}
    72  
    73  		app := models.Application{}
    74  		app.Name = "Application-1"
    75  		app.GUID = "Application-1-guid"
    76  		app.State = "started"
    77  		app.RunningInstances = 1
    78  		app.InstanceCount = 1
    79  		app.Memory = 512
    80  		app.DiskQuota = 1024
    81  		app.Routes = app1Routes
    82  		app.AppPorts = []int{8080, 9090}
    83  
    84  		app2 := models.Application{}
    85  		app2.Name = "Application-2"
    86  		app2.GUID = "Application-2-guid"
    87  		app2.State = "started"
    88  		app2.RunningInstances = 1
    89  		app2.InstanceCount = 2
    90  		app2.Memory = 256
    91  		app2.DiskQuota = 1024
    92  		app2.Routes = app2Routes
    93  
    94  		appSummaryRepo.GetSummariesInCurrentSpaceApps = []models.Application{app, app2}
    95  
    96  		deps = commandregistry.NewDependency(os.Stdout, new(tracefakes.FakePrinter), "")
    97  	})
    98  
    99  	runCommand := func(args ...string) bool {
   100  		return testcmd.RunCLICommand("apps", args, requirementsFactory, updateCommandDependency, false, ui)
   101  	}
   102  
   103  	Describe("requirements", func() {
   104  		var cmd commandregistry.Command
   105  		var flagContext flags.FlagContext
   106  
   107  		BeforeEach(func() {
   108  			cmd = &application.ListApps{}
   109  			cmd.SetDependency(deps, false)
   110  			flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
   111  
   112  		})
   113  
   114  		It("requires the user to be logged in", func() {
   115  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
   116  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
   117  			Expect(err).NotTo(HaveOccurred())
   118  
   119  			Expect(testcmd.RunRequirements(reqs)).To(HaveOccurred())
   120  		})
   121  
   122  		It("requires the user to have a space targeted", func() {
   123  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
   124  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
   125  			Expect(err).NotTo(HaveOccurred())
   126  
   127  			Expect(testcmd.RunRequirements(reqs)).To(HaveOccurred())
   128  		})
   129  
   130  		It("should fail with usage when provided any arguments", func() {
   131  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
   132  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
   133  
   134  			flagContext.Parse("blahblah")
   135  
   136  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
   137  			Expect(err).NotTo(HaveOccurred())
   138  
   139  			err = testcmd.RunRequirements(reqs)
   140  			Expect(err).To(HaveOccurred())
   141  			Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
   142  			Expect(err.Error()).To(ContainSubstring("No argument required"))
   143  		})
   144  
   145  		It("succeeds with all", func() {
   146  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
   147  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
   148  
   149  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
   150  			Expect(err).NotTo(HaveOccurred())
   151  
   152  			Expect(testcmd.RunRequirements(reqs)).NotTo(HaveOccurred())
   153  		})
   154  	})
   155  
   156  	Describe("when invoked by a plugin", func() {
   157  		var (
   158  			pluginAppModels []plugin_models.GetAppsModel
   159  		)
   160  
   161  		BeforeEach(func() {
   162  			pluginAppModels = []plugin_models.GetAppsModel{}
   163  			deps.PluginModels.AppsSummary = &pluginAppModels
   164  		})
   165  
   166  		It("populates the plugin models upon execution", func() {
   167  			testcmd.RunCLICommand("apps", []string{}, requirementsFactory, updateCommandDependency, true, ui)
   168  
   169  			Expect(pluginAppModels[0].Name).To(Equal("Application-1"))
   170  			Expect(pluginAppModels[0].Guid).To(Equal("Application-1-guid"))
   171  			Expect(pluginAppModels[1].Name).To(Equal("Application-2"))
   172  			Expect(pluginAppModels[1].Guid).To(Equal("Application-2-guid"))
   173  			Expect(pluginAppModels[0].State).To(Equal("started"))
   174  			Expect(pluginAppModels[0].TotalInstances).To(Equal(1))
   175  			Expect(pluginAppModels[0].RunningInstances).To(Equal(1))
   176  			Expect(pluginAppModels[0].Memory).To(Equal(int64(512)))
   177  			Expect(pluginAppModels[0].DiskQuota).To(Equal(int64(1024)))
   178  			// Commented to hide app-ports for release #117189491
   179  			// Expect(pluginAppModels[0].AppPorts).To(Equal([]int{8080, 9090}))
   180  			Expect(pluginAppModels[0].Routes[0].Host).To(Equal("app1"))
   181  			Expect(pluginAppModels[0].Routes[1].Host).To(Equal("app1"))
   182  			Expect(pluginAppModels[0].Routes[0].Domain.Name).To(Equal("cfapps.io"))
   183  			Expect(pluginAppModels[0].Routes[0].Domain.Shared).To(BeTrue())
   184  			Expect(pluginAppModels[0].Routes[0].Domain.OwningOrganizationGuid).To(Equal("org-123"))
   185  			Expect(pluginAppModels[0].Routes[0].Domain.Guid).To(Equal("domain-guid"))
   186  		})
   187  	})
   188  
   189  	Context("when the user is logged in and a space is targeted", func() {
   190  		It("lists apps in a table", func() {
   191  			runCommand()
   192  
   193  			Expect(ui.Outputs()).To(ContainSubstrings(
   194  				[]string{"Getting apps in", "my-org", "my-space", "my-user"},
   195  				[]string{"OK"},
   196  				[]string{"name", "requested state", "instances", "memory", "disk", "urls"},
   197  				[]string{"Application-1", "started", "1/1", "512M", "1G", "app1.cfapps.io", "app1.example.com"},
   198  				[]string{"Application-2", "started", "1/2", "256M", "1G", "app2.cfapps.io"},
   199  			))
   200  		})
   201  
   202  		Context("when an app's running instances is unknown", func() {
   203  			It("dipslays a '?' for running instances", func() {
   204  				appRoutes := []models.RouteSummary{
   205  					{
   206  						Host:   "app1",
   207  						Domain: models.DomainFields{Name: "cfapps.io"},
   208  					}}
   209  				app := models.Application{}
   210  				app.Name = "Application-1"
   211  				app.GUID = "Application-1-guid"
   212  				app.State = "started"
   213  				app.RunningInstances = -1
   214  				app.InstanceCount = 2
   215  				app.Memory = 512
   216  				app.DiskQuota = 1024
   217  				app.Routes = appRoutes
   218  
   219  				appSummaryRepo.GetSummariesInCurrentSpaceApps = []models.Application{app}
   220  
   221  				runCommand()
   222  
   223  				Expect(ui.Outputs()).To(ContainSubstrings(
   224  					[]string{"Getting apps in", "my-org", "my-space", "my-user"},
   225  					[]string{"OK"},
   226  					[]string{"Application-1", "started", "?/2", "512M", "1G", "app1.cfapps.io"},
   227  				))
   228  			})
   229  		})
   230  
   231  		Context("when there are no apps", func() {
   232  			It("tells the user that there are no apps", func() {
   233  				appSummaryRepo.GetSummariesInCurrentSpaceApps = []models.Application{}
   234  
   235  				runCommand()
   236  				Expect(ui.Outputs()).To(ContainSubstrings(
   237  					[]string{"Getting apps in", "my-org", "my-space", "my-user"},
   238  					[]string{"OK"},
   239  					[]string{"No apps found"},
   240  				))
   241  			})
   242  		})
   243  	})
   244  })