github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/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 Providers: map[string]ResourceProviderFactory{ 102 "aws": testProviderFuncFixed(p), 103 }, 104 }) 105 106 _, err = ctx.Plan() 107 if err != nil { 108 t.Fatalf("err: %s", err) 109 } 110 111 err = CloseDebugInfo() 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 gz, err := gzip.NewReader(&out) 117 if err != nil { 118 t.Fatal(err) 119 } 120 tr := tar.NewReader(gz) 121 122 graphLogs := 0 123 for { 124 hdr, err := tr.Next() 125 if err == io.EOF { 126 break 127 } 128 if err != nil { 129 t.Fatal(err) 130 } 131 132 // record any file that contains data 133 if hdr.Size > 0 { 134 if strings.HasSuffix(hdr.Name, "graph.json") { 135 graphLogs++ 136 } 137 } 138 } 139 140 if graphLogs == 0 { 141 t.Fatal("no json graphs") 142 } 143 } 144 145 // verify that no hooks panic on nil input 146 func TestDebugHook_nilArgs(t *testing.T) { 147 // make sure debug isn't nil, so the hooks try to execute 148 var w bytes.Buffer 149 var err error 150 dbug, err = newDebugInfo("test-debug-info", &w) 151 if err != nil { 152 t.Fatal(err) 153 } 154 155 var h DebugHook 156 h.PostApply(nil, nil, nil) 157 h.PostDiff(nil, nil) 158 h.PostImportState(nil, nil) 159 h.PostProvision(nil, "", nil) 160 h.PostProvisionResource(nil, nil) 161 h.PostRefresh(nil, nil) 162 h.PostStateUpdate(nil) 163 h.PreApply(nil, nil, nil) 164 h.PreDiff(nil, nil) 165 h.PreImportState(nil, "") 166 h.PreProvision(nil, "") 167 h.PreProvisionResource(nil, nil) 168 h.PreRefresh(nil, nil) 169 h.ProvisionOutput(nil, "", "") 170 }