github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/packer_docker_example_test.go (about)

     1  package test
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"strconv"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/gruntwork-io/terratest/modules/docker"
    11  	http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
    12  	"github.com/gruntwork-io/terratest/modules/packer"
    13  	"github.com/gruntwork-io/terratest/modules/random"
    14  )
    15  
    16  // An example of how to test the Packer template in examples/packer-docker-example completely locally using Terratest
    17  // and Docker.
    18  func TestPackerDockerExampleLocal(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	// website::tag::1::Configure Packer to build Docker image.
    22  	packerOptions := &packer.Options{
    23  		// The path to where the Packer template is located
    24  		Template: "../examples/packer-docker-example/build.json",
    25  
    26  		// Only build the Docker image for local testing
    27  		Only: "ubuntu-docker",
    28  
    29  		// Configure retries for intermittent errors
    30  		RetryableErrors:    DefaultRetryablePackerErrors,
    31  		TimeBetweenRetries: DefaultTimeBetweenPackerRetries,
    32  		MaxRetries:         DefaultMaxPackerRetries,
    33  	}
    34  
    35  	// website::tag::2::Build the Docker image using Packer
    36  	packer.BuildArtifact(t, packerOptions)
    37  
    38  	serverPort := 8080
    39  	expectedServerText := fmt.Sprintf("Hello, %s!", random.UniqueId())
    40  
    41  	dockerOptions := &docker.Options{
    42  		// website::tag::3::Set path to 'docker-compose.yml' and environment variables to run Docker image.
    43  		// Directory where docker-compose.yml lives
    44  		WorkingDir: "../examples/packer-docker-example",
    45  
    46  		// Configure the port the web app will listen on and the text it will return using environment variables
    47  		EnvVars: map[string]string{
    48  			"SERVER_PORT": strconv.Itoa(serverPort),
    49  			"SERVER_TEXT": expectedServerText,
    50  		},
    51  	}
    52  
    53  	// website::tag::6::Make sure to shut down the Docker container at the end of the test.
    54  	defer docker.RunDockerCompose(t, dockerOptions, "down")
    55  
    56  	// website::tag::4::Run Docker Compose to fire up the web app. We run it in the background (-d) so it doesn't block this test.
    57  	docker.RunDockerCompose(t, dockerOptions, "up", "-d")
    58  
    59  	// It can take a few seconds for the Docker container boot up, so retry a few times
    60  	maxRetries := 5
    61  	timeBetweenRetries := 2 * time.Second
    62  	url := fmt.Sprintf("http://localhost:%d", serverPort)
    63  
    64  	// Setup a TLS configuration to submit with the helper, a blank struct is acceptable
    65  	tlsConfig := tls.Config{}
    66  
    67  	// website::tag::5::Verify that we get back a 200 OK with the expected text
    68  	http_helper.HttpGetWithRetry(t, url, &tlsConfig, 200, expectedServerText, maxRetries, timeBetweenRetries)
    69  }