image_children_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package containerd
  2. import (
  3. "testing"
  4. "github.com/opencontainers/go-digest"
  5. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  6. "gotest.tools/v3/assert"
  7. is "gotest.tools/v3/assert/cmp"
  8. )
  9. func TestIsRootfsChildOf(t *testing.T) {
  10. // Each unique letter is one distinct DiffID
  11. ab := toRootfs("AB")
  12. abc := toRootfs("ABC")
  13. abd := toRootfs("ABD")
  14. xyz := toRootfs("XYZ")
  15. xyzab := toRootfs("XYZAB")
  16. for _, tc := range []struct {
  17. name string
  18. parent ocispec.RootFS
  19. child ocispec.RootFS
  20. out bool
  21. }{
  22. {parent: ab, child: abc, out: true, name: "one additional layer"},
  23. {parent: xyz, child: xyzab, out: true, name: "two additional layers"},
  24. {parent: xyz, child: xyz, out: false, name: "parent is not a child of itself"},
  25. {parent: abc, child: abd, out: false, name: "sibling"},
  26. {parent: abc, child: xyz, out: false, name: "completely different rootfs, but same length"},
  27. {parent: abc, child: ab, out: false, name: "child can't be shorter than parent"},
  28. {parent: ab, child: xyzab, out: false, name: "parent layers appended"},
  29. } {
  30. tc := tc
  31. t.Run(tc.name, func(t *testing.T) {
  32. out := isRootfsChildOf(tc.child, tc.parent)
  33. assert.Check(t, is.Equal(out, tc.out))
  34. })
  35. }
  36. }
  37. func toRootfs(values string) ocispec.RootFS {
  38. dgsts := []digest.Digest{}
  39. for _, v := range values {
  40. vd := digest.FromString(string(v))
  41. dgsts = append(dgsts, vd)
  42. }
  43. return ocispec.RootFS{
  44. Type: "layers",
  45. DiffIDs: dgsts,
  46. }
  47. }