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

     1  package service_test
     2  
     3  import (
     4  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     6  	"github.com/cloudfoundry/cli/plugin/models"
     7  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     8  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
     9  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    10  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    11  
    12  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    13  	"github.com/cloudfoundry/cli/cf/models"
    14  
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("services", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		configRepo          core_config.Repository
    25  		requirementsFactory *testreq.FakeReqFactory
    26  		serviceSummaryRepo  *testapi.FakeServiceSummaryRepo
    27  		deps                command_registry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.Ui = ui
    32  		deps.Config = configRepo
    33  		deps.RepoLocator = deps.RepoLocator.SetServiceSummaryRepository(serviceSummaryRepo)
    34  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("services").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	runCommand := func(args ...string) bool {
    38  		return testcmd.RunCliCommand("services", args, requirementsFactory, updateCommandDependency, false)
    39  	}
    40  
    41  	BeforeEach(func() {
    42  		ui = &testterm.FakeUI{}
    43  		configRepo = testconfig.NewRepositoryWithDefaults()
    44  		serviceSummaryRepo = &testapi.FakeServiceSummaryRepo{}
    45  		requirementsFactory = &testreq.FakeReqFactory{
    46  			LoginSuccess:         true,
    47  			TargetedSpaceSuccess: true,
    48  			TargetedOrgSuccess:   true,
    49  		}
    50  
    51  		deps = command_registry.NewDependency()
    52  	})
    53  
    54  	Describe("services requirements", func() {
    55  
    56  		Context("when not logged in", func() {
    57  			BeforeEach(func() {
    58  				requirementsFactory.LoginSuccess = false
    59  			})
    60  
    61  			It("fails requirements", func() {
    62  				Expect(runCommand()).To(BeFalse())
    63  			})
    64  		})
    65  
    66  		Context("when no space is targeted", func() {
    67  			BeforeEach(func() {
    68  				requirementsFactory.TargetedSpaceSuccess = false
    69  			})
    70  
    71  			It("fails requirements", func() {
    72  				Expect(runCommand()).To(BeFalse())
    73  			})
    74  		})
    75  		It("should fail with usage when provided any arguments", func() {
    76  			requirementsFactory.LoginSuccess = true
    77  			requirementsFactory.TargetedSpaceSuccess = true
    78  			Expect(runCommand("blahblah")).To(BeFalse())
    79  			Expect(ui.Outputs).To(ContainSubstrings(
    80  				[]string{"Incorrect Usage", "No argument required"},
    81  			))
    82  		})
    83  	})
    84  
    85  	It("lists available services", func() {
    86  		plan := models.ServicePlanFields{
    87  			Guid: "spark-guid",
    88  			Name: "spark",
    89  		}
    90  
    91  		plan2 := models.ServicePlanFields{
    92  			Guid: "spark-guid-2",
    93  			Name: "spark-2",
    94  		}
    95  
    96  		offering := models.ServiceOfferingFields{Label: "cleardb"}
    97  
    98  		serviceInstance := models.ServiceInstance{}
    99  		serviceInstance.Name = "my-service-1"
   100  		serviceInstance.LastOperation.Type = "create"
   101  		serviceInstance.LastOperation.State = "in progress"
   102  		serviceInstance.LastOperation.Description = "fake state description"
   103  		serviceInstance.ServicePlan = plan
   104  		serviceInstance.ApplicationNames = []string{"cli1", "cli2"}
   105  		serviceInstance.ServiceOffering = offering
   106  
   107  		serviceInstance2 := models.ServiceInstance{}
   108  		serviceInstance2.Name = "my-service-2"
   109  		serviceInstance2.LastOperation.Type = "create"
   110  		serviceInstance2.LastOperation.State = ""
   111  		serviceInstance2.LastOperation.Description = "fake state description"
   112  		serviceInstance2.ServicePlan = plan2
   113  		serviceInstance2.ApplicationNames = []string{"cli1"}
   114  		serviceInstance2.ServiceOffering = offering
   115  
   116  		userProvidedServiceInstance := models.ServiceInstance{}
   117  		userProvidedServiceInstance.Name = "my-service-provided-by-user"
   118  
   119  		serviceInstances := []models.ServiceInstance{serviceInstance, serviceInstance2, userProvidedServiceInstance}
   120  
   121  		serviceSummaryRepo.GetSummariesInCurrentSpaceInstances = serviceInstances
   122  
   123  		runCommand()
   124  		Expect(ui.Outputs).To(ContainSubstrings(
   125  			[]string{"Getting services in org", "my-org", "my-space", "my-user"},
   126  			[]string{"name", "service", "plan", "bound apps", "last operation"},
   127  			[]string{"OK"},
   128  			[]string{"my-service-1", "cleardb", "spark", "cli1, cli2", "create in progress"},
   129  			[]string{"my-service-2", "cleardb", "spark-2", "cli1", ""},
   130  			[]string{"my-service-provided-by-user", "user-provided", "", "", ""},
   131  		))
   132  	})
   133  
   134  	It("lists no services when none are found", func() {
   135  		serviceInstances := []models.ServiceInstance{}
   136  		serviceSummaryRepo.GetSummariesInCurrentSpaceInstances = serviceInstances
   137  
   138  		runCommand()
   139  
   140  		Expect(ui.Outputs).To(ContainSubstrings(
   141  			[]string{"Getting services in org", "my-org", "my-space", "my-user"},
   142  			[]string{"OK"},
   143  			[]string{"No services found"},
   144  		))
   145  
   146  		Expect(ui.Outputs).ToNot(ContainSubstrings(
   147  			[]string{"name", "service", "plan", "bound apps"},
   148  		))
   149  	})
   150  
   151  	Describe("when invoked by a plugin", func() {
   152  
   153  		var (
   154  			pluginModels []plugin_models.GetServices_Model
   155  		)
   156  
   157  		BeforeEach(func() {
   158  
   159  			pluginModels = []plugin_models.GetServices_Model{}
   160  			deps.PluginModels.Services = &pluginModels
   161  			plan := models.ServicePlanFields{
   162  				Guid: "spark-guid",
   163  				Name: "spark",
   164  			}
   165  
   166  			plan2 := models.ServicePlanFields{
   167  				Guid: "spark-guid-2",
   168  				Name: "spark-2",
   169  			}
   170  
   171  			offering := models.ServiceOfferingFields{Label: "cleardb"}
   172  
   173  			serviceInstance := models.ServiceInstance{}
   174  			serviceInstance.Name = "my-service-1"
   175  			serviceInstance.Guid = "123"
   176  			serviceInstance.LastOperation.Type = "create"
   177  			serviceInstance.LastOperation.State = "in progress"
   178  			serviceInstance.LastOperation.Description = "fake state description"
   179  			serviceInstance.ServicePlan = plan
   180  			serviceInstance.ApplicationNames = []string{"cli1", "cli2"}
   181  			serviceInstance.ServiceOffering = offering
   182  
   183  			serviceInstance2 := models.ServiceInstance{}
   184  			serviceInstance2.Name = "my-service-2"
   185  			serviceInstance2.Guid = "345"
   186  			serviceInstance2.LastOperation.Type = "create"
   187  			serviceInstance2.LastOperation.State = ""
   188  			serviceInstance2.LastOperation.Description = "fake state description"
   189  			serviceInstance2.ServicePlan = plan2
   190  			serviceInstance2.ApplicationNames = []string{"cli1"}
   191  			serviceInstance2.ServiceOffering = offering
   192  
   193  			userProvidedServiceInstance := models.ServiceInstance{}
   194  			userProvidedServiceInstance.Name = "my-service-provided-by-user"
   195  			userProvidedServiceInstance.Guid = "678"
   196  
   197  			serviceInstances := []models.ServiceInstance{serviceInstance, serviceInstance2, userProvidedServiceInstance}
   198  
   199  			serviceSummaryRepo.GetSummariesInCurrentSpaceInstances = serviceInstances
   200  		})
   201  
   202  		It("populates the plugin model", func() {
   203  			testcmd.RunCliCommand("services", []string{}, requirementsFactory, updateCommandDependency, true)
   204  
   205  			Ω(len(pluginModels)).To(Equal(3))
   206  			Ω(pluginModels[0].Name).To(Equal("my-service-1"))
   207  			Ω(pluginModels[0].Guid).To(Equal("123"))
   208  			Ω(pluginModels[0].ServicePlan.Name).To(Equal("spark"))
   209  			Ω(pluginModels[0].ServicePlan.Guid).To(Equal("spark-guid"))
   210  			Ω(pluginModels[0].Service.Name).To(Equal("cleardb"))
   211  			Ω(pluginModels[0].ApplicationNames).To(Equal([]string{"cli1", "cli2"}))
   212  			Ω(pluginModels[0].LastOperation.Type).To(Equal("create"))
   213  			Ω(pluginModels[0].LastOperation.State).To(Equal("in progress"))
   214  			Ω(pluginModels[0].IsUserProvided).To(BeFalse())
   215  
   216  			Ω(pluginModels[1].Name).To(Equal("my-service-2"))
   217  			Ω(pluginModels[1].Guid).To(Equal("345"))
   218  			Ω(pluginModels[1].ServicePlan.Name).To(Equal("spark-2"))
   219  			Ω(pluginModels[1].ServicePlan.Guid).To(Equal("spark-guid-2"))
   220  			Ω(pluginModels[1].Service.Name).To(Equal("cleardb"))
   221  			Ω(pluginModels[1].ApplicationNames).To(Equal([]string{"cli1"}))
   222  			Ω(pluginModels[1].LastOperation.Type).To(Equal("create"))
   223  			Ω(pluginModels[1].LastOperation.State).To(Equal(""))
   224  			Ω(pluginModels[1].IsUserProvided).To(BeFalse())
   225  
   226  			Ω(pluginModels[2].Name).To(Equal("my-service-provided-by-user"))
   227  			Ω(pluginModels[2].Guid).To(Equal("678"))
   228  			Ω(pluginModels[2].ServicePlan.Name).To(Equal(""))
   229  			Ω(pluginModels[2].ServicePlan.Guid).To(Equal(""))
   230  			Ω(pluginModels[2].Service.Name).To(Equal(""))
   231  			Ω(pluginModels[2].ApplicationNames).To(BeNil())
   232  			Ω(pluginModels[2].LastOperation.Type).To(Equal(""))
   233  			Ω(pluginModels[2].LastOperation.State).To(Equal(""))
   234  			Ω(pluginModels[2].IsUserProvided).To(BeTrue())
   235  
   236  		})
   237  
   238  	})
   239  })