-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7260f04
commit 779694e
Showing
7 changed files
with
319 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import streamConsumers from 'stream/consumers'; | ||
|
||
import _ from 'lodash'; | ||
|
||
import { expect } from 'e2e/fixtures/global-fixtures'; | ||
import { safeName } from 'e2e/utils/naming'; | ||
import { ProjectsApi, V1PostProjectRequest, V1PostProjectResponse } from 'services/api-ts-sdk/api'; | ||
|
||
import { ApiAuthFixture } from './api.auth.fixture'; | ||
|
||
export class ApiProjectFixture { | ||
readonly apiAuth: ApiAuthFixture; | ||
constructor(apiAuth: ApiAuthFixture) { | ||
this.apiAuth = apiAuth; | ||
} | ||
|
||
new({ projectProps = {}, projectPrefix = 'test-project' } = {}): V1PostProjectRequest { | ||
const defaults = { | ||
name: safeName(projectPrefix), | ||
workspaceId: 0, | ||
}; | ||
return { | ||
...defaults, | ||
...projectProps, | ||
}; | ||
} | ||
|
||
private static normalizeUrl(url: string): string { | ||
if (url.endsWith('/')) { | ||
return url.substring(0, url.length - 1); | ||
} | ||
return url; | ||
} | ||
|
||
private async startProjectRequest(): Promise<ProjectsApi> { | ||
return new ProjectsApi( | ||
{ apiKey: await this.apiAuth.getBearerToken() }, | ||
ApiProjectFixture.normalizeUrl(this.apiAuth.baseURL), | ||
fetch, | ||
); | ||
} | ||
|
||
/** | ||
* Creates a project with the given parameters via the API. | ||
* @param {number} workspaceId workspace id to create the project in. | ||
* @param {V1PostProjectRequest} req the project request with the config for the new project. | ||
* See apiProject.newRandom() for the default config. | ||
* @returns {Promise<V1PostProjectRequest>} Representation of the created project. The request is returned since the | ||
* password is not stored on the V1Project object and it is not returned in the response. However the Request is a | ||
* strict superset of the Response, so no info is lost. | ||
*/ | ||
async createProject( | ||
workspaceId: number, | ||
req: V1PostProjectRequest, | ||
): Promise<V1PostProjectResponse> { | ||
const projectResp = await (await this.startProjectRequest()) | ||
.postProject(workspaceId, req, {}) | ||
.catch(async function (error) { | ||
const respBody = await streamConsumers.text(error.body); | ||
throw new Error( | ||
`Create Project Request failed. Status: ${error.status} Request: ${JSON.stringify( | ||
req, | ||
)} Response: ${respBody}`, | ||
); | ||
}); | ||
return _.merge(req, projectResp); | ||
} | ||
|
||
/** | ||
* | ||
* @summary Delete a project. | ||
* @param {number} id The id of the project. | ||
*/ | ||
async deleteProject(id: number): Promise<void> { | ||
await expect | ||
.poll( | ||
async () => { | ||
const projectResp = await (await this.startProjectRequest()) | ||
.deleteProject(id) | ||
.catch(async function (error) { | ||
const respBody = await streamConsumers.text(error.body); | ||
if (error.status === 404) { | ||
return { completed: true }; | ||
} | ||
throw new Error( | ||
`Delete Project Request failed. Status: ${error.status} Request: ${JSON.stringify( | ||
id, | ||
)} Response: ${respBody}`, | ||
); | ||
}); | ||
return projectResp.completed; | ||
}, | ||
{ | ||
message: `Delete Project Request failed ${JSON.stringify(id)}`, | ||
timeout: 15_000, | ||
}, | ||
) | ||
.toBe(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import streamConsumers from 'stream/consumers'; | ||
|
||
import _ from 'lodash'; | ||
|
||
import { expect } from 'e2e/fixtures/global-fixtures'; | ||
import { safeName } from 'e2e/utils/naming'; | ||
import { | ||
V1PostWorkspaceRequest, | ||
V1PostWorkspaceResponse, | ||
WorkspacesApi, | ||
} from 'services/api-ts-sdk/api'; | ||
|
||
import { ApiAuthFixture } from './api.auth.fixture'; | ||
|
||
export class ApiWorkspaceFixture { | ||
readonly apiAuth: ApiAuthFixture; | ||
constructor(apiAuth: ApiAuthFixture) { | ||
this.apiAuth = apiAuth; | ||
} | ||
|
||
new({ workspaceProps = {}, workspacePrefix = 'test-workspace' } = {}): V1PostWorkspaceRequest { | ||
const defaults = { | ||
name: safeName(workspacePrefix), | ||
// agentWorkspaceGroup: { | ||
// agentGid: 0, | ||
// agentGroup: null, | ||
// agentUid: 0, | ||
// agentWorkspace: null | ||
// }, | ||
// checkpointStorageConfig: {}, | ||
// defaultComputePool: "string", | ||
// defaultAuxPool: "string" | ||
}; | ||
return { | ||
...defaults, | ||
...workspaceProps, | ||
}; | ||
} | ||
|
||
private static normalizeUrl(url: string): string { | ||
if (url.endsWith('/')) { | ||
return url.substring(0, url.length - 1); | ||
} | ||
return url; | ||
} | ||
|
||
private async startWorkspaceRequest(): Promise<WorkspacesApi> { | ||
return new WorkspacesApi( | ||
{ apiKey: await this.apiAuth.getBearerToken() }, | ||
ApiWorkspaceFixture.normalizeUrl(this.apiAuth.baseURL), | ||
fetch, | ||
); | ||
} | ||
|
||
/** | ||
* Creates a workspace with the given parameters via the API. | ||
* @param {V1PostWorkspaceRequest} req the workspace request with the config for the new workspace. | ||
* See apiWorkspace.newRandom() for the default config. | ||
* @returns {Promise<V1PostWorkspaceRequest>} Representation of the created workspace. The request is returned since the | ||
* password is not stored on the V1Workspace object and it is not returned in the response. However the Request is a | ||
* strict superset of the Response, so no info is lost. | ||
*/ | ||
async createWorkspace(req: V1PostWorkspaceRequest): Promise<V1PostWorkspaceResponse> { | ||
const workspaceResp = await (await this.startWorkspaceRequest()) | ||
.postWorkspace(req, {}) | ||
.catch(async function (error) { | ||
const respBody = await streamConsumers.text(error.body); | ||
throw new Error( | ||
`Create Workspace Request failed. Status: ${error.status} Request: ${JSON.stringify( | ||
req, | ||
)} Response: ${respBody}`, | ||
); | ||
}); | ||
return _.merge(req, workspaceResp); | ||
} | ||
|
||
/** | ||
* | ||
* @summary Delete a workspace. | ||
* @param {number} id The id of the workspace. | ||
*/ | ||
async deleteWorkspace(id: number): Promise<void> { | ||
await expect | ||
.poll( | ||
async () => { | ||
const workspaceResp = await (await this.startWorkspaceRequest()) | ||
.deleteWorkspace(id) | ||
.catch(async function (error) { | ||
const respBody = await streamConsumers.text(error.body); | ||
if (error.status === 404) { | ||
return { completed: true }; | ||
} | ||
throw new Error( | ||
`Delete Workspace Request failed. Status: ${error.status} Request: ${JSON.stringify( | ||
id, | ||
)} Response: ${respBody}`, | ||
); | ||
}); | ||
return workspaceResp.completed; | ||
}, | ||
{ | ||
message: `Delete Project Request failed ${JSON.stringify(id)}`, | ||
timeout: 15_000, | ||
}, | ||
) | ||
.toBe(true); | ||
} | ||
} |
Oops, something went wrong.