github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/application/set_health_check_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/cloudfoundry/cli/cf/api/applications/applicationsfakes"
     7  	"github.com/cloudfoundry/cli/cf/commandregistry"
     8  	"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
     9  	"github.com/cloudfoundry/cli/cf/models"
    10  	"github.com/cloudfoundry/cli/cf/requirements"
    11  	"github.com/cloudfoundry/cli/cf/requirements/requirementsfakes"
    12  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    13  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    14  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    15  
    16  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("set-health-check command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		requirementsFactory *requirementsfakes.FakeFactory
    25  		appRepo             *applicationsfakes.FakeRepository
    26  		configRepo          coreconfig.Repository
    27  		deps                commandregistry.Dependency
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		ui = &testterm.FakeUI{}
    32  		configRepo = testconfig.NewRepositoryWithDefaults()
    33  		requirementsFactory = new(requirementsfakes.FakeFactory)
    34  		appRepo = new(applicationsfakes.FakeRepository)
    35  	})
    36  
    37  	updateCommandDependency := func(pluginCall bool) {
    38  		deps.UI = ui
    39  		deps.Config = configRepo
    40  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    41  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("set-health-check").SetDependency(deps, pluginCall))
    42  	}
    43  
    44  	runCommand := func(args ...string) bool {
    45  		return testcmd.RunCLICommand("set-health-check", args, requirementsFactory, updateCommandDependency, false, ui)
    46  	}
    47  
    48  	Describe("requirements", func() {
    49  		It("fails with usage when called without enough arguments", func() {
    50  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    51  
    52  			runCommand("FAKE_APP")
    53  			Expect(ui.Outputs()).To(ContainSubstrings(
    54  				[]string{"Incorrect Usage", "Requires", "argument"},
    55  			))
    56  		})
    57  
    58  		It("fails with usage when health_check_type is not provided with 'none' or 'port'", func() {
    59  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    60  
    61  			runCommand("FAKE_APP", "bad_type")
    62  			Expect(ui.Outputs()).To(ContainSubstrings(
    63  				[]string{"Incorrect Usage", "HEALTH_CHECK_TYPE", "port", "none"},
    64  			))
    65  		})
    66  
    67  		It("fails requirements when not logged in", func() {
    68  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    69  			Expect(runCommand("my-app", "none")).To(BeFalse())
    70  		})
    71  
    72  		It("fails if a space is not targeted", func() {
    73  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    74  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
    75  			Expect(runCommand("my-app", "none")).To(BeFalse())
    76  		})
    77  	})
    78  
    79  	Describe("setting health_check_type", func() {
    80  		var (
    81  			app models.Application
    82  		)
    83  
    84  		BeforeEach(func() {
    85  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    86  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    87  
    88  			app = models.Application{}
    89  			app.Name = "my-app"
    90  			app.GUID = "my-app-guid"
    91  			app.HealthCheckType = "none"
    92  
    93  			applicationReq := new(requirementsfakes.FakeApplicationRequirement)
    94  			applicationReq.GetApplicationReturns(app)
    95  			requirementsFactory.NewApplicationRequirementReturns(applicationReq)
    96  		})
    97  
    98  		Context("when health_check_type is already set to the desired state", func() {
    99  			It("notifies the user", func() {
   100  				runCommand("my-app", "none")
   101  
   102  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"my-app", "already set to 'none'"}))
   103  			})
   104  		})
   105  
   106  		Context("Updating health_check_type when not already set to the desired state", func() {
   107  			Context("Update successfully", func() {
   108  				BeforeEach(func() {
   109  					app = models.Application{}
   110  					app.Name = "my-app"
   111  					app.GUID = "my-app-guid"
   112  					app.HealthCheckType = "port"
   113  
   114  					appRepo.UpdateReturns(app, nil)
   115  				})
   116  
   117  				It("updates the app's health_check_type", func() {
   118  					runCommand("my-app", "port")
   119  
   120  					Expect(appRepo.UpdateCallCount()).To(Equal(1))
   121  					appGUID, params := appRepo.UpdateArgsForCall(0)
   122  					Expect(appGUID).To(Equal("my-app-guid"))
   123  					Expect(*params.HealthCheckType).To(Equal("port"))
   124  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"Updating", "my-app", "port"}))
   125  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   126  				})
   127  			})
   128  
   129  			Context("Update fails", func() {
   130  				It("notifies user of any api error", func() {
   131  					appRepo.UpdateReturns(models.Application{}, errors.New("Error updating app."))
   132  					runCommand("my-app", "port")
   133  
   134  					Expect(appRepo.UpdateCallCount()).To(Equal(1))
   135  					Expect(ui.Outputs()).To(ContainSubstrings(
   136  						[]string{"FAILED"},
   137  						[]string{"Error updating app"},
   138  					))
   139  
   140  				})
   141  
   142  				It("notifies user when updated result is not in the desired state", func() {
   143  					app = models.Application{}
   144  					app.Name = "my-app"
   145  					app.GUID = "my-app-guid"
   146  					app.HealthCheckType = "none"
   147  					appRepo.UpdateReturns(app, nil)
   148  
   149  					runCommand("my-app", "port")
   150  
   151  					Expect(appRepo.UpdateCallCount()).To(Equal(1))
   152  					Expect(ui.Outputs()).To(ContainSubstrings(
   153  						[]string{"FAILED"},
   154  						[]string{"health_check_type", "not set"},
   155  					))
   156  
   157  				})
   158  			})
   159  		})
   160  	})
   161  
   162  })