Skip to content

Commit

Permalink
Add and fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
divyanshiGupta committed Oct 19, 2020
1 parent 761d200 commit c56c357
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
BanIcon,
ExclamationTriangleIcon,
UnknownIcon,
OutlinedHourglassIcon,
} from '@patternfly/react-icons';
import { DASH } from '../../constants';
import { YellowExclamationTriangleIcon } from './icons';
Expand Down Expand Up @@ -92,6 +93,9 @@ export const Status: React.FC<StatusProps> = ({
case 'Unknown':
return <StatusIconAndText {...statusProps} icon={<UnknownIcon />} />;

case 'PipelineNotStarted':
return <StatusIconAndText {...statusProps} icon={<OutlinedHourglassIcon />} />;

default:
return <>{status || DASH}</>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const EditApplicationPage: React.FunctionComponent<ImportPageProps> = ({ match,
},
{
kind: referenceForModel(PipelineModel),
prop: 'pipeline',
prop: PipelineModel.id,
name: appName,
namespace,
optional: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,130 @@ export const appResources: AppResources = {
},
},
},
pipeline: {
loaded: true,
loadError: '',
data: {
apiVersion: 'tekton.dev/v1beta1',
kind: 'Pipeline',
metadata: {
selfLink: '/apis/tekton.dev/v1beta1/namespaces/div/pipelines/nationalparks-py',
resourceVersion: '406718',
name: 'nationalparks-py',
uid: '131662ce-62a8-4e4c-8520-a85bc35a31b3',
creationTimestamp: '2020-10-13T12:56:49Z',
generation: 1,
namespace: 'div',
labels: {
'app.kubernetes.io/instance': 'nationalparks-py',
'pipeline.openshift.io/runtime': 'python',
'pipeline.openshift.io/type': 'kubernetes',
},
},
spec: {
params: [
{
default: 'nationalparks-py',
name: 'APP_NAME',
type: 'string',
},
{
default: 'https://github.com/divyanshiGupta/nationalparks-py',
name: 'GIT_REPO',
type: 'string',
},
{
default: 'master',
name: 'GIT_REVISION',
type: 'string',
},
{
default: 'image-registry.openshift-image-registry.svc:5000/div/nationalparks-py',
name: 'IMAGE_NAME',
type: 'string',
},
],
tasks: [
{
name: 'fetch-repository',
params: [
{
name: 'url',
value: '$(params.GIT_REPO)',
},
{
name: 'revision',
value: '$(params.GIT_REVISION)',
},
{
name: 'subdirectory',
value: '',
},
{
name: 'deleteExisting',
value: 'true',
},
],
taskRef: {
kind: 'ClusterTask',
name: 'git-clone',
},
workspaces: [
{
name: 'output',
},
],
},
{
name: 'build',
params: [
{
name: 'IMAGE',
value: '$(params.IMAGE_NAME)',
},
{
name: 'TLSVERIFY',
value: 'false',
},
],
runAfter: ['fetch-repository'],
taskRef: {
kind: 'ClusterTask',
name: 's2i-python-3',
},
workspaces: [
{
name: 'source',
},
],
},
{
name: 'deploy',
params: [
{
name: 'SCRIPT',
value: 'kubectl $@',
},
{
name: 'ARGS',
value: ['rollout', 'status', 'deploy/$(params.APP_NAME)'],
},
],
runAfter: ['build'],
taskRef: {
kind: 'ClusterTask',
name: 'openshift-client',
},
},
],
workspaces: [
{
name: 'workspace',
},
],
},
},
},
imageStream: {
loaded: true,
loadError: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getInitialValues,
getExternalImagelValues,
} from '../edit-application-utils';
import { Resources } from '../../import/import-types';
import { GitImportFormData, Resources } from '../../import/import-types';
import {
knativeService,
knAppResources,
Expand All @@ -29,7 +29,31 @@ describe('Edit Application Utils', () => {
});

it('getInitialValues should return values based on the resources and the create flow used to create the application', () => {
const { route, editAppResource, buildConfig, imageStream } = appResources;
const { route, editAppResource, buildConfig, pipeline, imageStream } = appResources;
const gitImportValues: GitImportFormData = {
...gitImportInitialValues,
git: {
...gitImportInitialValues.git,
ref: 'master',
},
pipeline: {
enabled: true,
},
build: {
...gitImportInitialValues.build,
triggers: { config: false, image: false, webhook: false },
},
image: {
...gitImportInitialValues.image,
tag: '',
},
};
expect(
getInitialValues({ pipeline, editAppResource, route }, 'nationalparks-py', 'div'),
).toEqual(gitImportValues);
expect(
getInitialValues({ buildConfig, editAppResource, route }, 'nationalparks-py', 'div'),
).toEqual(gitImportInitialValues);
expect(
getInitialValues({ buildConfig, editAppResource, route }, 'nationalparks-py', 'div'),
).toEqual(gitImportInitialValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ export const getBuildData = (buildConfig: K8sResourceKind, pipeline: Pipeline, g
},
strategy:
buildStrategyType ||
pipeline?.metadata?.labels?.['pipeline.openshift.io/strategy'] ===
_.toLower(BuildStrategyType.Docker)
(pipeline?.metadata?.labels?.['pipeline.openshift.io/strategy'] ===
_.toLower(BuildStrategyType.Docker)
? BuildStrategyType.Docker
: BuildStrategyType.Source,
: BuildStrategyType.Source),
};
return buildData;
};
Expand Down Expand Up @@ -316,9 +316,7 @@ export const getGitAndDockerfileInitialValues = (
return {};
}

const currentImage =
_.split(buildConfig?.spec?.strategy?.sourceStrategy?.from?.name ?? '', ':') ||
pipeline?.metadata.labels?.['pipeline.openshift.io/runtime'];
const currentImage = _.split(buildConfig?.spec?.strategy?.sourceStrategy?.from?.name ?? '', ':');
const git = !_.isEmpty(buildConfig)
? getGitDataFromBuildConfig(buildConfig)
: getGitDataFromPipeline(pipeline);
Expand All @@ -332,7 +330,8 @@ export const getGitAndDockerfileInitialValues = (
containerPort: parseInt(_.split(_.get(route, 'spec.port.targetPort'), '-')[0], 10),
},
image: {
selected: currentImage[0] || '',
selected:
currentImage[0] || (pipeline?.metadata?.labels?.['pipeline.openshift.io/runtime'] ?? ''),
recommended: '',
tag: currentImage[1] || '',
tagObj: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Resources } from '../import-types';
import * as submitUtils from '../import-submit-utils';
import * as pipelineUtils from '../pipeline/pipeline-template-utils';
import { defaultData, nodeJsBuilderImage as buildImage } from './import-submit-utils-data';
import { PipelineModel } from '../../../models';
import { PipelineModel, PipelineRunModel } from '../../../models';

const { createOrUpdateDeployment, createOrUpdateResources } = submitUtils;

Expand Down Expand Up @@ -162,7 +162,30 @@ describe('Import Submit Utils', () => {
const mockData = _.cloneDeep(defaultData);
mockData.pipeline.enabled = true;

const createPipelineResourceSpy = jest.spyOn(pipelineUtils, 'createPipelineForImportFlow');
const createPipelineResourceSpy = jest
.spyOn(pipelineUtils, 'createPipelineForImportFlow')
.mockImplementation((formData) => {
const {
name,
project: { name: namespace },
} = formData;
return {
metadata: {
name,
namespace,
labels: { 'app.kubernetes.io/instance': name },
},
spec: {
params: [],
resources: [],
tasks: [],
},
};
});
const createPipelineRunResourceSpy = jest.spyOn(
pipelineUtils,
'createPipelineRunForImportFlow',
);

const returnValue = await createOrUpdateResources(
mockData,
Expand All @@ -172,8 +195,9 @@ describe('Import Submit Utils', () => {
'create',
);
expect(createPipelineResourceSpy).toHaveBeenCalledWith(mockData);
expect(createPipelineRunResourceSpy).toHaveBeenCalledTimes(1);
const models = returnValue.map((data) => _.get(data, 'model.kind'));
expect(models.includes(PipelineModel.kind)).toEqual(true);
expect(models.includes(PipelineRunModel.kind)).toEqual(true);
done();
});

Expand All @@ -192,8 +216,8 @@ describe('Import Submit Utils', () => {
);

expect(createPipelineResourceSpy).toHaveBeenCalledWith(mockData);
const pipelineResource = returnValue[6].data;
expect(pipelineResource.metadata.name).toEqual(mockData.name);
const pipelineRunResource = returnValue[1].data;
expect(pipelineRunResource.metadata.name.includes(mockData.name)).toEqual(true);
done();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import {
GitReadableTypes,
Resources,
} from './import-types';
import { createPipelineRunForImportFlow } from './pipeline/pipeline-template-utils';
import {
createPipelineForImportFlow,
createPipelineRunForImportFlow,
} from './pipeline/pipeline-template-utils';
import { Perspective } from '@console/plugin-sdk';

export const generateSecret = () => {
Expand Down Expand Up @@ -462,7 +465,8 @@ export const createOrUpdateResources = async (
),
);
} else if (pipeline.template && !dryRun) {
requests.push(createPipelineRunForImportFlow(formData));
const newPipeline = await createPipelineForImportFlow(formData);
requests.push(createPipelineRunForImportFlow(formData, newPipeline));
}

verb === 'create' && requests.push(createWebhookSecret(formData, 'generic', dryRun));
Expand Down

0 comments on commit c56c357

Please sign in to comment.