Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

Commit

Permalink
[FEATURE] Completing spk setup command (#464)
Browse files Browse the repository at this point in the history
* create storage account

* stage

* prompt merging second PR

* Update setup.md

* finishing touch

* update config.yaml with repo info

* Update jest.config.js

* added storage tests

* minor fixes

* add docker image in config,yaml

* final touch

* fix unit test

* Update setup.test.ts

* Update validator.test.ts

* Update pipeline.test.ts
  • Loading branch information
dennisseah authored Mar 31, 2020
1 parent 44c9970 commit c624b62
Show file tree
Hide file tree
Showing 26 changed files with 942 additions and 331 deletions.
2 changes: 1 addition & 1 deletion docs/commands/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"description": "Path to the file that contains answers to the questions."
}
],
"markdown": "## Description\n\nThis command assists in creating resources in Azure DevOps so that you can get\nstarted with using Bedrock. It creates\n\n1. An Azure DevOps project.\n\nBy Default, it runs in an interactive mode where you are prompted for answers\nfor a few questions\n\n1. Azure DevOps Organization Name\n2. Azure DevOps Project Name, the project to be created.\n3. Azure DevOps Personal Access Token. The token needs to have these permissions\n 1. Read and write projects.\n 2. Read and write codes.\n4. To create a sample application Repo\n 1. If Yes, a Azure Service Principal is needed. You have 2 options\n 1. have the command line tool to create it. Azure command line tool shall\n be used. You will be prompted to select a subscription identifier.\n 2. Provide the Service Principal Id, Password, and Tenant Id. From this\n information, the tool will retrieve the subscription identifier.\n\nIt can also run in a non interactive mode by providing a file that contains\nanswers to the above questions.\n\n```\nspk setup --file <file-name>\n```\n\nContent of this file is as follow\n\n```\nazdo_org_name=<Azure DevOps Organization Name>\nazdo_project_name=<Azure DevOps Project Name>\nazdo_pat=<Azure DevOps Personal Access Token>\naz_create_app=<true to create sample service app>\naz_create_sp=<true to have command line to create service principal>\naz_sp_id=<sevice principal Id need if az_create_app=true and az_create_sp=false>\naz_sp_password=<sevice principal password need if az_create_app=true and az_create_sp=false>\naz_sp_tenant=<sevice principal tenant Id need if az_create_app=true and az_create_sp=false>\naz_subscription_id=<subscription id>\naz_acr_name=<name of azure container registry>\n```\n\n`azdo_project_name` is optional and default value is `BedrockRocks`.\n\nThe followings shall be created\n\n1. A working directory, `quick-start-env`\n2. Project shall not be created if it already exists.\n3. A Git Repo, `quick-start-hld`, it shall be deleted and recreated if it\n already exists.\n 1. And initial commit shall be made to this repo\n4. A Git Repo, `quick-start-manifest`, it shall be deleted and recreated if it\n already exists.\n 1. And initial commit shall be made to this repo\n5. A High Level Definition (HLD) to Manifest pipeline.\n6. If user chose to create sample app repo\n 1. A Service Principal (if requested)\n 2. A resource group, `quick-start-rg` if it does not exist.\n 3. A Azure Container Registry, `quickStartACR` in resource group,\n `quick-start-rg` if it does not exist.\n 4. A Git Repo, `quick-start-helm`, it shall be deleted and recreated if is\n already exists.\n 5. A Git Repo, `quick-start-app`, it shall be deleted and recreated if is\n already exists.\n 6. A Lifecycle pipeline.\n 7. A Build pipeline.\n\n## Setup log\n\nA `setup.log` file is created after running this command. This file contains\ninformation about what are created and the execution status (completed or\nincomplete). This file will not be created if input validation failed.\n\n## Note\n\nTo remove the service principal that it is created by the tool, you can do the\nfollowings:\n\n1. Get the identifier from `setup.log` (look for `az_sp_id`)\n2. run on terminal `az ad sp delete --id <the sp id>`\n"
"markdown": "## Description\n\nThis command assists in creating resources in Azure DevOps so that you can get\nstarted with using Bedrock. It creates\n\n1. An Azure DevOps project.\n\nBy Default, it runs in an interactive mode where you are prompted for answers\nfor a few questions\n\n1. Azure DevOps Organization Name\n2. Azure DevOps Project Name, the project to be created.\n3. Azure DevOps Personal Access Token. The token needs to have these permissions\n 1. Read and write projects.\n 2. Read and write codes.\n4. To create a sample application Repo\n 1. If Yes, a Azure Service Principal is needed. You have 2 options\n 1. have the command line tool to create it. Azure command line tool shall\n be used. You will be prompted to select a subscription identifier.\n 2. Provide the Service Principal Id, Password, and Tenant Id. From this\n information, the tool will retrieve the subscription identifier.\n\nIt can also run in a non interactive mode by providing a file that contains\nanswers to the above questions.\n\nAfter this command is successfully executed, you can launch the introspection\ndashboard to view the status of pipelines.\n\n```\nspk setup --file <file-name>\n```\n\nContent of this file is as follow\n\n```\nazdo_org_name=<Azure DevOps Organization Name>\nazdo_project_name=<Azure DevOps Project Name>\nazdo_pat=<Azure DevOps Personal Access Token>\naz_create_app=<true to create sample service app>\naz_create_sp=<true to have command line to create service principal>\naz_sp_id=<sevice principal Id need if az_create_app=true and az_create_sp=false>\naz_sp_password=<sevice principal password need if az_create_app=true and az_create_sp=false>\naz_sp_tenant=<sevice principal tenant Id need if az_create_app=true and az_create_sp=false>\naz_subscription_id=<subscription id>\naz_acr_name=<name of azure container registry>\n```\n\n`azdo_project_name` is optional and default value is `BedrockRocks`.\n\nThe followings shall be created\n\n1. A working directory, `quick-start-env`\n2. Project shall not be created if it already exists.\n3. A Git Repo, `quick-start-hld`, it shall be deleted and recreated if it\n already exists.\n 1. And initial commit shall be made to this repo\n4. A Git Repo, `quick-start-manifest`, it shall be deleted and recreated if it\n already exists.\n 1. And initial commit shall be made to this repo\n5. A High Level Definition (HLD) to Manifest pipeline.\n6. If user chose to create sample app repo\n 1. A Service Principal (if requested)\n 1. A resource group, `quick-start-rg` if it does not exist.\n 1. A storage account if it does not exist. Storage Account name has to be\n unqiue acess Azure.\n 1. A storage table in the storage account.\n 1. A Azure Container Registry, `quickStartACR` in resource group,\n `quick-start-rg` if it does not exist.\n 1. A Git Repo, `quick-start-helm`, it shall be deleted and recreated if is\n already exists.\n 1. A Git Repo, `quick-start-app`, it shall be deleted and recreated if is\n already exists.\n 1. A Lifecycle pipeline.\n 1. A Build pipeline.\n\n## Setup log\n\nA `setup.log` file is created after running this command. This file contains\ninformation about what are created and the execution status (completed or\nincomplete). This file will not be created if input validation failed.\n\n## Note\n\nTo remove the service principal that it is created by the tool, you can do the\nfollowings:\n\n1. Get the identifier from `setup.log` (look for `az_sp_id`)\n2. run on terminal `az ad sp delete --id <the sp id>`\n"
},
"deployment create": {
"command": "create",
Expand Down
8 changes: 4 additions & 4 deletions guides/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const Sheep = ({ says, legs }: IAnimal) => {
says: () => "bahhh",
legs: 4,
says,
legs
legs,
};
};
```
Expand Down Expand Up @@ -305,7 +305,7 @@ function foo(bar: number): string {
}

// Don't do
const foo = function(bar: number): string {
const foo = function (bar: number): string {
return bar.toString();
};

Expand Down Expand Up @@ -339,10 +339,10 @@ const promiseBasedReadFile = promisify(readFile);

// Full promise based
promiseBasedReadFile("/etc/passwd")
.then(data => {
.then((data) => {
console.log(data);
})
.catch(err => {
.catch((err) => {
console.error(err);
return Promise.reject(err);
});
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node"
testEnvironment: "node",
};
62 changes: 23 additions & 39 deletions src/commands/hld/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ afterAll(() => {
disableVerboseLogging();
});

jest.spyOn(azdo, "validateRepository").mockReturnValue(Promise.resolve());
jest.spyOn(azdo, "validateRepository").mockResolvedValue();

describe("test emptyStringIfUndefined function", () => {
it("pass in undefined", () => {
Expand All @@ -73,6 +73,7 @@ describe("test emptyStringIfUndefined function", () => {
});

const orgNameTest = (hasVal: boolean): void => {
jest.spyOn(config, "Config").mockReturnValueOnce({});
const data = {
buildScriptUrl: "",
devopsProject: "project",
Expand All @@ -97,6 +98,7 @@ const orgNameTest = (hasVal: boolean): void => {
};

const projectNameTest = (hasVal: boolean): void => {
jest.spyOn(config, "Config").mockReturnValueOnce({});
const data = {
buildScriptUrl: "",
devopsProject: hasVal ? "project\\abc" : "",
Expand All @@ -122,11 +124,7 @@ const projectNameTest = (hasVal: boolean): void => {

describe("test populateValues function", () => {
it("with all values in command opts", () => {
jest.spyOn(config, "Config").mockImplementationOnce(
(): ConfigYaml => {
return MOCKED_CONFIG;
}
);
jest.spyOn(config, "Config").mockReturnValueOnce(MOCKED_CONFIG);
const mockedObject = getMockObject();
expect(populateValues(mockedObject)).toEqual(mockedObject);
});
Expand Down Expand Up @@ -195,15 +193,11 @@ describe("test populateValues function", () => {

describe("test execute function", () => {
it("positive test", async () => {
jest.spyOn(config, "Config").mockImplementationOnce(
(): ConfigYaml => {
return MOCKED_CONFIG;
}
);
jest.spyOn(config, "Config").mockReturnValueOnce(MOCKED_CONFIG);
const exitFn = jest.fn();
jest
.spyOn(pipeline, "installHldToManifestPipeline")
.mockReturnValueOnce(Promise.resolve());
.mockResolvedValueOnce();

await execute(MOCKED_VALUES, exitFn);
expect(exitFn).toBeCalledTimes(1);
Expand Down Expand Up @@ -243,43 +237,33 @@ describe("required pipeline variables", () => {

describe("create hld to manifest pipeline test", () => {
it("should create a pipeline", async () => {
(createPipelineForDefinition as jest.Mock).mockReturnValue({ id: 10 });
(createPipelineForDefinition as jest.Mock).mockReturnValueOnce({ id: 10 });
await installHldToManifestPipeline(getMockObject());
});

it("should fail if the build client cant be instantiated", async () => {
(getBuildApiClient as jest.Mock).mockReturnValue(Promise.reject("Error"));
try {
await installHldToManifestPipeline(getMockObject());
expect(true).toBe(false);
} catch (err) {
expect(err).toBeDefined();
}
(getBuildApiClient as jest.Mock).mockRejectedValueOnce(Error("Error"));
await expect(installHldToManifestPipeline(getMockObject())).rejects.toThrow(
"Error"
);
});

it("should fail if the pipeline definition cannot be created", async () => {
(getBuildApiClient as jest.Mock).mockReturnValue({});
(createPipelineForDefinition as jest.Mock).mockReturnValue(
Promise.reject("Error")
(getBuildApiClient as jest.Mock).mockReturnValueOnce({});
(createPipelineForDefinition as jest.Mock).mockRejectedValueOnce(
Error("Error")
);
await expect(installHldToManifestPipeline(getMockObject())).rejects.toThrow(
"Error"
);
try {
await installHldToManifestPipeline(getMockObject());
expect(true).toBe(false);
} catch (err) {
expect(err).toBeDefined();
}
});

it("should fail if a build cannot be queued on the pipeline", async () => {
(getBuildApiClient as jest.Mock).mockReturnValue({});
(createPipelineForDefinition as jest.Mock).mockReturnValue({ id: 10 });
(queueBuild as jest.Mock).mockReturnValue(Promise.reject("Error"));

try {
await installHldToManifestPipeline(getMockObject());
expect(true).toBe(false);
} catch (err) {
expect(err).toBeDefined();
}
(getBuildApiClient as jest.Mock).mockReturnValueOnce({});
(createPipelineForDefinition as jest.Mock).mockReturnValueOnce({ id: 10 });
(queueBuild as jest.Mock).mockRejectedValueOnce(Error("Error"));
await expect(installHldToManifestPipeline(getMockObject())).rejects.toThrow(
"Error"
);
});
});
18 changes: 12 additions & 6 deletions src/commands/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ for a few questions
It can also run in a non interactive mode by providing a file that contains
answers to the above questions.

After this command is successfully executed, you can launch the introspection
dashboard to view the status of pipelines.

```
spk setup --file <file-name>
```
Expand Down Expand Up @@ -57,15 +60,18 @@ The followings shall be created
5. A High Level Definition (HLD) to Manifest pipeline.
6. If user chose to create sample app repo
1. A Service Principal (if requested)
2. A resource group, `quick-start-rg` if it does not exist.
3. A Azure Container Registry, `quickStartACR` in resource group,
1. A resource group, `quick-start-rg` if it does not exist.
1. A storage account if it does not exist. Storage Account name has to be
unqiue acess Azure.
1. A storage table in the storage account.
1. A Azure Container Registry, `quickStartACR` in resource group,
`quick-start-rg` if it does not exist.
4. A Git Repo, `quick-start-helm`, it shall be deleted and recreated if is
1. A Git Repo, `quick-start-helm`, it shall be deleted and recreated if is
already exists.
5. A Git Repo, `quick-start-app`, it shall be deleted and recreated if is
1. A Git Repo, `quick-start-app`, it shall be deleted and recreated if is
already exists.
6. A Lifecycle pipeline.
7. A Build pipeline.
1. A Lifecycle pipeline.
1. A Build pipeline.

## Setup log

Expand Down
Loading

0 comments on commit c624b62

Please sign in to comment.