From 1aa368d0d8867b1321389ce6d57d3dda5e5ad6db Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 30 Nov 2018 16:51:45 -0800 Subject: [PATCH] configs/configupgrade: Add some logging and enable it for tests --- configs/configupgrade/analysis.go | 7 +++++++ configs/configupgrade/upgrade_native.go | 10 +++++++++- configs/configupgrade/upgrade_test.go | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/configs/configupgrade/analysis.go b/configs/configupgrade/analysis.go index 61e8411580..3abfb80540 100644 --- a/configs/configupgrade/analysis.go +++ b/configs/configupgrade/analysis.go @@ -2,6 +2,7 @@ package configupgrade import ( "fmt" + "log" hcl1 "github.com/hashicorp/hcl" hcl1ast "github.com/hashicorp/hcl/hcl/ast" @@ -48,12 +49,15 @@ func (u *Upgrader) analyze(ms ModuleSources) (*analysis, error) { continue } + log.Printf("[TRACE] configupgrade: Analyzing %q", name) + f, err := hcl1parser.Parse(src) if err != nil { // If we encounter a syntax error then we'll just skip for now // and assume that we'll catch this again when we do the upgrade. // If not, we'll break the upgrade step of renaming .tf files to // .tf.json if they seem to be JSON syntax. + log.Printf("[ERROR] Failed to parse %q: %s", name, err) continue } @@ -104,6 +108,7 @@ func (u *Upgrader) analyze(ms ModuleSources) (*analysis, error) { if alias != "" { inst = moduledeps.ProviderInstance(name + "." + alias) } + log.Printf("[TRACE] Provider block requires provider %q", inst) m.Providers[inst] = moduledeps.ProviderDependency{ Constraints: constraints, Reason: moduledeps.ProviderDependencyExplicit, @@ -157,6 +162,7 @@ func (u *Upgrader) analyze(ms ModuleSources) (*analysis, error) { } inst := moduledeps.ProviderInstance(providerKey) + log.Printf("[TRACE] Resource block for %q %q requires provider %q", typeName, name, inst) if _, exists := m.Providers[inst]; !exists { m.Providers[inst] = moduledeps.ProviderDependency{ Reason: moduledeps.ProviderDependencyImplicit, @@ -173,6 +179,7 @@ func (u *Upgrader) analyze(ms ModuleSources) (*analysis, error) { } for name, fn := range providerFactories { + log.Printf("[TRACE] Fetching schema from provider %q", name) provider, err := fn() if err != nil { return nil, fmt.Errorf("failed to load provider %q: %s", name, err) diff --git a/configs/configupgrade/upgrade_native.go b/configs/configupgrade/upgrade_native.go index 21a0884936..6fb0e717f6 100644 --- a/configs/configupgrade/upgrade_native.go +++ b/configs/configupgrade/upgrade_native.go @@ -1,6 +1,7 @@ package configupgrade import ( + "log" "bytes" "fmt" "io" @@ -31,6 +32,8 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal var result upgradeFileResult var diags tfdiags.Diagnostics + log.Printf("[TRACE] configupgrade: Working on %q", filename) + var buf bytes.Buffer f, err := hcl1parser.Parse(src) @@ -100,6 +103,7 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal rAddr.Mode = addrs.DataResourceMode } + log.Printf("[TRACE] configupgrade: Upgrading %s at %s", rAddr, declRange) moreDiags := u.upgradeNativeSyntaxResource(filename, &buf, rAddr, item, an, adhocComments) diags = diags.Append(moreDiags) @@ -115,6 +119,7 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal } pType := labels[0] + log.Printf("[TRACE] configupgrade: Upgrading provider.%s at %s", pType, declRange) moreDiags := u.upgradeNativeSyntaxProvider(filename, &buf, pType, item, an, adhocComments) diags = diags.Append(moreDiags) @@ -155,6 +160,7 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal "map": `map(string)`, }), } + log.Printf("[TRACE] configupgrade: Upgrading var.%s at %s", labels[0], declRange) bodyDiags := u.upgradeBlockBody(filename, fmt.Sprintf("var.%s", labels[0]), &buf, body.List.Items, rules, adhocComments) diags = diags.Append(bodyDiags) buf.WriteString("}\n\n") @@ -179,11 +185,13 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal "sensitive": noInterpAttributeRule(filename, cty.Bool, an), "depends_on": dependsOnAttributeRule(filename, an), } + log.Printf("[TRACE] configupgrade: Upgrading output.%s at %s", labels[0], declRange) bodyDiags := u.upgradeBlockBody(filename, fmt.Sprintf("output.%s", labels[0]), &buf, body.List.Items, rules, adhocComments) diags = diags.Append(bodyDiags) buf.WriteString("}\n\n") case "locals": + log.Printf("[TRACE] configupgrade: Upgrading locals block at %s", declRange) printComments(&buf, item.LeadComment) printBlockOpen(&buf, blockType, labels, item.LineComment) @@ -280,7 +288,7 @@ func (u *Upgrader) upgradeNativeSyntaxResource(filename string, buf *bytes.Buffe panic(fmt.Sprintf("missing schema for provider type %q", providerType)) } schema, _ := providerSchema.SchemaForResourceAddr(addr) - if !ok { + if schema == nil { diags = diags.Append(&hcl2.Diagnostic{ Severity: hcl2.DiagError, Summary: "Unknown resource type", diff --git a/configs/configupgrade/upgrade_test.go b/configs/configupgrade/upgrade_test.go index b1bfc4760c..57f9b1b4fb 100644 --- a/configs/configupgrade/upgrade_test.go +++ b/configs/configupgrade/upgrade_test.go @@ -4,15 +4,18 @@ import ( "bytes" "io" "io/ioutil" + "log" "os" "os/exec" "path/filepath" "testing" + "github.com/davecgh/go-spew/spew" "github.com/zclconf/go-cty/cty" backendinit "github.com/hashicorp/terraform/backend/init" "github.com/hashicorp/terraform/configs/configschema" + "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/providers" "github.com/hashicorp/terraform/terraform" ) @@ -213,3 +216,22 @@ func init() { // Initialize the backends backendinit.Init(nil) } + +func TestMain(m *testing.M) { + if testing.Verbose() { + // if we're verbose, use the logging requested by TF_LOG + logging.SetOutput() + } else { + // otherwise silence all logs + log.SetOutput(ioutil.Discard) + } + + // We have fmt.Stringer implementations on lots of objects that hide + // details that we very often want to see in tests, so we just disable + // spew's use of String methods globally on the assumption that spew + // usage implies an intent to see the raw values and ignore any + // abstractions. + spew.Config.DisableMethods = true + + os.Exit(m.Run()) +}