github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/client/driver/qemu_test.go (about)

     1  package driver
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/nomad/client/config"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  
    12  	ctestutils "github.com/hashicorp/nomad/client/testutil"
    13  )
    14  
    15  // qemuLocated looks to see whether qemu binaries are available on this system
    16  // before we try to run tests. We may need to tweak this for cross-OS support
    17  // but I think this should work on *nix at least.
    18  func qemuLocated() bool {
    19  	_, err := exec.Command("qemu-x86_64", "-version").CombinedOutput()
    20  	return err == nil
    21  }
    22  
    23  func TestQemuDriver_Handle(t *testing.T) {
    24  	h := &qemuHandle{
    25  		proc:   &os.Process{Pid: 123},
    26  		vmID:   "vmid",
    27  		doneCh: make(chan struct{}),
    28  		waitCh: make(chan error, 1),
    29  	}
    30  
    31  	actual := h.ID()
    32  	expected := `QEMU:{"Pid":123,"VmID":"vmid"}`
    33  	if actual != expected {
    34  		t.Errorf("Expected `%s`, found `%s`", expected, actual)
    35  	}
    36  }
    37  
    38  // The fingerprinter test should always pass, even if QEMU is not installed.
    39  func TestQemuDriver_Fingerprint(t *testing.T) {
    40  	ctestutils.QemuCompatible(t)
    41  	d := NewQemuDriver(testDriverContext(""))
    42  	node := &structs.Node{
    43  		Attributes: make(map[string]string),
    44  	}
    45  	apply, err := d.Fingerprint(&config.Config{}, node)
    46  	if err != nil {
    47  		t.Fatalf("err: %v", err)
    48  	}
    49  	if !apply {
    50  		t.Fatalf("should apply")
    51  	}
    52  	if node.Attributes["driver.qemu"] == "" {
    53  		t.Fatalf("Missing Qemu driver")
    54  	}
    55  	if node.Attributes["driver.qemu.version"] == "" {
    56  		t.Fatalf("Missing Qemu driver version")
    57  	}
    58  }
    59  
    60  func TestQemuDriver_Start(t *testing.T) {
    61  	if !qemuLocated() {
    62  		t.Skip("QEMU not found; skipping")
    63  	}
    64  
    65  	// TODO: use test server to load from a fixture
    66  	task := &structs.Task{
    67  		Name: "linux",
    68  		Config: map[string]string{
    69  			"image_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/linux-0.2.img",
    70  			"checksum":     "a5e836985934c3392cbbd9b26db55a7d35a8d7ae1deb7ca559dd9c0159572544",
    71  			"accelerator":  "tcg",
    72  			"guest_ports":  "22,8080",
    73  		},
    74  		Resources: &structs.Resources{
    75  			MemoryMB: 512,
    76  			Networks: []*structs.NetworkResource{
    77  				&structs.NetworkResource{
    78  					ReservedPorts: []int{22000, 80},
    79  				},
    80  			},
    81  		},
    82  	}
    83  
    84  	driverCtx := testDriverContext(task.Name)
    85  	ctx := testDriverExecContext(task, driverCtx)
    86  	defer ctx.AllocDir.Destroy()
    87  	d := NewQemuDriver(driverCtx)
    88  
    89  	handle, err := d.Start(ctx, task)
    90  	if err != nil {
    91  		t.Fatalf("err: %v", err)
    92  	}
    93  	if handle == nil {
    94  		t.Fatalf("missing handle")
    95  	}
    96  
    97  	// Attempt to open
    98  	handle2, err := d.Open(ctx, handle.ID())
    99  	if err != nil {
   100  		t.Fatalf("err: %v", err)
   101  	}
   102  	if handle2 == nil {
   103  		t.Fatalf("missing handle")
   104  	}
   105  
   106  	// Clean up
   107  	if err := handle.Kill(); err != nil {
   108  		fmt.Printf("\nError killing Qemu test: %s", err)
   109  	}
   110  }
   111  
   112  func TestQemuDriver_RequiresMemory(t *testing.T) {
   113  	if !qemuLocated() {
   114  		t.Skip("QEMU not found; skipping")
   115  	}
   116  
   117  	// TODO: use test server to load from a fixture
   118  	task := &structs.Task{
   119  		Name: "linux",
   120  		Config: map[string]string{
   121  			"image_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/linux-0.2.img",
   122  			"accelerator":  "tcg",
   123  			"host_port":    "8080",
   124  			"guest_port":   "8081",
   125  			"checksum":     "a5e836985934c3392cbbd9b26db55a7d35a8d7ae1deb7ca559dd9c0159572544",
   126  			// ssh u/p would be here
   127  		},
   128  	}
   129  
   130  	driverCtx := testDriverContext(task.Name)
   131  	ctx := testDriverExecContext(task, driverCtx)
   132  	defer ctx.AllocDir.Destroy()
   133  	d := NewQemuDriver(driverCtx)
   134  
   135  	_, err := d.Start(ctx, task)
   136  	if err == nil {
   137  		t.Fatalf("Expected error when not specifying memory")
   138  	}
   139  
   140  }