From 354f04f104f7918a2e66e137ae52217fef3757e7 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 4 Nov 2016 11:30:51 -0400 Subject: [PATCH] Add some basic tests Also make DebugHook noop when there is no debuginfo set --- terraform/debug_test.go | 179 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 terraform/debug_test.go diff --git a/terraform/debug_test.go b/terraform/debug_test.go new file mode 100644 index 000000000..cd105f238 --- /dev/null +++ b/terraform/debug_test.go @@ -0,0 +1,179 @@ +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.WriteGraph(nil) + 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) + + files := 0 + graphs := 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 { + files++ + + // and any dot graph with data + if strings.HasSuffix(hdr.Name, ".dot") { + graphs++ + } + } + } + + if files == 0 { + t.Fatal("no files with data found") + } + + if graphs == 0 { + t.Fatal("no no-empty graphs found") + } +} + +// 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, "") + 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, "", "") +}