From 3f7079dbd6ad5322e7ec9c8b56ba8f82d1a13dba Mon Sep 17 00:00:00 2001 From: snehajais22 Date: Wed, 31 Jul 2024 15:11:52 -0400 Subject: [PATCH] Unit tests and updated docs Signed-off-by: snehajais22 --- API.md | 159 +++++++++++------- src/builders.ts | 6 +- .../pipelinebuilder.test.ts.snap | 65 +++++++ test/pipelinebuilder.test.ts | 70 +++++++- 4 files changed, 235 insertions(+), 65 deletions(-) diff --git a/API.md b/API.md index da0cffb..4f6b515 100644 --- a/API.md +++ b/API.md @@ -1616,7 +1616,7 @@ const pipelineRunSpec: PipelineRunSpec = { ... } | **Name** | **Type** | **Description** | | --- | --- | --- | -| pipelineRef | PipelineRef | Required `Pipeline` reference. | +| pipelineRef | RemoteRef \| PipelineRef | Required `Pipeline` reference. | | params | PipelineRunParam[] | *No description.* | | workspaces | PipelineRunWorkspace[] | *No description.* | @@ -1625,10 +1625,10 @@ const pipelineRunSpec: PipelineRunSpec = { ... } ##### `pipelineRef`Required ```typescript -public readonly pipelineRef: PipelineRef; +public readonly pipelineRef: RemoteRef | PipelineRef; ``` -- *Type:* PipelineRef +- *Type:* RemoteRef | PipelineRef Required `Pipeline` reference. @@ -1806,7 +1806,7 @@ const pipelineTask: PipelineTask = { ... } | name | string | *No description.* | | params | TaskParam[] | *No description.* | | runAfter | string[] | *No description.* | -| taskRef | TaskRef \| RemoteTaskRef | *No description.* | +| taskRef | RemoteRef \| TaskRef | *No description.* | | workspaces | PipelineTaskWorkspace[] | *No description.* | --- @@ -1844,10 +1844,10 @@ public readonly runAfter: string[]; ##### `taskRef`Optional ```typescript -public readonly taskRef: TaskRef | RemoteTaskRef; +public readonly taskRef: RemoteRef | TaskRef; ``` -- *Type:* TaskRef | RemoteTaskRef +- *Type:* RemoteRef | TaskRef --- @@ -1880,7 +1880,7 @@ const pipelineTaskDef: PipelineTaskDef = { ... } | name | string | *No description.* | | params | TaskParam[] | *No description.* | | runAfter | string[] | *No description.* | -| taskRef | TaskRef \| RemoteTaskRef | *No description.* | +| taskRef | RemoteRef \| TaskRef | *No description.* | | workspaces | PipelineTaskWorkspace[] | *No description.* | | refParams | PipelineParam[] | *No description.* | | refWorkspaces | PipelineTaskWorkspace[] | *No description.* | @@ -1920,10 +1920,10 @@ public readonly runAfter: string[]; ##### `taskRef`Optional ```typescript -public readonly taskRef: TaskRef | RemoteTaskRef; +public readonly taskRef: RemoteRef | TaskRef; ``` -- *Type:* TaskRef | RemoteTaskRef +- *Type:* RemoteRef | TaskRef --- @@ -2695,40 +2695,49 @@ public readonly logicalID: string; ## Classes -### ClusterTaskResolver +### ClusterRemoteResolver -- *Implements:* IRemoteTaskResolver +- *Implements:* IRemoteResolver Resolves the provided cluster-scoped task into yaml for the taskRef field. -#### Initializers +#### Initializers ```typescript -import { ClusterTaskResolver } from 'cdk8s-pipelines' +import { ClusterRemoteResolver } from 'cdk8s-pipelines' -new ClusterTaskResolver(name: string, namespace: string) +new ClusterRemoteResolver(kind: string, name: string, namespace: string) ``` | **Name** | **Type** | **Description** | | --- | --- | --- | -| name | string | The name of the cluster-scoped task. | -| namespace | string | The namespace of the cluster-scoped task. | +| kind | string | task \| pipeline. | +| name | string | The name of the cluster-scoped object. | +| namespace | string | The namespace of the cluster-scoped object. | --- -##### `name`Required +##### `kind`Required - *Type:* string -The name of the cluster-scoped task. +task | pipeline. --- -##### `namespace`Required +##### `name`Required - *Type:* string -The namespace of the cluster-scoped task. +The name of the cluster-scoped object. + +--- + +##### `namespace`Required + +- *Type:* string + +The namespace of the cluster-scoped object. --- @@ -2738,25 +2747,36 @@ The namespace of the cluster-scoped task. | **Name** | **Type** | **Description** | | --- | --- | --- | -| taskRef | RemoteTaskRef | Gets the YAML representation of cluster-scoped task. | -| params | ResolverParam[] | *No description.* | -| resolver | string | *No description.* | +| remoteRef | RemoteRef | Gets the YAML reference to the cluster-scoped object. | +| kind | string | *No description.* | +| params | ResolverParam[] | *No description.* | +| resolver | string | *No description.* | --- -##### `taskRef`Required +##### `remoteRef`Required ```typescript -public readonly taskRef: RemoteTaskRef; +public readonly remoteRef: RemoteRef; ``` -- *Type:* RemoteTaskRef +- *Type:* RemoteRef + +Gets the YAML reference to the cluster-scoped object. + +--- + +##### `kind`Optional + +```typescript +public readonly kind: string; +``` -Gets the YAML representation of cluster-scoped task. +- *Type:* string --- -##### `params`Optional +##### `params`Optional ```typescript public readonly params: ResolverParam[]; @@ -2766,7 +2786,7 @@ public readonly params: ResolverParam[]; --- -##### `resolver`Optional +##### `resolver`Optional ```typescript public readonly resolver: string; @@ -3251,14 +3271,14 @@ Builds a `PipelineRun` using the supplied configuration. ```typescript import { PipelineRunBuilder } from 'cdk8s-pipelines' -new PipelineRunBuilder(scope: Construct, id: string, pipeline: PipelineBuilder) +new PipelineRunBuilder(scope: Construct, id: string, pipeline: IRemoteResolver | PipelineBuilder) ``` | **Name** | **Type** | **Description** | | --- | --- | --- | | scope | constructs.Construct | The `Construct` in which to create the `PipelineRun`. | | id | string | The logical ID of the `PipelineRun` construct. | -| pipeline | PipelineBuilder | The `Pipeline` for which to create this run, using the `PipelineBuilder`. | +| pipeline | IRemoteResolver \| PipelineBuilder | The `Pipeline` for which to create this run, using the `PipelineBuilder` or `IRemoteResolver` for a remote `Pipeline`. | --- @@ -3280,9 +3300,9 @@ The logical ID of the `PipelineRun` construct. ##### `pipeline`Required -- *Type:* PipelineBuilder +- *Type:* IRemoteResolver | PipelineBuilder -The `Pipeline` for which to create this run, using the `PipelineBuilder`. +The `Pipeline` for which to create this run, using the `PipelineBuilder` or `IRemoteResolver` for a remote `Pipeline`. --- @@ -3306,6 +3326,9 @@ public buildPipelineRun(opts?: BuilderOptions): void Builds the `PipelineRun` for the configured `Pipeline` used in the constructor. +If the `PipelineRun` references a remote pipeline, consistency checks for parameters +and workspaces expected by the pipeline are omitted. + ###### `opts`Optional - *Type:* BuilderOptions @@ -3334,6 +3357,7 @@ Adds a run parameter to the `PipelineRun`. It will throw an error if you try to add a parameter that does not exist on the pipeline. +If the `PipelineRun` references a remote pipeline, consistency checks are omitted. ###### `name`Required @@ -3407,34 +3431,34 @@ The sub path on the `persistentVolumeClaim` to use for the `workspace`. -### RemoteTaskRef +### RemoteRef -A remote `Task` reference. +A remote `Task` or `Pipeline` reference. -Will be generated as a `taskRef`. +Generated as `taskRef` or `pipelineRef`, respectively. -#### Initializers +#### Initializers ```typescript -import { RemoteTaskRef } from 'cdk8s-pipelines' +import { RemoteRef } from 'cdk8s-pipelines' -new RemoteTaskRef(resolver: string, params: ResolverParam[]) +new RemoteRef(resolver: string, params: ResolverParam[]) ``` | **Name** | **Type** | **Description** | | --- | --- | --- | -| resolver | string | *No description.* | -| params | ResolverParam[] | *No description.* | +| resolver | string | *No description.* | +| params | ResolverParam[] | *No description.* | --- -##### `resolver`Required +##### `resolver`Required - *Type:* string --- -##### `params`Required +##### `params`Required - *Type:* ResolverParam[] @@ -3446,12 +3470,12 @@ new RemoteTaskRef(resolver: string, params: ResolverParam[]) | **Name** | **Type** | **Description** | | --- | --- | --- | -| params | ResolverParam[] | *No description.* | -| resolver | string | *No description.* | +| params | ResolverParam[] | *No description.* | +| resolver | string | *No description.* | --- -##### `params`Optional +##### `params`Optional ```typescript public readonly params: ResolverParam[]; @@ -3461,7 +3485,7 @@ public readonly params: ResolverParam[]; --- -##### `resolver`Optional +##### `resolver`Optional ```typescript public readonly resolver: string; @@ -3535,7 +3559,7 @@ Builds the `Task`. ##### `referencingTask` ```typescript -public referencingTask(task: string | IRemoteTaskResolver): TaskBuilder +public referencingTask(task: string | IRemoteResolver): TaskBuilder ``` Sets the taskRef field of the `Task`. @@ -3545,7 +3569,7 @@ overrides `logicalID as the name of the `Task` in its individual yaml. ###### `task`Required -- *Type:* string | IRemoteTaskResolver +- *Type:* string | IRemoteResolver as string: name of the local task being referenced as IRemoteTaskResolver: resolver for a task in remote location. @@ -3720,7 +3744,7 @@ Adds the specified workspace to the `Task`. | --- | --- | --- | | logicalID | string | *No description.* | | name | string | Gets the name of the `Task` in the context of a pipeline. | -| taskRef | TaskRef \| RemoteTaskRef | Gets the taskRef field of the `Task` for use within a pipeline. | +| taskRef | RemoteRef \| TaskRef | Gets the taskRef field of the `Task` for use within a pipeline. | | description | string | Gets the `description` of the `Task`. | | parameters | ParameterBuilder[] | *No description.* | | runAfter | string[] | Gets the list of task names for the runAfter value of the `Task`. | @@ -3755,10 +3779,10 @@ If not set, the 'Task' id is used. ##### `taskRef`Required ```typescript -public readonly taskRef: TaskRef | RemoteTaskRef; +public readonly taskRef: RemoteRef | TaskRef; ``` -- *Type:* TaskRef | RemoteTaskRef +- *Type:* RemoteRef | TaskRef Gets the taskRef field of the `Task` for use within a pipeline. @@ -4304,34 +4328,45 @@ Gets the name of the workspace. ## Protocols -### IRemoteTaskResolver +### IRemoteResolver -- *Implemented By:* ClusterTaskResolver, IRemoteTaskResolver +- *Implemented By:* ClusterRemoteResolver, IRemoteResolver #### Properties | **Name** | **Type** | **Description** | | --- | --- | --- | -| taskRef | RemoteTaskRef | Gets the taskRef yaml for a remote Task. | -| params | ResolverParam[] | *No description.* | -| resolver | string | *No description.* | +| remoteRef | RemoteRef | Gets the taskRef yaml for a remote Task. | +| kind | string | *No description.* | +| params | ResolverParam[] | *No description.* | +| resolver | string | *No description.* | --- -##### `taskRef`Required +##### `remoteRef`Required ```typescript -public readonly taskRef: RemoteTaskRef; +public readonly remoteRef: RemoteRef; ``` -- *Type:* RemoteTaskRef +- *Type:* RemoteRef Gets the taskRef yaml for a remote Task. --- -##### `params`Optional +##### `kind`Optional + +```typescript +public readonly kind: string; +``` + +- *Type:* string + +--- + +##### `params`Optional ```typescript public readonly params: ResolverParam[]; @@ -4341,7 +4376,7 @@ public readonly params: ResolverParam[]; --- -##### `resolver`Optional +##### `resolver`Optional ```typescript public readonly resolver: string; diff --git a/src/builders.ts b/src/builders.ts index 756cb8c..c1af9ea 100644 --- a/src/builders.ts +++ b/src/builders.ts @@ -905,7 +905,7 @@ export class TaskBuilder { this._taskref = { name: task }; } else { if (task.kind != 'task') { - throw new Error(`Remote resource be of kind 'task' in taskRef of Task ${this.name}.`); + throw new Error(`Remote resource must be of kind 'task' in taskRef of Task '${this.name}'.`); } this._taskref = task.remoteRef; } @@ -1376,6 +1376,10 @@ export class PipelineRunBuilder { }); } else { + if (this._pipeline.kind != 'pipeline') { + throw new Error(`Remote resource must be of kind 'pipeline' in pipelineRef of PipelineRun '${this._id}'.`); + } + new PipelineRun(this._scope, this._id, { metadata: { name: this._id, diff --git a/test/__snapshots__/pipelinebuilder.test.ts.snap b/test/__snapshots__/pipelinebuilder.test.ts.snap index 138c8b6..87dd5f0 100644 --- a/test/__snapshots__/pipelinebuilder.test.ts.snap +++ b/test/__snapshots__/pipelinebuilder.test.ts.snap @@ -718,3 +718,68 @@ exports[`PipelineBuilderTest PipelineRunBuilderCustom 1`] = ` }, ] `; + +exports[`PipelineBuilderTest PipelineRunBuilderWithResolver 1`] = ` +[ + { + "apiVersion": "rbac.authorization.k8s.io/v1", + "kind": "ClusterRoleBinding", + "metadata": { + "name": "pipeline-admin-default-crb", + "namespace": "default", + }, + "roleRef": { + "kind": "ClusterRole", + "name": "cluster-admin", + }, + "subjects": [ + { + "kind": "ServiceAccount", + "name": "pipeline", + "namespace": "default", + }, + ], + }, + { + "apiVersion": "tekton.dev/v1", + "kind": "PipelineRun", + "metadata": { + "name": "my-pipeline-run", + }, + "spec": { + "params": [ + { + "name": "repo-url", + "value": "https://github.com/exmaple/my-repo", + }, + ], + "pipelineRef": { + "params": [ + { + "name": "name", + "value": "clone-build-push", + }, + { + "name": "namespace", + "value": "default", + }, + { + "name": "kind", + "value": "pipeline", + }, + ], + "resolver": "cluster", + }, + "workspaces": [ + { + "name": "shared-data", + "persistentVolumeClaim": { + "claimName": "dataPVC", + }, + "subPath": "my-shared-data", + }, + ], + }, + }, +] +`; diff --git a/test/pipelinebuilder.test.ts b/test/pipelinebuilder.test.ts index fed1955..10f1d38 100644 --- a/test/pipelinebuilder.test.ts +++ b/test/pipelinebuilder.test.ts @@ -11,7 +11,7 @@ import { fromPipelineParam, constant, createRoleBindingProps, - ClusterTaskResolver, + ClusterRemoteResolver, } from '../src'; class PipelineRunTest extends Chart { @@ -454,7 +454,7 @@ class PipelineTestWithResolver extends Chart { const urlParam = new ParameterBuilder('url') .withValue(fromPipelineParam(pipelineParam)); - const resolver = new ClusterTaskResolver('git-clone', 'default'); + const resolver = new ClusterRemoteResolver('task', 'git-clone', 'default'); const myTask = new TaskBuilder(this, 'fetch-source') .referencingTask(resolver) @@ -470,6 +470,49 @@ class PipelineTestWithResolver extends Chart { } } +class PipelineTestWithResolverError extends Chart { + constructor(scope: Construct, id: string, props?: ChartProps) { + super(scope, id, props); + + const resolver = new ClusterRemoteResolver('pipeline', 'git-clone', 'default'); + + const myTask = new TaskBuilder(this, 'fetch-source') + .referencingTask(resolver); + + new PipelineBuilder(this, 'clone-build-push') + .withDescription('This pipeline closes a repository, builds a Docker image, etc.') + .withTask(myTask) + .buildPipeline({ includeDependencies: true }); + } +} + + +class PipelineRunTestWithResolver extends Chart { + constructor(scope: Construct, id: string, props?: ChartProps) { + super(scope, id, props); + + const resolver = new ClusterRemoteResolver('pipeline', 'clone-build-push', 'default'); + + new PipelineRunBuilder(this, 'my-pipeline-run', resolver) + .withRunParam('repo-url', 'https://github.com/exmaple/my-repo') + .withWorkspace('shared-data', 'dataPVC', 'my-shared-data') + .buildPipelineRun({ includeDependencies: true }); + } +} + +class PipelineRunTestWithResolverError extends Chart { + constructor(scope: Construct, id: string, props?: ChartProps) { + super(scope, id, props); + + const resolver = new ClusterRemoteResolver('task', 'clone-build-push', 'default'); + + new PipelineRunBuilder(this, 'my-pipeline-run', resolver) + .withRunParam('repo-url', 'https://github.com/exmaple/my-repo') + .withWorkspace('shared-data', 'dataPVC', 'my-shared-data') + .buildPipelineRun({ includeDependencies: true }); + } +} + describe('PipelineBuilderTest', () => { test('PipelineRunBuilder', () => { const app = Testing.app(); @@ -597,4 +640,27 @@ describe('PipelineBuilderTest', () => { const results = Testing.synth(chart); expect(results).toMatchSnapshot(); }); + + test('PipelineBuilderWithResolverError', () => { + const app = Testing.app(); + const f = () => { + new PipelineTestWithResolverError(app, 'test-chart'); + }; + expect(f).toThrowError('Remote resource must be of kind \'task\' in taskRef of Task \'fetch-source\'.'); + }); + + test('PipelineRunBuilderWithResolver', () => { + const app = Testing.app(); + const chart = new PipelineRunTestWithResolver(app, 'test-chart'); + const results = Testing.synth(chart); + expect(results).toMatchSnapshot(); + }); + + test('PipelineRunBuilderWithResolverError', () => { + const app = Testing.app(); + const f = () => { + new PipelineRunTestWithResolverError(app, 'test-chart'); + }; + expect(f).toThrowError('Remote resource must be of kind \'pipeline\' in pipelineRef of PipelineRun \'my-pipeline-run\'.'); + }); });