Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 4bd4e18

Browse files
committed
core: use same logging setup for acctests
We weren't doing any log setup for acceptance tests, which made it difficult to wrangle log output in CI. This moves the log setup functions we use in `main` over into a helper package so we can use them for acceptance tests as well. This means that acceptance tests will by default be a _lot_ quieter, only printing out actual test output. Setting `TF_LOG=trace` will restore the full prior noise level. Only minor behavior change is to make `ioutil.Discard` the default return value rather than a `nil` that needs to be checked for.
1 parent baeb790 commit 4bd4e18

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

log.go renamed to helper/logging/logging.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package main
1+
package logging
22

33
import (
44
"io"
5+
"io/ioutil"
56
"log"
67
"os"
78
"strings"
@@ -18,9 +19,9 @@ const (
1819

1920
var validLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
2021

21-
// logOutput determines where we should send logs (if anywhere) and the log level.
22-
func logOutput() (logOutput io.Writer, err error) {
23-
logOutput = nil
22+
// LogOutput determines where we should send logs (if anywhere) and the log level.
23+
func LogOutput() (logOutput io.Writer, err error) {
24+
logOutput = ioutil.Discard
2425
envLevel := os.Getenv(EnvLog)
2526
if envLevel == "" {
2627
return

helper/resource/testing.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/hashicorp/go-getter"
1515
"github.com/hashicorp/terraform/config/module"
16+
"github.com/hashicorp/terraform/helper/logging"
1617
"github.com/hashicorp/terraform/terraform"
1718
)
1819

@@ -103,6 +104,12 @@ func Test(t TestT, c TestCase) {
103104
return
104105
}
105106

107+
logWriter, err := logging.LogOutput()
108+
if err != nil {
109+
t.Error(fmt.Errorf("error setting up logging: %s", err))
110+
}
111+
log.SetOutput(logWriter)
112+
106113
// We require verbose mode so that the user knows what is going on.
107114
if !testTesting && !testing.Verbose() {
108115
t.Fatal("Acceptance tests must be run with the -v flag on tests")

main.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"sync"
1010

11+
"github.com/hashicorp/terraform/helper/logging"
1112
"github.com/hashicorp/terraform/plugin"
1213
"github.com/mitchellh/cli"
1314
"github.com/mitchellh/panicwrap"
@@ -23,14 +24,11 @@ func realMain() int {
2324

2425
if !panicwrap.Wrapped(&wrapConfig) {
2526
// Determine where logs should go in general (requested by the user)
26-
logWriter, err := logOutput()
27+
logWriter, err := logging.LogOutput()
2728
if err != nil {
2829
fmt.Fprintf(os.Stderr, "Couldn't setup log output: %s", err)
2930
return 1
3031
}
31-
if logWriter == nil {
32-
logWriter = ioutil.Discard
33-
}
3432

3533
// We always send logs to a temporary file that we use in case
3634
// there is a panic. Otherwise, we delete it.
@@ -42,10 +40,6 @@ func realMain() int {
4240
defer os.Remove(logTempFile.Name())
4341
defer logTempFile.Close()
4442

45-
// Tell the logger to log to this file
46-
os.Setenv(EnvLog, "")
47-
os.Setenv(EnvLogFile, "")
48-
4943
// Setup the prefixed readers that send data properly to
5044
// stdout/stderr.
5145
doneCh := make(chan struct{})

0 commit comments

Comments
 (0)