github.com/mponton/terratest@v0.44.0/modules/gcp/cloudbuild_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 gcp
     7  
     8  import (
     9  	"archive/tar"
    10  	"bytes"
    11  	"compress/gzip"
    12  	"fmt"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/mponton/terratest/modules/logger"
    17  	"github.com/mponton/terratest/modules/random"
    18  	"github.com/stretchr/testify/require"
    19  	cloudbuildpb "google.golang.org/genproto/googleapis/devtools/cloudbuild/v1"
    20  )
    21  
    22  func TestCreateBuild(t *testing.T) {
    23  	t.Parallel()
    24  	// This test performs the following steps:
    25  	//
    26  	// 1. Creates a tarball with a single Dockerfile
    27  	// 2. Creates a GCS bucket
    28  	// 3. Uploads the tarball to the GCS Bucket
    29  	// 4. Triggers a build using the Cloud Build API
    30  	// 5. Untags and deletes all pushed Build images
    31  	// 6. Deletes the GCS bucket
    32  
    33  	// Create and add some files to the archive.
    34  	tarball := createSampleAppTarball(t)
    35  
    36  	// Create GCS bucket
    37  	projectID := GetGoogleProjectIDFromEnvVar(t)
    38  	id := random.UniqueId()
    39  	gsBucketName := "cloud-build-terratest-" + strings.ToLower(id)
    40  	sampleAppPath := "docker-example.tar.gz"
    41  	imagePath := fmt.Sprintf("gcr.io/%s/test-image-%s", projectID, strings.ToLower(id))
    42  
    43  	logger.Logf(t, "Random values selected Bucket Name = %s\n", gsBucketName)
    44  
    45  	CreateStorageBucket(t, projectID, gsBucketName, nil)
    46  	defer DeleteStorageBucket(t, gsBucketName)
    47  
    48  	// Write the compressed archive to the storage bucket
    49  	objectURL := WriteBucketObject(t, gsBucketName, sampleAppPath, tarball, "application/gzip")
    50  	logger.Logf(t, "Got URL: %s", objectURL)
    51  
    52  	// Create a new build
    53  	build := &cloudbuildpb.Build{
    54  		Source: &cloudbuildpb.Source{
    55  			Source: &cloudbuildpb.Source_StorageSource{
    56  				StorageSource: &cloudbuildpb.StorageSource{
    57  					Bucket: gsBucketName,
    58  					Object: sampleAppPath,
    59  				},
    60  			},
    61  		},
    62  		Steps: []*cloudbuildpb.BuildStep{{
    63  			Name: "gcr.io/cloud-builders/docker",
    64  			Args: []string{"build", "-t", imagePath, "."},
    65  		}},
    66  		Images: []string{imagePath},
    67  	}
    68  
    69  	// CreateBuild blocks until the build is complete
    70  	b := CreateBuild(t, projectID, build)
    71  
    72  	// Delete the pushed build images
    73  	// We could just use the `b` struct above, but we want to explicitly test
    74  	// the `GetBuild` method.
    75  	b2 := GetBuild(t, projectID, b.GetId())
    76  	for _, image := range b2.GetImages() {
    77  		DeleteGCRRepo(t, image)
    78  	}
    79  
    80  	// Empty the storage bucket so we can delete it
    81  	defer EmptyStorageBucket(t, gsBucketName)
    82  }
    83  
    84  func createSampleAppTarball(t *testing.T) *bytes.Reader {
    85  	var buf bytes.Buffer
    86  	tw := tar.NewWriter(&buf)
    87  
    88  	file := `FROM busybox:latest
    89  MAINTAINER Rob Morgan (rob@gruntwork.io)
    90  	`
    91  
    92  	hdr := &tar.Header{
    93  		Name: "Dockerfile",
    94  		Mode: 0600,
    95  		Size: int64(len(file)),
    96  	}
    97  
    98  	err := tw.WriteHeader(hdr)
    99  	require.NoError(t, err)
   100  
   101  	_, werr := tw.Write([]byte(file))
   102  	require.NoError(t, werr)
   103  
   104  	cerr := tw.Close()
   105  	require.NoError(t, cerr)
   106  
   107  	// gzip the tar archive
   108  	var zbuf bytes.Buffer
   109  	gzw := gzip.NewWriter(&zbuf)
   110  	_, gwerr := gzw.Write(buf.Bytes())
   111  	require.NoError(t, gwerr)
   112  
   113  	gcerr := gzw.Close()
   114  	require.NoError(t, gcerr)
   115  
   116  	// return the compressed buffer
   117  	return bytes.NewReader(zbuf.Bytes())
   118  }