File: supported_test.go

package info (click to toggle)
golang-github-containers-common 0.64.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,528 kB
  • sloc: makefile: 130; sh: 102
file content (113 lines) | stat: -rw-r--r-- 2,910 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package supported

import (
	"errors"
	"os"
	"testing"

	"github.com/containers/common/pkg/apparmor/internal/supported/supportedfakes"
	"github.com/stretchr/testify/require"
)

func TestSingleton(t *testing.T) {
	// Create the singleton
	sut := NewAppArmorVerifier()
	mock := &supportedfakes.FakeVerifierImpl{}
	sut.impl = mock
	mock.OsStatReturns(nil, errors.New(""))

	// Retrieve the mocked path
	const testBinaryPath = "/some/test/path"
	mock.ExecLookPathReturns(testBinaryPath, nil)
	res, err := sut.FindAppArmorParserBinary()
	require.Nil(t, err)
	require.Equal(t, testBinaryPath, res)

	// Make the mock fail
	mock.ExecLookPathReturns("", errors.New(""))

	// Check if we still return the memoized result
	res, err = sut.FindAppArmorParserBinary()
	require.Nil(t, err)
	require.Equal(t, testBinaryPath, res)

	// A new singleton instance should return the same memoized result
	sutNew := NewAppArmorVerifier()
	res, err = sutNew.FindAppArmorParserBinary()
	require.Nil(t, err)
	require.Equal(t, testBinaryPath, res)
}

func TestApparmorVerifier(t *testing.T) {
	for _, tc := range []struct {
		description string
		prepare     func(*supportedfakes.FakeVerifierImpl)
		shoulderr   bool
	}{
		{
			description: "success with binary in /sbin",
			prepare: func(mock *supportedfakes.FakeVerifierImpl) {
				mock.UnshareIsRootlessReturns(false)
				mock.RuncIsEnabledReturns(true)

				file, err := os.CreateTemp(t.TempDir(), "")
				require.Nil(t, err)
				fileInfo, err := file.Stat()
				require.Nil(t, err)
				mock.OsStatReturns(fileInfo, nil)
			},
			shoulderr: false,
		},
		{
			description: "success with binary in $PATH",
			prepare: func(mock *supportedfakes.FakeVerifierImpl) {
				mock.UnshareIsRootlessReturns(false)
				mock.RuncIsEnabledReturns(true)
				mock.OsStatReturns(nil, errors.New(""))
				mock.ExecLookPathReturns("", nil)
			},
			shoulderr: false,
		},
		{
			description: "error binary not in /sbin or $PATH",
			prepare: func(mock *supportedfakes.FakeVerifierImpl) {
				mock.UnshareIsRootlessReturns(false)
				mock.RuncIsEnabledReturns(true)
				mock.OsStatReturns(nil, errors.New(""))
				mock.ExecLookPathReturns("", errors.New(""))
			},
			shoulderr: true,
		},
		{
			description: "error runc AppAmor not enabled",
			prepare: func(mock *supportedfakes.FakeVerifierImpl) {
				mock.UnshareIsRootlessReturns(false)
				mock.RuncIsEnabledReturns(false)
			},
			shoulderr: true,
		},
		{
			description: "error rootless",
			prepare: func(mock *supportedfakes.FakeVerifierImpl) {
				mock.UnshareIsRootlessReturns(true)
			},
			shoulderr: true,
		},
	} {
		// Given
		sut := &ApparmorVerifier{impl: &defaultVerifier{}}
		mock := &supportedfakes.FakeVerifierImpl{}
		tc.prepare(mock)
		sut.impl = mock

		// When
		err := sut.IsSupported()

		// Then
		if tc.shoulderr {
			require.NotNil(t, err, tc.description)
		} else {
			require.Nil(t, err, tc.description)
		}
	}
}