terratest icon indicating copy to clipboard operation
terratest copied to clipboard

Logging always points to terratest's packages

Open pmalek opened this issue 2 years ago • 0 comments

I'm trying to figure out how to use the logging packages in terratest to avoid referencing files in there (using testing.Helper()).

I tried several things and all of them fail:

  • using default

    package main
    
    import (
        "os"
        "testing"
    
        "github.com/gruntwork-io/terratest/modules/k8s"
    )
    
    const (
        envNameKubeConfig = "KUBECONFIG"
    )
    
    func TestKubeconfig(t *testing.T) {
        kubectlOptions := k8s.NewKubectlOptions("", os.Getenv(envNameKubeConfig), "")
        k8s.RunKubectl(t, kubectlOptions, "get", "node")
    }
    

    prints

    === RUN   TestKubeconfig
    TestKubeconfig 2023-02-04T10:34:33+01:00 logger.go:66: Running command kubectl with args [--kubeconfig /Users/USER/.kube/config get node]
    TestKubeconfig 2023-02-04T10:34:34+01:00 logger.go:66: NAME                   STATUS   ROLES           AGE     VERSION
    TestKubeconfig 2023-02-04T10:34:34+01:00 logger.go:66: colima-control-plane   Ready    control-plane   3d22h   v1.26.0
    --- PASS: TestKubeconfig (0.05s)
    

  • using logger.TestingT

    package main
    
    import (
        "os"
        "testing"
    
        "github.com/gruntwork-io/terratest/modules/k8s"
        "github.com/gruntwork-io/terratest/modules/logger"
    )
    
    const (
        envNameKubeConfig = "KUBECONFIG"
    )
    
    func TestKubeconfig(t *testing.T) {
        logger.Default = logger.TestingT
        kubectlOptions := k8s.NewKubectlOptions("", os.Getenv(envNameKubeConfig), "")
        k8s.RunKubectl(t, kubectlOptions, "get", "node")
    }
    

    prints

    === RUN   TestKubeconfig
      command.go:100: Running command kubectl with args [--kubeconfig /Users/USER/.kube/config get node]
      command.go:189: NAME                   STATUS   ROLES           AGE     VERSION
      command.go:189: colima-control-plane   Ready    control-plane   3d22h   v1.26.0
    
    --- PASS: TestKubeconfig (0.05s)
    

  • using custom logger calling t.Helper()

    package main
    
    import (
        "os"
        "testing"
    
        "github.com/gruntwork-io/terratest/modules/k8s"
        "github.com/gruntwork-io/terratest/modules/logger"
        grunttesting "github.com/gruntwork-io/terratest/modules/testing"
    )
    
    const (
        envNameKubeConfig = "KUBECONFIG"
    )
    
    type Logger struct{}
    
    func (l Logger) Logf(t grunttesting.TestingT, format string, args ...interface{}) {
        tt, ok := t.(*testing.T)
        if ok {
      	  tt.Helper()
      	  tt.Logf(format, args...)
        }
    }
    
    func TestKubeconfig(t *testing.T) {
        logger.Default = logger.New(Logger{})
        kubectlOptions := k8s.NewKubectlOptions("", os.Getenv(envNameKubeConfig), "")
        k8s.RunKubectl(t, kubectlOptions, "get", "node")
    }
    

    prints

    === RUN   TestKubeconfig
      command.go:100: Running command kubectl with args [--kubeconfig /Users/USER/.kube/config get node]
      command.go:189: NAME                   STATUS   ROLES           AGE     VERSION
      command.go:189: colima-control-plane   Ready    control-plane   3d22h   v1.26.0
    
    --- PASS: TestKubeconfig (0.05s)
    

As can be seen, all of them either reference command.go or logger.go.

pmalek avatar Feb 04 '23 09:02 pmalek