Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 357a609

Browse files
committed
fix: Clean up downloaded artifact after rollback (#289)
Resolves #287
1 parent 725dae2 commit 357a609

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/services/rollbackService.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ArmDeployment } from "../models/armTemplates";
55
import { DeploymentConfig } from "../models/serverless";
66
import { MockFactory } from "../test/mockFactory";
77
import { RollbackService } from "./rollbackService";
8+
import fs from "fs";
89

910
jest.mock("./azureBlobStorageService");
1011
import { AzureBlobStorageService } from "./azureBlobStorageService";
@@ -31,9 +32,10 @@ describe("Rollback Service", () => {
3132
const containerName = "deployment-artifacts";
3233
const artifactName = MockFactory.createTestDeployment().name.replace(
3334
configConstants.naming.suffix.deployment, configConstants.naming.suffix.artifact) + ".zip";
34-
const artifactPath = `.serverless${path.sep}${artifactName}`
35+
const artifactPath = path.join(".serverless", artifactName);
3536
const armDeployment: ArmDeployment = { template, parameters };
3637
const deploymentString = "deployments";
38+
let unlinkSpy: jest.SpyInstance;
3739

3840
function createOptions(timestamp?: string): Serverless.Options {
3941
return {
@@ -64,10 +66,12 @@ describe("Rollback Service", () => {
6466
ResourceService.prototype.listDeployments = jest.fn(() => Promise.resolve(deploymentString))
6567
AzureBlobStorageService.prototype.generateBlobSasTokenUrl = jest.fn(() => sasURL) as any;
6668
FunctionAppService.prototype.get = jest.fn(() => appStub) as any;
69+
unlinkSpy = jest.spyOn(fs, "unlinkSync");
6770
});
6871

6972
afterEach(() => {
7073
mockFs.restore();
74+
unlinkSpy.mockRestore();
7175
jest.resetAllMocks();
7276
});
7377

@@ -94,6 +98,11 @@ describe("Rollback Service", () => {
9498
});
9599

96100
it("should deploy blob package directly to function app", async () => {
101+
const fsConfig = {};
102+
fsConfig[artifactPath] = "contents";
103+
// Mocking the existence of the downloaded artifact because the downloadBinary
104+
// method won't write to the mock file system
105+
mockFs(fsConfig);
97106
const service = createService();
98107
await service.rollback();
99108
expect(AzureBlobStorageService.prototype.initialize).toBeCalled();
@@ -107,7 +116,8 @@ describe("Rollback Service", () => {
107116
expect(FunctionAppService.prototype.uploadZippedArfifactToFunctionApp).toBeCalledWith(
108117
appStub,
109118
artifactPath
110-
)
119+
);
120+
expect(unlinkSpy).toBeCalledWith(artifactPath);
111121
});
112122

113123
it("should deploy function app with SAS URL", async () => {
@@ -133,5 +143,6 @@ describe("Rollback Service", () => {
133143
);
134144
expect(FunctionAppService.prototype.get).not.toBeCalled();
135145
expect(FunctionAppService.prototype.uploadZippedArfifactToFunctionApp).not.toBeCalled();
146+
expect(unlinkSpy).not.toBeCalled();
136147
});
137148
});

src/services/rollbackService.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { BaseService } from "./baseService";
88
import { FunctionAppService } from "./functionAppService";
99
import { ResourceService } from "./resourceService";
1010
import { ArmDeployment } from "../models/armTemplates";
11+
import fs from "fs";
1112

1213
/**
1314
* Services for the Rollback Plugin
@@ -98,6 +99,9 @@ export class RollbackService extends BaseService {
9899
const functionAppService = new FunctionAppService(this.serverless, this.options);
99100
const functionApp = await functionAppService.get();
100101
await functionAppService.uploadZippedArfifactToFunctionApp(functionApp, artifactPath);
102+
if (fs.existsSync(artifactPath)) {
103+
fs.unlinkSync(artifactPath);
104+
}
101105
}
102106

103107
/**

src/test/mockFactory.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ArmDeployment, ArmResourceTemplate, ArmTemplateProvisioningState } from
1616
import { ServicePrincipalEnvVariables } from "../models/azureProvider";
1717
import { Logger } from "../models/generic";
1818
import { ServerlessAzureConfig, ServerlessAzureProvider, ServerlessAzureFunctionConfig, ServerlessCliCommand } from "../models/serverless";
19+
import configConstants from "../config";
1920

2021
function getAttribute(object: any, prop: string, defaultValue: any): any {
2122
if (object && object[prop]) {
@@ -174,7 +175,7 @@ export class MockFactory {
174175
const result = [];
175176
const originalTimestamp = +MockFactory.createTestTimestamp();
176177
for (let i = 0; i < count; i++) {
177-
const name = (includeTimestamp) ? `deployment${i + 1}-t${originalTimestamp + i}` : `deployment${i + 1}`;
178+
const name = (includeTimestamp) ? `${configConstants.naming.suffix.deployment}${i + 1}-t${originalTimestamp + i}` : `deployment${i + 1}`;
178179
result.push(
179180
MockFactory.createTestDeployment(name, i)
180181
)
@@ -194,7 +195,7 @@ export class MockFactory {
194195

195196
public static createTestDeployment(name?: string, second: number = 0): DeploymentExtended {
196197
return {
197-
name: name || `deployment1-t${MockFactory.createTestTimestamp()}`,
198+
name: name || `${configConstants.naming.suffix.deployment}1-t${MockFactory.createTestTimestamp()}`,
198199
properties: {
199200
timestamp: new Date(2019, 1, 1, 0, 0, second),
200201
parameters: MockFactory.createTestParameters(),

0 commit comments

Comments
 (0)