Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added unit tests to achieve 97% code coverage #52

Merged
merged 2 commits into from
Jul 29, 2024
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
4 changes: 2 additions & 2 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const DefaultPipelineServiceAccountName = 'default:pipeline';
* @param sa
* @param saNamespace
*/
function createRoleBindingProps(bindingName: string, bindingNs: string, rolename: string, sa: string, saNamespace: string): ApiObjectProps {
export function createRoleBindingProps(bindingName: string, bindingNs: string, rolename: string, sa: string, saNamespace: string): ApiObjectProps {
return {
apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'ClusterRoleBinding',
Expand Down Expand Up @@ -855,7 +855,7 @@ export class TaskBuilder {
// First, check to see if there is already a result with this name
const existing = this._results.find((obj) => obj.name === name);
if (existing) {
throw new Error(`Cannot add result ${name}, as it already exists.`);
throw new Error(`Cannot add result '${name}', as it already exists.`);
}
this._results.push({
name: name,
Expand Down
98 changes: 98 additions & 0 deletions test/__snapshots__/pipelinebuilder.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 121 additions & 0 deletions test/pipelinebuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
WorkspaceBuilder,
fromPipelineParam,
constant,
createRoleBindingProps,
ClusterTaskResolver,
} from '../src';

Expand Down Expand Up @@ -40,6 +41,40 @@ class PipelineRunTest extends Chart {
}
}

class PipelineRunTestCustom extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);

const pipelineParam = new ParameterBuilder('repo-url')
.withDefaultValue('');

const myTask = new TaskBuilder(this, 'git-clone')
.withName('fetch-source')
.withStringParam(new ParameterBuilder('url').withValue(fromPipelineParam(pipelineParam)));

const pipeline = new PipelineBuilder(this, 'clone-build-push')
.withDescription('This pipeline closes a repository, builds a Docker image, etc.')
.withTask(myTask)
.withStringParam(pipelineParam);
pipeline.buildPipeline({ includeDependencies: true });

const CRB = createRoleBindingProps(
'pipeline-admin-default-crb',
'default',
'cluster-admin',
'pipeline',
'default');

const serviceAccount = 'default:pipeline';

new PipelineRunBuilder(this, 'my-pipeline-run', pipeline)
.withRunParam('repo-url', 'https://github.com/exmaple/my-repo')
.withClusterRoleBindingProps(CRB)
.withServiceAccount(serviceAccount)
.buildPipelineRun({ includeDependencies: true });
}
}

class PipelineRunTestWithUndefinedWorkspaceError extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);
Expand Down Expand Up @@ -195,6 +230,61 @@ class MyTestChartWithDuplicateParams extends Chart {
}
}

class MyTestChartWithPipelineParamError extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);

const pipelineParam = new ParameterBuilder('repo-url')
.withDefaultValue('');

const urlParam = new ParameterBuilder('url')
.withValue(fromPipelineParam(pipelineParam));

const myTask = new TaskBuilder(this, 'fetch-source')
.withName('git-clone')
.withStringParam(urlParam);

new PipelineBuilder(this, 'clone-build-push')
.withDescription('This pipeline closes a repository, builds a Docker image, etc.')
.withTask(myTask)
.buildPipeline();
}
}

class MyTestChartWithDuplicateResultError extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);

const myTask = new TaskBuilder(this, 'fetch-source')
.withName('git-clone')
.withResult('result', 'The original result')
.withResult('result', 'The duplicate result');

new PipelineBuilder(this, 'clone-build-push')
.withDescription('This pipeline closes a repository, builds a Docker image, etc.')
.withTask(myTask)
.buildPipeline();
}
}

class MyTestChartWithWorkspaceError extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);

const myWorkspace = new WorkspaceBuilder('output')
.withDescription('The files cloned by the task');

const myTask = new TaskBuilder(this, 'fetch-source')
.withName('git-clone')
.withWorkspace(myWorkspace);

new PipelineBuilder(this, 'clone-build-push')
.withDescription('This pipeline closes a repository, builds a Docker image, etc.')
.withTask(myTask)
.buildPipeline();
}
}

class MyTestChartWithStaticOverride extends Chart {

constructor(scope: Construct, id: string, props?: ChartProps) {
Expand Down Expand Up @@ -388,6 +478,13 @@ describe('PipelineBuilderTest', () => {
expect(results).toMatchSnapshot();
});

test('PipelineRunBuilderCustom', () => {
const app = Testing.app();
const chart = new PipelineRunTestCustom(app, 'test-chart');
const results = Testing.synth(chart);
expect(results).toMatchSnapshot();
});

test('PipelineRunBuilderWithError', () => {
const app = Testing.app();
const f = () => {
Expand Down Expand Up @@ -426,6 +523,30 @@ describe('PipelineBuilderTest', () => {
expect(results).toMatchSnapshot();
});

test('PipelineBuilderWithPipelineParamError', () => {
const app = Testing.app();
const f = () => {
new MyTestChartWithPipelineParamError(app, 'test-chart');
};
expect(f).toThrowError('Parameter \'url\' in Task \'git-clone\' expects Pipeline value from parameter \'repo-url\', which is not defined.');
});

test('PipelineBuilderWithDuplicateResults', () => {
const app = Testing.app();
const f = () => {
new MyTestChartWithDuplicateResultError(app, 'test-chart');
};
expect(f).toThrowError('Cannot add result \'result\', as it already exists.');
});

test('PipelineBuilderWithWorkspaceError', () => {
const app = Testing.app();
const f = () => {
new MyTestChartWithWorkspaceError(app, 'test-chart');
};
expect(f).toThrowError('Workspace \'output\' in Task \'git-clone\' has no binding to a workspace in Pipeline \'clone-build-push\'.');
});

test('PipelineBuilderWithStaticOverride', () => {
const app = Testing.app();
const chart = new MyTestChartWithStaticOverride(app, 'test-chart');
Expand Down
Loading