2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-10-07 11:55:43 -05:00
|
|
|
package backend
|
2015-11-12 14:39:41 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
2018-09-19 10:58:05 -05:00
|
|
|
"os/user"
|
2015-11-12 14:39:41 -06:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
)
|
|
|
|
|
2020-10-07 11:55:43 -05:00
|
|
|
func TestReadPathOrContents_Path(t *testing.T) {
|
2015-11-12 14:39:41 -06:00
|
|
|
f, cleanup := testTempFile(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if _, err := io.WriteString(f, "foobar"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
2020-10-07 11:55:43 -05:00
|
|
|
contents, err := ReadPathOrContents(f.Name())
|
2015-11-12 14:39:41 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if contents != "foobar" {
|
|
|
|
t.Fatalf("expected contents %s, got %s", "foobar", contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 11:55:43 -05:00
|
|
|
func TestReadPathOrContents_TildePath(t *testing.T) {
|
2015-11-12 14:39:41 -06:00
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
f, cleanup := testTempFile(t, home)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if _, err := io.WriteString(f, "foobar"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
r := strings.NewReplacer(home, "~")
|
|
|
|
homePath := r.Replace(f.Name())
|
2020-10-07 11:55:43 -05:00
|
|
|
contents, err := ReadPathOrContents(homePath)
|
2015-11-12 14:39:41 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if contents != "foobar" {
|
|
|
|
t.Fatalf("expected contents %s, got %s", "foobar", contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRead_PathNoPermission(t *testing.T) {
|
2018-09-19 10:58:05 -05:00
|
|
|
// This skip condition is intended to get this test out of the way of users
|
2023-09-21 07:38:46 -05:00
|
|
|
// who are building and testing OpenTofu from within a Linux-based Docker
|
2018-09-19 10:58:05 -05:00
|
|
|
// container, where it is common for processes to be running as effectively
|
|
|
|
// root within the container.
|
|
|
|
if u, err := user.Current(); err == nil && u.Uid == "0" {
|
|
|
|
t.Skip("This test is invalid when running as root, since root can read every file")
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:39:41 -06:00
|
|
|
f, cleanup := testTempFile(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if _, err := io.WriteString(f, "foobar"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
if err := os.Chmod(f.Name(), 0); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-10-07 11:55:43 -05:00
|
|
|
contents, err := ReadPathOrContents(f.Name())
|
2015-11-12 14:39:41 -06:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error, got none!")
|
|
|
|
}
|
|
|
|
if contents != "" {
|
|
|
|
t.Fatalf("expected contents %s, got %s", "", contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 11:55:43 -05:00
|
|
|
func TestReadPathOrContents_Contents(t *testing.T) {
|
2015-11-12 14:39:41 -06:00
|
|
|
input := "hello"
|
|
|
|
|
2020-10-07 11:55:43 -05:00
|
|
|
contents, err := ReadPathOrContents(input)
|
2015-11-12 14:39:41 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if contents != input {
|
|
|
|
t.Fatalf("expected contents %s, got %s", input, contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 11:55:43 -05:00
|
|
|
func TestReadPathOrContents_TildeContents(t *testing.T) {
|
2015-11-12 14:39:41 -06:00
|
|
|
input := "~/hello/notafile"
|
|
|
|
|
2020-10-07 11:55:43 -05:00
|
|
|
contents, err := ReadPathOrContents(input)
|
2015-11-12 14:39:41 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if contents != input {
|
|
|
|
t.Fatalf("expected contents %s, got %s", input, contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an open tempfile based at baseDir and a function to clean it up.
|
|
|
|
func testTempFile(t *testing.T, baseDir ...string) (*os.File, func()) {
|
|
|
|
base := ""
|
|
|
|
if len(baseDir) == 1 {
|
|
|
|
base = baseDir[0]
|
|
|
|
}
|
2023-09-06 12:41:23 -05:00
|
|
|
f, err := os.CreateTemp(base, "tf")
|
2015-11-12 14:39:41 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, func() {
|
|
|
|
os.Remove(f.Name())
|
|
|
|
}
|
|
|
|
}
|