Skip to content

Commit 571cf05

Browse files
committed
feat(build): #232 test terraform
- Add /testTerraform to makes.nix - Add argument, docs, etc
1 parent 72edbf6 commit 571cf05

File tree

9 files changed

+160
-2
lines changed

9 files changed

+160
-2
lines changed

.github/workflows/dev.yml

+8
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ jobs:
139139
name: /lintWithLizard
140140
with:
141141
args: sh -c "nix-env -if . && m . /lintWithLizard"
142+
testTerraform_module:
143+
runs-on: ubuntu-latest
144+
steps:
145+
- uses: actions/checkout@v2
146+
- uses: docker://docker.io/nixos/nix:2.3.12
147+
name: /testTerraform/module
148+
with:
149+
args: sh -c "nix-env -if . && m . /testTerraform/module"
142150
name: dev
143151
on:
144152
pull_request:

README.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ in just a few steps, in any technology.
4747
- [lintPython](#lintpython)
4848
- [lintTerraform](#lintterraform)
4949
- [lintWithLizard](#lintwithlizard)
50+
- [Testing](#testing)
51+
- [testTerraform](#testterraform)
5052
- [Pinning](#pinning)
5153
- [inputs](#inputs)
5254
- [requiredMakesVersion](#requiredmakesversion)
@@ -925,7 +927,7 @@ Custom Types:
925927
Defaults to `[ ]`.
926928
- src (`str`):
927929
Path to the [Terraform][TERRAFORM] module.
928-
- version (`str`):
930+
- version (`enum [ "0.12" "0.13" "0.14" "0.15" "0.16" ]`):
929931
[Terraform][TERRAFORM] version your module is built with.
930932

931933
Example `makes.nix`:
@@ -980,6 +982,52 @@ Example `makes.nix`:
980982

981983
Example invocation: `$ m . /lintWithLizard`
982984

985+
## Testing
986+
987+
### testTerraform
988+
989+
Test [Terraform][TERRAFORM] code
990+
by performing a `terraform plan`
991+
over the specified [Terraform][TERRAFORM] modules.
992+
993+
Attributes:
994+
995+
- modules (`attrsOf moduleType`): Optional.
996+
Path to [Terraform][TERRAFORM] modules to lint.
997+
Defaults to `{ }`.
998+
999+
Custom Types:
1000+
1001+
- moduleType (`submodule`):
1002+
- authentication (`listOf package`): Optional.
1003+
[Makes Secrets][MAKES_SECRETS] to use (if required by your module).
1004+
Defaults to `[ ]`.
1005+
- src (`str`):
1006+
Path to the [Terraform][TERRAFORM] module.
1007+
- version (`enum [ "0.12" "0.13" "0.14" "0.15" "0.16" ]`):
1008+
[Terraform][TERRAFORM] version your module is built with.
1009+
1010+
Example `makes.nix`:
1011+
1012+
```nix
1013+
{
1014+
testTerraform = {
1015+
modules = {
1016+
module1 = {
1017+
src = "/my/module1";
1018+
version = "0.12";
1019+
};
1020+
module2 = {
1021+
src = "/my/module2";
1022+
version = "0.16";
1023+
};
1024+
};
1025+
};
1026+
}
1027+
```
1028+
1029+
Example invocation: `$ m . /testTerraform`
1030+
9831031
## Pinning
9841032

9851033
### inputs
@@ -1709,6 +1757,8 @@ Guidelines:
17091757
- Write an argument: `/src/args`
17101758
- Write a module (if applies): `/src/evaluator/modules`
17111759
- Write docs: `/README.md`
1760+
- Write a test: `/makes.nix` or `/makes/**/main.nix`
1761+
- Write a test [GitHub workflow][GITHUB_WORKFLOWS]: `/.github/workflows/dev.yml`
17121762
17131763
Examples:
17141764

makes.nix

+9-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
lintTerraform = {
9191
modules = {
9292
module = {
93-
src = "/test/lintTerraform/module";
93+
src = "/test/terraform/module";
9494
version = "0.13";
9595
};
9696
};
@@ -100,4 +100,12 @@
100100
targets = [ "/" ];
101101
};
102102
requiredMakesVersion = "21.08";
103+
testTerraform = {
104+
modules = {
105+
module = {
106+
src = "/test/terraform/module";
107+
version = "0.13";
108+
};
109+
};
110+
};
103111
}

src/args/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ let
4242
pathImpure = path: headImpure + path;
4343
sortAscii = builtins.sort (a: b: a < b);
4444
sortAsciiCaseless = builtins.sort (a: b: lib.toLower a < lib.toLower b);
45+
testTerraform = import ./test-terraform/default.nix args;
4546
toDerivationName = lib.strings.sanitizeDerivationName;
4647
toFileJson = import ./to-file-json/default.nix args;
4748
toFileLst = import ./to-file-lst/default.nix;

src/args/test-terraform/default.nix

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{ __nixpkgs__
2+
, makeScript
3+
, makeTerraformEnvironment
4+
, ...
5+
}:
6+
{ authentication
7+
, name
8+
, version
9+
, src
10+
, ...
11+
}:
12+
makeScript {
13+
entrypoint = ./entrypoint.sh;
14+
replace = {
15+
__argSrc__ = src;
16+
};
17+
name = "test-terraform-for-${name}";
18+
searchPaths = {
19+
source = [
20+
(makeTerraformEnvironment {
21+
inherit version;
22+
})
23+
] ++ authentication;
24+
};
25+
}

src/args/test-terraform/entrypoint.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# shellcheck shell=bash
2+
3+
function main {
4+
cd "$(mktemp -d)" \
5+
&& copy '__argSrc__' . \
6+
&& info Initializing '__argSrc__' \
7+
&& terraform init \
8+
&& info Testing '__argSrc__' \
9+
&& terraform plan -lock=false -refresh=true
10+
}
11+
12+
main "${@}"

src/evaluator/modules/outputs/builtins/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ args:
1717
(import ./lint-python/default.nix args)
1818
(import ./lint-terraform/default.nix args)
1919
(import ./lint-with-lizard/default.nix args)
20+
(import ./test-terraform/default.nix args)
2021
];
2122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{ __nixpkgs__
2+
, __toModuleOutputs__
3+
, testTerraform
4+
, path
5+
, ...
6+
}:
7+
{ config
8+
, lib
9+
, ...
10+
}:
11+
let
12+
makeOutput = name: { authentication, src, version }: {
13+
name = "/testTerraform/${name}";
14+
value = testTerraform {
15+
inherit authentication;
16+
inherit name;
17+
src = path src;
18+
inherit version;
19+
};
20+
};
21+
in
22+
{
23+
options = {
24+
testTerraform = {
25+
modules = lib.mkOption {
26+
default = { };
27+
type = lib.types.attrsOf (lib.types.submodule (_: {
28+
options = {
29+
authentication = lib.mkOption {
30+
default = [ ];
31+
type = lib.types.listOf lib.types.package;
32+
};
33+
src = lib.mkOption {
34+
type = lib.types.str;
35+
};
36+
version = lib.mkOption {
37+
type = lib.types.enum [
38+
"0.12"
39+
"0.13"
40+
"0.14"
41+
"0.15"
42+
"0.16"
43+
];
44+
};
45+
};
46+
}));
47+
};
48+
};
49+
};
50+
config = {
51+
outputs = __toModuleOutputs__ makeOutput config.testTerraform.modules;
52+
};
53+
}
File renamed without changes.

0 commit comments

Comments
 (0)