Skip to content

Commit

Permalink
unit tests: use test-specific policy.json and registries.conf
Browse files Browse the repository at this point in the history
In unit tests that create a SystemContext, point that SystemContext at
the testing signature policy and registries configuration.  This should
cause us to pay attention to mirroring settings for images used by this
set of tests.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
  • Loading branch information
nalind committed Aug 8, 2024
1 parent d739057 commit c39f622
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
11 changes: 10 additions & 1 deletion buildah_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import (
"os"
"testing"

imagetypes "github.com/containers/image/v5/types"
"github.com/containers/storage"
"github.com/containers/storage/types"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
testSystemContext = imagetypes.SystemContext{
SignaturePolicyPath: "tests/policy.json",
SystemRegistriesConfPath: "tests/registries.conf",
}
)

func TestMain(m *testing.M) {
var logLevel string
debug := false
Expand Down Expand Up @@ -68,7 +76,8 @@ func TestOpenBuilderCommonBuildOpts(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, container)
b, err = ImportBuilder(ctx, store, ImportOptions{
Container: container.ID,
Container: container.ID,
SignaturePolicyPath: testSystemContext.SignaturePolicyPath,
})
require.NoError(t, err)
require.NotNil(t, b.CommonBuildOpts)
Expand Down
5 changes: 4 additions & 1 deletion cmd/buildah/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import (
var (
signaturePolicyPath = ""
storeOptions, _ = storage.DefaultStoreOptions()
testSystemContext = types.SystemContext{}
testSystemContext = types.SystemContext{
SignaturePolicyPath: "../../tests/policy.json",
SystemRegistriesConfPath: "../../tests/registries.conf",
}
)

func TestMain(m *testing.M) {
Expand Down
20 changes: 13 additions & 7 deletions commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

imageStorage "github.com/containers/image/v5/storage"
"github.com/containers/image/v5/types"
"github.com/containers/storage"
storageTypes "github.com/containers/storage/types"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -23,7 +22,6 @@ import (

func TestCommitLinkedLayers(t *testing.T) {
ctx := context.TODO()
systemContext := types.SystemContext{}
now := time.Now()

graphDriverName := os.Getenv("STORAGE_DRIVER")
Expand Down Expand Up @@ -84,14 +82,17 @@ func TestCommitLinkedLayers(t *testing.T) {
Name: string(rspec.NetworkNamespace),
Host: true,
}},
SystemContext: &testSystemContext,
}
b, err := NewBuilder(ctx, store, builderOptions)
require.NoError(t, err, "creating builder")
b.SetCreatedBy(imageName(layerNumber))
firstFile := makeFile("file0", 0)
err = b.Add("/", false, AddAndCopyOptions{}, firstFile)
require.NoError(t, err, "adding", firstFile)
commitOptions := CommitOptions{}
commitOptions := CommitOptions{
SystemContext: &testSystemContext,
}
ref, err := imageStorage.Transport.ParseStoreReference(store, imageName(layerNumber))
require.NoError(t, err, "parsing reference for to-be-committed image", imageName(layerNumber))
_, _, _, err = b.Commit(ctx, ref, commitOptions)
Expand All @@ -106,7 +107,9 @@ func TestCommitLinkedLayers(t *testing.T) {
secondFile := makeFile("file1", 0)
err = b.Add("/", false, AddAndCopyOptions{}, secondFile)
require.NoError(t, err, "adding", secondFile)
commitOptions = CommitOptions{}
commitOptions = CommitOptions{
SystemContext: &testSystemContext,
}
ref, err = imageStorage.Transport.ParseStoreReference(store, imageName(layerNumber))
require.NoError(t, err, "parsing reference for to-be-committed image", imageName(layerNumber))
_, _, _, err = b.Commit(ctx, ref, commitOptions)
Expand Down Expand Up @@ -161,6 +164,7 @@ func TestCommitLinkedLayers(t *testing.T) {
},
},
},
SystemContext: &testSystemContext,
}
b.AddAppendedLinkedLayer(nil, imageName(layerNumber+6), "", "", ninthArchiveFile)
ref, err = imageStorage.Transport.ParseStoreReference(store, imageName(layerNumber))
Expand All @@ -177,7 +181,9 @@ func TestCommitLinkedLayers(t *testing.T) {
tenthFile := makeFile("file9", 0)
err = b.Add("/", false, AddAndCopyOptions{}, tenthFile)
require.NoError(t, err, "adding", tenthFile)
commitOptions = CommitOptions{}
commitOptions = CommitOptions{
SystemContext: &testSystemContext,
}
ref, err = imageStorage.Transport.ParseStoreReference(store, imageName(layerNumber))
require.NoError(t, err, "parsing reference for to-be-committed image", imageName(layerNumber))
_, _, _, err = b.Commit(ctx, ref, commitOptions)
Expand All @@ -186,10 +192,10 @@ func TestCommitLinkedLayers(t *testing.T) {
// Get set to examine this image. At this point, each history entry
// should just have "image%d" as its CreatedBy field, and each layer
// should have the corresponding file (and nothing else) in it.
src, err := ref.NewImageSource(ctx, &systemContext)
src, err := ref.NewImageSource(ctx, &testSystemContext)
require.NoError(t, err, "opening image source")
defer src.Close()
img, err := ref.NewImage(ctx, &systemContext)
img, err := ref.NewImage(ctx, &testSystemContext)
require.NoError(t, err, "opening image")
defer img.Close()
config, err := img.OCIConfig(ctx)
Expand Down
5 changes: 2 additions & 3 deletions convertcw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/containers/buildah/internal/mkcw"
mkcwtypes "github.com/containers/buildah/internal/mkcw/types"
"github.com/containers/image/v5/types"
"github.com/containers/storage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -68,7 +67,6 @@ func (d *dummyAttestationHandler) ServeHTTP(rw http.ResponseWriter, req *http.Re

func TestCWConvertImage(t *testing.T) {
ctx := context.TODO()
systemContext := &types.SystemContext{}
for _, status := range []int{http.StatusOK, http.StatusInternalServerError} {
for _, ignoreChainRetrievalErrors := range []bool{false, true} {
for _, ignoreAttestationErrors := range []bool{false, true} {
Expand Down Expand Up @@ -110,8 +108,9 @@ func TestCWConvertImage(t *testing.T) {
AttestationURL: "http://" + addr.String(),
IgnoreAttestationErrors: ignoreAttestationErrors,
Slop: "16MB",
SignaturePolicyPath: testSystemContext.SignaturePolicyPath,
}
id, _, _, err := CWConvertImage(ctx, systemContext, store, options)
id, _, _, err := CWConvertImage(ctx, &testSystemContext, store, options)
if status != http.StatusOK && !ignoreAttestationErrors {
assert.Error(t, err)
return
Expand Down

0 comments on commit c39f622

Please sign in to comment.