github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/client/driver/qemu_test.go (about)

     1  package driver
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/client/config"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  
    10  	ctestutils "github.com/hashicorp/nomad/client/testutil"
    11  )
    12  
    13  // The fingerprinter test should always pass, even if QEMU is not installed.
    14  func TestQemuDriver_Fingerprint(t *testing.T) {
    15  	t.Parallel()
    16  	ctestutils.QemuCompatible(t)
    17  	driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"})
    18  	d := NewQemuDriver(driverCtx)
    19  	node := &structs.Node{
    20  		Attributes: make(map[string]string),
    21  	}
    22  	apply, err := d.Fingerprint(&config.Config{}, node)
    23  	if err != nil {
    24  		t.Fatalf("err: %v", err)
    25  	}
    26  	if !apply {
    27  		t.Fatalf("should apply")
    28  	}
    29  	if node.Attributes["driver.qemu"] == "" {
    30  		t.Fatalf("Missing Qemu driver")
    31  	}
    32  	if node.Attributes["driver.qemu.version"] == "" {
    33  		t.Fatalf("Missing Qemu driver version")
    34  	}
    35  }
    36  
    37  func TestQemuDriver_StartOpen_Wait(t *testing.T) {
    38  	t.Parallel()
    39  	ctestutils.QemuCompatible(t)
    40  	// TODO: use test server to load from a fixture
    41  	task := &structs.Task{
    42  		Name: "linux",
    43  		Config: map[string]interface{}{
    44  			"artifact_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/linux-0.2.img",
    45  			"checksum":        "sha256:a5e836985934c3392cbbd9b26db55a7d35a8d7ae1deb7ca559dd9c0159572544",
    46  			"accelerator":     "tcg",
    47  			"port_map": []map[string]int{{
    48  				"main": 22,
    49  				"web":  8080,
    50  			}},
    51  		},
    52  		Resources: &structs.Resources{
    53  			CPU:      500,
    54  			MemoryMB: 512,
    55  			Networks: []*structs.NetworkResource{
    56  				&structs.NetworkResource{
    57  					ReservedPorts: []structs.Port{{"main", 22000}, {"web", 80}},
    58  				},
    59  			},
    60  		},
    61  	}
    62  
    63  	driverCtx, execCtx := testDriverContexts(task)
    64  	defer execCtx.AllocDir.Destroy()
    65  	d := NewQemuDriver(driverCtx)
    66  
    67  	handle, err := d.Start(execCtx, task)
    68  	if err != nil {
    69  		t.Fatalf("err: %v", err)
    70  	}
    71  	if handle == nil {
    72  		t.Fatalf("missing handle")
    73  	}
    74  
    75  	// Attempt to open
    76  	handle2, err := d.Open(execCtx, handle.ID())
    77  	if err != nil {
    78  		t.Fatalf("err: %v", err)
    79  	}
    80  	if handle2 == nil {
    81  		t.Fatalf("missing handle")
    82  	}
    83  
    84  	// Clean up
    85  	if err := handle.Kill(); err != nil {
    86  		fmt.Printf("\nError killing Qemu test: %s", err)
    87  	}
    88  }
    89  
    90  func TestQemuDriver_RequiresMemory(t *testing.T) {
    91  	t.Parallel()
    92  	ctestutils.QemuCompatible(t)
    93  	// TODO: use test server to load from a fixture
    94  	task := &structs.Task{
    95  		Name: "linux",
    96  		Config: map[string]interface{}{
    97  			"artifact_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/linux-0.2.img",
    98  			"accelerator":     "tcg",
    99  			"host_port":       "8080",
   100  			"guest_port":      "8081",
   101  			"checksum":        "sha256:a5e836985934c3392cbbd9b26db55a7d35a8d7ae1deb7ca559dd9c0159572544",
   102  			// ssh u/p would be here
   103  		},
   104  	}
   105  
   106  	driverCtx, execCtx := testDriverContexts(task)
   107  	defer execCtx.AllocDir.Destroy()
   108  	d := NewQemuDriver(driverCtx)
   109  
   110  	_, err := d.Start(execCtx, task)
   111  	if err == nil {
   112  		t.Fatalf("Expected error when not specifying memory")
   113  	}
   114  }