-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to disable terraform's output when running test #358
Comments
I managed to do it by tweaking the The patch is: --- a/modules/logger/logger.go
+++ b/modules/logger/logger.go
@@ -11,6 +11,13 @@ import (
"time"
)
+var output io.Writer = os.Stdout
+
+// Use a new writer to send the logs to
+func Use(w io.Writer) {
+ output = w
+}
+
// Logf logs the given format and arguments, formatted using fmt.Sprintf, to stdout, along with a timestamp and information
// about what test and file is doing the logging. This is an alternative to t.Logf that logs to stdout immediately,
// rather than buffering all log output and only displaying it at the very end of the test. This is useful because:
@@ -29,14 +36,14 @@ import (
// Note that there is a proposal to improve t.Logf (https://github.com/golang/go/issues/24929), but until that's
// implemented, this method is our best bet.
func Logf(t *testing.T, format string, args ...interface{}) {
- DoLog(t, 2, os.Stdout, fmt.Sprintf(format, args...))
+ DoLog(t, 2, output, fmt.Sprintf(format, args...))
}
// Log logs the given arguments to stdout, along with a timestamp and information about what test and file is doing the
// logging. This is an alternative to t.Logf that logs to stdout immediately, rather than buffering all log output and
// only displaying it at the very end of the test. See the Logf method for more info.
func Log(t *testing.T, args ...interface{}) {
- DoLog(t, 2, os.Stdout, args...)
+ DoLog(t, 2, output, args...)
}
// DoLog logs the given arguments to the given writer, along with a timestamp and information about what test and file is In my tests, I simply do a logger.Use(ioutil.Discard) if I do not want the logs. WDYT? Do you want me to submit a PR? |
Neat workaround, but I think that would suppress logging for everything in Terratest globally, which doesn't offer the granularity we need. Perhaps add some sort of logging level to |
What kind of granularity do you usually need? |
Yes, but tests run in parallel, and this is a global setting, so tests will end up overwriting each other's settings. |
Any update on this? This is causing some pain in our automated test runs. We have tests that run |
That's is what I'm looking for. It's the best solution for me. |
I think the I also think packages like |
Also, Go's That's landed in Go 1.14, can confirm that this works. Would be great to support some kind of I'd propose - and if people agree, would start to implement - it with the following interface: type Logger interface {
Logf(t testing.TestingT, format string, args ...interface{})
} Complete, "minimal" usage example: package logger
import (
"fmt"
"io"
"os"
"testing"
"time"
tftesting "github.com/gruntwork-io/terratest/modules/testing"
)
// bareLogger uses *testing.T to log, by doing a type assertion.
type bareLogger struct{}
func (b *bareLogger) Logf(t tftesting.TestingT, format string, args ...interface{}) {
if tt, ok := t.(*testing.T); ok {
tt.Logf(format, args...)
}
}
func TestLogger(t *testing.T) {
RegisterLogger(&bareLogger{})
RegisterLogger(&customLogger{os.Stdout})
DoLog(t, "%s world", "hello")
time.Sleep(time.Second * 3)
DoLog(t, "%s world", "goodbye")
}
type customLogger struct {
io.Writer
}
func (c *customLogger) Logf(_ tftesting.TestingT, format string, args ...interface{}) {
_, _ = c.Write([]byte(fmt.Sprintf("CustomLogger: "+format+"\n", args...)))
}
type Logger interface {
Logf(t tftesting.TestingT, format string, args ...interface{})
}
func DoLog(t tftesting.TestingT, format string, args ...interface{}) {
for _, l := range loggers {
l.Logf(t, format, args...)
}
}
var loggers []Logger
func RegisterLogger(l Logger) {
loggers = append(loggers, l)
} I'd add a field to |
It would be good to have the same behavior for the k8s module. |
This commit adds the custom logging facility to kubectl options and to the Kubectl Run with output that can be used to suppress stdout from Kubectl run if needed. The logging facility was introduced on gruntwork-io#510 but it missed kubectl options. Fixes gruntwork-io#358 since @blame19 recalled that the behavior was missing on the k8s module. Signed-off-by: Anne Macedo <retpolanne@posteo.net>
@blame19 sent a patch adding this behavior to k8s and kubectl. |
Hello and thank you for you tool.
I am using it to write some tests against my infrastructure.
I would like to inhibit the output of terraform while running
go test
and use theoutput
of the functions to display what happened only if something fails:If the test succeeds, a
go test
will only returnPASS
Is there a way to easily achieve that?
The text was updated successfully, but these errors were encountered: