github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/service/service_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/command_registry"
     5  	"github.com/cloudfoundry/cli/cf/models"
     6  	"github.com/cloudfoundry/cli/plugin/models"
     7  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     8  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
     9  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    10  
    11  	. "github.com/cloudfoundry/cli/cf/commands/service"
    12  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("service command", func() {
    18  	var (
    19  		ui                  *testterm.FakeUI
    20  		requirementsFactory *testreq.FakeReqFactory
    21  		deps                command_registry.Dependency
    22  	)
    23  
    24  	updateCommandDependency := func(pluginCall bool) {
    25  		deps.Ui = ui
    26  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("service").SetDependency(deps, pluginCall))
    27  	}
    28  
    29  	BeforeEach(func() {
    30  		ui = &testterm.FakeUI{}
    31  		requirementsFactory = &testreq.FakeReqFactory{}
    32  
    33  		deps = command_registry.NewDependency()
    34  	})
    35  
    36  	runCommand := func(args ...string) bool {
    37  		return testcmd.RunCliCommand("service", args, requirementsFactory, updateCommandDependency, false)
    38  	}
    39  
    40  	Describe("requirements", func() {
    41  		It("fails when not provided the name of the service to show", func() {
    42  			requirementsFactory.LoginSuccess = true
    43  			requirementsFactory.TargetedSpaceSuccess = true
    44  			runCommand()
    45  
    46  			Expect(ui.Outputs).To(ContainSubstrings(
    47  				[]string{"Incorrect Usage", "Requires an argument"},
    48  			))
    49  		})
    50  
    51  		It("fails when not logged in", func() {
    52  			requirementsFactory.TargetedSpaceSuccess = true
    53  
    54  			Expect(runCommand("come-ON")).To(BeFalse())
    55  		})
    56  
    57  		It("fails when a space is not targeted", func() {
    58  			requirementsFactory.LoginSuccess = true
    59  
    60  			Expect(runCommand("okay-this-time-please??")).To(BeFalse())
    61  		})
    62  	})
    63  
    64  	Describe("After Requirement", func() {
    65  		createServiceInstanceWithState := func(state string) {
    66  			offering := models.ServiceOfferingFields{Label: "mysql", DocumentationUrl: "http://documentation.url", Description: "the-description"}
    67  			plan := models.ServicePlanFields{Guid: "plan-guid", Name: "plan-name"}
    68  
    69  			serviceInstance := models.ServiceInstance{}
    70  			serviceInstance.Name = "service1"
    71  			serviceInstance.Guid = "service1-guid"
    72  			serviceInstance.LastOperation.Type = "create"
    73  			serviceInstance.LastOperation.State = "in progress"
    74  			serviceInstance.LastOperation.Description = "creating resource - step 1"
    75  			serviceInstance.ServicePlan = plan
    76  			serviceInstance.ServiceOffering = offering
    77  			serviceInstance.DashboardUrl = "some-url"
    78  			serviceInstance.LastOperation.State = state
    79  			serviceInstance.LastOperation.CreatedAt = "created-date"
    80  			serviceInstance.LastOperation.UpdatedAt = "updated-date"
    81  			requirementsFactory.ServiceInstance = serviceInstance
    82  		}
    83  
    84  		createServiceInstance := func() {
    85  			createServiceInstanceWithState("")
    86  		}
    87  
    88  		Describe("when invoked by a plugin", func() {
    89  			var (
    90  				pluginModel *plugin_models.GetService_Model
    91  			)
    92  
    93  			BeforeEach(func() {
    94  				requirementsFactory.LoginSuccess = true
    95  				requirementsFactory.TargetedSpaceSuccess = true
    96  
    97  				pluginModel = &plugin_models.GetService_Model{}
    98  				deps.PluginModels.Service = pluginModel
    99  			})
   100  
   101  			It("populates the plugin model upon execution", func() {
   102  				createServiceInstanceWithState("in progress")
   103  				testcmd.RunCliCommand("service", []string{"service1"}, requirementsFactory, updateCommandDependency, true)
   104  				Ω(pluginModel.Name).To(Equal("service1"))
   105  				Ω(pluginModel.Guid).To(Equal("service1-guid"))
   106  				Ω(pluginModel.LastOperation.Type).To(Equal("create"))
   107  				Ω(pluginModel.LastOperation.State).To(Equal("in progress"))
   108  				Ω(pluginModel.LastOperation.Description).To(Equal("creating resource - step 1"))
   109  				Ω(pluginModel.LastOperation.CreatedAt).To(Equal("created-date"))
   110  				Ω(pluginModel.LastOperation.UpdatedAt).To(Equal("updated-date"))
   111  				Ω(pluginModel.LastOperation.Type).To(Equal("create"))
   112  				Ω(pluginModel.ServicePlan.Name).To(Equal("plan-name"))
   113  				Ω(pluginModel.ServicePlan.Guid).To(Equal("plan-guid"))
   114  				Ω(pluginModel.ServiceOffering.DocumentationUrl).To(Equal("http://documentation.url"))
   115  				Ω(pluginModel.ServiceOffering.Name).To(Equal("mysql"))
   116  			})
   117  		})
   118  
   119  		Context("when logged in, a space is targeted, and provided the name of a service that exists", func() {
   120  			BeforeEach(func() {
   121  				requirementsFactory.LoginSuccess = true
   122  				requirementsFactory.TargetedSpaceSuccess = true
   123  			})
   124  
   125  			Context("when the service is externally provided", func() {
   126  
   127  				It("shows the service", func() {
   128  					createServiceInstanceWithState("in progress")
   129  					runCommand("service1")
   130  
   131  					Expect(ui.Outputs).To(ContainSubstrings(
   132  						[]string{"Service instance:", "service1"},
   133  						[]string{"Service: ", "mysql"},
   134  						[]string{"Plan: ", "plan-name"},
   135  						[]string{"Description: ", "the-description"},
   136  						[]string{"Documentation url: ", "http://documentation.url"},
   137  						[]string{"Dashboard: ", "some-url"},
   138  						[]string{"Last Operation"},
   139  						[]string{"Status: ", "create in progress"},
   140  						[]string{"Message: ", "creating resource - step 1"},
   141  						[]string{"Started: ", "created-date"},
   142  						[]string{"Updated: ", "updated-date"},
   143  					))
   144  					Expect(requirementsFactory.ServiceInstanceName).To(Equal("service1"))
   145  				})
   146  
   147  				Context("when the service instance CreatedAt is empty", func() {
   148  					It("does not output the Started line", func() {
   149  						createServiceInstanceWithState("in progress")
   150  						requirementsFactory.ServiceInstance.LastOperation.CreatedAt = ""
   151  						runCommand("service1")
   152  
   153  						Expect(ui.Outputs).To(ContainSubstrings(
   154  							[]string{"Service instance:", "service1"},
   155  							[]string{"Service: ", "mysql"},
   156  							[]string{"Plan: ", "plan-name"},
   157  							[]string{"Description: ", "the-description"},
   158  							[]string{"Documentation url: ", "http://documentation.url"},
   159  							[]string{"Dashboard: ", "some-url"},
   160  							[]string{"Last Operation"},
   161  							[]string{"Status: ", "create in progress"},
   162  							[]string{"Message: ", "creating resource - step 1"},
   163  							[]string{"Updated: ", "updated-date"},
   164  						))
   165  						Expect(ui.Outputs).ToNot(ContainSubstrings(
   166  							[]string{"Started: "},
   167  						))
   168  					})
   169  				})
   170  
   171  				Context("shows correct status information based on service instance state", func() {
   172  					It("shows status: `create in progress` when state is `in progress`", func() {
   173  						createServiceInstanceWithState("in progress")
   174  						runCommand("service1")
   175  
   176  						Expect(ui.Outputs).To(ContainSubstrings(
   177  							[]string{"Status: ", "create in progress"},
   178  						))
   179  						Expect(requirementsFactory.ServiceInstanceName).To(Equal("service1"))
   180  					})
   181  
   182  					It("shows status: `create succeeded` when state is `succeeded`", func() {
   183  						createServiceInstanceWithState("succeeded")
   184  						runCommand("service1")
   185  
   186  						Expect(ui.Outputs).To(ContainSubstrings(
   187  							[]string{"Status: ", "create succeeded"},
   188  						))
   189  						Expect(requirementsFactory.ServiceInstanceName).To(Equal("service1"))
   190  					})
   191  
   192  					It("shows status: `create failed` when state is `failed`", func() {
   193  						createServiceInstanceWithState("failed")
   194  						runCommand("service1")
   195  
   196  						Expect(ui.Outputs).To(ContainSubstrings(
   197  							[]string{"Status: ", "create failed"},
   198  						))
   199  						Expect(requirementsFactory.ServiceInstanceName).To(Equal("service1"))
   200  					})
   201  
   202  					It("shows status: `` when state is ``", func() {
   203  						createServiceInstanceWithState("")
   204  						runCommand("service1")
   205  
   206  						Expect(ui.Outputs).To(ContainSubstrings(
   207  							[]string{"Status: ", ""},
   208  						))
   209  						Expect(requirementsFactory.ServiceInstanceName).To(Equal("service1"))
   210  					})
   211  				})
   212  
   213  				Context("when the guid flag is provided", func() {
   214  					It("shows only the service guid", func() {
   215  						createServiceInstance()
   216  						runCommand("--guid", "service1")
   217  
   218  						Expect(ui.Outputs).To(ContainSubstrings(
   219  							[]string{"service1-guid"},
   220  						))
   221  
   222  						Expect(ui.Outputs).ToNot(ContainSubstrings(
   223  							[]string{"Service instance:", "service1"},
   224  						))
   225  					})
   226  				})
   227  			})
   228  
   229  			Context("when the service is user provided", func() {
   230  				BeforeEach(func() {
   231  					serviceInstance := models.ServiceInstance{}
   232  					serviceInstance.Name = "service1"
   233  					serviceInstance.Guid = "service1-guid"
   234  					requirementsFactory.ServiceInstance = serviceInstance
   235  				})
   236  
   237  				It("shows user provided services", func() {
   238  					runCommand("service1")
   239  
   240  					Expect(ui.Outputs).To(ContainSubstrings(
   241  						[]string{"Service instance: ", "service1"},
   242  						[]string{"Service: ", "user-provided"},
   243  					))
   244  				})
   245  			})
   246  		})
   247  	})
   248  })
   249  
   250  var _ = Describe("ServiceInstanceStateToStatus", func() {
   251  	var operationType string
   252  	Context("when the service is not user provided", func() {
   253  		isUserProvided := false
   254  
   255  		Context("when operationType is `create`", func() {
   256  			BeforeEach(func() { operationType = "create" })
   257  
   258  			It("returns status: `create in progress` when state: `in progress`", func() {
   259  				status := ServiceInstanceStateToStatus(operationType, "in progress", isUserProvided)
   260  				Expect(status).To(Equal("create in progress"))
   261  			})
   262  
   263  			It("returns status: `create succeeded` when state: `succeeded`", func() {
   264  				status := ServiceInstanceStateToStatus(operationType, "succeeded", isUserProvided)
   265  				Expect(status).To(Equal("create succeeded"))
   266  			})
   267  
   268  			It("returns status: `create failed` when state: `failed`", func() {
   269  				status := ServiceInstanceStateToStatus(operationType, "failed", isUserProvided)
   270  				Expect(status).To(Equal("create failed"))
   271  			})
   272  
   273  			It("returns status: `` when state: ``", func() {
   274  				status := ServiceInstanceStateToStatus(operationType, "", isUserProvided)
   275  				Expect(status).To(Equal(""))
   276  			})
   277  		})
   278  	})
   279  
   280  	Context("when the service is user provided", func() {
   281  		isUserProvided := true
   282  
   283  		It("returns status: `` when state: ``", func() {
   284  			status := ServiceInstanceStateToStatus(operationType, "", isUserProvided)
   285  			Expect(status).To(Equal(""))
   286  		})
   287  	})
   288  })