github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/config/module/get_test.go (about)

     1  package module
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestGet_badSchema(t *testing.T) {
    11  	dst := tempDir(t)
    12  	u := testModule("basic")
    13  	u = strings.Replace(u, "file", "nope", -1)
    14  
    15  	if err := Get(dst, u); err == nil {
    16  		t.Fatal("should error")
    17  	}
    18  }
    19  
    20  func TestGet_file(t *testing.T) {
    21  	dst := tempDir(t)
    22  	u := testModule("basic")
    23  
    24  	if err := Get(dst, u); err != nil {
    25  		t.Fatalf("err: %s", err)
    26  	}
    27  
    28  	mainPath := filepath.Join(dst, "main.tf")
    29  	if _, err := os.Stat(mainPath); err != nil {
    30  		t.Fatalf("err: %s", err)
    31  	}
    32  }
    33  
    34  func TestGet_fileForced(t *testing.T) {
    35  	dst := tempDir(t)
    36  	u := testModule("basic")
    37  	u = "file::" + u
    38  
    39  	if err := Get(dst, u); err != nil {
    40  		t.Fatalf("err: %s", err)
    41  	}
    42  
    43  	mainPath := filepath.Join(dst, "main.tf")
    44  	if _, err := os.Stat(mainPath); err != nil {
    45  		t.Fatalf("err: %s", err)
    46  	}
    47  }
    48  
    49  func TestGet_fileSubdir(t *testing.T) {
    50  	dst := tempDir(t)
    51  	u := testModule("basic//subdir")
    52  
    53  	if err := Get(dst, u); err != nil {
    54  		t.Fatalf("err: %s", err)
    55  	}
    56  
    57  	mainPath := filepath.Join(dst, "sub.tf")
    58  	if _, err := os.Stat(mainPath); err != nil {
    59  		t.Fatalf("err: %s", err)
    60  	}
    61  }
    62  
    63  func TestGetCopy_dot(t *testing.T) {
    64  	dst := tempDir(t)
    65  	u := testModule("basic-dot")
    66  
    67  	if err := GetCopy(dst, u); err != nil {
    68  		t.Fatalf("err: %s", err)
    69  	}
    70  
    71  	mainPath := filepath.Join(dst, "main.tf")
    72  	if _, err := os.Stat(mainPath); err != nil {
    73  		t.Fatalf("err: %s", err)
    74  	}
    75  
    76  	mainPath = filepath.Join(dst, "foo.tf")
    77  	if _, err := os.Stat(mainPath); err == nil {
    78  		t.Fatal("should not have foo.tf")
    79  	}
    80  }
    81  
    82  func TestGetCopy_file(t *testing.T) {
    83  	dst := tempDir(t)
    84  	u := testModule("basic")
    85  
    86  	if err := GetCopy(dst, u); err != nil {
    87  		t.Fatalf("err: %s", err)
    88  	}
    89  
    90  	mainPath := filepath.Join(dst, "main.tf")
    91  	if _, err := os.Stat(mainPath); err != nil {
    92  		t.Fatalf("err: %s", err)
    93  	}
    94  }
    95  
    96  func TestGetDirSubdir(t *testing.T) {
    97  	cases := []struct {
    98  		Input    string
    99  		Dir, Sub string
   100  	}{
   101  		{
   102  			"hashicorp.com",
   103  			"hashicorp.com", "",
   104  		},
   105  		{
   106  			"hashicorp.com//foo",
   107  			"hashicorp.com", "foo",
   108  		},
   109  		{
   110  			"hashicorp.com//foo?bar=baz",
   111  			"hashicorp.com?bar=baz", "foo",
   112  		},
   113  		{
   114  			"file://foo//bar",
   115  			"file://foo", "bar",
   116  		},
   117  	}
   118  
   119  	for i, tc := range cases {
   120  		adir, asub := getDirSubdir(tc.Input)
   121  		if adir != tc.Dir {
   122  			t.Fatalf("%d: bad dir: %#v", i, adir)
   123  		}
   124  		if asub != tc.Sub {
   125  			t.Fatalf("%d: bad sub: %#v", i, asub)
   126  		}
   127  	}
   128  }