-
Notifications
You must be signed in to change notification settings - Fork 9
/
integration_test.go
107 lines (90 loc) · 2.73 KB
/
integration_test.go
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
package luks
import (
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/anatol/vmtest"
"github.com/tmc/scp"
"golang.org/x/crypto/ssh"
)
func compileExamples() error {
cmd := exec.Command("go", "test", "-c", "examples/end2end_test.go", "-o", "luks_end2end_test")
if testing.Verbose() {
log.Print("compile in-qemu test binary")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
return cmd.Run()
}
func withQemu(t *testing.T) {
t.Parallel()
// These integration tests use QEMU with a statically-compiled kernel (to avoid inintramfs) and a specially
// prepared rootfs. See [instructions](https://github.com/anatol/vmtest/blob/master/docs/prepare_image.md)
// how to prepare these binaries.
params := []string{"-net", "user,hostfwd=tcp::10022-:22", "-net", "nic", "-m", "8G", "-smp", strconv.Itoa(runtime.NumCPU())}
if os.Getenv("TEST_DISABLE_KVM") != "1" {
params = append(params, "-enable-kvm", "-cpu", "host")
}
opts := vmtest.QemuOptions{
OperatingSystem: vmtest.OS_LINUX,
Kernel: "bzImage",
Params: params,
Disks: []vmtest.QemuDisk{{Path: "rootfs.cow", Format: "qcow2"}},
Append: []string{"root=/dev/sda", "rw"},
Verbose: testing.Verbose(),
Timeout: 50 * time.Second,
}
// Run QEMU instance
qemu, err := vmtest.NewQemu(&opts)
require.NoError(t, err)
// Shutdown QEMU at the end of the test case
defer qemu.Shutdown()
config := &ssh.ClientConfig{
User: "root",
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
conn, err := ssh.Dial("tcp", "localhost:10022", config)
require.NoError(t, err)
defer conn.Close()
sess, err := conn.NewSession()
require.NoError(t, err)
defer sess.Close()
scpSess, err := conn.NewSession()
require.NoError(t, err)
require.NoError(t, scp.CopyPath("luks_end2end_test", "luks_end2end_test", scpSess))
testCmd := "./luks_end2end_test -test.parallel " + strconv.Itoa(runtime.NumCPU())
if testing.Verbose() {
testCmd += " -test.v"
}
output, err := sess.CombinedOutput(testCmd)
if testing.Verbose() {
fmt.Print(string(output))
}
require.NoError(t, err)
}
// withRoot runs integration tests at the local host. It requires root permissions.
func withRoot(t *testing.T) {
t.Parallel()
args := []string{"./luks_end2end_test", "-test.parallel", strconv.Itoa(runtime.NumCPU())}
if testing.Verbose() {
args = append(args, "-test.v")
}
cmd := exec.Command("sudo", args...)
if testing.Verbose() {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
require.NoError(t, cmd.Run())
}
func TestIntegration(t *testing.T) {
require.NoError(t, compileExamples())
// defer os.Remove("luks_end2end_test")
t.Run("Qemu", withQemu)
t.Run("Root", withRoot)
}