-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
osbuild,manifest: extract GenContainerStorageStages() helper
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
1 parent
4835faf
commit e259fdb
Showing
3 changed files
with
181 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
]`) | ||
} |