DEV Community

Cover image for Como ejecutar varios tests en Terratest
César M. Cristóbal for CallePuzzle Dev

Posted on • Originally published at dev.callepuzzle.com

2 2

Como ejecutar varios tests en Terratest

Terratest es un framework para ejecutar test en Terraform. En este caso yo tenía un módulo muy sencillo que compone un nombre y quería probar que todas las posibles entradas funcionasen.

A la hora de ejecutar varios tests en Terratest, lo primero que se me vino a la cabeza fue crear varias funciones con cada caso que quería probar:

package test

import (
    "testing"

    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func TestTerraformComplete(t *testing.T) {
    t.Parallel()

    terraformOptions := &terraform.Options{
        TerraformDir: "../",

        Vars: map[string]interface{}{
            "type": "avs",
            "company": "hb01",
            "environment": "hub01",
            "project": "shs01",
            "location": "weu",
            "function": "dns",
            "num": "01",
        },
    }

    terraform.InitAndApply(t, terraformOptions)

    name := terraform.Output(t, terraformOptions, "name")
    assert.Equal(t, name, "avs-hb01-hub01-shs01-weu-dns-01")
}

func TestTerraformNoFunction(t *testing.T) {
    t.Parallel()

    terraformOptions := &terraform.Options{
        TerraformDir: "../",

        Vars: map[string]interface{}{
            "type": "avs",
            "company": "hb01",
            "environment": "hub01",
            "project": "shs01",
            "location": "weu",
            "num": "01",
        },
    }

    terraform.InitAndApply(t, terraformOptions)

    name := terraform.Output(t, terraformOptions, "name")
    assert.Equal(t, name, "avs-hb01-hub01-shs01-weu-01")
}
Enter fullscreen mode Exit fullscreen mode

Con la sorpresa de que apareció un mensaje de error como este:

TestTerraformNoFunction 2020-08-11T22:35:11+02:00 logger.go:66: avs-hb01-hub01-shs01-weu-dns-01
    terraform_azure_test.go:52: 
            Error Trace:    terraform_azure_test.go:52
            Error:          Not equal: 
                            expected: "avs-hb01-hub01-shs01-weu-dns-01"
                            actual  : "avs-hb01-hub01-shs01-weu-01"

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -1 +1 @@
                            -avs-hb01-hub01-shs01-weu-dns-01
                            +avs-hb01-hub01-shs01-weu-01
            Test:           TestTerraformNoFunction
--- FAIL: TestTerraformNoFunction (4.74s)
TestTerraformComplete 2020-08-11T22:35:11+02:00 logger.go:66: avs-hb01-hub01-shs01-weu-dns-01
--- PASS: TestTerraformComplete (4.77s)
FAIL
exit status 1
FAIL    test    4.780s
Enter fullscreen mode Exit fullscreen mode

Después de revisarlo no veía error en el código, pero parecía que estuviese mezclando las variables de las dos funciones.

¿Cuál era el problema? Pues resultó ser el “t.Parallel()”. Yo sé de Golang lo justo para pasar el día pero tiene toda la pinta de que el ejecutar cada paso en paralelo Terraform se vuelve un poco loco.

Solución:

  • Quitar la ejecución en paralelo
package test

import (
    "testing"

    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func TestTerraformComplete(t *testing.T) {

    terraformOptions := &terraform.Options{
        TerraformDir: "../",

        Vars: map[string]interface{}{
            "type": "avs",
            "company": "hb01",
            "environment": "hub01",
            "project": "shs01",
            "location": "weu",
            "function": "dns",
            "num": "01",
        },
    }

    terraform.InitAndApply(t, terraformOptions)

    name := terraform.Output(t, terraformOptions, "name")
    assert.Equal(t, name, "avs-hb01-hub01-shs01-weu-dns-01")
}

func TestTerraformNoFunction(t *testing.T) {

    terraformOptions := &terraform.Options{
        TerraformDir: "../",

        Vars: map[string]interface{}{
            "type": "avs",
            "company": "hb01",
            "environment": "hub01",
            "project": "shs01",
            "location": "weu",
            "num": "01",
        },
    }

    terraform.InitAndApply(t, terraformOptions)

    name := terraform.Output(t, terraformOptions, "name")
    assert.Equal(t, name, "avs-hb01-hub01-shs01-weu-01")
}
Enter fullscreen mode Exit fullscreen mode
  • Ejecutar los tests desde una misma función
package test

import (
    "testing"

    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func TestTerraform(t *testing.T) {
    t.Parallel()

    Complete(t)
    NoFunction(t)
}

func Complete(t *testing.T) {

    terraformOptions := &terraform.Options{
        TerraformDir: "../",

        Vars: map[string]interface{}{
            "type": "avs",
            "company": "hb01",
            "environment": "hub01",
            "project": "shs01",
            "location": "weu",
            "function": "dns",
            "num": "01",
        },
    }

    terraform.InitAndApply(t, terraformOptions)

    name := terraform.Output(t, terraformOptions, "name")
    assert.Equal(t, name, "avs-hb01-hub01-shs01-weu-dns-01")
}

func NoFunction(t *testing.T) {

    terraformOptions := &terraform.Options{
        TerraformDir: "../",

        Vars: map[string]interface{}{
            "type": "avs",
            "company": "hb01",
            "environment": "hub01",
            "project": "shs01",
            "location": "weu",
            "num": "01",
        },
    }

    terraform.InitAndApply(t, terraformOptions)

    name := terraform.Output(t, terraformOptions, "name")
    assert.Equal(t, name, "avs-hb01-hub01-shs01-weu-01")
}
Enter fullscreen mode Exit fullscreen mode

Espero que os sirva de ayuda :)

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay