-
Notifications
You must be signed in to change notification settings - Fork 4
/
image_test.go
94 lines (77 loc) · 3.16 KB
/
image_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package ironcoreimage_test
import (
"context"
. "github.com/ironcore-dev/ironcore-image"
"github.com/ironcore-dev/ironcore-image/oci/image"
"github.com/ironcore-dev/ironcore-image/oci/imageutil"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Image", func() {
var (
ctx context.Context
config Config
kernelData, initramfsData, rootfsData, squashfsData []byte
configLayer, kernelLayer, initramfsLayer, rootfsLayer, squashfsLayer image.Layer
img image.Image
)
BeforeEach(func() {
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(context.Background())
DeferCleanup(cancel)
config = Config{}
kernelData = []byte("kernel")
initramfsData = []byte("initramfs")
rootfsData = []byte("rootfs")
squashfsData = []byte("squashfs")
c, err := imageutil.JSONValueLayer(config, imageutil.WithMediaType(ConfigMediaType))
Expect(err).NotTo(HaveOccurred())
configLayer = c
kernelLayer = imageutil.BytesLayer(kernelData, imageutil.WithMediaType(KernelLayerMediaType))
initramfsLayer = imageutil.BytesLayer(initramfsData, imageutil.WithMediaType(InitRAMFSLayerMediaType))
rootfsLayer = imageutil.BytesLayer(rootfsData, imageutil.WithMediaType(RootFSLayerMediaType))
squashfsLayer = imageutil.BytesLayer(squashfsData, imageutil.WithMediaType(SquashFSLayerMediaType))
i, err := imageutil.NewBuilder(configLayer).
Layers(kernelLayer, initramfsLayer, rootfsLayer, squashfsLayer).
Complete()
Expect(err).NotTo(HaveOccurred())
img = i
})
Describe("ResolveImage", func() {
It("should correctly resolve the image", func() {
By("resolving the image")
res, err := ResolveImage(ctx, img)
Expect(err).NotTo(HaveOccurred())
By("inspecting the config")
Expect(res.Config).To(Equal(config))
By("inspecting the layers")
Expect(imageutil.ReadLayerContent(ctx, res.Kernel)).To(Equal(kernelData))
Expect(imageutil.ReadLayerContent(ctx, res.RootFS)).To(Equal(rootfsData))
Expect(imageutil.ReadLayerContent(ctx, res.InitRAMFs)).To(Equal(initramfsData))
Expect(imageutil.ReadLayerContent(ctx, res.SquashFS)).To(Equal(squashfsData))
})
It("should error if the image contains invalid layers", func() {
By("creating an image with an additional invalid layer")
invalidLayer := imageutil.BytesLayer([]byte("invalid"))
img, err := imageutil.NewBuilder(configLayer).
Layers(kernelLayer, initramfsLayer, rootfsLayer, invalidLayer).
Complete()
Expect(err).NotTo(HaveOccurred())
By("resolving the invalid image")
_, err = ResolveImage(ctx, img)
Expect(err).To(HaveOccurred())
})
It("should error if the image is missing layers", func() {
By("creating an image with the kernel layer missing")
img, err := imageutil.NewBuilder(configLayer).
Layers(initramfsLayer, rootfsLayer).
Complete()
Expect(err).NotTo(HaveOccurred())
By("resolving the invalid image")
_, err = ResolveImage(ctx, img)
Expect(err).To(HaveOccurred())
})
})
})