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

Cleanups #59

Merged
merged 3 commits into from
Jan 30, 2024
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ For this reason Terravalet operates on local state and leaves to the operator th

Be careful when using Terraform workspaces, since they are invisible and persistent global state :-(. Remember to always explicitly run `terraform workspace select` before anything else.

### Interactions with the "moved" block

After the creation of Terravalet, Terraform introduced the `moved` block, which can be seen as an alternative to certain usages of Terravalet. See [Terraform: refactoring](https://developer.hashicorp.com/terraform/language/modules/develop/refactoring)) for more information.

## Install

### Install from binary package
Expand Down
95 changes: 95 additions & 0 deletions cmdimport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import "testing"

func TestRunImportSuccess(t *testing.T) {
testCases := []struct {
name string
resDefs string
srcPlanPath string
wantUpPath string
wantDownPath string
}{
{
name: "import resources",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/08_import_src-plan.json",
wantUpPath: "testdata/import/08_import_up.sh",
wantDownPath: "testdata/import/08_import_down.sh",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "import",
"--res-defs", tc.resDefs,
"--src-plan", tc.srcPlanPath,
}

runSuccess(t, args, tc.wantUpPath, tc.wantDownPath)
})
}
}

func TestRunImportFailure(t *testing.T) {
testCases := []struct {
name string
resDefs string
srcPlanPath string
wantErr string
}{
{
name: "non existing src-plan",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "src-plan-path-dummy",
wantErr: "opening the terraform plan file: open src-plan-path-dummy: no such file or directory",
},
{
name: "src-plan is invalid json",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/09_import_empty_src-plan.json",
wantErr: "parse src-plan: parsing the plan: unexpected end of JSON input",
},
{
name: "src-plan must create resource",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/10_import_no-new-resources.json",
wantErr: "parse src-plan: src-plan doesn't contains resources to create",
},
{
name: "src-plan contains only undefined resources",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/11_import_src-plan_undefined_resources.json",
wantErr: "parse src-plan: src-plan contains only undefined resources",
},
{
name: "src-plan contains a not existing resource parameter",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/12_import_src-plan_invalid_resource_param.json",
wantErr: "parse src-plan: error in resources definition dummy_resource2: field 'long_name' doesn't exist in plan",
},
{
name: "terravalet missing resources definitions file",
resDefs: "testdata/import/missing.file",
srcPlanPath: "testdata/import/08_import_src-plan.json",
wantErr: "opening the definitions file: open testdata/import/missing.file: no such file or directory",
},
{
name: "terravalet invalid resources definitions file",
resDefs: "testdata/import/invalid_imports_definitions.json",
srcPlanPath: "testdata/import/08_import_src-plan.json",
wantErr: "parse src-plan: parsing resources definitions: invalid character '}' after object key",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "import",
"--res-defs", tc.resDefs,
"--src-plan", tc.srcPlanPath,
}

runFailure(t, args, tc.wantErr)
})
}
}
Loading
Loading