|
| 1 | +import { write } from "../../config"; |
| 2 | +import * as azure from "../../lib/git/azure"; |
| 3 | +import * as gitutils from "../../lib/gitutils"; |
| 4 | +import { createTempDir } from "../../lib/ioUtil"; |
| 5 | +import { IBedrockFile } from "../../types"; |
| 6 | +import { |
| 7 | + getDefaultRings, |
| 8 | + getSourceBranch, |
| 9 | + makePullRequest |
| 10 | +} from "./create-revision"; |
| 11 | + |
| 12 | +jest |
| 13 | + .spyOn(gitutils, "getCurrentBranch") |
| 14 | + .mockReturnValueOnce(Promise.resolve("prod")) |
| 15 | + .mockReturnValue(Promise.resolve("")); |
| 16 | +const prSpy = jest |
| 17 | + .spyOn(azure, "createPullRequest") |
| 18 | + .mockReturnValue(Promise.resolve("done")); |
| 19 | + |
| 20 | +describe("Default rings", () => { |
| 21 | + test("Get multiple default rings", () => { |
| 22 | + const randomTmpDir = createTempDir(); |
| 23 | + const validBedrockYaml: IBedrockFile = { |
| 24 | + rings: { |
| 25 | + master: { isDefault: true }, |
| 26 | + prod: { isDefault: false }, |
| 27 | + westus: { isDefault: true } |
| 28 | + }, |
| 29 | + services: { |
| 30 | + "foo/a": { |
| 31 | + helm: { |
| 32 | + chart: { |
| 33 | + chart: "elastic", |
| 34 | + repository: "some-repo" |
| 35 | + } |
| 36 | + }, |
| 37 | + k8sBackend: "backendservice", |
| 38 | + k8sBackendPort: 1337, |
| 39 | + pathPrefix: "servicepath", |
| 40 | + pathPrefixMajorVersion: "v1" |
| 41 | + } |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + write(validBedrockYaml, randomTmpDir); |
| 46 | + const defaultRings = getDefaultRings(undefined, validBedrockYaml); |
| 47 | + expect(defaultRings.length).toBe(2); |
| 48 | + expect(defaultRings[0]).toBe("master"); |
| 49 | + expect(defaultRings[1]).toBe("westus"); |
| 50 | + }); |
| 51 | + |
| 52 | + test("No default rings", () => { |
| 53 | + const randomTmpDir = createTempDir(); |
| 54 | + const validBedrockYaml: IBedrockFile = { |
| 55 | + rings: { |
| 56 | + master: { isDefault: false }, |
| 57 | + prod: { isDefault: false }, |
| 58 | + westus: { isDefault: false } |
| 59 | + }, |
| 60 | + services: { |
| 61 | + "foo/a": { |
| 62 | + helm: { |
| 63 | + chart: { |
| 64 | + chart: "elastic", |
| 65 | + repository: "some-repo" |
| 66 | + } |
| 67 | + }, |
| 68 | + k8sBackend: "backendservice", |
| 69 | + k8sBackendPort: 1337, |
| 70 | + pathPrefix: "servicepath", |
| 71 | + pathPrefixMajorVersion: "v1" |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + write(validBedrockYaml, randomTmpDir); |
| 77 | + let hasError = false; |
| 78 | + |
| 79 | + try { |
| 80 | + getDefaultRings(undefined, validBedrockYaml); |
| 81 | + } catch (err) { |
| 82 | + hasError = true; |
| 83 | + } |
| 84 | + expect(hasError).toBe(true); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe("Source branch", () => { |
| 89 | + test("Defined source branch", async () => { |
| 90 | + const branch = "master"; |
| 91 | + const sourceBranch = await getSourceBranch(branch); |
| 92 | + expect(sourceBranch).toBe("master"); |
| 93 | + }); |
| 94 | + test("Defined source branch", async () => { |
| 95 | + const branch = undefined; |
| 96 | + const sourceBranch = await getSourceBranch(branch); |
| 97 | + expect(sourceBranch).toBe("prod"); |
| 98 | + }); |
| 99 | + test("No source branch", async () => { |
| 100 | + const branch = undefined; |
| 101 | + let hasError = false; |
| 102 | + try { |
| 103 | + await getSourceBranch(branch); |
| 104 | + } catch (err) { |
| 105 | + hasError = true; |
| 106 | + } |
| 107 | + expect(hasError).toBe(true); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe("Create pull request", () => { |
| 112 | + test("invalid parameters", async () => { |
| 113 | + for (const i of Array(4).keys()) { |
| 114 | + let hasError = false; |
| 115 | + try { |
| 116 | + await makePullRequest( |
| 117 | + ["master"], |
| 118 | + "testTitle", |
| 119 | + i === 0 ? undefined : "branch", |
| 120 | + "description", |
| 121 | + i === 1 ? undefined : "org", |
| 122 | + i === 2 ? undefined : "url", |
| 123 | + i === 3 ? undefined : "token" |
| 124 | + ); |
| 125 | + } catch (err) { |
| 126 | + hasError = true; |
| 127 | + } |
| 128 | + expect(hasError).toBe(true); |
| 129 | + } |
| 130 | + }); |
| 131 | + test("Valid parameters", async () => { |
| 132 | + await makePullRequest( |
| 133 | + ["master"], |
| 134 | + "testTitle", |
| 135 | + "testBranch", |
| 136 | + "testDescription", |
| 137 | + "testOrg", |
| 138 | + "testUrl", |
| 139 | + "testToken" |
| 140 | + ); |
| 141 | + expect(prSpy).toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + test("Default description", async () => { |
| 144 | + await makePullRequest( |
| 145 | + ["master"], |
| 146 | + "testTitle", |
| 147 | + "testBranch", |
| 148 | + undefined, |
| 149 | + "testOrg", |
| 150 | + "testUrl", |
| 151 | + "testToken" |
| 152 | + ); |
| 153 | + expect(prSpy).toHaveBeenCalled(); |
| 154 | + }); |
| 155 | + test("Default title", async () => { |
| 156 | + await makePullRequest( |
| 157 | + ["master"], |
| 158 | + undefined, |
| 159 | + "testBranch", |
| 160 | + "description", |
| 161 | + "testOrg", |
| 162 | + "testUrl", |
| 163 | + "testToken" |
| 164 | + ); |
| 165 | + expect(prSpy).toHaveBeenCalled(); |
| 166 | + }); |
| 167 | +}); |
0 commit comments