Skip to content

Commit cb0dc0d

Browse files
committed
Add more tests to for builds without kodata
Signed-off-by: Ciprian Hacman <[email protected]>
1 parent 7c6b386 commit cb0dc0d

File tree

1 file changed

+24
-48
lines changed

1 file changed

+24
-48
lines changed

pkg/build/gobuild_test.go

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -577,18 +577,7 @@ func TestGoBuildNoKoData(t *testing.T) {
577577
t.Fatalf("Build() not an Image: %T", result)
578578
}
579579

580-
ls, err := img.Layers()
581-
if err != nil {
582-
t.Fatalf("Layers() = %v", err)
583-
}
584-
585-
// Check that we have the expected number of layers.
586-
t.Run("check layer count", func(t *testing.T) {
587-
// We get a layer for the go binary
588-
if got, want := int64(len(ls)), baseLayers+1; got != want {
589-
t.Fatalf("len(Layers()) = %v, want %v", got, want)
590-
}
591-
})
580+
validateImage(t, img.(oci.SignedImage), baseLayers, creationTime, true, true, false)
592581

593582
// Check that rebuilding the image again results in the same image digest.
594583
t.Run("check determinism", func(t *testing.T) {
@@ -610,54 +599,31 @@ func TestGoBuildNoKoData(t *testing.T) {
610599
t.Errorf("Digest mismatch: %s != %s", d1, d2)
611600
}
612601
})
613-
614-
// Check that the entrypoint of the image is configured to invoke our Go application
615-
t.Run("check entrypoint", func(t *testing.T) {
616-
cfg, err := img.ConfigFile()
617-
if err != nil {
618-
t.Errorf("ConfigFile() = %v", err)
619-
}
620-
entrypoint := cfg.Config.Entrypoint
621-
if got, want := len(entrypoint), 1; got != want {
622-
t.Errorf("len(entrypoint) = %v, want %v", got, want)
623-
}
624-
625-
if got, want := entrypoint[0], "/ko-app/ko"; got != want {
626-
t.Errorf("entrypoint = %v, want %v", got, want)
627-
}
628-
})
629-
630-
t.Run("check creation time", func(t *testing.T) {
631-
cfg, err := img.ConfigFile()
632-
if err != nil {
633-
t.Errorf("ConfigFile() = %v", err)
634-
}
635-
636-
actual := cfg.Created
637-
if actual.Time != creationTime.Time {
638-
t.Errorf("created = %v, want %v", actual, creationTime)
639-
}
640-
})
641602
}
642603

643-
func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creationTime v1.Time, checkAnnotations bool, expectSBOM bool) {
604+
func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creationTime v1.Time, checkAnnotations, expectSBOM, expectData bool) {
644605
t.Helper()
645606

646607
ls, err := img.Layers()
647608
if err != nil {
648609
t.Fatalf("Layers() = %v", err)
649610
}
650611

612+
additionalLayers := int64(1)
613+
if expectData {
614+
additionalLayers += 1
615+
}
616+
651617
// Check that we have the expected number of layers.
652618
t.Run("check layer count", func(t *testing.T) {
653619
// We get a layer for the go binary and a layer for the kodata/
654-
if got, want := int64(len(ls)), baseLayers+2; got != want {
620+
if got, want := int64(len(ls)), baseLayers+additionalLayers; got != want {
655621
t.Fatalf("len(Layers()) = %v, want %v", got, want)
656622
}
657623
})
658624

659625
t.Run("check app layer contents", func(t *testing.T) {
660-
dataLayer := ls[baseLayers]
626+
dataLayer := ls[baseLayers+additionalLayers-1]
661627

662628
if _, err := dataLayer.Digest(); err != nil {
663629
t.Errorf("Digest() = %v", err)
@@ -679,6 +645,9 @@ func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creation
679645
// Check that the kodata layer contains the expected data (even though it was a symlink
680646
// outside kodata).
681647
t.Run("check kodata", func(t *testing.T) {
648+
if !expectData {
649+
t.Skip("skipping kodata check")
650+
}
682651
dataLayer := ls[baseLayers]
683652
r, err := dataLayer.Uncompressed()
684653
if err != nil {
@@ -722,7 +691,11 @@ func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creation
722691
t.Errorf("len(entrypoint) = %v, want %v", got, want)
723692
}
724693

725-
if got, want := entrypoint[0], "/ko-app/test"; got != want {
694+
want := "/ko-app/test"
695+
if !expectData {
696+
want = "/ko-app/ko"
697+
}
698+
if got := entrypoint[0]; got != want {
726699
t.Errorf("entrypoint = %v, want %v", got, want)
727700
}
728701
})
@@ -739,9 +712,12 @@ func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creation
739712
found = true
740713
}
741714
}
742-
if !found {
715+
if expectData && !found {
743716
t.Error("Didn't find KO_DATA_PATH.")
744717
}
718+
if !expectData && found {
719+
t.Error("Found unexpected KO_DATA_PATH.")
720+
}
745721
})
746722

747723
// Check that PATH contains the directory of the produced binary.
@@ -863,7 +839,7 @@ func TestGoBuild(t *testing.T) {
863839
t.Fatalf("Build() not a SignedImage: %T", result)
864840
}
865841

866-
validateImage(t, img, baseLayers, creationTime, true, true)
842+
validateImage(t, img, baseLayers, creationTime, true, true, true)
867843

868844
// Check that rebuilding the image again results in the same image digest.
869845
t.Run("check determinism", func(t *testing.T) {
@@ -1105,7 +1081,7 @@ func TestGoBuildWithoutSBOM(t *testing.T) {
11051081
t.Fatalf("Build() not a SignedImage: %T", result)
11061082
}
11071083

1108-
validateImage(t, img, baseLayers, creationTime, true, false)
1084+
validateImage(t, img, baseLayers, creationTime, true, false, true)
11091085
}
11101086

11111087
func TestGoBuildIndex(t *testing.T) {
@@ -1151,7 +1127,7 @@ func TestGoBuildIndex(t *testing.T) {
11511127
if err != nil {
11521128
t.Fatalf("idx.Image(%s) = %v", desc.Digest, err)
11531129
}
1154-
validateImage(t, img, baseLayers, creationTime, false, true)
1130+
validateImage(t, img, baseLayers, creationTime, false, true, true)
11551131
}
11561132

11571133
if want, got := images, int64(len(im.Manifests)); want != got {

0 commit comments

Comments
 (0)