github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/gcp/packer_gcp_basic_example_test.go (about)

     1  //go:build gcp
     2  // +build gcp
     3  
     4  // NOTE: We use build tags to differentiate GCP testing for better isolation and parallelism when executing our tests.
     5  
     6  package test
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/gruntwork-io/terratest/modules/gcp"
    13  	"github.com/gruntwork-io/terratest/modules/packer"
    14  )
    15  
    16  // Occasionally, a Packer build may fail due to intermittent issues (e.g., brief network outage or EC2 issue). We try
    17  // to make our tests resilient to that by specifying those known common errors here and telling our builds to retry if
    18  // they hit those errors.
    19  var DefaultRetryablePackerErrors = map[string]string{
    20  	"Script disconnected unexpectedly":                                                 "Occasionally, Packer seems to lose connectivity to AWS, perhaps due to a brief network outage",
    21  	"can not open /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_xenial_InRelease": "Occasionally, apt-get fails on ubuntu to update the cache",
    22  }
    23  var DefaultTimeBetweenPackerRetries = 15 * time.Second
    24  
    25  // Regions that support running f1-micro instances
    26  var RegionsThatSupportF1Micro = []string{"us-central1", "us-east1", "us-west1", "europe-west1"}
    27  
    28  // Zones that support running f1-micro instances
    29  var ZonesThatSupportF1Micro = []string{"us-central1-a", "us-east1-b", "us-west1-a", "europe-north1-a", "europe-west1-b", "europe-central2-a"}
    30  
    31  const DefaultMaxPackerRetries = 3
    32  
    33  // An example of how to test the Packer template in examples/packer-basic-example using Terratest.
    34  func TestPackerGCPBasicExample(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	// Get the Project Id to use
    38  	projectID := gcp.GetGoogleProjectIDFromEnvVar(t)
    39  
    40  	// Pick a random GCP zone to test in. This helps ensure your code works in all regions.
    41  	zone := gcp.GetRandomZone(t, projectID, ZonesThatSupportF1Micro, nil, nil)
    42  
    43  	packerOptions := &packer.Options{
    44  		// The path to where the Packer template is located
    45  		Template: "../../examples/packer-basic-example/build-gcp.pkr.hcl",
    46  
    47  		// Variables to pass to our Packer build using -var options
    48  		Vars: map[string]string{
    49  			"gcp_project_id": projectID,
    50  			"gcp_zone":       zone,
    51  		},
    52  
    53  		// Only build the Google Compute Image
    54  		Only: "googlecompute.ubuntu-bionic",
    55  
    56  		// Configure retries for intermittent errors
    57  		RetryableErrors:    DefaultRetryablePackerErrors,
    58  		TimeBetweenRetries: DefaultTimeBetweenPackerRetries,
    59  		MaxRetries:         DefaultMaxPackerRetries,
    60  	}
    61  
    62  	// Make sure the Packer build completes successfully
    63  	imageName := packer.BuildArtifact(t, packerOptions)
    64  
    65  	// Delete the Image after we're done
    66  	image := gcp.FetchImage(t, projectID, imageName)
    67  	defer image.DeleteImage(t)
    68  }