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

     1  package application_test
     2  
     3  import (
     4  	"time"
     5  
     6  	testAppInstanaces "github.com/cloudfoundry/cli/cf/api/app_instances/fakes"
     7  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     8  	. "github.com/cloudfoundry/cli/cf/commands/application"
     9  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    10  	"github.com/cloudfoundry/cli/cf/errors"
    11  	"github.com/cloudfoundry/cli/cf/formatters"
    12  	"github.com/cloudfoundry/cli/cf/models"
    13  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    14  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    15  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    16  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    17  	testtime "github.com/cloudfoundry/cli/testhelpers/time"
    18  
    19  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("app Command", func() {
    25  	var (
    26  		ui                  *testterm.FakeUI
    27  		configRepo          core_config.ReadWriter
    28  		appSummaryRepo      *testapi.FakeAppSummaryRepo
    29  		appInstancesRepo    *testAppInstanaces.FakeAppInstancesRepository
    30  		requirementsFactory *testreq.FakeReqFactory
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		ui = &testterm.FakeUI{}
    35  		appSummaryRepo = &testapi.FakeAppSummaryRepo{}
    36  		appInstancesRepo = &testAppInstanaces.FakeAppInstancesRepository{}
    37  		configRepo = testconfig.NewRepositoryWithDefaults()
    38  		requirementsFactory = &testreq.FakeReqFactory{
    39  			LoginSuccess:         true,
    40  			TargetedSpaceSuccess: true,
    41  		}
    42  	})
    43  
    44  	runCommand := func(args ...string) bool {
    45  		cmd := NewShowApp(ui, configRepo, appSummaryRepo, appInstancesRepo)
    46  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    47  	}
    48  
    49  	Describe("requirements", func() {
    50  		It("fails if not logged in", func() {
    51  			requirementsFactory.LoginSuccess = false
    52  			Expect(runCommand("cf-plays-dwarf-fortress")).To(BeFalse())
    53  		})
    54  
    55  		It("fails if a space is not targeted", func() {
    56  			requirementsFactory.TargetedSpaceSuccess = false
    57  			Expect(runCommand("cf-plays-dwarf-fortress")).To(BeFalse())
    58  		})
    59  
    60  		It("fails with usage when not provided exactly one arg", func() {
    61  			passed := runCommand()
    62  			Expect(ui.FailedWithUsage).To(BeTrue())
    63  			Expect(passed).To(BeFalse())
    64  		})
    65  
    66  	})
    67  
    68  	Describe("displaying a summary of an app", func() {
    69  		BeforeEach(func() {
    70  			app := makeAppWithRoute("my-app")
    71  			appInstance := models.AppInstanceFields{
    72  				State:     models.InstanceRunning,
    73  				Since:     testtime.MustParse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2012"),
    74  				CpuUsage:  1.0,
    75  				DiskQuota: 1 * formatters.GIGABYTE,
    76  				DiskUsage: 32 * formatters.MEGABYTE,
    77  				MemQuota:  64 * formatters.MEGABYTE,
    78  				MemUsage:  13 * formatters.BYTE,
    79  			}
    80  
    81  			appInstance2 := models.AppInstanceFields{
    82  				State: models.InstanceDown,
    83  				Since: testtime.MustParse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Apr 1 15:04:05 -0700 MST 2012"),
    84  			}
    85  
    86  			instances := []models.AppInstanceFields{appInstance, appInstance2}
    87  
    88  			appSummaryRepo.GetSummarySummary = app
    89  			appInstancesRepo.GetInstancesReturns(instances, nil)
    90  			requirementsFactory.Application = app
    91  		})
    92  
    93  		It("displays a summary of the app", func() {
    94  			runCommand("my-app")
    95  
    96  			Expect(appSummaryRepo.GetSummaryAppGuid).To(Equal("app-guid"))
    97  
    98  			Expect(ui.Outputs).To(ContainSubstrings(
    99  				[]string{"Showing health and status", "my-app"},
   100  				[]string{"state", "started"},
   101  				[]string{"instances", "2/2"},
   102  				[]string{"usage", "256M x 2 instances"},
   103  				[]string{"urls", "my-app.example.com", "foo.example.com"},
   104  				[]string{"last uploaded", "Wed Oct 24 19:54:00 UTC 2012"},
   105  				[]string{"#0", "running", "2012-01-02 03:04:05 PM", "100.0%", "13 of 64M", "32M of 1G"},
   106  				[]string{"#1", "down", "2012-04-01 03:04:05 PM", "0%", "0 of 0", "0 of 0"},
   107  			))
   108  		})
   109  
   110  		Describe("when the package updated at is nil", func() {
   111  			BeforeEach(func() {
   112  				appSummaryRepo.GetSummarySummary.PackageUpdatedAt = nil
   113  			})
   114  
   115  			It("should output whatever greg sez", func() {
   116  				runCommand("my-app")
   117  				Expect(ui.Outputs).To(ContainSubstrings(
   118  					[]string{"last uploaded", "unknown"},
   119  				))
   120  			})
   121  		})
   122  	})
   123  
   124  	Describe("when the app is not running", func() {
   125  		BeforeEach(func() {
   126  			application := models.Application{}
   127  			application.Name = "my-app"
   128  			application.Guid = "my-app-guid"
   129  			application.State = "stopped"
   130  			application.InstanceCount = 2
   131  			application.RunningInstances = 0
   132  			application.Memory = 256
   133  			now := time.Now()
   134  			application.PackageUpdatedAt = &now
   135  
   136  			appSummaryRepo.GetSummarySummary = application
   137  			requirementsFactory.Application = application
   138  		})
   139  
   140  		It("displays nice output when the app is stopped", func() {
   141  			appSummaryRepo.GetSummaryErrorCode = errors.APP_STOPPED
   142  			runCommand("my-app")
   143  
   144  			Expect(appSummaryRepo.GetSummaryAppGuid).To(Equal("my-app-guid"))
   145  			Expect(appInstancesRepo.GetInstancesArgsForCall(0)).To(Equal("my-app-guid"))
   146  
   147  			Expect(ui.Outputs).To(ContainSubstrings(
   148  				[]string{"Showing health and status", "my-app", "my-org", "my-space", "my-user"},
   149  				[]string{"state", "stopped"},
   150  				[]string{"instances", "0/2"},
   151  				[]string{"usage", "256M x 2 instances"},
   152  				[]string{"no running instances"},
   153  			))
   154  		})
   155  
   156  		It("displays nice output when the app has not yet finished staging", func() {
   157  			appSummaryRepo.GetSummaryErrorCode = errors.APP_NOT_STAGED
   158  			runCommand("my-app")
   159  
   160  			Expect(appSummaryRepo.GetSummaryAppGuid).To(Equal("my-app-guid"))
   161  			Expect(appInstancesRepo.GetInstancesArgsForCall(0)).To(Equal("my-app-guid"))
   162  
   163  			Expect(ui.Outputs).To(ContainSubstrings(
   164  				[]string{"Showing health and status", "my-app", "my-org", "my-space", "my-user"},
   165  				[]string{"state", "stopped"},
   166  				[]string{"instances", "0/2"},
   167  				[]string{"usage", "256M x 2 instances"},
   168  				[]string{"no running instances"},
   169  			))
   170  		})
   171  	})
   172  
   173  	Describe("when running instances is unknown", func() {
   174  		BeforeEach(func() {
   175  			app := makeAppWithRoute("my-app")
   176  			app.RunningInstances = -1
   177  			appInstance := models.AppInstanceFields{
   178  				State:     models.InstanceRunning,
   179  				Since:     testtime.MustParse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2012"),
   180  				CpuUsage:  5.0,
   181  				DiskQuota: 4 * formatters.GIGABYTE,
   182  				DiskUsage: 3 * formatters.GIGABYTE,
   183  				MemQuota:  2 * formatters.GIGABYTE,
   184  				MemUsage:  1 * formatters.GIGABYTE,
   185  			}
   186  
   187  			appInstance2 := models.AppInstanceFields{
   188  				State: models.InstanceRunning,
   189  				Since: testtime.MustParse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Apr 1 15:04:05 -0700 MST 2012"),
   190  			}
   191  
   192  			instances := []models.AppInstanceFields{appInstance, appInstance2}
   193  
   194  			appSummaryRepo.GetSummarySummary = app
   195  			appInstancesRepo.GetInstancesReturns(instances, nil)
   196  			requirementsFactory.Application = app
   197  		})
   198  
   199  		It("displays a '?' for running instances", func() {
   200  			runCommand("my-app")
   201  
   202  			Expect(appSummaryRepo.GetSummaryAppGuid).To(Equal("app-guid"))
   203  			Expect(appSummaryRepo.GetSummaryAppGuid).To(Equal("app-guid"))
   204  
   205  			Expect(ui.Outputs).To(ContainSubstrings(
   206  				[]string{"Showing health and status", "my-app"},
   207  				[]string{"state", "started"},
   208  				[]string{"instances", "?/2"},
   209  				[]string{"usage", "256M x 2 instances"},
   210  				[]string{"urls", "my-app.example.com", "foo.example.com"},
   211  				[]string{"#0", "running", "2012-01-02 03:04:05 PM", "500.0%", "1G of 2G", "3G of 4G"},
   212  				[]string{"#1", "running", "2012-04-01 03:04:05 PM", "0%", "0 of 0", "0 of 0"},
   213  			))
   214  		})
   215  	})
   216  
   217  	Describe("when the user passes the --guid flag", func() {
   218  		var app models.Application
   219  		BeforeEach(func() {
   220  			app = makeAppWithRoute("my-app")
   221  
   222  			requirementsFactory.Application = app
   223  		})
   224  
   225  		It("displays guid for the requested app", func() {
   226  			runCommand("--guid", "my-app")
   227  
   228  			Expect(ui.Outputs).To(ContainSubstrings(
   229  				[]string{app.Guid},
   230  			))
   231  			Expect(ui.Outputs).ToNot(ContainSubstrings(
   232  				[]string{"Showing health and status", "my-app"},
   233  			))
   234  		})
   235  	})
   236  })
   237  
   238  func makeAppWithRoute(appName string) models.Application {
   239  	application := models.Application{}
   240  	application.Name = appName
   241  	application.Guid = "app-guid"
   242  
   243  	domain := models.DomainFields{}
   244  	domain.Name = "example.com"
   245  
   246  	route := models.RouteSummary{Host: "foo", Domain: domain}
   247  	secondRoute := models.RouteSummary{Host: appName, Domain: domain}
   248  	packgeUpdatedAt, _ := time.Parse("2006-01-02T15:04:05Z07:00", "2012-10-24T19:54:00Z")
   249  
   250  	application.State = "started"
   251  	application.InstanceCount = 2
   252  	application.RunningInstances = 2
   253  	application.Memory = 256
   254  	application.Routes = []models.RouteSummary{route, secondRoute}
   255  	application.PackageUpdatedAt = &packgeUpdatedAt
   256  
   257  	return application
   258  }