github.com/arvindram03/terraform@v0.3.7-0.20150212015210-408f838db36d/config/module/get_file_test.go (about)

     1  package module
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  func TestFileGetter_impl(t *testing.T) {
    10  	var _ Getter = new(FileGetter)
    11  }
    12  
    13  func TestFileGetter(t *testing.T) {
    14  	g := new(FileGetter)
    15  	dst := tempDir(t)
    16  
    17  	// With a dir that doesn't exist
    18  	if err := g.Get(dst, testModuleURL("basic")); err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  
    22  	// Verify the destination folder is a symlink
    23  	fi, err := os.Lstat(dst)
    24  	if err != nil {
    25  		t.Fatalf("err: %s", err)
    26  	}
    27  	if fi.Mode()&os.ModeSymlink == 0 {
    28  		t.Fatal("destination is not a symlink")
    29  	}
    30  
    31  	// Verify the main file exists
    32  	mainPath := filepath.Join(dst, "main.tf")
    33  	if _, err := os.Stat(mainPath); err != nil {
    34  		t.Fatalf("err: %s", err)
    35  	}
    36  }
    37  
    38  func TestFileGetter_sourceFile(t *testing.T) {
    39  	g := new(FileGetter)
    40  	dst := tempDir(t)
    41  
    42  	// With a source URL that is a path to a file
    43  	u := testModuleURL("basic")
    44  	u.Path += "/main.tf"
    45  	if err := g.Get(dst, u); err == nil {
    46  		t.Fatal("should error")
    47  	}
    48  }
    49  
    50  func TestFileGetter_sourceNoExist(t *testing.T) {
    51  	g := new(FileGetter)
    52  	dst := tempDir(t)
    53  
    54  	// With a source URL that doesn't exist
    55  	u := testModuleURL("basic")
    56  	u.Path += "/main"
    57  	if err := g.Get(dst, u); err == nil {
    58  		t.Fatal("should error")
    59  	}
    60  }
    61  
    62  func TestFileGetter_dir(t *testing.T) {
    63  	g := new(FileGetter)
    64  	dst := tempDir(t)
    65  
    66  	if err := os.MkdirAll(dst, 0755); err != nil {
    67  		t.Fatalf("err: %s", err)
    68  	}
    69  
    70  	// With a dir that exists that isn't a symlink
    71  	if err := g.Get(dst, testModuleURL("basic")); err == nil {
    72  		t.Fatal("should error")
    73  	}
    74  }
    75  
    76  func TestFileGetter_dirSymlink(t *testing.T) {
    77  	g := new(FileGetter)
    78  	dst := tempDir(t)
    79  	dst2 := tempDir(t)
    80  
    81  	// Make parents
    82  	if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  	if err := os.MkdirAll(dst2, 0755); err != nil {
    86  		t.Fatalf("err: %s", err)
    87  	}
    88  
    89  	// Make a symlink
    90  	if err := os.Symlink(dst2, dst); err != nil {
    91  		t.Fatalf("err: %s", err)
    92  	}
    93  
    94  	// With a dir that exists that isn't a symlink
    95  	if err := g.Get(dst, testModuleURL("basic")); err != nil {
    96  		t.Fatalf("err: %s", err)
    97  	}
    98  
    99  	// Verify the main file exists
   100  	mainPath := filepath.Join(dst, "main.tf")
   101  	if _, err := os.Stat(mainPath); err != nil {
   102  		t.Fatalf("err: %s", err)
   103  	}
   104  }