Skip to content

Commit

Permalink
added integration test to check layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya Wadhwa committed Jun 14, 2018
1 parent 6c1154a commit 6a2e047
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package integration
import (
"encoding/json"
"fmt"
"math"
"os"
"os/exec"
"path"
Expand All @@ -27,6 +28,9 @@ import (
"strings"
"testing"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/daemon"

"github.com/GoogleContainerTools/kaniko/testutil"
)

Expand Down Expand Up @@ -206,6 +210,60 @@ func TestRun(t *testing.T) {
}
}

func TestLayers(t *testing.T) {
dockerfiles, err := filepath.Glob(path.Join(dockerfilesPath, "Dockerfile_test*"))
if err != nil {
t.Error(err)
t.FailNow()
}
offset := map[string]int{
"Dockerfile_test_add": 9,
"Dockerfile_test_scratch": 3,
}
for _, dockerfile := range dockerfiles {
t.Run("test_layer_"+dockerfile, func(t *testing.T) {
// Pull the kaniko image
dockerImage := strings.ToLower(testRepo + dockerPrefix + dockerfile)
kanikoImage := strings.ToLower(testRepo + kanikoPrefix + dockerfile)
pullCmd := exec.Command("docker", "pull", kanikoImage)
RunCommand(pullCmd, t)
if err := checkLayers(dockerImage, kanikoImage, offset[dockerfile]); err != nil {
t.Error(err)
t.Fail()
}
})
}
}

func checkLayers(image1, image2 string, offset int) error {
lenImage1, err := numLayers(image1)
if err != nil {
return err
}
lenImage2, err := numLayers(image2)
if err != nil {
return err
}
actualOffset := int(math.Abs(float64(lenImage1 - lenImage2)))
if actualOffset != offset {
return fmt.Errorf("incorrect offset between layers of %s and %s: expected %d but got %d", image1, image2, offset, actualOffset)
}
return nil
}

func numLayers(image string) (int, error) {
ref, err := name.ParseReference(image, name.WeakValidation)
if err != nil {
return 0, err
}
img, err := daemon.Image(ref, nil)
if err != nil {
return 0, err
}
layers, err := img.Layers()
return len(layers), err
}

func RunCommand(cmd *exec.Cmd, t *testing.T) []byte {
output, err := cmd.CombinedOutput()
if err != nil {
Expand Down

0 comments on commit 6a2e047

Please sign in to comment.