Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test terragrunt local module #2202

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/wc-create-pr-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,21 @@ jobs:
repositories: >-
["${{github.event.repository.name}}"]

- name: Test list-module-callers
uses: ./list-module-callers
id: list-module-callers
with:
module_files: js/test/list-module-callers/terragrunt/module_files.txt
config_files: js/test/list-module-callers/terragrunt/config_files.txt

- name: Test actions' outputs
uses: ./js
with:
action: test-action
env:
TFACTION_TEST_ACTION_TERRAGRUNT: "true"
LIST_MODULE_CALLERS: ${{ toJSON(steps.list-module-callers.outputs) }}

- name: Test setup
uses: ./setup
with:
Expand Down
3 changes: 3 additions & 0 deletions js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as core from "@actions/core";
import { main } from "./run";
import * as testAction from "./test-action";
import * as testActionTerragrunt from "./test-action-terragrunt";

try {
if (process.env.TFACTION_TEST_ACTION) {
testAction.main();
} else if (process.env.TFACTION_TEST_ACTION_TERRAGRUNT) {
testActionTerragrunt.main();
} else {
main({
action: core.getInput("action"),
Expand Down
15 changes: 15 additions & 0 deletions js/src/list-module-callers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ export const main = async () => {
}
},
);

if (fs.existsSync(tfDir + "/terragrunt.hcl")) {
child_process.execSync(
`terragrunt render-json --terragrunt-working-dir ${tfDir}`,
);
const tgInspection = JSON.parse(
fs.readFileSync(tfDir + "/terragrunt_rendered.json", "utf8"),
);
const source = tgInspection.terraform?.source;
if (source.startsWith("./") || source.startsWith("../")) {
rawModuleCalls[tfDir].push(source.replace("//", "/"));
} else {
return;
}
}
});

const moduleCallers = buildModuleToCallers(
Expand Down
38 changes: 38 additions & 0 deletions js/src/test-action-terragrunt/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as lib from "../lib";
import * as core from "@actions/core";
import * as fs from "fs";
import { diffString } from "json-diff";

type TestData = {
name: string;
expected: {
file: string;
};
actual: string;
};

export const main = async () => {
// Compare outputs
const testdata: TestData[] = [
{
name: "list-module-callers",
expected: {
file: '{"setup/test/terragrunt/module":["setup/test/terragrunt/foo"]}',
},
actual: process.env.LIST_MODULE_CALLERS || "",
},
];
let failed = false;
testdata.forEach((data) => {
let a = JSON.parse(data.actual || "{}");
const b = diffString(data.expected, a);
if (b !== "") {
console.log(`Test failed: ${data.name}`);
console.log(b);
failed = true;
}
});
if (failed) {
throw new Error("Test failed");
}
};
1 change: 1 addition & 0 deletions js/test/list-module-callers/terragrunt/config_files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setup/test/terragrunt/foo/tfaction.yaml
1 change: 1 addition & 0 deletions js/test/list-module-callers/terragrunt/module_files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setup/test/terragrunt/module/tfaction_module.yaml
2 changes: 1 addition & 1 deletion setup/test/terragrunt/foo/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
terraform {
source = "module"
source = "../module"
}
1 change: 1 addition & 0 deletions setup/test/terragrunt/module/tfaction_module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading