Ejemplo n.º 1
0
func TestPushingAppWhenItAlreadyExistsAndDomainIsSpecifiedIsAlreadyBound(t *testing.T) {
	deps := getPushDependencies()

	domain := cf.DomainFields{}
	domain.Name = "example.com"
	domain.Guid = "domain-guid"

	existingRoute := cf.RouteSummary{}
	existingRoute.Host = "existing-app"
	existingRoute.Domain = domain

	existingApp := cf.Application{}
	existingApp.Name = "existing-app"
	existingApp.Guid = "existing-app-guid"
	existingApp.Routes = []cf.RouteSummary{existingRoute}

	foundRoute := cf.Route{}
	foundRoute.RouteFields = existingRoute.RouteFields
	foundRoute.Domain = existingRoute.Domain

	deps.appRepo.ReadApp = existingApp
	deps.appRepo.UpdateAppResult = existingApp
	deps.routeRepo.FindByHostAndDomainRoute = foundRoute

	ui := callPush(t, []string{"-d", "example.com", "existing-app"}, deps)

	testassert.SliceContains(t, ui.Outputs, testassert.Lines{
		{"Using route", "existing-app", "example.com"},
	})
	assert.Equal(t, deps.appBitsRepo.UploadedAppGuid, "existing-app-guid")
}
Ejemplo n.º 2
0
func TestMapRouteWhenBinding(t *testing.T) {

	domain := cf.Domain{}
	domain.Guid = "my-domain-guid"
	domain.Name = "example.com"
	route := cf.Route{}
	route.Guid = "my-route-guid"
	route.Host = "foo"
	route.Domain = domain.DomainFields

	app := cf.Application{}
	app.Guid = "my-app-guid"
	app.Name = "my-app"

	routeRepo := &testapi.FakeRouteRepository{}
	reqFactory := &testreq.FakeReqFactory{LoginSuccess: true, Application: app, Domain: domain}
	routeCreator := &testcmd.FakeRouteCreator{ReservedRoute: route}

	ui := callMapRoute(t, []string{"-n", "my-host", "my-app", "my-domain.com"}, reqFactory, routeRepo, routeCreator)

	testassert.SliceContains(t, ui.Outputs, testassert.Lines{
		{"Adding route", "foo.example.com", "my-app", "my-org", "my-space", "my-user"},
		{"OK"},
	})

	assert.Equal(t, routeRepo.BoundRouteGuid, "my-route-guid")
	assert.Equal(t, routeRepo.BoundAppGuid, "my-app-guid")
}
Ejemplo n.º 3
0
func (repo CloudControllerApplicationRepository) appFromResource(res ApplicationResource) (app cf.Application) {
	app = cf.Application{
		Guid:            res.Metadata.Guid,
		Name:            res.Entity.Name,
		EnvironmentVars: res.Entity.EnvironmentJson,
		State:           strings.ToLower(res.Entity.State),
		Instances:       res.Entity.Instances,
		Memory:          uint64(res.Entity.Memory),
	}
	for _, routeResource := range res.Entity.Routes {
		domainResource := routeResource.Entity.Domain

		route := cf.Route{
			Guid: routeResource.Metadata.Guid,
			Host: routeResource.Entity.Host,
		}
		route.Domain = cf.Domain{
			Guid: domainResource.Metadata.Guid,
			Name: domainResource.Entity.Name,
		}

		app.Routes = append(app.Routes, route)
	}
	return
}
Ejemplo n.º 4
0
func TestPushingAppWhenItDoesNotExistButRouteExists(t *testing.T) {
	deps := getPushDependencies()

	domain := cf.Domain{}
	domain.Name = "foo.cf-app.com"
	domain.Guid = "foo-domain-guid"
	domain.Shared = true

	route := cf.Route{}
	route.Guid = "my-route-guid"
	route.Host = "my-new-app"
	route.Domain = domain.DomainFields

	deps.domainRepo.ListSharedDomainsDomains = []cf.Domain{domain}

	deps.routeRepo.FindByHostAndDomainRoute = route
	deps.appRepo.ReadNotFound = true

	ui := callPush(t, []string{"my-new-app"}, deps)

	assert.Empty(t, deps.routeRepo.CreatedHost)
	assert.Empty(t, deps.routeRepo.CreatedDomainGuid)
	assert.Equal(t, deps.routeRepo.FindByHostAndDomainHost, "my-new-app")
	assert.Equal(t, deps.routeRepo.BoundAppGuid, "my-new-app-guid")
	assert.Equal(t, deps.routeRepo.BoundRouteGuid, "my-route-guid")

	testassert.SliceContains(t, ui.Outputs, testassert.Lines{
		{"Using", "my-new-app.foo.cf-app.com"},
		{"Binding", "my-new-app.foo.cf-app.com"},
		{"OK"},
	})
}
Ejemplo n.º 5
0
func TestDeleteRouteWithConfirmation(t *testing.T) {
	domain := cf.DomainFields{}
	domain.Guid = "domain-guid"
	domain.Name = "example.com"
	reqFactory := &testreq.FakeReqFactory{LoginSuccess: true}
	route := cf.Route{}
	route.Guid = "route-guid"
	route.Host = "my-host"
	route.Domain = domain
	routeRepo := &testapi.FakeRouteRepository{
		FindByHostAndDomainRoute: route,
	}

	ui := callDeleteRoute(t, "y", []string{"-n", "my-host", "example.com"}, reqFactory, routeRepo)

	testassert.SliceContains(t, ui.Prompts, testassert.Lines{
		{"Really delete", "my-host"},
	})

	testassert.SliceContains(t, ui.Outputs, testassert.Lines{
		{"Deleting route", "my-host.example.com"},
		{"OK"},
	})
	assert.Equal(t, routeRepo.DeleteRouteGuid, "route-guid")
}
Ejemplo n.º 6
0
func TestListingRoutes(t *testing.T) {
	domain := cf.DomainFields{}
	domain.Name = "example.com"
	domain2 := cf.DomainFields{}
	domain2.Name = "cfapps.com"
	domain3 := cf.DomainFields{}
	domain3.Name = "another-example.com"

	app1 := cf.ApplicationFields{}
	app1.Name = "dora"
	app2 := cf.ApplicationFields{}
	app2.Name = "dora2"

	app3 := cf.ApplicationFields{}
	app3.Name = "my-app"
	app4 := cf.ApplicationFields{}
	app4.Name = "my-app2"

	app5 := cf.ApplicationFields{}
	app5.Name = "july"

	route := cf.Route{}
	route.Host = "hostname-1"
	route.Domain = domain
	route.Apps = []cf.ApplicationFields{app1, app2}
	route2 := cf.Route{}
	route2.Host = "hostname-2"
	route2.Domain = domain2
	route2.Apps = []cf.ApplicationFields{app3, app4}
	route3 := cf.Route{}
	route3.Host = "hostname-3"
	route3.Domain = domain3
	route3.Apps = []cf.ApplicationFields{app5}
	routes := []cf.Route{route, route2, route3}

	routeRepo := &testapi.FakeRouteRepository{Routes: routes}

	ui := callListRoutes(t, []string{}, &testreq.FakeReqFactory{}, routeRepo)

	testassert.SliceContains(t, ui.Outputs, testassert.Lines{
		{"Getting routes", "my-user"},
		{"host", "domain", "apps"},
		{"hostname-1", "example.com", "dora", "dora2"},
		{"hostname-2", "cfapps.com", "my-app", "my-app2"},
		{"hostname-3", "another-example.com", "july"},
	})
}
Ejemplo n.º 7
0
func (repo CloudControllerApplicationRepository) FindByName(name string) (app cf.Application, apiErr *net.ApiError) {
	path := fmt.Sprintf("%s/v2/spaces/%s/apps?q=name%s&inline-relations-depth=1", repo.config.Target, repo.config.Space.Guid, "%3A"+name)
	request, apiErr := repo.gateway.NewRequest("GET", path, repo.config.AccessToken, nil)
	if apiErr != nil {
		return
	}

	findResponse := new(ApplicationsApiResponse)
	apiErr = repo.gateway.PerformRequestForJSONResponse(request, findResponse)
	if apiErr != nil {
		return
	}

	if len(findResponse.Resources) == 0 {
		apiErr = net.NewApiErrorWithMessage(fmt.Sprintf("Application %s not found", name))
		return
	}

	res := findResponse.Resources[0]
	path = fmt.Sprintf("%s/v2/apps/%s/summary", repo.config.Target, res.Metadata.Guid)
	request, apiErr = repo.gateway.NewRequest("GET", path, repo.config.AccessToken, nil)
	if apiErr != nil {
		return
	}

	summaryResponse := new(ApplicationSummary)
	apiErr = repo.gateway.PerformRequestForJSONResponse(request, summaryResponse)
	if apiErr != nil {
		return
	}

	urls := []string{}
	// This is a little wonky but we made a concious effort
	// to keep the domain very separate from the API repsonses
	// to maintain flexibility.
	domainRoute := cf.Route{}
	for _, route := range summaryResponse.Routes {
		domainRoute.Domain = cf.Domain{Name: route.Domain.Name}
		domainRoute.Host = route.Host
		urls = append(urls, domainRoute.URL())
	}

	app = cf.Application{
		Name:             summaryResponse.Name,
		Guid:             summaryResponse.Guid,
		Instances:        summaryResponse.Instances,
		RunningInstances: summaryResponse.RunningInstances,
		Memory:           summaryResponse.Memory,
		EnvironmentVars:  res.Entity.EnvironmentJson,
		Urls:             urls,
		State:            strings.ToLower(summaryResponse.State),
	}

	return
}
Ejemplo n.º 8
0
func (repo CloudControllerApplicationRepository) FindByName(name string) (app cf.Application, err error) {
	path := fmt.Sprintf("%s/v2/spaces/%s/apps?q=name%s&inline-relations-depth=1", repo.config.Target, repo.config.Space.Guid, "%3A"+name)
	request, err := NewRequest("GET", path, repo.config.AccessToken, nil)
	if err != nil {
		return
	}

	findResponse := new(ApplicationsApiResponse)
	_, err = repo.apiClient.PerformRequestAndParseResponse(request, findResponse)
	if err != nil {
		return
	}

	if len(findResponse.Resources) == 0 {
		err = errors.New(fmt.Sprintf("Application %s not found", name))
		return
	}

	res := findResponse.Resources[0]
	path = fmt.Sprintf("%s/v2/apps/%s/summary", repo.config.Target, res.Metadata.Guid)
	request, err = NewRequest("GET", path, repo.config.AccessToken, nil)
	if err != nil {
		return
	}

	summaryResponse := new(ApplicationSummary)
	_, err = repo.apiClient.PerformRequestAndParseResponse(request, summaryResponse)
	if err != nil {
		return
	}

	urls := []string{}
	// This is a little wonky but we made a concious effort
	// to keep the domain very separate from the API repsonses
	// to maintain flexibility.
	domainRoute := cf.Route{}
	for _, route := range summaryResponse.Routes {
		domainRoute.Domain = cf.Domain{Name: route.Domain.Name}
		domainRoute.Host = route.Host
		urls = append(urls, domainRoute.URL())
	}

	app = cf.Application{
		Name:      summaryResponse.Name,
		Guid:      summaryResponse.Guid,
		Instances: summaryResponse.Instances,
		Memory:    summaryResponse.Memory,
		Urls:      urls,
	}

	return
}
Ejemplo n.º 9
0
func TestMapRouteWhenRouteNotReserved(t *testing.T) {
	domain := cf.DomainFields{}
	domain.Name = "my-domain.com"
	route := cf.Route{}
	route.Guid = "my-app-guid"
	route.Host = "my-host"
	route.Domain = domain
	app := cf.Application{}
	app.Guid = "my-app-guid"
	app.Name = "my-app"

	routeRepo := &testapi.FakeRouteRepository{}
	reqFactory := &testreq.FakeReqFactory{LoginSuccess: true, Application: app}
	routeCreator := &testcmd.FakeRouteCreator{ReservedRoute: route}

	callMapRoute(t, []string{"-n", "my-host", "my-app", "my-domain.com"}, reqFactory, routeRepo, routeCreator)

	assert.Equal(t, routeCreator.ReservedRoute, route)
}
Ejemplo n.º 10
0
func TestCreateRouteIsIdempotent(t *testing.T) {
	space := cf.SpaceFields{}
	space.Guid = "my-space-guid"
	space.Name = "my-space"
	domain := cf.DomainFields{}
	domain.Guid = "domain-guid"
	domain.Name = "example.com"
	reqFactory := &testreq.FakeReqFactory{
		LoginSuccess:       true,
		TargetedOrgSuccess: true,
		Domain:             cf.Domain{DomainFields: domain},
		Space:              cf.Space{SpaceFields: space},
	}

	route := cf.Route{}
	route.Guid = "my-route-guid"
	route.Host = "host"
	route.Domain = domain
	route.Space = space
	routeRepo := &testapi.FakeRouteRepository{
		CreateInSpaceErr:         true,
		FindByHostAndDomainRoute: route,
	}

	ui := callCreateRoute(t, []string{"-n", "host", "my-space", "example.com"}, reqFactory, routeRepo)

	testassert.SliceContains(t, ui.Outputs, testassert.Lines{
		{"Creating route"},
		{"OK"},
		{"host.example.com", "already exists"},
	})

	assert.Equal(t, routeRepo.CreateInSpaceHost, "host")
	assert.Equal(t, routeRepo.CreateInSpaceDomainGuid, "domain-guid")
	assert.Equal(t, routeRepo.CreateInSpaceSpaceGuid, "my-space-guid")
}