Skip to content

Commit

Permalink
osbuild,manifest: extract GenContainerStorageStages() helper
Browse files Browse the repository at this point in the history
Similar to e.g. the partition stage provide a helper that takes
container specs as input and return the right stages. It also
comes with unit tests.
  • Loading branch information
mvo5 authored and ondrejbudai committed Feb 28, 2024
1 parent 4835faf commit e259fdb
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 17 deletions.
19 changes: 2 additions & 17 deletions pkg/manifest/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,28 +406,13 @@ func (p *OS) serialize() osbuild.Pipeline {
}

if len(p.containerSpecs) > 0 {
images := osbuild.NewContainersInputForSources(p.containerSpecs)
localImages := osbuild.NewLocalContainersInputForSources(p.containerSpecs)

var storagePath string

if containerStore := p.OSCustomizations.ContainersStorage; containerStore != nil {
storagePath = *containerStore
storageConf := "/etc/containers/storage.conf"

containerStoreOpts := osbuild.NewContainerStorageOptions(storageConf, storagePath)
pipeline.AddStage(osbuild.NewContainersStorageConfStage(containerStoreOpts))
}

if len(images.References) > 0 {
manifests := osbuild.NewFilesInputForManifestLists(p.containerSpecs)
skopeo := osbuild.NewSkopeoStageWithContainersStorage(storagePath, images, manifests)
pipeline.AddStage(skopeo)
}

if len(localImages.References) > 0 {
skopeo := osbuild.NewSkopeoStageWithContainersStorage(storagePath, localImages, nil)
pipeline.AddStage(skopeo)
for _, stage := range osbuild.GenContainerStorageStages(storagePath, p.containerSpecs) {
pipeline.AddStage(stage)
}
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/osbuild/containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package osbuild

import (
"github.com/osbuild/images/pkg/container"
)

func GenContainerStorageStages(storagePath string, containerSpecs []container.Spec) (stages []*Stage) {
if storagePath != "" {
storageConf := "/etc/containers/storage.conf"

containerStoreOpts := NewContainerStorageOptions(storageConf, storagePath)
stages = append(stages, NewContainersStorageConfStage(containerStoreOpts))
}

images := NewContainersInputForSources(containerSpecs)
localImages := NewLocalContainersInputForSources(containerSpecs)

if len(images.References) > 0 {
manifests := NewFilesInputForManifestLists(containerSpecs)
stages = append(stages, NewSkopeoStageWithContainersStorage(storagePath, images, manifests))
}

if len(localImages.References) > 0 {
stages = append(stages, NewSkopeoStageWithContainersStorage(storagePath, localImages, nil))

}

return stages
}
150 changes: 150 additions & 0 deletions pkg/osbuild/containers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package osbuild_test

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"

"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/osbuild"
)

func TestGenContainerStorageStagesTrivial(t *testing.T) {
storagePath := ""
var containerSpecs []container.Spec
stages := osbuild.GenContainerStorageStages(storagePath, containerSpecs)
assert.Equal(t, len(stages), 0)
}

func TestGenContainerStorageStagesSkopeoOnly(t *testing.T) {
storagePath := ""
containerSpecs := []container.Spec{
{
LocalName: "some-name",
ImageID: "sha256:1851d5f64ebaeac67c5c2d9e4adc1e73aa6433b44a167268a3510c3d056062db",
},
}
stages := osbuild.GenContainerStorageStages(storagePath, containerSpecs)
assert.Equal(t, len(stages), 1)
assert.Equal(t, stages[0].Type, "org.osbuild.skopeo")
assert.Equal(t, stages[0].Inputs.(osbuild.SkopeoStageInputs).Images.Type, "org.osbuild.containers")
}

func TestGenContainerStorageStagesSkopeoMixed(t *testing.T) {
storagePath := ""
containerSpecs := []container.Spec{
{
LocalName: "some-name",
ImageID: "sha256:1851d5f64ebaeac67c5c2d9e4adc1e73aa6433b44a167268a3510c3d056062db",
},
{
LocalName: "other-name",
ImageID: "sha256:aabbccf64ebaeac67c5c2d9e4adc1e73aa6433b44a167268a3510c3d056062db",
LocalStorage: true,
},
}
stages := osbuild.GenContainerStorageStages(storagePath, containerSpecs)
assert.Equal(t, len(stages), 2)
assert.Equal(t, stages[0].Type, "org.osbuild.skopeo")
assert.Equal(t, stages[0].Inputs.(osbuild.SkopeoStageInputs).Images.Type, "org.osbuild.containers")
assert.Equal(t, stages[1].Type, "org.osbuild.skopeo")
assert.Equal(t, stages[1].Inputs.(osbuild.SkopeoStageInputs).Images.Type, "org.osbuild.containers-storage")
}

func TestGenContainerStorageStagesSkopeoWithStoragePath(t *testing.T) {
storagePath := "/some/storage/path"
containerSpecs := []container.Spec{
{
LocalName: "some-name",
ImageID: "sha256:1851d5f64ebaeac67c5c2d9e4adc1e73aa6433b44a167268a3510c3d056062db",
},
{
LocalName: "other-name",
ImageID: "sha256:aabbccf64ebaeac67c5c2d9e4adc1e73aa6433b44a167268a3510c3d056062db",
LocalStorage: true,
},
}
stages := osbuild.GenContainerStorageStages(storagePath, containerSpecs)
assert.Equal(t, len(stages), 3)
assert.Equal(t, stages[0].Type, "org.osbuild.containers.storage.conf")
assert.Equal(t, stages[1].Type, "org.osbuild.skopeo")
assert.Equal(t, stages[1].Inputs.(osbuild.SkopeoStageInputs).Images.Type, "org.osbuild.containers")
assert.Equal(t, stages[2].Type, "org.osbuild.skopeo")
assert.Equal(t, stages[2].Inputs.(osbuild.SkopeoStageInputs).Images.Type, "org.osbuild.containers-storage")
}

func TestGenContainerStorageStagesIntegration(t *testing.T) {
storagePath := "/some/storage/path.conf"
containerSpecs := []container.Spec{
{
LocalName: "some-name",
ImageID: "sha256:1851d5f64ebaeac67c5c2d9e4adc1e73aa6433b44a167268a3510c3d056062db",
},
{
LocalName: "local-name",
ImageID: "sha256:aabbccf64ebaeac67c5c2d9e4adc1e73aa6433b44a167268a3510c3d056062db",
LocalStorage: true,
},
}
stages := osbuild.GenContainerStorageStages(storagePath, containerSpecs)
jsonOutput, err := json.MarshalIndent(stages, "", " ")
assert.NoError(t, err)
assert.Equal(t, string(jsonOutput), `[
{
"type": "org.osbuild.containers.storage.conf",
"options": {
"filename": "/etc/containers/storage.conf",
"config": {
"storage": {
"options": {
"additionalimagestores": [
"/some/storage/path.conf"
]
}
}
}
}
},
{
"type": "org.osbuild.skopeo",
"inputs": {
"images": {
"type": "org.osbuild.containers",
"origin": "org.osbuild.source",
"references": {
"sha256:1851d5f64ebaeac67c5c2d9e4adc1e73aa6433b44a167268a3510c3d056062db": {
"name": "some-name"
}
}
}
},
"options": {
"destination": {
"type": "containers-storage",
"storage-path": "/some/storage/path.conf"
}
}
},
{
"type": "org.osbuild.skopeo",
"inputs": {
"images": {
"type": "org.osbuild.containers-storage",
"origin": "org.osbuild.source",
"references": {
"sha256:aabbccf64ebaeac67c5c2d9e4adc1e73aa6433b44a167268a3510c3d056062db": {
"name": "local-name"
}
}
}
},
"options": {
"destination": {
"type": "containers-storage",
"storage-path": "/some/storage/path.conf"
}
}
}
]`)
}

0 comments on commit e259fdb

Please sign in to comment.