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 114 115 116 117 118 119 120 121 122 123 124
|
package supported
import (
"io/ioutil"
"os"
"testing"
"github.com/containers/common/pkg/apparmor/internal/supported/supportedfakes"
"github.com/pkg/errors"
"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) func()
shoulderr bool
}{
{
description: "success with binary in /sbin",
prepare: func(mock *supportedfakes.FakeVerifierImpl) func() {
mock.UnshareIsRootlessReturns(false)
mock.RuncIsEnabledReturns(true)
file, err := ioutil.TempFile("", "")
require.Nil(t, err)
fileInfo, err := file.Stat()
require.Nil(t, err)
mock.OsStatReturns(fileInfo, nil)
return func() {
require.Nil(t, os.RemoveAll(file.Name()))
}
},
shoulderr: false,
},
{
description: "success with binary in $PATH",
prepare: func(mock *supportedfakes.FakeVerifierImpl) func() {
mock.UnshareIsRootlessReturns(false)
mock.RuncIsEnabledReturns(true)
mock.OsStatReturns(nil, errors.New(""))
mock.ExecLookPathReturns("", nil)
return func() {}
},
shoulderr: false,
},
{
description: "error binary not in /sbin or $PATH",
prepare: func(mock *supportedfakes.FakeVerifierImpl) func() {
mock.UnshareIsRootlessReturns(false)
mock.RuncIsEnabledReturns(true)
mock.OsStatReturns(nil, errors.New(""))
mock.ExecLookPathReturns("", errors.New(""))
return func() {}
},
shoulderr: true,
},
{
description: "error runc AppAmor not enabled",
prepare: func(mock *supportedfakes.FakeVerifierImpl) func() {
mock.UnshareIsRootlessReturns(false)
mock.RuncIsEnabledReturns(false)
return func() {}
},
shoulderr: true,
},
{
description: "error rootless",
prepare: func(mock *supportedfakes.FakeVerifierImpl) func() {
mock.UnshareIsRootlessReturns(true)
return func() {}
},
shoulderr: true,
},
} {
// Given
sut := &ApparmorVerifier{impl: &defaultVerifier{}}
mock := &supportedfakes.FakeVerifierImpl{}
cleanup := tc.prepare(mock)
defer cleanup()
sut.impl = mock
// When
err := sut.IsSupported()
// Then
if tc.shoulderr {
require.NotNil(t, err, tc.description)
} else {
require.Nil(t, err, tc.description)
}
}
}
|