Skip to content

Commit

Permalink
Merge pull request #5672 from nalind/unit-tests-mirrors
Browse files Browse the repository at this point in the history
unit tests: use test-specific policy.json and registries.conf
  • Loading branch information
openshift-merge-bot[bot] committed Aug 9, 2024
2 parents f215679 + c39f622 commit 94aa41b
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 94aa41b

Please sign in to comment.