Skip to content

Commit 50fd0bf

Browse files
authored
Add resourcemanager project unit tests (#110)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent c6caf87 commit 50fd0bf

8 files changed

Lines changed: 4375 additions & 3926 deletions

File tree

package-lock.json

Lines changed: 446 additions & 3926 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"network_area_unit_test": "file:tests/ts/iaas/network-area",
1818
"network_interface_unit_test": "file:tests/ts/iaas/network-interface",
1919
"network_unit_test": "file:tests/ts/iaas/network",
20+
"project_unit_test": "file:tests/ts/resourcemanager/project",
2021
"public_ip_unit_test": "file:tests/ts/iaas/public_ip",
2122
"routing_table_unit_test": "file:tests/ts/iaas/routingTable",
2223
"securitygroup_unit_test": "file:tests/ts/iaas/securityGroup",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: project_unit_test
2+
description: A minimal TypeScript Pulumi program
3+
runtime:
4+
name: nodejs
5+
options:
6+
packagemanager: npm
7+
config:
8+
pulumi:tags:
9+
value:
10+
pulumi:template: typescript
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as pulumi from "@pulumi/pulumi";
2+
import * as stackit from "@stackitcloud/pulumi-stackit";
3+
4+
export const projectParentContainerId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
5+
export const projectName = "example-project-name";
6+
export const projectOwnerEmail = "john.doe@stackit.cloud";
7+
8+
export const projectLabelKey = "unit-test";
9+
export const projectLabelValue = "test-label-value";
10+
11+
// datasource
12+
export const projectId = "project-id-to-read";
13+
14+
export const exampleProjectOnlyRequired = new stackit.ResourcemanagerProject("example_project_req", {
15+
parentContainerId: projectParentContainerId,
16+
name: projectName,
17+
labels: {[projectLabelKey]:projectLabelValue},
18+
ownerEmail: projectOwnerEmail,
19+
});
20+
21+
// datasource
22+
export const projectDatasource = stackit.getResourcemanagerProjectOutput({
23+
projectId: projectId,
24+
});

tests/ts/resourcemanager/project/package-lock.json

Lines changed: 3715 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "project_unit_test",
3+
"main": "index.ts",
4+
"devDependencies": {
5+
"@types/mocha": "^10.0.10",
6+
"@types/node": "^18",
7+
"mocha": "^11.7.5",
8+
"ts-node": "^10.9.2",
9+
"typescript": "^5.0.0"
10+
},
11+
"dependencies": {
12+
"@pulumi/pulumi": "^3.113.0",
13+
"@stackitcloud/pulumi-stackit": "file:../../../../sdk/nodejs/bin"
14+
},
15+
"scripts": {
16+
"test": "mocha -r ts-node/register test.ts"
17+
}
18+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import * as assert from "assert";
2+
import * as pulumi from "@pulumi/pulumi";
3+
import "mocha";
4+
import { projectName, projectLabelKey, projectLabelValue, projectId, projectDatasource, exampleProjectOnlyRequired, projectParentContainerId, projectOwnerEmail } from "./index";
5+
6+
pulumi.runtime.setMocks({
7+
newResource: function(args: pulumi.runtime.MockResourceArgs): {id: string, state: any} {
8+
return {
9+
id: args.inputs.name + "_id",
10+
state: args.inputs,
11+
};
12+
},
13+
call: function(args: pulumi.runtime.MockCallArgs) {
14+
// We check the token to identify which data source is being called.
15+
if (args.token === "stackit:index/getResourcemanagerProject:getResourcemanagerProject") {
16+
// Check if the input parameters were passed correctly
17+
if (args.inputs.projectId !== projectId) {
18+
throw new Error("getResourcemanagerProject call received incorrect input parameters.");
19+
}
20+
// Return the complete object
21+
return { ...exampleProjectOnlyRequired, ...args.inputs };
22+
}
23+
return args.inputs;
24+
},
25+
},
26+
"project",
27+
"stack",
28+
false, // Sets the flag `dryRun`, which indicates if pulumi is running in preview mode.
29+
);
30+
31+
32+
describe("exampleProjectOnlyRequired", () => {
33+
let infra: typeof import("./index");
34+
35+
before(async function() {
36+
// It's important to import the program _after_ the mocks are defined.
37+
infra = await import("./index");
38+
})
39+
40+
it("project must have a parentContainerId", function(done) {
41+
pulumi.all([infra.exampleProjectOnlyRequired.urn, infra.exampleProjectOnlyRequired]).apply(([urn, exampleProjectOnlyRequired]) => {
42+
if (!exampleProjectOnlyRequired?.parentContainerId) {
43+
done(new Error(`Missing parentContainerId tag on exampleProjectOnlyRequired ${urn}`));
44+
} else {
45+
done();
46+
}
47+
});
48+
});
49+
50+
it("project must have a name", function(done) {
51+
pulumi.all([infra.exampleProjectOnlyRequired.urn, infra.exampleProjectOnlyRequired]).apply(([urn, exampleProjectOnlyRequired]) => {
52+
if (!exampleProjectOnlyRequired?.name) {
53+
done(new Error(`Missing a name tag on exampleProjectOnlyRequired ${urn}`));
54+
} else {
55+
done();
56+
}
57+
});
58+
});
59+
60+
it("check if parentContainerId was correctly set", function(done) {
61+
pulumi.all([infra.exampleProjectOnlyRequired.urn, infra.exampleProjectOnlyRequired.parentContainerId]).apply(([urn, parentContainerId]) => {
62+
if (parentContainerId === projectParentContainerId) {
63+
done();
64+
} else {
65+
done(new Error(`Provided parentContainerId ${parentContainerId} was not set correctly on exampleProjectOnlyRequired ${urn}`));
66+
}
67+
});
68+
});
69+
70+
it("check if name was correctly set", function(done) {
71+
pulumi.all([infra.exampleProjectOnlyRequired.urn, infra.exampleProjectOnlyRequired.name]).apply(([urn, name]) => {
72+
if (name === projectName) {
73+
done();
74+
} else {
75+
done(new Error(`Provided name ${name} was not set correctly on exampleProjectOnlyRequired ${urn}`));
76+
}
77+
});
78+
});
79+
80+
it("check if ownerEmail was correctly set", function(done) {
81+
pulumi.all([infra.exampleProjectOnlyRequired.urn, infra.exampleProjectOnlyRequired.ownerEmail]).apply(([urn, ownerEmail]) => {
82+
if (ownerEmail === projectOwnerEmail) {
83+
done();
84+
} else {
85+
done(new Error(`Provided ownerEmail ${ownerEmail} was not set correctly on exampleProjectOnlyRequired ${urn}`));
86+
}
87+
});
88+
});
89+
90+
it("check if the 'labels' map contains the correct key and value", function(done) {
91+
pulumi.all([infra.exampleProjectOnlyRequired.urn, infra.exampleProjectOnlyRequired.labels]).apply(([urn, labels]) => {
92+
const actualValue = labels?.[projectLabelKey];
93+
if (actualValue === projectLabelValue) {
94+
done();
95+
} else {
96+
done(new Error(`Label '${projectLabelKey}' was not set correctly. Actual: ${actualValue}, Expected: ${projectLabelValue} on resource ${urn}`));
97+
}
98+
});
99+
});
100+
101+
});
102+
103+
// datasource
104+
describe("project datasource test", () => {
105+
let infra: typeof import("./index");
106+
107+
// It's important to import the program _after_ the mocks are defined.
108+
before(async function() {
109+
infra = await import("./index");
110+
})
111+
112+
it("check if projectId was correctly set", function(done) {
113+
pulumi.all([infra.projectDatasource, infra.projectDatasource.projectId]).apply(([urn, projectId]) => {
114+
if (projectId === projectId) {
115+
done();
116+
} else {
117+
done(new Error(`Provided projectId ${projectId} was not set correctly on datasource ${urn.name}`));
118+
}
119+
});
120+
});
121+
122+
it("check if name was correctly set", function(done) {
123+
pulumi.all([infra.projectDatasource, infra.projectDatasource.name]).apply(([urn, name]) => {
124+
if (name === projectName) {
125+
done();
126+
} else {
127+
done(new Error(`Provided name ${name} was not set correctly on datasource ${urn.name}`));
128+
}
129+
});
130+
});
131+
132+
it("check if the 'labels' map contains the correct key and value", function(done) {
133+
pulumi.all([infra.projectDatasource, infra.projectDatasource.labels]).apply(([urn, labels]) => {
134+
const actualValue = labels?.[projectLabelKey];
135+
if (actualValue === projectLabelValue) {
136+
done();
137+
} else {
138+
done(new Error(`Label '${projectLabelKey}' was not set correctly. Actual: ${actualValue}, Expected: ${projectLabelValue} on datasource ${urn.name}`));
139+
}
140+
});
141+
});
142+
143+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"outDir": "bin",
5+
"target": "es2020",
6+
"module": "commonjs",
7+
"moduleResolution": "node",
8+
"sourceMap": true,
9+
"experimentalDecorators": true,
10+
"pretty": true,
11+
"noFallthroughCasesInSwitch": true,
12+
"noImplicitReturns": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.ts"
17+
]
18+
}

0 commit comments

Comments
 (0)