Repository navigation
Expand file tree
/
Copy pathdebug_test.go
More file actions
170 lines (147 loc) · 3.06 KB
/
Copy pathdebug_test.go
File metadata and controls
170 lines (147 loc) · 3.06 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package terraform
import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"regexp"
"strings"
"testing"
)
// debugInfo should be safe when nil
func TestDebugInfo_nil(t *testing.T) {
var d *debugInfo
d.SetPhase("none")
d.WriteFile("none", nil)
d.Close()
}
func TestDebugInfo_basicFile(t *testing.T) {
var w bytes.Buffer
debug, err := newDebugInfo("test-debug-info", &w)
if err != nil {
t.Fatal(err)
}
debug.SetPhase("test")
fileData := map[string][]byte{
"file1": []byte("file 1 data"),
"file2": []byte("file 2 data"),
"file3": []byte("file 3 data"),
}
for f, d := range fileData {
err = debug.WriteFile(f, d)
if err != nil {
t.Fatal(err)
}
}
err = debug.Close()
if err != nil {
t.Fatal(err)
}
gz, err := gzip.NewReader(&w)
if err != nil {
t.Fatal(err)
}
tr := tar.NewReader(gz)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
// get the filename part of the archived file
name := regexp.MustCompile(`\w+$`).FindString(hdr.Name)
data := fileData[name]
delete(fileData, name)
tarData, err := ioutil.ReadAll(tr)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, tarData) {
t.Fatalf("got '%s' for file '%s'", tarData, name)
}
}
for k := range fileData {
t.Fatalf("didn't find file %s", k)
}
}
// Test that we get logs and graphs from a walk. We're not looking for anything
// specific, since the output is going to change in the near future.
func TestDebug_plan(t *testing.T) {
var out bytes.Buffer
d, err := newDebugInfo("test-debug-info", &out)
if err != nil {
t.Fatal(err)
}
// set the global debug value
dbug = d
// run a basic plan
m := testModule(t, "plan-good")
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
_, err = ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
}
err = CloseDebugInfo()
if err != nil {
t.Fatal(err)
}
gz, err := gzip.NewReader(&out)
if err != nil {
t.Fatal(err)
}
tr := tar.NewReader(gz)
graphLogs := 0
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
// record any file that contains data
if hdr.Size > 0 {
if strings.HasSuffix(hdr.Name, "graph.json") {
graphLogs++
}
}
}
if graphLogs == 0 {
t.Fatal("no json graphs")
}
}
// verify that no hooks panic on nil input
func TestDebugHook_nilArgs(t *testing.T) {
// make sure debug isn't nil, so the hooks try to execute
var w bytes.Buffer
var err error
dbug, err = newDebugInfo("test-debug-info", &w)
if err != nil {
t.Fatal(err)
}
var h DebugHook
h.PostApply(nil, nil, nil)
h.PostDiff(nil, nil)
h.PostImportState(nil, nil)
h.PostProvision(nil, "", nil)
h.PostProvisionResource(nil, nil)
h.PostRefresh(nil, nil)
h.PostStateUpdate(nil)
h.PreApply(nil, nil, nil)
h.PreDiff(nil, nil)
h.PreImportState(nil, "")
h.PreProvision(nil, "")
h.PreProvisionResource(nil, nil)
h.PreRefresh(nil, nil)
h.ProvisionOutput(nil, "", "")
}