-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_file_test.go
88 lines (83 loc) · 1.96 KB
/
data_file_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"log"
"os"
"os/user"
"strconv"
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func Test_dataSourceFileRead(t *testing.T) {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
directory := usr.HomeDir
if strings.Contains(directory, `\`) {
directory = strings.Replace(directory, `\`, `\\`, -1)
}
executable := os.Args[0]
if strings.Contains(executable, `\`) {
executable = strings.Replace(executable, `\`, `\\`, -1)
}
var cases = []struct {
path string
config string
key string
wantExists, wantDir bool
}{
{
directory,
fmt.Sprintf(`data "fs_file" "dir1" {
filename = "%s"
}`, directory),
"data.fs_file.dir1",
true, true,
},
{
directory + "2",
fmt.Sprintf(`data "fs_file" "dir2" {
filename = "%s"
}`, directory+"2"),
"data.fs_file.dir2",
false, false,
},
{
executable,
fmt.Sprintf(`data "fs_file" "dir3" {
filename = "%s"
}`, executable),
"data.fs_file.dir3",
true, false,
},
}
for _, test := range cases {
t.Run("", func(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testProviders,
Steps: []resource.TestStep{
{
Config: test.config,
Check: func(s *terraform.State) error {
m := s.RootModule()
i := m.Resources[test.key].Primary
filename := i.Attributes[fsFILENAME]
existsStr := i.Attributes[fsEXISTS]
exists, _ := strconv.ParseBool(existsStr)
isDirStr := i.Attributes[fsISDIR]
isDir, _ := strconv.ParseBool(isDirStr)
fmt.Printf("File: %s, exists: %v, isDir: %v\n", filename, exists, isDir)
if isDir != test.wantDir || exists != test.wantExists {
t.Fatalf("Expected: %v, %v, got: %v, %v", test.wantDir, test.wantExists, isDir, exists)
}
return nil
},
},
},
})
})
}
}