configs/configupgrade: Add some logging and enable it for tests

This commit is contained in:
Martin Atkins 2018-11-30 16:51:45 -08:00
parent e8999eefdc
commit 1aa368d0d8
3 changed files with 38 additions and 1 deletions

View File

@ -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)

View File

@ -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",

View File

@ -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())
}