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