github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    14  	"code.cloudfoundry.org/cli/plugin/models"
    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/cf/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  
    83  		app2 := models.Application{}
    84  		app2.Name = "Application-2"
    85  		app2.GUID = "Application-2-guid"
    86  		app2.State = "started"
    87  		app2.RunningInstances = 1
    88  		app2.InstanceCount = 2
    89  		app2.Memory = 256
    90  		app2.DiskQuota = 1024
    91  		app2.Routes = app2Routes
    92  
    93  		appSummaryRepo.GetSummariesInCurrentSpaceApps = []models.Application{app, app2}
    94  
    95  		deps = commandregistry.NewDependency(os.Stdout, new(tracefakes.FakePrinter), "")
    96  	})
    97  
    98  	runCommand := func(args ...string) bool {
    99  		return testcmd.RunCLICommand("apps", args, requirementsFactory, updateCommandDependency, false, ui)
   100  	}
   101  
   102  	Describe("requirements", func() {
   103  		var cmd commandregistry.Command
   104  		var flagContext flags.FlagContext
   105  
   106  		BeforeEach(func() {
   107  			cmd = &application.ListApps{}
   108  			cmd.SetDependency(deps, false)
   109  			flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
   110  
   111  		})
   112  
   113  		It("requires the user to be logged in", func() {
   114  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
   115  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
   116  			Expect(err).NotTo(HaveOccurred())
   117  
   118  			Expect(testcmd.RunRequirements(reqs)).To(HaveOccurred())
   119  		})
   120  
   121  		It("requires the user to have a space targeted", func() {
   122  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
   123  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
   124  			Expect(err).NotTo(HaveOccurred())
   125  
   126  			Expect(testcmd.RunRequirements(reqs)).To(HaveOccurred())
   127  		})
   128  
   129  		It("should fail with usage when provided any arguments", func() {
   130  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
   131  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
   132  
   133  			flagContext.Parse("blahblah")
   134  
   135  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
   136  			Expect(err).NotTo(HaveOccurred())
   137  
   138  			err = testcmd.RunRequirements(reqs)
   139  			Expect(err).To(HaveOccurred())
   140  			Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
   141  			Expect(err.Error()).To(ContainSubstring("No argument required"))
   142  		})
   143  
   144  		It("succeeds with all", func() {
   145  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
   146  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
   147  
   148  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
   149  			Expect(err).NotTo(HaveOccurred())
   150  
   151  			Expect(testcmd.RunRequirements(reqs)).NotTo(HaveOccurred())
   152  		})
   153  	})
   154  
   155  	Describe("when invoked by a plugin", func() {
   156  		var (
   157  			pluginAppModels []plugin_models.GetAppsModel
   158  		)
   159  
   160  		BeforeEach(func() {
   161  			pluginAppModels = []plugin_models.GetAppsModel{}
   162  			deps.PluginModels.AppsSummary = &pluginAppModels
   163  		})
   164  
   165  		It("populates the plugin models upon execution", func() {
   166  			testcmd.RunCLICommand("apps", []string{}, requirementsFactory, updateCommandDependency, true, ui)
   167  
   168  			Expect(pluginAppModels[0].Name).To(Equal("Application-1"))
   169  			Expect(pluginAppModels[0].Guid).To(Equal("Application-1-guid"))
   170  			Expect(pluginAppModels[1].Name).To(Equal("Application-2"))
   171  			Expect(pluginAppModels[1].Guid).To(Equal("Application-2-guid"))
   172  			Expect(pluginAppModels[0].State).To(Equal("started"))
   173  			Expect(pluginAppModels[0].TotalInstances).To(Equal(1))
   174  			Expect(pluginAppModels[0].RunningInstances).To(Equal(1))
   175  			Expect(pluginAppModels[0].Memory).To(Equal(int64(512)))
   176  			Expect(pluginAppModels[0].DiskQuota).To(Equal(int64(1024)))
   177  			Expect(pluginAppModels[0].Routes[0].Host).To(Equal("app1"))
   178  			Expect(pluginAppModels[0].Routes[1].Host).To(Equal("app1"))
   179  			Expect(pluginAppModels[0].Routes[0].Domain.Name).To(Equal("cfapps.io"))
   180  			Expect(pluginAppModels[0].Routes[0].Domain.Shared).To(BeTrue())
   181  			Expect(pluginAppModels[0].Routes[0].Domain.OwningOrganizationGuid).To(Equal("org-123"))
   182  			Expect(pluginAppModels[0].Routes[0].Domain.Guid).To(Equal("domain-guid"))
   183  		})
   184  	})
   185  
   186  	Context("when the user is logged in and a space is targeted", func() {
   187  		It("lists apps in a table", func() {
   188  			runCommand()
   189  
   190  			Expect(ui.Outputs()).To(ContainSubstrings(
   191  				[]string{"Getting apps in", "my-org", "my-space", "my-user"},
   192  				[]string{"OK"},
   193  				[]string{"name", "requested state", "instances", "memory", "disk", "urls"},
   194  				[]string{"Application-1", "started", "1/1", "512M", "1G", "app1.cfapps.io", "app1.example.com"},
   195  				[]string{"Application-2", "started", "1/2", "256M", "1G", "app2.cfapps.io"},
   196  			))
   197  		})
   198  
   199  		Context("when an app's running instances is unknown", func() {
   200  			It("dipslays a '?' for running instances", func() {
   201  				appRoutes := []models.RouteSummary{
   202  					{
   203  						Host:   "app1",
   204  						Domain: models.DomainFields{Name: "cfapps.io"},
   205  					}}
   206  				app := models.Application{}
   207  				app.Name = "Application-1"
   208  				app.GUID = "Application-1-guid"
   209  				app.State = "started"
   210  				app.RunningInstances = -1
   211  				app.InstanceCount = 2
   212  				app.Memory = 512
   213  				app.DiskQuota = 1024
   214  				app.Routes = appRoutes
   215  
   216  				appSummaryRepo.GetSummariesInCurrentSpaceApps = []models.Application{app}
   217  
   218  				runCommand()
   219  
   220  				Expect(ui.Outputs()).To(ContainSubstrings(
   221  					[]string{"Getting apps in", "my-org", "my-space", "my-user"},
   222  					[]string{"OK"},
   223  					[]string{"Application-1", "started", "?/2", "512M", "1G", "app1.cfapps.io"},
   224  				))
   225  			})
   226  		})
   227  
   228  		Context("when there are no apps", func() {
   229  			It("tells the user that there are no apps", func() {
   230  				appSummaryRepo.GetSummariesInCurrentSpaceApps = []models.Application{}
   231  
   232  				runCommand()
   233  				Expect(ui.Outputs()).To(ContainSubstrings(
   234  					[]string{"Getting apps in", "my-org", "my-space", "my-user"},
   235  					[]string{"OK"},
   236  					[]string{"No apps found"},
   237  				))
   238  			})
   239  		})
   240  	})
   241  })