Skip to content

Commit

Permalink
feat: Cache provider config on initialization (#385)
Browse files Browse the repository at this point in the history
Cache provider configuration in serverless.variables in ConfigService initialization
  • Loading branch information
tbarlow12 committed Oct 25, 2019
1 parent 832e3b9 commit 7b57c82
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
50 changes: 35 additions & 15 deletions src/services/configService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ describe("Config Service", () => {

let serverless: Serverless;

beforeEach(() => {
serverless = MockFactory.createTestServerless();
const config = (serverless.service as any as ServerlessAzureConfig);
function createServerless() {
const sls = MockFactory.createTestServerless();
const config = (sls.service as any as ServerlessAzureConfig);
config.service = serviceName;

delete config.provider.resourceGroup;
delete config.provider.region;
delete config.provider.stage;
delete config.provider.prefix;
return sls;
}

beforeEach(() => {
serverless = createServerless();
});

describe("Configurable Variables", () => {
Expand Down Expand Up @@ -120,36 +125,51 @@ describe("Config Service", () => {
});

it("Gets deployment config from SLS yaml using external property", () => {
const sls1 = createServerless();
const deploymentConfig1: DeploymentConfig = {
external: true
}
serverless.service.provider["deployment"] = deploymentConfig1
const service1 = new ConfigService(serverless, { } as any);
sls1.service.provider["deployment"] = deploymentConfig1
const service1 = new ConfigService(sls1, { } as any);
expect(service1.getDeploymentConfig().external).toBe(true);

const sls2 = createServerless();

const deploymentConfig2: DeploymentConfig = {
external: false
}
serverless.service.provider["deployment"] = deploymentConfig2
const service2 = new ConfigService(serverless, { } as any);
sls2.service.provider["deployment"] = deploymentConfig2
const service2 = new ConfigService(sls2, { } as any);
expect(service2.getDeploymentConfig().external).toBe(false);
});

it("Gets deployment config from SLS yaml using runFromBlobUrl property", () => {
const deploymentConfig1 = {
const sls1 = createServerless();
const deploymentConfig1: DeploymentConfig = {
runFromBlobUrl: true
}
serverless.service.provider["deployment"] = deploymentConfig1
const service1 = new ConfigService(serverless, { } as any);
} as any;
sls1.service.provider["deployment"] = deploymentConfig1
const service1 = new ConfigService(sls1, { } as any);
expect(service1.getDeploymentConfig().external).toBe(true);

const deploymentConfig2 = {
const sls2 = createServerless();

const deploymentConfig2: DeploymentConfig = {
runFromBlobUrl: false
}
serverless.service.provider["deployment"] = deploymentConfig2
const service2 = new ConfigService(serverless, { } as any);
} as any;
sls2.service.provider["deployment"] = deploymentConfig2
const service2 = new ConfigService(sls2, { } as any);
expect(service2.getDeploymentConfig().external).toBe(false);
});

it("caches the configuration and does not initialize if config is cached", () => {
const setDefaultValues = jest.spyOn(ConfigService.prototype as any, "setDefaultValues");
new ConfigService(serverless, {} as any);
expect(setDefaultValues).toBeCalled();
setDefaultValues.mockClear();
new ConfigService(serverless, {} as any);
expect(setDefaultValues).not.toBeCalled();
});
});

describe("Service Principal Configuration", () => {
Expand Down
11 changes: 9 additions & 2 deletions src/services/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ export class ConfigService {
*/
private initializeConfig(service: Service): ServerlessAzureConfig {
const config: ServerlessAzureConfig = service as any;
const providerConfig = Utils.get(this.serverless.variables, constants.variableKeys.providerConfig);
if (providerConfig) {
config.provider = providerConfig;
return config;
}
this.serverless.cli.log("Initializing provider configuration...");
this.setDefaultValues(config);

const {
Expand Down Expand Up @@ -184,7 +190,7 @@ export class ConfigService {
this.getOption("resourceGroup", config.provider.resourceGroup)
) || AzureNamingService.getResourceName(options);

// Get property from `runFromBlobUrl` for backwards compatability
// Get property from `runFromBlobUrl` for backwards compatibility
if (deployment && deployment.external === undefined && deployment["runFromBlobUrl"] !== undefined) {
deployment.external = deployment["runFromBlobUrl"];
}
Expand All @@ -194,8 +200,9 @@ export class ConfigService {
...deployment
}

config.provider.functionRuntime = this.getRuntime(runtime)
config.provider.functionRuntime = this.getRuntime(runtime);

this.serverless.variables[constants.variableKeys.providerConfig] = config.provider;
return config;
}

Expand Down
3 changes: 2 additions & 1 deletion src/services/functionAppService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ describe("Function App Service", () => {
const logCalls = (sls.cli.log as any).mock.calls as any[];
for (let i = 0; i < functionNames.length; i++) {
const functionName = functionNames[i];
expect(logCalls[i + 1][0]).toEqual(`-> Deleting function: ${functionName}`);
const call = logCalls.find((c) => c[0] === `-> Deleting function: ${functionName}`);
expect(call).toBeTruthy();
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/services/rollbackService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe("Rollback Service", () => {
const service = createService(sls, options);
await service.rollback();
const calls = (sls.cli.log as any).mock.calls;
expect(calls[0][0]).toEqual(`Couldn't find deployment with timestamp: ${timestamp}`);
expect(calls[1][0]).toEqual(`Couldn't find deployment with timestamp: ${timestamp}`);
});

it("should deploy blob package directly to function app", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const constants = {
xAzureSettings: "x-azure-settings",
entryPoint: "entryPoint",
variableKeys: {
config: "serverlessAzureConfig",
providerConfig: "serverlessAzureProviderConfig",
subscriptionId: "subscriptionId",
tenantId: "tenantId",
appId: "appId",
Expand Down

0 comments on commit 7b57c82

Please sign in to comment.