summaryrefslogtreecommitdiffhomepage
path: root/runsc/cmd/mitigate_test.go
blob: 294fc645c4bb0b449986c476a3b0c80c85f884a0 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2021 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build amd64
// +build amd64

package cmd

import (
	"testing"

	"github.com/google/subcommands"
	"gvisor.dev/gvisor/pkg/log"
	"gvisor.dev/gvisor/runsc/mitigate"
)

type mockMachineControl struct {
	enabled bool
	cpus    mitigate.CPUSet
}

func (m *mockMachineControl) enable() error {
	m.enabled = true
	return nil
}

func (m *mockMachineControl) disable() error {
	if m.cpus.IsVulnerable() {
		m.enabled = false
	}
	return nil
}

func (m *mockMachineControl) isEnabled() (bool, error) {
	return m.enabled, nil
}

func (m *mockMachineControl) getCPUs() (mitigate.CPUSet, error) {
	set := m.cpus
	if !m.enabled {
		set = m.cpus[:len(m.cpus)/2]
	}

	// Instead of just returning the created CPU set stored in this struct, call
	// NewCPUSet to exercise that code path as the machineControlImpl would.
	return mitigate.NewCPUSet(set.String())
}

type executeTestCase struct {
	name                string
	cpu                 mitigate.MockCPU
	mitigateWantCPUs    int
	mitigateError       subcommands.ExitStatus
	mitigateWantEnabled bool
	reverseWantCPUs     int
	reverseError        subcommands.ExitStatus
	reverseWantEnabled  bool
	dryrun              bool
}

func TestExecute(t *testing.T) {
	for _, tc := range []executeTestCase{
		{
			name:                "CascadeLake4",
			cpu:                 mitigate.CascadeLake4,
			mitigateWantCPUs:    2,
			mitigateWantEnabled: false,
			reverseWantCPUs:     4,
			reverseWantEnabled:  true,
		},
		{
			name:                "CascadeLake4DryRun",
			cpu:                 mitigate.CascadeLake4,
			mitigateWantCPUs:    4,
			mitigateWantEnabled: true,
			reverseWantCPUs:     4,
			reverseWantEnabled:  true,
			dryrun:              true,
		},
		{
			name:                "AMD8",
			cpu:                 mitigate.AMD8,
			mitigateWantCPUs:    8,
			mitigateWantEnabled: true,
			reverseWantCPUs:     8,
			reverseWantEnabled:  true,
		},
		{
			name:          "Empty",
			cpu:           mitigate.Empty,
			mitigateError: Errorf(`mitigate operation failed: no cpus found for: ""`),
			reverseError:  Errorf(`mitigate operation failed: no cpus found for: ""`),
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			set := tc.cpu.MakeCPUSet()
			m := &Mitigate{
				control: &mockMachineControl{
					enabled: true,
					cpus:    set,
				},
				dryRun: tc.dryrun,
			}
			t.Run("Mitigate", func(t *testing.T) {
				m.doExecuteTest(t, tc.mitigateWantEnabled, tc.mitigateWantCPUs, tc.mitigateError)
			})

			m.reverse = true
			t.Run("Reverse", func(t *testing.T) {
				m.doExecuteTest(t, tc.reverseWantEnabled, tc.reverseWantCPUs, tc.reverseError)
			})
		})
	}
}

// doExecuteTest runs Execute with the mitigate operation and reverse operation.
func (m *Mitigate) doExecuteTest(t *testing.T, wantEnabled bool, wantCPUs int, wantErr subcommands.ExitStatus) {
	subError := m.execute()
	if subError != wantErr {
		t.Fatalf("Mitigate error mismatch: want: %v got: %v", wantErr, subError)
	}

	// case where test should end in error or we don't care
	// about how many cpus are returned.
	if wantErr != subcommands.ExitSuccess {
		log.Infof("return")
		return
	}

	gotEnabled, _ := m.control.isEnabled()
	if wantEnabled != gotEnabled {
		t.Fatalf("Incorrect enabled state: want: %t got: %t", wantEnabled, gotEnabled)
	}

	gotCPUs, _ := m.control.getCPUs()
	if len(gotCPUs) != wantCPUs {
		t.Fatalf("Incorrect number of CPUs: want: %d got: %d", wantCPUs, len(gotCPUs))
	}
}