gopkg.in/hugelgupf/u-root.v9@v9.0.0-20180831063832-3f6f1057f09b/integration/integration_test.go (about) 1 // Copyright 2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package integration 6 7 import ( 8 "io/ioutil" 9 "os" 10 "path" 11 "path/filepath" 12 "testing" 13 14 "github.com/u-root/u-root/pkg/cp" 15 "github.com/u-root/u-root/pkg/golang" 16 "github.com/u-root/u-root/pkg/qemu" 17 "github.com/u-root/u-root/pkg/uroot" 18 ) 19 20 // Returns temporary directory and QEMU instance. 21 // 22 // - `uinitName` is the name of a directory containing uinit found at 23 // `github.com/u-root/u-root/integration/testdata`. 24 func testWithQEMU(t *testing.T, uinitName string, extraArgs []string) (string, *qemu.QEMU) { 25 if _, ok := os.LookupEnv("UROOT_QEMU"); !ok { 26 t.Skip("test is skipped unless UROOT_QEMU is set") 27 } 28 if _, ok := os.LookupEnv("UROOT_KERNEL"); !ok { 29 t.Skip("test is skipped unless UROOT_KERNEL is set") 30 } 31 32 // TempDir 33 tmpDir, err := ioutil.TempDir("", "uroot-integration") 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 // Env 39 env := golang.Default() 40 env.CgoEnabled = false 41 42 // Builder 43 builder, err := uroot.GetBuilder("bb") 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 // Packages 49 pkgs := []string{ 50 "github.com/u-root/u-root/cmds/*", 51 path.Join("github.com/u-root/u-root/integration/testdata", uinitName, "uinit"), 52 } 53 54 // Archiver 55 archiver, err := uroot.GetArchiver("cpio") 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 // OutputFile 61 outputFile := filepath.Join(tmpDir, "initramfs.cpio") 62 w, err := archiver.OpenWriter(outputFile, "", "") 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 // Build u-root 68 opts := uroot.Opts{ 69 TempDir: tmpDir, 70 Env: env, 71 Commands: []uroot.Commands{ 72 { 73 Builder: builder, 74 Packages: pkgs, 75 }, 76 }, 77 Archiver: archiver, 78 OutputFile: w, 79 InitCmd: "init", 80 DefaultShell: "rush", 81 } 82 if err := uroot.CreateInitramfs(opts); err != nil { 83 t.Fatal(err) 84 } 85 86 // Copy kernel to tmpDir. 87 bzImage := filepath.Join(tmpDir, "bzImage") 88 if err := cp.Copy(os.Getenv("UROOT_KERNEL"), bzImage); err != nil { 89 t.Fatal(err) 90 } 91 92 // Expose the temp directory to QEMU as /dev/sda1 93 extraArgs = append(extraArgs, "-drive", "file=fat:ro:"+tmpDir+",if=none,id=tmpdir") 94 extraArgs = append(extraArgs, "-device", "ich9-ahci,id=ahci") 95 extraArgs = append(extraArgs, "-device", "ide-drive,drive=tmpdir,bus=ahci.0") 96 97 // Start QEMU 98 q := &qemu.QEMU{ 99 InitRAMFS: outputFile, 100 Kernel: bzImage, 101 ExtraArgs: extraArgs, 102 } 103 t.Logf("command line:\n%s", q.CmdLineQuoted()) 104 if err := q.Start(); err != nil { 105 t.Fatal("could not spawn QEMU: ", err) 106 } 107 return tmpDir, q 108 } 109 110 func cleanup(t *testing.T, tmpDir string, q *qemu.QEMU) { 111 q.Close() 112 if t.Failed() { 113 t.Log("keeping temp dir: ", tmpDir) 114 } else { 115 if err := os.RemoveAll(tmpDir); err != nil { 116 t.Logf("failed to remove temporary directory %s", tmpDir) 117 } 118 } 119 }