Example #1
0
func testUploadApp(dir string, requests []testnet.TestRequest) (app models.Application, apiErr errors.Error) {
	ts, handler := testnet.NewServer(requests)
	defer ts.Close()

	configRepo := testconfig.NewRepositoryWithDefaults()
	configRepo.SetApiEndpoint(ts.URL)
	gateway := net.NewCloudControllerGateway(configRepo)
	gateway.PollingThrottle = time.Duration(0)
	zipper := app_files.ApplicationZipper{}
	repo := NewCloudControllerApplicationBitsRepository(configRepo, gateway, zipper)

	var (
		reportedPath                          string
		reportedFileCount, reportedUploadSize uint64
	)
	apiErr = repo.UploadApp("my-cool-app-guid", dir, func(path string, uploadSize, fileCount uint64) {
		reportedPath = path
		reportedUploadSize = uploadSize
		reportedFileCount = fileCount
	})

	Expect(reportedPath).To(Equal(dir))
	Expect(reportedFileCount).To(Equal(uint64(len(expectedApplicationContent))))
	Expect(reportedUploadSize).To(Equal(uint64(759)))
	Expect(handler).To(testnet.HaveAllRequestsCalled())

	return
}
Example #2
0
func testRenameService(endpointPath string, serviceInstance models.ServiceInstance) {
	req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
		Method:   "PUT",
		Path:     endpointPath,
		Matcher:  testnet.RequestBodyMatcher(`{"name":"new-name"}`),
		Response: testnet.TestResponse{Status: http.StatusCreated},
	})

	testServer, handler, repo := createServiceRepo([]testnet.TestRequest{req})
	defer testServer.Close()

	apiErr := repo.RenameService(serviceInstance, "new-name")
	Expect(handler).To(testnet.HaveAllRequestsCalled())
	Expect(apiErr).NotTo(HaveOccurred())
}
Example #3
0
func testUnsetOrgRoleWithValidRole(role string, path string) {
	req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
		Method:   "DELETE",
		Path:     path,
		Response: testnet.TestResponse{Status: http.StatusOK},
	})

	cc, handler, repo := createUsersRepoWithoutUAAEndpoints([]testnet.TestRequest{req})
	defer cc.Close()

	apiErr := repo.UnsetOrgRole("my-user-guid", "my-org-guid", role)

	Expect(handler).To(testnet.HaveAllRequestsCalled())
	Expect(apiErr).NotTo(HaveOccurred())
}
Example #4
0
func testSpacesDidNotFindByNameWithOrg(orgGuid string, findByName func(SpaceRepository, string) (models.Space, error)) {
	request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
		Method: "GET",
		Path:   fmt.Sprintf("/v2/organizations/%s/spaces?q=name%%3Aspace1&inline-relations-depth=1", orgGuid),
		Response: testnet.TestResponse{
			Status: http.StatusOK,
			Body:   ` { "resources": [ ] }`,
		},
	})

	ts, handler, repo := createSpacesRepo(request)
	defer ts.Close()

	_, apiErr := findByName(repo, "Space1")
	Expect(handler).To(testnet.HaveAllRequestsCalled())

	Expect(apiErr.(*errors.ModelNotFoundError)).NotTo(BeNil())
}
Example #5
0
func testSetSpaceRoleWithValidRole(role string, path string) {
	addToOrgReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
		Method:   "PUT",
		Path:     "/v2/organizations/my-org-guid/users/my-user-guid",
		Response: testnet.TestResponse{Status: http.StatusOK},
	})

	setRoleReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
		Method:   "PUT",
		Path:     path,
		Response: testnet.TestResponse{Status: http.StatusOK},
	})

	cc, handler, repo := createUsersRepoWithoutUAAEndpoints([]testnet.TestRequest{addToOrgReq, setRoleReq})
	defer cc.Close()

	apiErr := repo.SetSpaceRole("my-user-guid", "my-space-guid", "my-org-guid", role)

	Expect(handler).To(testnet.HaveAllRequestsCalled())
	Expect(apiErr).NotTo(HaveOccurred())
}
Example #6
0
			Method: "GET",
			Path:   "/v2/endpoint",
			Response: testnet.TestResponse{
				Status: http.StatusOK,
				Body:   expectedJSONResponse},
		})
		ts, handler := testnet.NewServer([]testnet.TestRequest{req})
		defer ts.Close()

		deps := newCurlDependencies()
		deps.config.SetApiEndpoint(ts.URL)

		repo := NewCloudControllerCurlRepository(deps.config, deps.gateway)
		headers, body, apiErr := repo.Request("GET", "/v2/endpoint", "", "")

		Expect(handler).To(testnet.HaveAllRequestsCalled())
		Expect(headers).To(ContainSubstring("200"))
		Expect(headers).To(ContainSubstring("Content-Type"))
		Expect(headers).To(ContainSubstring("text/plain"))
		testassert.JSONStringEquals(body, expectedJSONResponse)
		Expect(apiErr).NotTo(HaveOccurred())
	})

	It("TestCurlPostRequest", func() {
		req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
			Method:  "POST",
			Path:    "/v2/endpoint",
			Matcher: testnet.RequestBodyMatcher(`{"key":"val"}`),
			Response: testnet.TestResponse{
				Status: http.StatusOK,
				Body:   expectedJSONResponse},
Example #7
0
		It("fails to upload a buildpack with an invalid directory", func() {
			apiErr := repo.UploadBuildpack(buildpack, "/foo/bar")
			Expect(apiErr).NotTo(BeNil())
			Expect(apiErr.Error()).To(ContainSubstring("Error opening buildpack file"))
		})

		It("uploads a valid buildpack directory", func() {
			buildpackPath := filepath.Join(buildpacksDir, "example-buildpack")

			os.Chmod(filepath.Join(buildpackPath, "bin/compile"), 0755)
			os.Chmod(filepath.Join(buildpackPath, "bin/detect"), 0755)
			err := os.Chmod(filepath.Join(buildpackPath, "bin/release"), 0755)
			Expect(err).NotTo(HaveOccurred())

			apiErr := repo.UploadBuildpack(buildpack, buildpackPath)
			Expect(testServerHandler).To(testnet.HaveAllRequestsCalled())
			Expect(apiErr).NotTo(HaveOccurred())
		})

		It("uploads a valid zipped buildpack", func() {
			buildpackPath := filepath.Join(buildpacksDir, "example-buildpack.zip")

			apiErr := repo.UploadBuildpack(buildpack, buildpackPath)
			Expect(testServerHandler).To(testnet.HaveAllRequestsCalled())
			Expect(apiErr).NotTo(HaveOccurred())
		})

		Describe("when the buildpack is wrapped in an extra top-level directory", func() {
			It("uploads a zip file containing only the actual buildpack", func() {
				buildpackPath := filepath.Join(buildpacksDir, "example-buildpack-in-dir.zip")
Example #8
0
func testSpacesFindByNameWithOrg(orgGuid string, findByName func(SpaceRepository, string) (models.Space, error)) {
	findSpaceByNameResponse := testnet.TestResponse{
		Status: http.StatusOK,
		Body: `
{
  "resources": [
    {
      "metadata": {
        "guid": "space1-guid"
      },
      "entity": {
        "name": "Space1",
        "organization_guid": "org1-guid",
        "organization": {
          "metadata": {
            "guid": "org1-guid"
          },
          "entity": {
            "name": "Org1"
          }
        },
        "apps": [
          {
            "metadata": {
              "guid": "app1-guid"
            },
            "entity": {
              "name": "app1"
            }
          },
          {
            "metadata": {
              "guid": "app2-guid"
            },
            "entity": {
              "name": "app2"
            }
          }
        ],
        "domains": [
          {
            "metadata": {
              "guid": "domain1-guid"
            },
            "entity": {
              "name": "domain1"
            }
          }
        ],
        "service_instances": [
          {
			"metadata": {
              "guid": "service1-guid"
            },
            "entity": {
              "name": "service1"
            }
          }
        ]
      }
    }
  ]
}`}
	request := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
		Method:   "GET",
		Path:     fmt.Sprintf("/v2/organizations/%s/spaces?q=name%%3Aspace1&inline-relations-depth=1", orgGuid),
		Response: findSpaceByNameResponse,
	})

	ts, handler, repo := createSpacesRepo(request)
	defer ts.Close()

	space, apiErr := findByName(repo, "Space1")
	Expect(handler).To(testnet.HaveAllRequestsCalled())
	Expect(apiErr).NotTo(HaveOccurred())
	Expect(space.Name).To(Equal("Space1"))
	Expect(space.Guid).To(Equal("space1-guid"))

	Expect(space.Organization.Guid).To(Equal("org1-guid"))

	Expect(len(space.Applications)).To(Equal(2))
	Expect(space.Applications[0].Guid).To(Equal("app1-guid"))
	Expect(space.Applications[1].Guid).To(Equal("app2-guid"))

	Expect(len(space.Domains)).To(Equal(1))
	Expect(space.Domains[0].Guid).To(Equal("domain1-guid"))

	Expect(len(space.ServiceInstances)).To(Equal(1))
	Expect(space.ServiceInstances[0].Guid).To(Equal("service1-guid"))

	Expect(apiErr).NotTo(HaveOccurred())
	return
}
Example #9
0
	setupUAAServer := func(requests ...testnet.TestRequest) {
		uaaServer, uaaHandler = testnet.NewServer(requests)
		config.SetUaaEndpoint(uaaServer.URL)
	}

	Describe("listing the users with a given role", func() {
		It("lists the users in an organization with a given role", func() {
			ccReqs, uaaReqs := createUsersByRoleEndpoints("/v2/organizations/my-org-guid/managers")

			setupCCServer(ccReqs...)
			setupUAAServer(uaaReqs...)

			users, apiErr := repo.ListUsersInOrgForRole("my-org-guid", models.ORG_MANAGER)

			Expect(ccHandler).To(testnet.HaveAllRequestsCalled())
			Expect(uaaHandler).To(testnet.HaveAllRequestsCalled())
			Expect(apiErr).NotTo(HaveOccurred())

			Expect(len(users)).To(Equal(3))
			Expect(users[0].Guid).To(Equal("user-1-guid"))
			Expect(users[0].Username).To(Equal("Super user 1"))
			Expect(users[1].Guid).To(Equal("user-2-guid"))
			Expect(users[1].Username).To(Equal("Super user 2"))
		})

		It("lists the users in a space with a given role", func() {
			ccReqs, uaaReqs := createUsersByRoleEndpoints("/v2/spaces/my-space-guid/managers")

			setupCCServer(ccReqs...)
			setupUAAServer(uaaReqs...)