Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #39 from microsoft/edaena-svc-bug
Browse files Browse the repository at this point in the history
Extract service path for pipeline definition location instead of service name for service install-build-pipeline
  • Loading branch information
edaena authored May 6, 2020
2 parents ca1f98f + 6d30fc1 commit 3989c3a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
35 changes: 30 additions & 5 deletions src/commands/service/pipeline.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as azdoGit from "../../lib/git/azure";
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";

import { create as createBedrockYaml } from "../../lib/bedrockYaml";
jest.mock("../../lib/pipelines/pipelines");

import {
Expand All @@ -17,6 +17,7 @@ import {
requiredPipelineVariables,
} from "./pipeline";
import * as pipeline from "./pipeline";
import { createTempDir } from "../../lib/ioUtil";

const MOCKED_VALUES: CommandOptions = {
buildScriptUrl: "buildScriptUrl",
Expand Down Expand Up @@ -80,35 +81,59 @@ describe("test fetchValues function", () => {
});

describe("test execute function", () => {
const tmpDir = createTempDir();
createBedrockYaml(tmpDir, {
rings: {
master: {
isDefault: true,
},
},
services: [
{
displayName: "serviceName",
path: "./my-service",
helm: {
chart: {
branch: "master",
git: "https://github.com/microsoft/bedrock-cli-demo-repo.git",
path: "my-service",
},
},
k8sBackendPort: 80,
},
],
variableGroups: ["testvg"],
version: "1.0",
});
it("positive test: with all values set", async () => {
const exitFn = jest.fn();
jest
.spyOn(pipeline, "installBuildUpdatePipeline")
.mockReturnValueOnce(Promise.resolve());

await execute("serviceName", getMockedValues(), exitFn);
await execute("serviceName", tmpDir, getMockedValues(), exitFn);
expect(exitFn).toBeCalledTimes(1);
expect(exitFn.mock.calls).toEqual([[0]]);
});
it("negative test: definitionForAzureRepoPipeline without return id", async () => {
const exitFn = jest.fn();
(createPipelineForDefinition as jest.Mock).mockReturnValueOnce({}); // without id
await execute("serviceName", getMockedValues(), exitFn);
await execute("serviceName", tmpDir, getMockedValues(), exitFn);
expect(exitFn).toBeCalledTimes(1);
expect(exitFn.mock.calls).toEqual([[1]]);
});
it("negative test: repo url not defined", async () => {
const exitFn = jest.fn();
const mockedVals = getMockedValues();
mockedVals.repoUrl = "";
await execute("serviceName", mockedVals, exitFn);
await execute("serviceName", tmpDir, mockedVals, exitFn);
expect(exitFn).toBeCalledTimes(1);
});
it("negative test: github repo not supported", async () => {
const exitFn = jest.fn();
const mockedVals = getMockedValues();
mockedVals.repoUrl = "https://github.com/microsoft/bedrock";
await execute("serviceName", mockedVals, exitFn);
await execute("serviceName", tmpDir, mockedVals, exitFn);
expect(exitFn).toBeCalledTimes(1);
});
});
Expand Down
32 changes: 28 additions & 4 deletions src/commands/service/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BuildDefinitionVariable,
} from "azure-devops-node-api/interfaces/BuildInterfaces";
import commander from "commander";
import { read as loadBedrockFile } from "../../lib/bedrockYaml";
import path from "path";
import { Config } from "../../config";
import { validateRepository } from "../../lib/git/azure";
Expand Down Expand Up @@ -39,6 +40,7 @@ import {
validateOrgNameThrowable,
validateProjectNameThrowable,
} from "../../lib/validator";
import { BedrockFile } from "../../types";

export interface CommandOptions {
orgName: string;
Expand Down Expand Up @@ -173,6 +175,7 @@ export const installBuildUpdatePipeline = async (

export const execute = async (
serviceName: string,
projectPath: string,
opts: CommandOptions,
exitFn: (status: number) => Promise<void>
): Promise<void> => {
Expand All @@ -189,6 +192,22 @@ export const execute = async (
}

await fetchValues(serviceName, opts);
const bedrockFile: BedrockFile = loadBedrockFile(projectPath);
let servicePath = "";
bedrockFile.services.forEach((service) => {
if (service.displayName === serviceName) {
servicePath = service.path;
return;
}
});

if (servicePath === "") {
throw buildError(errorStatusCode.VALIDATION_ERR, {
errorKey: "project-pipeline-err-service-missing",
values: [serviceName],
});
}

const accessOpts: AzureDevOpsOpts = {
orgName: opts.orgName,
personalAccessToken: opts.personalAccessToken,
Expand All @@ -200,7 +219,7 @@ export const execute = async (
? // if a packages dir is supplied, concat <packages-dir>/<service-name>
path.join(opts.packagesDir, serviceName, SERVICE_PIPELINE_FILENAME)
: // if no packages dir, then just concat with the service directory.
path.join(serviceName, SERVICE_PIPELINE_FILENAME);
path.join(servicePath, SERVICE_PIPELINE_FILENAME);

// By default the version descriptor is for the master branch
await validateRepository(
Expand Down Expand Up @@ -228,9 +247,14 @@ export const execute = async (
export const commandDecorator = (command: commander.Command): void => {
buildCmd(command, decorator).action(
async (serviceName: string, opts: CommandOptions) => {
await execute(serviceName, opts, async (status: number) => {
await exitCmd(logger, process.exit, status);
});
await execute(
serviceName,
process.cwd(),
opts,
async (status: number) => {
await exitCmd(logger, process.exit, status);
}
);
}
);
};
1 change: 1 addition & 0 deletions src/lib/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"project-pipeline-err-bedrock-config-missing": "Bedrock Config was missing",
"project-pipeline-err-pipeline-create": "Error occurred during pipeline creation for {0}",
"project-pipeline-err-invalid-build-definition": "Invalid BuildDefinition created, parameter 'id' was missing from {0}",
"project-pipeline-err-service-missing": "Service definition for service '{0}' was not found. Check that the service is definied in the bedrock.yaml file.",

"git-err-show-current-branch": "Could not get the current branch of git. Ensure 'git branch --show-current' returns a proper response.",
"git-err-checkout": "Could not checkout git branch, {0}.",
Expand Down
2 changes: 1 addition & 1 deletion tests/validations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ frontend_pipeline_name="$FrontEnd-pipeline"
pipeline_exists $AZDO_ORG_URL $AZDO_PROJECT $frontend_pipeline_name

# Create a pipeline since the code exists in remote repo
bedrock service install-build-pipeline --org-name $AZDO_ORG -u $remote_repo_url -d $AZDO_PROJECT -l $services_dir --personal-access-token $ACCESS_TOKEN_SECRET -n $frontend_pipeline_name -v $FrontEnd >> $TEST_WORKSPACE/log.txt
bedrock service install-build-pipeline --org-name $AZDO_ORG -u $remote_repo_url -d $AZDO_PROJECT --personal-access-token $ACCESS_TOKEN_SECRET -n $frontend_pipeline_name -v $FrontEnd >> $TEST_WORKSPACE/log.txt

# Verify frontend service pipeline was created
pipeline_created=$(az pipelines show --name $frontend_pipeline_name --org $AZDO_ORG_URL --p $AZDO_PROJECT)
Expand Down

0 comments on commit 3989c3a

Please sign in to comment.