|
| 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 | +}); |
0 commit comments