github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/validate_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/copy"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  func setupTest(fixturepath string, args ...string) (*cli.MockUi, int) {
    13  	ui := new(cli.MockUi)
    14  	c := &ValidateCommand{
    15  		Meta: Meta{
    16  			testingOverrides: metaOverridesForProvider(testProvider()),
    17  			Ui:               ui,
    18  		},
    19  	}
    20  
    21  	args = append(args, testFixturePath(fixturepath))
    22  
    23  	code := c.Run(args)
    24  	return ui, code
    25  }
    26  
    27  func TestValidateCommand(t *testing.T) {
    28  	if ui, code := setupTest("validate-valid"); code != 0 {
    29  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    30  	}
    31  }
    32  
    33  func TestValidateCommandWithTfvarsFile(t *testing.T) {
    34  	// Create a temporary working directory that is empty because this test
    35  	// requires scanning the current working directory by validate command.
    36  	td := tempDir(t)
    37  	copy.CopyDir(testFixturePath("validate-valid/with-tfvars-file"), td)
    38  	defer os.RemoveAll(td)
    39  	defer testChdir(t, td)()
    40  
    41  	ui := new(cli.MockUi)
    42  	c := &ValidateCommand{
    43  		Meta: Meta{
    44  			testingOverrides: metaOverridesForProvider(testProvider()),
    45  			Ui:               ui,
    46  		},
    47  	}
    48  
    49  	args := []string{}
    50  	if code := c.Run(args); code != 0 {
    51  		t.Fatalf("bad %d\n\n%s", code, ui.ErrorWriter.String())
    52  	}
    53  }
    54  
    55  func TestValidateFailingCommand(t *testing.T) {
    56  	if ui, code := setupTest("validate-invalid"); code != 1 {
    57  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
    58  	}
    59  }
    60  
    61  func TestValidateFailingCommandMissingQuote(t *testing.T) {
    62  	ui, code := setupTest("validate-invalid/missing_quote")
    63  
    64  	if code != 1 {
    65  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
    66  	}
    67  	if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "IDENT test") {
    68  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
    69  	}
    70  }
    71  
    72  func TestValidateFailingCommandMissingVariable(t *testing.T) {
    73  	ui, code := setupTest("validate-invalid/missing_var")
    74  	if code != 1 {
    75  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
    76  	}
    77  	if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "config: unknown variable referenced: 'description'; define it with a 'variable' block") {
    78  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
    79  	}
    80  }
    81  
    82  func TestSameProviderMutipleTimesShouldFail(t *testing.T) {
    83  	ui, code := setupTest("validate-invalid/multiple_providers")
    84  	if code != 1 {
    85  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
    86  	}
    87  	if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "provider.aws: multiple configurations present; only one configuration is allowed per provider") {
    88  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
    89  	}
    90  }
    91  
    92  func TestSameModuleMultipleTimesShouldFail(t *testing.T) {
    93  	ui, code := setupTest("validate-invalid/multiple_modules")
    94  	if code != 1 {
    95  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
    96  	}
    97  	if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "module \"multi_module\": module repeated multiple times") {
    98  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
    99  	}
   100  }
   101  
   102  func TestSameResourceMultipleTimesShouldFail(t *testing.T) {
   103  	ui, code := setupTest("validate-invalid/multiple_resources")
   104  	if code != 1 {
   105  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
   106  	}
   107  	if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "aws_instance.web: resource repeated multiple times") {
   108  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
   109  	}
   110  }
   111  
   112  func TestOutputWithoutValueShouldFail(t *testing.T) {
   113  	ui, code := setupTest("validate-invalid/outputs")
   114  	if code != 1 {
   115  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
   116  	}
   117  	if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "output \"myvalue\": missing required 'value' argument") {
   118  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
   119  	}
   120  }
   121  
   122  func TestModuleWithIncorrectNameShouldFail(t *testing.T) {
   123  	ui, code := setupTest("validate-invalid/incorrectmodulename")
   124  	if code != 1 {
   125  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
   126  	}
   127  
   128  	if !strings.Contains(ui.ErrorWriter.String(), "module name must be a letter or underscore followed by only letters, numbers, dashes, and underscores") {
   129  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
   130  	}
   131  	if !strings.Contains(ui.ErrorWriter.String(), "module source cannot contain interpolations") {
   132  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
   133  	}
   134  }
   135  
   136  func TestWronglyUsedInterpolationShouldFail(t *testing.T) {
   137  	ui, code := setupTest("validate-invalid/interpolation")
   138  	if code != 1 {
   139  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
   140  	}
   141  
   142  	if !strings.Contains(ui.ErrorWriter.String(), "depends on value cannot contain interpolations") {
   143  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
   144  	}
   145  	if !strings.Contains(ui.ErrorWriter.String(), "variable \"vairable_with_interpolation\": default may not contain interpolations") {
   146  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
   147  	}
   148  }
   149  
   150  func TestMissingDefinedVar(t *testing.T) {
   151  	ui, code := setupTest("validate-invalid/missing_defined_var")
   152  	if code != 1 {
   153  		t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
   154  	}
   155  
   156  	if !strings.Contains(ui.ErrorWriter.String(), "Required variable not set:") {
   157  		t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
   158  	}
   159  }
   160  
   161  func TestMissingDefinedVarConfigOnly(t *testing.T) {
   162  	ui, code := setupTest("validate-invalid/missing_defined_var", "-check-variables=false")
   163  	if code != 0 {
   164  		t.Fatalf("Should have passed: %d\n\n%s", code, ui.ErrorWriter.String())
   165  	}
   166  }