Skip to content

Commit

Permalink
Add a bit more context to layer offset failures
Browse files Browse the repository at this point in the history
In #251 we are investigating test flakes due to layer offsets not
matching, this change will give us a bit more context so we can be sure
which image has which number of layers, and it will also include the
digest of the image, since kaniko always pushes images to a remote repo,
so if the test fails we can pull the digest and see what is up.

Also updated reproducible Dockerfile to be built with reproducible flag,
which I think was the original intent (without this change, there is no
difference between how `kaniko-dockerfile_test_copy_reproducible` and
`kaniko-dockerfile_test_copy` are built.
  • Loading branch information
bobcatfish committed Jul 31, 2018
1 parent 0d7eba9 commit 57b1159
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion integration/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var additionalKanikoFlagsMap = map[string][]string{
}

var bucketContextTests = []string{"Dockerfile_test_copy_bucket"}
var reproducibleTests = []string{"Dockerfile_test_env"}
var reproducibleTests = []string{"Dockerfile_test_reproducible"}

// GetDockerImage constructs the name of the docker image that would be built with
// dockerfile if it was tagged with imageRepo.
Expand Down
50 changes: 35 additions & 15 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ type gcpConfig struct {
onbuildBaseImage string
}

type imageDetails struct {
name string
numLayers int
digest string
}

func (i imageDetails) String() string {
return fmt.Sprintf("Image: [%s] Digest: [%s] Number of Layers: [%d]", i.name, i.digest, i.numLayers)
}

func initGCPConfig() *gcpConfig {
var c gcpConfig
flag.StringVar(&c.gcsBucket, "bucket", "", "The gcs bucket argument to uploaded the tar-ed contents of the `integration` dir to.")
Expand Down Expand Up @@ -206,42 +216,52 @@ func TestLayers(t *testing.T) {
kanikoImage := GetKanikoImage(config.imageRepo, dockerfile)
pullCmd := exec.Command("docker", "pull", kanikoImage)
RunCommand(pullCmd, t)
if err := checkLayers(dockerImage, kanikoImage, offset[dockerfile]); err != nil {
if err := checkLayers(t, dockerImage, kanikoImage, offset[dockerfile]); err != nil {
t.Error(err)
t.Fail()
}
})
}
}

func checkLayers(image1, image2 string, offset int) error {
lenImage1, err := numLayers(image1)
func checkLayers(t *testing.T, image1, image2 string, offset int) error {
img1, err := getImageDetails(image1)
if err != nil {
return fmt.Errorf("Couldn't get number of layers for image1 (%s): %s", image1, err)
return fmt.Errorf("Couldn't get details from image reference for (%s): %s", image1, err)
}
lenImage2, err := numLayers(image2)

img2, err := getImageDetails(image2)
if err != nil {
return fmt.Errorf("Couldn't get number of layers for image2 (%s): %s", image2, err)
return fmt.Errorf("Couldn't get details from image reference for (%s): %s", image2, err)
}
actualOffset := int(math.Abs(float64(lenImage1 - lenImage2)))

actualOffset := int(math.Abs(float64(img1.numLayers - img2.numLayers)))
if actualOffset != offset {
return fmt.Errorf("incorrect offset between layers of %s and %s: expected %d but got %d", image1, image2, offset, actualOffset)
return fmt.Errorf("Difference in number of layers in each image is %d but should be %d. Image 1: %s, Image 2: %s", actualOffset, offset, img1, img2)
}
return nil
}

func numLayers(image string) (int, error) {
func getImageDetails(image string) (*imageDetails, error) {
ref, err := name.ParseReference(image, name.WeakValidation)
if err != nil {
return 0, fmt.Errorf("Couldn't parse referance to image %s: %s", image, err)
return nil, fmt.Errorf("Couldn't parse referance to image %s: %s", image, err)
}
imgRef, err := daemon.Image(ref)
if err != nil {
return nil, fmt.Errorf("Couldn't get reference to image %s from daemon: %s", image, err)
}
img, err := daemon.Image(ref)
layers, err := imgRef.Layers()
if err != nil {
return 0, fmt.Errorf("Couldn't get reference to image %s from daemon: %s", image, err)
return nil, fmt.Errorf("Error getting layers for image %s: %s", image, err)
}
layers, err := img.Layers()
digest, err := imgRef.Digest()
if err != nil {
return 0, fmt.Errorf("Error getting layers for image %s: %s", image, err)
return nil, fmt.Errorf("Error getting digest for image %s: %s", image, err)
}
return len(layers), nil
return &imageDetails{
name: image,
numLayers: len(layers),
digest: digest.Hex,
}, nil
}

0 comments on commit 57b1159

Please sign in to comment.