github.com/someshkoli/terratest@v0.41.1/modules/docker/build_test.go (about) 1 package docker 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/gruntwork-io/terratest/modules/git" 9 "github.com/gruntwork-io/terratest/modules/logger" 10 "github.com/gruntwork-io/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 TestBuildMultiArch(t *testing.T) { 32 t.Parallel() 33 34 tag := "gruntwork-io/test-image:v1" 35 text := "Hello, World!" 36 37 options := &BuildOptions{ 38 Tags: []string{tag}, 39 BuildArgs: []string{fmt.Sprintf("text=%s", text)}, 40 Architectures: []string{"linux/arm64", "linux/amd64"}, 41 Load: true, 42 } 43 44 Build(t, "../../test/fixtures/docker", options) 45 out := Run(t, tag, &RunOptions{Remove: true}) 46 require.Contains(t, out, text) 47 } 48 49 func TestBuildWithTarget(t *testing.T) { 50 t.Parallel() 51 52 tag := "gruntwork-io/test-image:target1" 53 text := "Hello, World!" 54 text1 := "Hello, World! This is build target 1!" 55 56 options := &BuildOptions{ 57 Tags: []string{tag}, 58 BuildArgs: []string{fmt.Sprintf("text=%s", text), fmt.Sprintf("text1=%s", text1)}, 59 Target: "step1", 60 } 61 62 Build(t, "../../test/fixtures/docker", options) 63 64 out := Run(t, tag, &RunOptions{Remove: true}) 65 require.Contains(t, out, text1) 66 } 67 68 func TestGitCloneAndBuild(t *testing.T) { 69 t.Parallel() 70 71 uniqueID := strings.ToLower(random.UniqueId()) 72 imageTag := "gruntwork-io-foo-test:" + uniqueID 73 text := "Hello, World!" 74 75 buildOpts := &BuildOptions{ 76 Tags: []string{imageTag}, 77 BuildArgs: []string{fmt.Sprintf("text=%s", text)}, 78 } 79 gitBranchName := git.GetCurrentBranchName(t) 80 if gitBranchName == "" { 81 logger.Logf(t, "WARNING: git.GetCurrentBranchName returned an empty string; falling back to master") 82 gitBranchName = "master" 83 } 84 GitCloneAndBuild(t, "git@github.com:gruntwork-io/terratest.git", gitBranchName, "test/fixtures/docker", buildOpts) 85 86 out := Run(t, imageTag, &RunOptions{Remove: true}) 87 require.Contains(t, out, text) 88 }