File: buildah_test.go

package info (click to toggle)
golang-github-containers-buildah 1.41.4%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,152 kB
  • sloc: sh: 2,569; makefile: 241; perl: 187; asm: 16; awk: 12; ansic: 1
file content (95 lines) | stat: -rw-r--r-- 2,920 bytes parent folder | download | duplicates (2)
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
95
package buildah

import (
	"context"
	"flag"
	"os"
	"strings"
	"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
	if InitReexec() {
		return
	}
	flag.BoolVar(&debug, "debug", false, "turn on debug logging")
	flag.StringVar(&logLevel, "log-level", "error", "log level")
	flag.Parse()
	level, err := logrus.ParseLevel(logLevel)
	if err != nil {
		logrus.Fatalf("error parsing log level %q: %v", logLevel, err)
	}
	if debug && level < logrus.DebugLevel {
		level = logrus.DebugLevel
	}
	logrus.SetLevel(level)
	os.Exit(m.Run())
}

func TestOpenBuilderCommonBuildOpts(t *testing.T) {
	// This test cannot be parallelized as this uses NewBuilder()
	// which eventually and indirectly accesses a global variable
	// defined in `go-selinux`, this must be fixed at `go-selinux`
	// or builder must enable sometime of locking mechanism i.e if
	// routine is creating Builder other's must wait for it.
	// Tracked here: https://github.com/containers/buildah/issues/5967
	ctx := context.TODO()
	store, err := storage.GetStore(types.StoreOptions{
		RunRoot:         t.TempDir(),
		GraphRoot:       t.TempDir(),
		GraphDriverName: "vfs",
	})
	if err != nil {
		t.Skipf("Not enough permissions to execute test: %s", err)
	}
	require.NoError(t, err)
	t.Cleanup(func() { _, err := store.Shutdown(true); assert.NoError(t, err) })
	b, err := NewBuilder(ctx, store, BuilderOptions{})
	if strings.Contains(err.Error(), "netavark") {
		t.Skipf("Cannot execute, failed to construct builder: %v", err)
	}
	require.NoError(t, err)
	require.NotNil(t, b.CommonBuildOpts)
	b.CommonBuildOpts = nil
	builderContainerID := b.ContainerID
	err = b.Save()
	require.NoError(t, err)
	b, err = OpenBuilder(store, builderContainerID)
	require.NoError(t, err)
	require.NotNil(t, b.CommonBuildOpts)
	builders, err := OpenAllBuilders(store)
	require.NoError(t, err)
	for _, b := range builders {
		require.NotNil(t, b.CommonBuildOpts)
	}
	imageID, _, _, err := b.Commit(ctx, nil, CommitOptions{})
	require.NoError(t, err)
	b, err = ImportBuilderFromImage(ctx, store, ImportFromImageOptions{
		Image: imageID,
	})
	require.NoError(t, err)
	require.NotNil(t, b.CommonBuildOpts)
	container, err := store.CreateContainer("", nil, imageID, "", "", &storage.ContainerOptions{})
	require.NoError(t, err)
	require.NotNil(t, container)
	b, err = ImportBuilder(ctx, store, ImportOptions{
		Container:           container.ID,
		SignaturePolicyPath: testSystemContext.SignaturePolicyPath,
	})
	require.NoError(t, err)
	require.NotNil(t, b.CommonBuildOpts)
}