I am using mocha to unit test my pulumi code. I was able to test and mock my resources by injecting a custom pulumi.runtime.Mocks.
However, when I start using pulumi.Config() with some required fields, pulumi while under test does not read my config files. Some resources are instantiated and configure from index.ts.
How I am checking is calling const config = new pulumi.Config(), and seeing if my config shows up.
I tried the following:
- setting
pulumi.runtime.setAllConfigglobally, returned an empty config object. - adding
getConfigfunction intopulumi.runtime.Mocks, returned an empty config object. - using
pulumi.runtime.setConfigfor individual config items within before(), returned an empty config object.
I have no idea what to do.
Here is a simplified example of my issue
// index.ts
import * as aws from "@pulumi/aws";
const config = new pulumi.Config();
const env = config.require("env");
const region = config.require("region");
const userData = `#!/bin/bash echo "Hello, World!" > index.html nohup python -m SimpleHTTPServer 80 &`;
export const server = new aws.ec2.Instance("web-server-www", {
instanceType: "t2.micro",
securityGroups: [group.name], // reference the group object above
ami: "ami-c55673a0", // AMI for us-east-2 (Ohio)
userData: userData, // start a simple webserver
tags: { Name: "www-server", env: env, region: region }, // name tag
});
// test.ts
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as mocha from "mocha";
// Mocks for Pulumi configuration
class MyMock implements pulumi.runtime.Mocks {
public newResource(args: pulumi.runtime.MockResourceArgs): { id: string; state: any } {
return {
id: args.inputs.name + "_id",
state: args.inputs,
};
}
public call(args: pulumi.runtime.MockCallArgs): Record<string, any> {
return {};
}
public getConfig(key: string): string {
const config: { [key: string]: string } = {
env: "dev",
region: "us-west-2",
};
return config[key];
}
}
// Sets the mocks for the Pulumi runtime
pulumi.runtime.setAllConfig({env:"dev", region:"us-west-2"}) // config object is empty
// Function to read and log configuration
function getConfig() {
const config = new pulumi.Config();
const env = config.require("env");
const region = config.require("region");
}
describe("Infrastructure", function () {
let infra: typeof import("./index");
before(async function () {
// It's important to import the program _after_ the mocks are defined.
getConfig() // config object is empty
pulumi.runtime.setConfig("env","dev")
pulumi.runtime.setConfig("region","us-west-2") // config object is empty
infra = await import("./index");
const config = pulumi.Config() // where I am checking if configs are loaded
});
describe("#server", function () {
// check 1: Instances have a Name tag.
it("must have a name tag", function (done) {
pulumi.all([infra.server.urn, infra.server.tags]).apply(([urn, tags]) => {
if (!tags || !tags["Name"]) {
done(new Error(`Missing a name tag on server ${urn}`));
} else {
done();
}
});
});
});
});