github.com/pix4d/terravalet@v0.8.1-0.20240131132849-abcd6a79eeeb/cmdimport_test.go (about)

     1  package main
     2  
     3  import "testing"
     4  
     5  func TestRunImportSuccess(t *testing.T) {
     6  	testCases := []struct {
     7  		name         string
     8  		resDefs      string
     9  		srcPlanPath  string
    10  		wantUpPath   string
    11  		wantDownPath string
    12  	}{
    13  		{
    14  			name:         "import resources",
    15  			resDefs:      "testdata/import/terravalet_imports_definitions.json",
    16  			srcPlanPath:  "testdata/import/08_import_src-plan.json",
    17  			wantUpPath:   "testdata/import/08_import_up.sh",
    18  			wantDownPath: "testdata/import/08_import_down.sh",
    19  		},
    20  	}
    21  
    22  	for _, tc := range testCases {
    23  		t.Run(tc.name, func(t *testing.T) {
    24  			args := []string{"terravalet", "import",
    25  				"--res-defs", tc.resDefs,
    26  				"--src-plan", tc.srcPlanPath,
    27  			}
    28  
    29  			runSuccess(t, args, tc.wantUpPath, tc.wantDownPath)
    30  		})
    31  	}
    32  }
    33  
    34  func TestRunImportFailure(t *testing.T) {
    35  	testCases := []struct {
    36  		name        string
    37  		resDefs     string
    38  		srcPlanPath string
    39  		wantErr     string
    40  	}{
    41  		{
    42  			name:        "non existing src-plan",
    43  			resDefs:     "testdata/import/terravalet_imports_definitions.json",
    44  			srcPlanPath: "src-plan-path-dummy",
    45  			wantErr:     "opening the terraform plan file: open src-plan-path-dummy: no such file or directory",
    46  		},
    47  		{
    48  			name:        "src-plan is invalid json",
    49  			resDefs:     "testdata/import/terravalet_imports_definitions.json",
    50  			srcPlanPath: "testdata/import/09_import_empty_src-plan.json",
    51  			wantErr:     "parse src-plan: parsing the plan: unexpected end of JSON input",
    52  		},
    53  		{
    54  			name:        "src-plan must create resource",
    55  			resDefs:     "testdata/import/terravalet_imports_definitions.json",
    56  			srcPlanPath: "testdata/import/10_import_no-new-resources.json",
    57  			wantErr:     "parse src-plan: src-plan doesn't contains resources to create",
    58  		},
    59  		{
    60  			name:        "src-plan contains only undefined resources",
    61  			resDefs:     "testdata/import/terravalet_imports_definitions.json",
    62  			srcPlanPath: "testdata/import/11_import_src-plan_undefined_resources.json",
    63  			wantErr:     "parse src-plan: src-plan contains only undefined resources",
    64  		},
    65  		{
    66  			name:        "src-plan contains a not existing resource parameter",
    67  			resDefs:     "testdata/import/terravalet_imports_definitions.json",
    68  			srcPlanPath: "testdata/import/12_import_src-plan_invalid_resource_param.json",
    69  			wantErr:     "parse src-plan: error in resources definition dummy_resource2: field 'long_name' doesn't exist in plan",
    70  		},
    71  		{
    72  			name:        "terravalet missing resources definitions file",
    73  			resDefs:     "testdata/import/missing.file",
    74  			srcPlanPath: "testdata/import/08_import_src-plan.json",
    75  			wantErr:     "opening the definitions file: open testdata/import/missing.file: no such file or directory",
    76  		},
    77  		{
    78  			name:        "terravalet invalid resources definitions file",
    79  			resDefs:     "testdata/import/invalid_imports_definitions.json",
    80  			srcPlanPath: "testdata/import/08_import_src-plan.json",
    81  			wantErr:     "parse src-plan: parsing resources definitions: invalid character '}' after object key",
    82  		},
    83  	}
    84  
    85  	for _, tc := range testCases {
    86  		t.Run(tc.name, func(t *testing.T) {
    87  			args := []string{"terravalet", "import",
    88  				"--res-defs", tc.resDefs,
    89  				"--src-plan", tc.srcPlanPath,
    90  			}
    91  
    92  			runFailure(t, args, tc.wantErr)
    93  		})
    94  	}
    95  }