blob: 2062be7d0ceb5cab089992f01bc66c991aa60e0a [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Lorenz Brun2d284b52023-03-08 17:05:12 +01004package e2e
5
6import (
7 "bufio"
8 "bytes"
9 "crypto/ed25519"
10 "crypto/rand"
11 "encoding/json"
12 "fmt"
13 "io"
14 "net"
15 "os"
16 "os/exec"
Tim Windelschmidt4bd25e82025-07-11 19:36:28 +020017 "path/filepath"
Lorenz Brun2d284b52023-03-08 17:05:12 +010018 "strings"
19 "testing"
20 "time"
21
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +010022 "github.com/bazelbuild/rules_go/go/runfiles"
Lorenz Brun2d284b52023-03-08 17:05:12 +010023 "github.com/pkg/sftp"
24 "golang.org/x/crypto/ssh"
25 "google.golang.org/protobuf/proto"
26
27 "source.monogon.dev/cloud/agent/api"
Tim Windelschmidt2a1d1b22024-02-06 07:07:42 +010028
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020029 "source.monogon.dev/osbase/fat32"
30 "source.monogon.dev/osbase/freeport"
Jan Schärc1b6df42025-03-20 08:52:18 +000031 "source.monogon.dev/osbase/structfs"
Lorenz Brun2d284b52023-03-08 17:05:12 +010032)
33
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000034var (
35 // These are filled by bazel at linking time with the canonical path of
36 // their corresponding file. Inside the init function we resolve it
37 // with the rules_go runfiles package to the real path.
38 xCloudImagePath string
39 xOvmfVarsPath string
40 xOvmfCodePath string
41 xTakeoverPath string
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020042 xQEMUPath string
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000043)
44
45func init() {
46 var err error
47 for _, path := range []*string{
48 &xCloudImagePath, &xOvmfVarsPath, &xOvmfCodePath,
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020049 &xTakeoverPath, &xQEMUPath,
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000050 } {
51 *path, err = runfiles.Rlocation(*path)
52 if err != nil {
53 panic(err)
54 }
55 }
Tim Windelschmidt4bd25e82025-07-11 19:36:28 +020056 // When running QEMU with snapshot=on set, QEMU creates a copy of the
57 // provided file in $TMPDIR. If $TMPDIR is set to /tmp, it will always
58 // be overridden to /var/tmp. This creates an issue for us, as the
59 // bazel tests only wire up /tmp, with /var/tmp being unaccessible
60 // because of permissions. Bazel provides $TEST_TMPDIR for this
61 // usecase, which we resolve to an absolute path and then override
62 // $TMPDIR.
63 tmpDir, err := filepath.Abs(os.Getenv("TEST_TMPDIR"))
64 if err != nil {
65 panic(err)
66 }
67 os.Setenv("TMPDIR", tmpDir)
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000068}
69
Lorenz Brun2d284b52023-03-08 17:05:12 +010070func TestE2E(t *testing.T) {
71 pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
72 if err != nil {
73 t.Fatal(err)
74 }
75
76 sshPubKey, err := ssh.NewPublicKey(pubKey)
77 if err != nil {
78 t.Fatal(err)
79 }
80
81 sshPrivkey, err := ssh.NewSignerFromKey(privKey)
82 if err != nil {
83 t.Fatal(err)
84 }
85
86 // CloudConfig doesn't really have a rigid spec, so just put things into it
87 cloudConfig := make(map[string]any)
88 cloudConfig["ssh_authorized_keys"] = []string{
89 strings.TrimSuffix(string(ssh.MarshalAuthorizedKey(sshPubKey)), "\n"),
90 }
91
92 userData, err := json.Marshal(cloudConfig)
93 if err != nil {
94 t.Fatal(err)
95 }
96
Jan Schärc1b6df42025-03-20 08:52:18 +000097 root := structfs.Tree{
98 structfs.File("user-data", structfs.Bytes("#cloud-config\n"+string(userData))),
99 structfs.File("meta-data", structfs.Bytes("")),
Lorenz Brun2d284b52023-03-08 17:05:12 +0100100 }
101 cloudInitDataFile, err := os.CreateTemp("", "cidata*.img")
102 if err != nil {
103 t.Fatal(err)
104 }
105 defer os.Remove(cloudInitDataFile.Name())
Jan Schärc1b6df42025-03-20 08:52:18 +0000106 if err := fat32.WriteFS(cloudInitDataFile, root, fat32.Options{Label: "cidata"}); err != nil {
Lorenz Brun2d284b52023-03-08 17:05:12 +0100107 t.Fatal(err)
108 }
Lorenz Brun2d284b52023-03-08 17:05:12 +0100109
110 sshPort, sshPortCloser, err := freeport.AllocateTCPPort()
111 if err != nil {
112 t.Fatal(err)
113 }
114
115 qemuArgs := []string{
116 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults", "-m", "1024",
117 "-cpu", "host", "-smp", "sockets=1,cpus=1,cores=2,threads=2,maxcpus=4",
Tim Windelschmidt82e6af72024-07-23 00:05:42 +0000118 "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfCodePath,
119 "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfVarsPath,
120 "-drive", "if=virtio,format=qcow2,snapshot=on,cache=unsafe,file=" + xCloudImagePath,
Lorenz Brun2d284b52023-03-08 17:05:12 +0100121 "-drive", "if=virtio,format=raw,snapshot=on,file=" + cloudInitDataFile.Name(),
122 "-netdev", fmt.Sprintf("user,id=net0,net=10.42.0.0/24,dhcpstart=10.42.0.10,hostfwd=tcp::%d-:22", sshPort),
123 "-device", "virtio-net-pci,netdev=net0,mac=22:d5:8e:76:1d:07",
124 "-device", "virtio-rng-pci",
125 "-serial", "stdio",
126 "-no-reboot",
127 }
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +0200128 qemuCmd := exec.Command(xQEMUPath, qemuArgs...)
Lorenz Brun2d284b52023-03-08 17:05:12 +0100129 stdoutPipe, err := qemuCmd.StdoutPipe()
130 if err != nil {
131 t.Fatal(err)
132 }
133 agentStarted := make(chan struct{})
134 go func() {
135 s := bufio.NewScanner(stdoutPipe)
136 for s.Scan() {
137 t.Log("kernel: " + s.Text())
138 if strings.Contains(s.Text(), "Monogon BMaaS Agent started") {
139 agentStarted <- struct{}{}
140 break
141 }
142 }
143 qemuCmd.Wait()
144 }()
145 qemuCmd.Stderr = os.Stderr
146 sshPortCloser.Close()
147 if err := qemuCmd.Start(); err != nil {
148 t.Fatal(err)
149 }
150 defer qemuCmd.Process.Kill()
151
152 var c *ssh.Client
153 for {
154 c, err = ssh.Dial("tcp", net.JoinHostPort("localhost", fmt.Sprintf("%d", sshPort)), &ssh.ClientConfig{
155 User: "debian",
156 Auth: []ssh.AuthMethod{ssh.PublicKeys(sshPrivkey)},
157 HostKeyCallback: ssh.InsecureIgnoreHostKey(),
158 Timeout: 5 * time.Second,
159 })
160 if err != nil {
161 t.Logf("error connecting via SSH, retrying: %v", err)
162 time.Sleep(1 * time.Second)
163 continue
164 }
165 break
166 }
167 defer c.Close()
168 sc, err := sftp.NewClient(c)
169 if err != nil {
170 t.Fatal(err)
171 }
172 defer sc.Close()
173 takeoverFile, err := sc.Create("takeover")
174 if err != nil {
175 t.Fatal(err)
176 }
177 defer takeoverFile.Close()
178 if err := takeoverFile.Chmod(0o755); err != nil {
179 t.Fatal(err)
180 }
Tim Windelschmidt82e6af72024-07-23 00:05:42 +0000181 takeoverSrcFile, err := os.Open(xTakeoverPath)
Lorenz Brun2d284b52023-03-08 17:05:12 +0100182 if err != nil {
183 t.Fatal(err)
184 }
185 defer takeoverSrcFile.Close()
Tim Windelschmidt681d5152025-01-08 00:19:33 +0100186
Lorenz Brun2d284b52023-03-08 17:05:12 +0100187 if _, err := io.Copy(takeoverFile, takeoverSrcFile); err != nil {
188 t.Fatal(err)
189 }
190 if err := takeoverFile.Close(); err != nil {
191 t.Fatal(err)
192 }
193 sc.Close()
194
195 sess, err := c.NewSession()
196 if err != nil {
197 t.Fatal(err)
198 }
199 defer sess.Close()
200
201 init := api.TakeoverInit{
Lorenz Brun5b8b8602023-03-09 17:22:21 +0100202 MachineId: "test",
Lorenz Brun2d284b52023-03-08 17:05:12 +0100203 BmaasEndpoint: "localhost:1234",
204 }
205 initRaw, err := proto.Marshal(&init)
206 if err != nil {
207 t.Fatal(err)
208 }
209 sess.Stdin = bytes.NewReader(initRaw)
210 var stdoutBuf bytes.Buffer
211 var stderrBuf bytes.Buffer
212 sess.Stdout = &stdoutBuf
213 sess.Stderr = &stderrBuf
214 if err := sess.Run("sudo ./takeover"); err != nil {
215 t.Errorf("stderr:\n%s\n\n", stderrBuf.String())
216 t.Fatal(err)
217 }
218 var resp api.TakeoverResponse
219 if err := proto.Unmarshal(stdoutBuf.Bytes(), &resp); err != nil {
220 t.Fatal(err)
221 }
222 switch res := resp.Result.(type) {
223 case *api.TakeoverResponse_Success:
224 if res.Success.InitMessage.BmaasEndpoint != init.BmaasEndpoint {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100225 t.Fatal("InitMessage not passed through properly")
Lorenz Brun2d284b52023-03-08 17:05:12 +0100226 }
227 case *api.TakeoverResponse_Error:
228 t.Fatalf("takeover returned error: %v", res.Error.Message)
229 }
230 select {
231 case <-agentStarted:
232 // Done, test passed
233 case <-time.After(30 * time.Second):
234 t.Fatal("Waiting for BMaaS agent startup timed out")
235 }
236}