Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ const testRequest: TestRequest = {
}
```

You may also reference secrets when downloading remote artifacts:

```ts
microcksContainer
.withMainRemoteArtifacts([
{ url: "https://gitlab.com/user/private_repo/artifact1.yaml", secretName: "gl-secret" },
"https://github.com/user/public_repo/artifact2.yaml"
])
.withSecondaryRemoteArtifacts([
{ url: "https://gitlab.com/user/private_repo/examples1.yaml", secretName: "gl-secret" },
"https://github.com/user/public_repo/examples2.yaml"
]);
```


### Advanced features with MicrocksContainersEnsemble

The `MicrocksContainer` referenced above supports essential features of Microcks provided by the main Microcks container. The list of supported features is the following:
Expand Down
45 changes: 30 additions & 15 deletions src/microcks-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class MicrocksContainer extends GenericContainer {
private snapshots: string[] = [];
private mainArtifacts: string[] = [];
private secondaryArtifacts: string[] = [];
private mainRemoteArtifacts: string[] = [];
private secondaryRemoteArtifacts: string[] = [];
private mainRemoteArtifacts: RemoteArtifact[] = [];
private secondaryRemoteArtifacts: RemoteArtifact[] = [];
private secrets: Secret[] = [];

constructor(image = "quay.io/microcks/microcks-uber:1.12.0") {
Expand Down Expand Up @@ -59,21 +59,21 @@ export class MicrocksContainer extends GenericContainer {
/**
* Provide urls of remote artifacts that will be imported as primary or main ones within the Microcks container
* once it will be started and healthy.
* @param {[String]} remoteArtifactUrls The urls or remote artifacts (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @param {[RemoteArtifact]} remoteArtifactUrls Array of URLs (strings) or objects with {url, secretName} (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @returns this
*/
public withMainRemoteArtifacts(remoteArtifactUrls: string[]): this {
public withMainRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
this.mainRemoteArtifacts = this.mainRemoteArtifacts.concat(remoteArtifactUrls);
return this;
}

/**
* Provide urls of remote artifacts that will be imported as secondary ones within the Microcks container
* once it will be started and healthy.
* @param {[String]} remoteArtifactUrls The furls or remote (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @param {[RemoteArtifact]} remoteArtifactUrls Array of URLs (strings) or objects with {url, secretName} (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @returns this
*/
public withSecondaryRemoteArtifacts(remoteArtifactUrls: string[]): this {
public withSecondaryRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
this.secondaryRemoteArtifacts = this.secondaryRemoteArtifacts.concat(remoteArtifactUrls);
return this;
}
Expand Down Expand Up @@ -115,12 +115,14 @@ export class MicrocksContainer extends GenericContainer {
}
// Load remote artifacts before local ones.
for (let i=0; i<this.mainRemoteArtifacts.length; i++) {
await startedContainer.downloadAsMainArtifact(this.mainRemoteArtifacts[i]);
const { url, secretName } = this.extractArtifactInfo(this.mainRemoteArtifacts[i]);
await startedContainer.downloadAsMainArtifact(url, secretName);
}
for (let i=0; i<this.secondaryRemoteArtifacts.length; i++) {
await startedContainer.downloadAsSecondaryArtifact(this.secondaryRemoteArtifacts[i]);
const { url, secretName } = this.extractArtifactInfo(this.secondaryRemoteArtifacts[i]);
await startedContainer.downloadAsSecondaryArtifact(url, secretName);
}
// Import artifacts declared in configuration.
// Import artifacts declared in configuration.
for (let i=0; i<this.mainArtifacts.length; i++) {
await startedContainer.importAsMainArtifact(this.mainArtifacts[i]);
}
Expand All @@ -133,6 +135,12 @@ export class MicrocksContainer extends GenericContainer {

return startedContainer;
}

private extractArtifactInfo(artifact: RemoteArtifact): { url: string; secretName?: string } {
return typeof artifact === 'string'
? { url: artifact }
: { url: artifact.url, secretName: artifact.secretName };
}
}

export enum OAuth2GrantType {
Expand Down Expand Up @@ -287,6 +295,11 @@ export interface DailyInvocationStatistic {
minuteCount: {string: number};
}

export type RemoteArtifact = string | {
url: string;
secretName?: string;
}


export class StartedMicrocksContainer extends AbstractStartedContainer {
private readonly httpPort: number;
Expand Down Expand Up @@ -439,17 +452,17 @@ export class StartedMicrocksContainer extends AbstractStartedContainer {
* @param {String} remoteArtifactUrl The URL to remote artifact (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @returns Success or error via Promise
*/
public async downloadAsMainArtifact(remoteArtifactUrl: string): Promise<void> {
return this.downloadArtifact(remoteArtifactUrl, true);
public async downloadAsMainArtifact(remoteArtifactUrl: string, secretName?: string): Promise<void> {
return this.downloadArtifact(remoteArtifactUrl, true, secretName);
}

/**
* Download a remote artifact as a secondary one within the Microcks container.
* @param {String} remoteArtifactUrl The URL to remote artifact (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @returns Success or error via Promise
*/
public async downloadAsSecondaryArtifact(remoteArtifactUrl: string): Promise<void> {
return this.downloadArtifact(remoteArtifactUrl, false);
public async downloadAsSecondaryArtifact(remoteArtifactUrl: string, secretName?: string): Promise<void> {
return this.downloadArtifact(remoteArtifactUrl, false, secretName);
}

/**
Expand Down Expand Up @@ -597,7 +610,6 @@ export class StartedMicrocksContainer extends AbstractStartedContainer {
return invocationStats.dailyCount;
}


private async importArtifact(artifactPath: string, mainArtifact: boolean): Promise<void> {
const isFile = await this.isFile(artifactPath);
if (!isFile) {
Expand All @@ -613,11 +625,14 @@ export class StartedMicrocksContainer extends AbstractStartedContainer {
}
}

private async downloadArtifact(remoteArtifactUrl: string, mainArtifact: boolean): Promise<void> {
private async downloadArtifact(remoteArtifactUrl: string, mainArtifact: boolean, secretName?: string): Promise<void> {
let formBody = new URLSearchParams({
"mainArtifact": String(mainArtifact),
"url": remoteArtifactUrl
});
if (secretName) {
formBody.append("secretName", secretName);
}

// Prepare headers with content type and length.
const headers: Record<string, string> = {
Expand Down
10 changes: 5 additions & 5 deletions src/microcks-containers-ensemble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import { GenericContainer, StartedNetwork, StartedTestContainer, StopOptions, Wait } from "testcontainers";
import { MicrocksContainer, Secret, StartedMicrocksContainer } from "./microcks-container.js";
import { MicrocksContainer, Secret, StartedMicrocksContainer, RemoteArtifact } from "./microcks-container.js";
import {
AmazonServiceConnection, GenericConnection, KafkaConnection,
MicrocksAsyncMinionContainer, StartedMicrocksAsyncMinionContainer
Expand Down Expand Up @@ -162,21 +162,21 @@ export class MicrocksContainersEnsemble {
/**
* Provide urls of remote artifacts that will be imported as primary or main ones within the Microcks container
* once it will be started and healthy.
* @param {[String]} remoteArtifactUrls The urls or remote artifacts (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @param {[RemoteArtifact]} remoteArtifactUrls The urls or remote artifacts (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @returns this
*/
public withMainRemoteArtifacts(remoteArtifactUrls: string[]): this {
public withMainRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
this.microcksContainer.withMainRemoteArtifacts(remoteArtifactUrls);
return this;
}

/**
* Provide urls of remote artifacts that will be imported as secondary ones within the Microcks container
* once it will be started and healthy.
* @param {[String]} remoteArtifactUrls The furls or remote (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @param {[RemoteArtifact]} remoteArtifactUrls The furls or remote (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
* @returns this
*/
public withSecondaryRemoteArtifacts(remoteArtifactUrls: string[]): this {
public withSecondaryRemoteArtifacts(remoteArtifactUrls: RemoteArtifact[]): this {
this.microcksContainer.withSecondaryRemoteArtifacts(remoteArtifactUrls);
return this;
}
Expand Down