This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathazureDeployPlugin.test.ts
108 lines (89 loc) · 4.18 KB
/
azureDeployPlugin.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Site } from "@azure/arm-appservice/esm/models";
import Serverless from "serverless";
import { ServerlessAzureOptions, ServerlessAzureConfig } from "../../models/serverless";
import { MockFactory } from "../../test/mockFactory";
import { invokeHook } from "../../test/utils";
import { AzureDeployPlugin } from "./azureDeployPlugin";
import {vol} from "memfs"
import fs from "fs"
jest.mock("../../services/functionAppService");
import { FunctionAppService } from "../../services/functionAppService";
jest.mock("../../services/resourceService");
import { ResourceService } from "../../services/resourceService";
import { ApimService } from "../../services/apimService";
describe("Deploy plugin", () => {
let sls: Serverless;
let options: ServerlessAzureOptions;
let plugin: AzureDeployPlugin;
beforeAll(() => {
vol.fromNestedJSON({
"serviceName.zip": "contents",
}, process.cwd());
});
beforeEach(() => {
FunctionAppService.prototype.getFunctionZipFile = jest.fn(() => "serviceName.zip");
ApimService.prototype.deploy = jest.fn();
sls = MockFactory.createTestServerless();
options = MockFactory.createTestServerlessOptions();
plugin = new AzureDeployPlugin(sls, options);
});
afterEach(() => {
jest.resetAllMocks();
});
afterAll(() => {
vol.reset();
})
it("calls deploy", async () => {
const existsSpy = jest.spyOn(fs, "existsSync");
existsSpy.mockImplementation(() => true);
const deployResourceGroup = jest.fn();
const functionAppStub: Site = MockFactory.createTestSite();
const deploy = jest.fn(() => Promise.resolve(functionAppStub));
const uploadFunctions = jest.fn();
ResourceService.prototype.deployResourceGroup = deployResourceGroup;
FunctionAppService.prototype.deploy = deploy;
FunctionAppService.prototype.uploadFunctions = uploadFunctions;
await invokeHook(plugin, "deploy:deploy");
expect(deployResourceGroup).toBeCalled();
expect(deploy).toBeCalled();
expect(uploadFunctions).toBeCalledWith(functionAppStub);
});
it("Crashes deploy if function is specified", async () => {
plugin = new AzureDeployPlugin(sls, { function: "myFunction" } as any);
await expect(invokeHook(plugin, "deploy:deploy"))
.rejects.toThrow("The Azure Functions plugin does not currently support deployments of individual functions. " +
"Azure Functions are zipped up as a package and deployed together as a unit");
});
it("lists deployments from sub-command", async () => {
const deploymentString = "deployments";
ResourceService.prototype.listDeployments = jest.fn(() => Promise.resolve(deploymentString));
await invokeHook(plugin, "deploy:list:list");
expect(ResourceService.prototype.listDeployments).toBeCalled();
expect(sls.cli.log).lastCalledWith(deploymentString);
});
it("deploys APIM from sub-command if configured", async () => {
(sls.service as any as ServerlessAzureConfig).provider.apim = {} as any;
plugin = new AzureDeployPlugin(sls, {} as any);
await invokeHook(plugin, "deploy:apim:apim");
expect(ApimService.prototype.deploy).toBeCalled();
});
it("skips deployment of APIM from sub-command if not configured", async () => {
delete (sls.service as any as ServerlessAzureConfig).provider.apim;
plugin = new AzureDeployPlugin(sls, {} as any);
await invokeHook(plugin, "deploy:apim:apim");
expect(ApimService.prototype.deploy).not.toBeCalled();
});
it("crashes deploy list if function is specified", async () => {
plugin = new AzureDeployPlugin(sls, { function: "myFunction" } as any);
await expect(invokeHook(plugin, "deploy:list:list"))
.rejects.toThrow("The Azure Functions plugin does not currently support deployments of individual functions. " +
"Azure Functions are zipped up as a package and deployed together as a unit");
});
it("crashes deploy if zip file is not found", async () => {
const existsSpy = jest.spyOn(fs, "existsSync");
existsSpy.mockImplementation(() => true);
FunctionAppService.prototype.getFunctionZipFile = jest.fn(() => "notExisting.zip");
await expect(invokeHook(plugin, "deploy:deploy"))
.resolves.toThrow(/Function app zip file '.*' does not exist/)
});
});