github.com/hugorut/terraform@v1.1.3/src/backend/backend_test.go (about) 1 package backend 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 TestReadPathOrContents_Path(t *testing.T) { 15 f, cleanup := testTempFile(t) 16 defer cleanup() 17 18 if _, err := io.WriteString(f, "foobar"); err != nil { 19 t.Fatalf("err: %s", err) 20 } 21 f.Close() 22 23 contents, err := ReadPathOrContents(f.Name()) 24 25 if err != nil { 26 t.Fatalf("err: %s", err) 27 } 28 if contents != "foobar" { 29 t.Fatalf("expected contents %s, got %s", "foobar", contents) 30 } 31 } 32 33 func TestReadPathOrContents_TildePath(t *testing.T) { 34 home, err := homedir.Dir() 35 if err != nil { 36 t.Fatalf("err: %s", err) 37 } 38 f, cleanup := testTempFile(t, home) 39 defer cleanup() 40 41 if _, err := io.WriteString(f, "foobar"); err != nil { 42 t.Fatalf("err: %s", err) 43 } 44 f.Close() 45 46 r := strings.NewReplacer(home, "~") 47 homePath := r.Replace(f.Name()) 48 contents, err := ReadPathOrContents(homePath) 49 50 if err != nil { 51 t.Fatalf("err: %s", err) 52 } 53 if contents != "foobar" { 54 t.Fatalf("expected contents %s, got %s", "foobar", contents) 55 } 56 } 57 58 func TestRead_PathNoPermission(t *testing.T) { 59 // This skip condition is intended to get this test out of the way of users 60 // who are building and testing Terraform from within a Linux-based Docker 61 // container, where it is common for processes to be running as effectively 62 // root within the container. 63 if u, err := user.Current(); err == nil && u.Uid == "0" { 64 t.Skip("This test is invalid when running as root, since root can read every file") 65 } 66 67 f, cleanup := testTempFile(t) 68 defer cleanup() 69 70 if _, err := io.WriteString(f, "foobar"); err != nil { 71 t.Fatalf("err: %s", err) 72 } 73 f.Close() 74 75 if err := os.Chmod(f.Name(), 0); err != nil { 76 t.Fatalf("err: %s", err) 77 } 78 79 contents, err := ReadPathOrContents(f.Name()) 80 81 if err == nil { 82 t.Fatal("Expected error, got none!") 83 } 84 if contents != "" { 85 t.Fatalf("expected contents %s, got %s", "", contents) 86 } 87 } 88 89 func TestReadPathOrContents_Contents(t *testing.T) { 90 input := "hello" 91 92 contents, err := ReadPathOrContents(input) 93 94 if err != nil { 95 t.Fatalf("err: %s", err) 96 } 97 if contents != input { 98 t.Fatalf("expected contents %s, got %s", input, contents) 99 } 100 } 101 102 func TestReadPathOrContents_TildeContents(t *testing.T) { 103 input := "~/hello/notafile" 104 105 contents, err := ReadPathOrContents(input) 106 107 if err != nil { 108 t.Fatalf("err: %s", err) 109 } 110 if contents != input { 111 t.Fatalf("expected contents %s, got %s", input, contents) 112 } 113 } 114 115 // Returns an open tempfile based at baseDir and a function to clean it up. 116 func testTempFile(t *testing.T, baseDir ...string) (*os.File, func()) { 117 base := "" 118 if len(baseDir) == 1 { 119 base = baseDir[0] 120 } 121 f, err := ioutil.TempFile(base, "tf") 122 if err != nil { 123 t.Fatalf("err: %s", err) 124 } 125 126 return f, func() { 127 os.Remove(f.Name()) 128 } 129 }