File: preflight_ubuntu_linux_test.go

package info (click to toggle)
golang-github-crc-org-crc 2.34.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,548 kB
  • sloc: sh: 398; makefile: 326; javascript: 40
file content (56 lines) | stat: -rw-r--r-- 1,666 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
//go:build linux
// +build linux

package preflight

import (
	"errors"
	"os"
	"testing"

	"github.com/crc-org/crc/v2/pkg/crc/constants"
	"github.com/stretchr/testify/assert"
)

var (
	source = `#include <tunables/global>
profile LIBVIRT_TEMPLATE flags=(attach_disconnected) {
  #include <abstractions/libvirt-qemu>
}`
	expected = `#include <tunables/global>
profile LIBVIRT_TEMPLATE flags=(attach_disconnected) {
  ` + constants.MachineCacheDir + `/*/crc*.qcow2 rk,

  #include <abstractions/libvirt-qemu>
}`
)

func TestCheckAppArmor(t *testing.T) {
	assert.EqualError(t, checkAppArmorExceptionIsPresent(mockReader(source))(), "AppArmor profile not configured")
	assert.NoError(t, checkAppArmorExceptionIsPresent(mockReader(expected))())
}

func TestFixAppArmor(t *testing.T) {
	assert.NoError(t, addAppArmorExceptionForQcowDisks(mockReader(source), writerVerifier(expected))())
	assert.EqualError(t, addAppArmorExceptionForQcowDisks(mockReader("invalid"), writerVerifier(expected))(),
		"unexpected AppArmor template file /etc/apparmor.d/libvirt/TEMPLATE.qemu, cannot configure it automatically")
}

func TestCleanupAppArmor(t *testing.T) {
	assert.NoError(t, removeAppArmorExceptionForQcowDisks(mockReader(expected), writerVerifier(source))())
}

func writerVerifier(expected string) func(reason string, content string, filepath string, mode os.FileMode) error {
	return func(reason, content, filepath string, mode os.FileMode) error {
		if content == expected {
			return nil
		}
		return errors.New("unexpected content")
	}
}

func mockReader(input string) func(string) ([]byte, error) {
	return func(string) ([]byte, error) {
		return []byte(input), nil
	}
}