github.com/rhenning/terraform@v0.8.0-beta2/terraform/debug_test.go (about) 1 package terraform 2 3 import ( 4 "archive/tar" 5 "bytes" 6 "compress/gzip" 7 "io" 8 "io/ioutil" 9 "regexp" 10 "strings" 11 "testing" 12 ) 13 14 // debugInfo should be safe when nil 15 func TestDebugInfo_nil(t *testing.T) { 16 var d *debugInfo 17 18 d.SetPhase("none") 19 d.WriteGraph("", nil) 20 d.WriteFile("none", nil) 21 d.Close() 22 } 23 24 func TestDebugInfo_basicFile(t *testing.T) { 25 var w bytes.Buffer 26 debug, err := newDebugInfo("test-debug-info", &w) 27 if err != nil { 28 t.Fatal(err) 29 } 30 debug.SetPhase("test") 31 32 fileData := map[string][]byte{ 33 "file1": []byte("file 1 data"), 34 "file2": []byte("file 2 data"), 35 "file3": []byte("file 3 data"), 36 } 37 38 for f, d := range fileData { 39 err = debug.WriteFile(f, d) 40 if err != nil { 41 t.Fatal(err) 42 } 43 } 44 45 err = debug.Close() 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 gz, err := gzip.NewReader(&w) 51 if err != nil { 52 t.Fatal(err) 53 } 54 tr := tar.NewReader(gz) 55 56 for { 57 hdr, err := tr.Next() 58 if err == io.EOF { 59 break 60 } 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 // get the filename part of the archived file 66 name := regexp.MustCompile(`\w+$`).FindString(hdr.Name) 67 data := fileData[name] 68 69 delete(fileData, name) 70 71 tarData, err := ioutil.ReadAll(tr) 72 if err != nil { 73 t.Fatal(err) 74 } 75 if !bytes.Equal(data, tarData) { 76 t.Fatalf("got '%s' for file '%s'", tarData, name) 77 } 78 } 79 80 for k := range fileData { 81 t.Fatalf("didn't find file %s", k) 82 } 83 } 84 85 // Test that we get logs and graphs from a walk. We're not looking for anything 86 // specific, since the output is going to change in the near future. 87 func TestDebug_plan(t *testing.T) { 88 var out bytes.Buffer 89 d, err := newDebugInfo("test-debug-info", &out) 90 if err != nil { 91 t.Fatal(err) 92 } 93 // set the global debug value 94 dbug = d 95 96 // run a basic plan 97 m := testModule(t, "plan-good") 98 p := testProvider("aws") 99 p.DiffFn = testDiffFn 100 ctx := testContext2(t, &ContextOpts{ 101 Module: m, 102 Providers: map[string]ResourceProviderFactory{ 103 "aws": testProviderFuncFixed(p), 104 }, 105 }) 106 107 _, err = ctx.Plan() 108 if err != nil { 109 t.Fatalf("err: %s", err) 110 } 111 112 err = CloseDebugInfo() 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 gz, err := gzip.NewReader(&out) 118 if err != nil { 119 t.Fatal(err) 120 } 121 tr := tar.NewReader(gz) 122 123 files := 0 124 graphs := 0 125 json := 0 126 for { 127 hdr, err := tr.Next() 128 if err == io.EOF { 129 break 130 } 131 if err != nil { 132 t.Fatal(err) 133 } 134 135 // record any file that contains data 136 if hdr.Size > 0 { 137 files++ 138 139 // and any dot graph with data 140 if strings.HasSuffix(hdr.Name, ".dot") { 141 graphs++ 142 } 143 144 if strings.HasSuffix(hdr.Name, "graph.json") { 145 json++ 146 } 147 } 148 } 149 150 if files == 0 { 151 t.Fatal("no files with data found") 152 } 153 154 if graphs == 0 { 155 t.Fatal("no no-empty graphs found") 156 } 157 158 if json == 0 { 159 t.Fatal("no json graphs") 160 } 161 } 162 163 // verify that no hooks panic on nil input 164 func TestDebugHook_nilArgs(t *testing.T) { 165 // make sure debug isn't nil, so the hooks try to execute 166 var w bytes.Buffer 167 var err error 168 dbug, err = newDebugInfo("test-debug-info", &w) 169 if err != nil { 170 t.Fatal(err) 171 } 172 173 var h DebugHook 174 h.PostApply(nil, nil, nil) 175 h.PostDiff(nil, nil) 176 h.PostImportState(nil, nil) 177 h.PostProvision(nil, "") 178 h.PostProvisionResource(nil, nil) 179 h.PostRefresh(nil, nil) 180 h.PostStateUpdate(nil) 181 h.PreApply(nil, nil, nil) 182 h.PreDiff(nil, nil) 183 h.PreImportState(nil, "") 184 h.PreProvision(nil, "") 185 h.PreProvisionResource(nil, nil) 186 h.PreRefresh(nil, nil) 187 h.ProvisionOutput(nil, "", "") 188 }