github.com/mponton/terratest@v0.44.0/modules/docker/build_test.go (about) 1 package docker 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/mponton/terratest/modules/git" 9 "github.com/mponton/terratest/modules/logger" 10 "github.com/mponton/terratest/modules/random" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestBuild(t *testing.T) { 15 t.Parallel() 16 17 tag := "gruntwork-io/test-image:v1" 18 text := "Hello, World!" 19 20 options := &BuildOptions{ 21 Tags: []string{tag}, 22 BuildArgs: []string{fmt.Sprintf("text=%s", text)}, 23 } 24 25 Build(t, "../../test/fixtures/docker", options) 26 27 out := Run(t, tag, &RunOptions{Remove: true}) 28 require.Contains(t, out, text) 29 } 30 31 func TestBuildWithBuildKit(t *testing.T) { 32 t.Parallel() 33 34 tag := "gruntwork-io/test-image-with-buildkit:v1" 35 testToken := "testToken" 36 options := &BuildOptions{ 37 Tags: []string{tag}, 38 EnableBuildKit: true, 39 OtherOptions: []string{"--secret", fmt.Sprintf("id=github-token,env=%s", "GITHUB_OAUTH_TOKEN")}, 40 Env: map[string]string{"GITHUB_OAUTH_TOKEN": testToken}, 41 } 42 43 Build(t, "../../test/fixtures/docker-with-buildkit", options) 44 out := Run(t, tag, &RunOptions{Remove: false}) 45 require.Contains(t, out, testToken) 46 } 47 48 func TestBuildMultiArch(t *testing.T) { 49 t.Parallel() 50 51 tag := "gruntwork-io/test-image:v1" 52 text := "Hello, World!" 53 54 options := &BuildOptions{ 55 Tags: []string{tag}, 56 BuildArgs: []string{fmt.Sprintf("text=%s", text)}, 57 Architectures: []string{"linux/arm64", "linux/amd64"}, 58 Load: true, 59 } 60 61 Build(t, "../../test/fixtures/docker", options) 62 out := Run(t, tag, &RunOptions{Remove: true}) 63 require.Contains(t, out, text) 64 } 65 66 func TestBuildWithTarget(t *testing.T) { 67 t.Parallel() 68 69 tag := "gruntwork-io/test-image:target1" 70 text := "Hello, World!" 71 text1 := "Hello, World! This is build target 1!" 72 73 options := &BuildOptions{ 74 Tags: []string{tag}, 75 BuildArgs: []string{fmt.Sprintf("text=%s", text), fmt.Sprintf("text1=%s", text1)}, 76 Target: "step1", 77 } 78 79 Build(t, "../../test/fixtures/docker", options) 80 81 out := Run(t, tag, &RunOptions{Remove: true}) 82 require.Contains(t, out, text1) 83 } 84 85 func TestGitCloneAndBuild(t *testing.T) { 86 t.Parallel() 87 88 uniqueID := strings.ToLower(random.UniqueId()) 89 imageTag := "gruntwork-io-foo-test:" + uniqueID 90 text := "Hello, World!" 91 92 buildOpts := &BuildOptions{ 93 Tags: []string{imageTag}, 94 BuildArgs: []string{fmt.Sprintf("text=%s", text)}, 95 } 96 gitBranchName := git.GetCurrentBranchName(t) 97 if gitBranchName == "" { 98 logger.Logf(t, "WARNING: git.GetCurrentBranchName returned an empty string; falling back to master") 99 gitBranchName = "master" 100 } 101 GitCloneAndBuild(t, "git@github.com:gruntwork-io/terratest.git", gitBranchName, "test/fixtures/docker", buildOpts) 102 103 out := Run(t, imageTag, &RunOptions{Remove: true}) 104 require.Contains(t, out, text) 105 }