github.com/jpedro/terraform@v0.11.12-beta1/helper/pathorcontents/read_test.go (about) 1 package pathorcontents 2 3 import ( 4 "io" 5 "io/ioutil" 6 "os" 7 "os/user" 8 "strings" 9 "testing" 10 11 "github.com/mitchellh/go-homedir" 12 ) 13 14 func TestRead_Path(t *testing.T) { 15 isPath := true 16 f, cleanup := testTempFile(t) 17 defer cleanup() 18 19 if _, err := io.WriteString(f, "foobar"); err != nil { 20 t.Fatalf("err: %s", err) 21 } 22 f.Close() 23 24 contents, wasPath, err := Read(f.Name()) 25 26 if err != nil { 27 t.Fatalf("err: %s", err) 28 } 29 if wasPath != isPath { 30 t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) 31 } 32 if contents != "foobar" { 33 t.Fatalf("expected contents %s, got %s", "foobar", contents) 34 } 35 } 36 37 func TestRead_TildePath(t *testing.T) { 38 isPath := true 39 home, err := homedir.Dir() 40 if err != nil { 41 t.Fatalf("err: %s", err) 42 } 43 f, cleanup := testTempFile(t, home) 44 defer cleanup() 45 46 if _, err := io.WriteString(f, "foobar"); err != nil { 47 t.Fatalf("err: %s", err) 48 } 49 f.Close() 50 51 r := strings.NewReplacer(home, "~") 52 homePath := r.Replace(f.Name()) 53 contents, wasPath, err := Read(homePath) 54 55 if err != nil { 56 t.Fatalf("err: %s", err) 57 } 58 if wasPath != isPath { 59 t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) 60 } 61 if contents != "foobar" { 62 t.Fatalf("expected contents %s, got %s", "foobar", contents) 63 } 64 } 65 66 func TestRead_PathNoPermission(t *testing.T) { 67 // This skip condition is intended to get this test out of the way of users 68 // who are building and testing Terraform from within a Linux-based Docker 69 // container, where it is common for processes to be running as effectively 70 // root within the container. 71 if u, err := user.Current(); err == nil && u.Uid == "0" { 72 t.Skip("This test is invalid when running as root, since root can read every file") 73 } 74 75 isPath := true 76 f, cleanup := testTempFile(t) 77 defer cleanup() 78 79 if _, err := io.WriteString(f, "foobar"); err != nil { 80 t.Fatalf("err: %s", err) 81 } 82 f.Close() 83 84 if err := os.Chmod(f.Name(), 0); err != nil { 85 t.Fatalf("err: %s", err) 86 } 87 88 contents, wasPath, err := Read(f.Name()) 89 90 if err == nil { 91 t.Fatal("Expected error, got none!") 92 } 93 if wasPath != isPath { 94 t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) 95 } 96 if contents != "" { 97 t.Fatalf("expected contents %s, got %s", "", contents) 98 } 99 } 100 101 func TestRead_Contents(t *testing.T) { 102 isPath := false 103 input := "hello" 104 105 contents, wasPath, err := Read(input) 106 107 if err != nil { 108 t.Fatalf("err: %s", err) 109 } 110 if wasPath != isPath { 111 t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) 112 } 113 if contents != input { 114 t.Fatalf("expected contents %s, got %s", input, contents) 115 } 116 } 117 118 func TestRead_TildeContents(t *testing.T) { 119 isPath := false 120 input := "~/hello/notafile" 121 122 contents, wasPath, err := Read(input) 123 124 if err != nil { 125 t.Fatalf("err: %s", err) 126 } 127 if wasPath != isPath { 128 t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) 129 } 130 if contents != input { 131 t.Fatalf("expected contents %s, got %s", input, contents) 132 } 133 } 134 135 // Returns an open tempfile based at baseDir and a function to clean it up. 136 func testTempFile(t *testing.T, baseDir ...string) (*os.File, func()) { 137 base := "" 138 if len(baseDir) == 1 { 139 base = baseDir[0] 140 } 141 f, err := ioutil.TempFile(base, "tf") 142 if err != nil { 143 t.Fatalf("err: %s", err) 144 } 145 146 return f, func() { 147 os.Remove(f.Name()) 148 } 149 }