github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/client/driver/qemu_test.go (about)

     1  package driver
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  	"syscall"
     8  	"testing"
     9  
    10  	"github.com/ncodes/nomad/client/config"
    11  	"github.com/ncodes/nomad/nomad/structs"
    12  
    13  	ctestutils "github.com/ncodes/nomad/client/testutil"
    14  )
    15  
    16  // The fingerprinter test should always pass, even if QEMU is not installed.
    17  func TestQemuDriver_Fingerprint(t *testing.T) {
    18  	ctestutils.QemuCompatible(t)
    19  	task := &structs.Task{
    20  		Name:      "foo",
    21  		Driver:    "qemu",
    22  		Resources: structs.DefaultResources(),
    23  	}
    24  	ctx := testDriverContexts(t, task)
    25  	defer ctx.AllocDir.Destroy()
    26  	d := NewQemuDriver(ctx.DriverCtx)
    27  
    28  	node := &structs.Node{
    29  		Attributes: make(map[string]string),
    30  	}
    31  	apply, err := d.Fingerprint(&config.Config{}, node)
    32  	if err != nil {
    33  		t.Fatalf("err: %v", err)
    34  	}
    35  	if !apply {
    36  		t.Fatalf("should apply")
    37  	}
    38  	if node.Attributes["driver.qemu"] == "" {
    39  		t.Fatalf("Missing Qemu driver")
    40  	}
    41  	if node.Attributes["driver.qemu.version"] == "" {
    42  		t.Fatalf("Missing Qemu driver version")
    43  	}
    44  }
    45  
    46  func TestQemuDriver_StartOpen_Wait(t *testing.T) {
    47  	ctestutils.QemuCompatible(t)
    48  	task := &structs.Task{
    49  		Name:   "linux",
    50  		Driver: "qemu",
    51  		Config: map[string]interface{}{
    52  			"image_path":  "linux-0.2.img",
    53  			"accelerator": "tcg",
    54  			"port_map": []map[string]int{{
    55  				"main": 22,
    56  				"web":  8080,
    57  			}},
    58  			"args": []string{"-nodefconfig", "-nodefaults"},
    59  		},
    60  		LogConfig: &structs.LogConfig{
    61  			MaxFiles:      10,
    62  			MaxFileSizeMB: 10,
    63  		},
    64  		Resources: &structs.Resources{
    65  			CPU:      500,
    66  			MemoryMB: 512,
    67  			Networks: []*structs.NetworkResource{
    68  				&structs.NetworkResource{
    69  					ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
    70  				},
    71  			},
    72  		},
    73  	}
    74  
    75  	ctx := testDriverContexts(t, task)
    76  	defer ctx.AllocDir.Destroy()
    77  	d := NewQemuDriver(ctx.DriverCtx)
    78  
    79  	// Copy the test image into the task's directory
    80  	dst := ctx.ExecCtx.TaskDir.Dir
    81  	copyFile("./test-resources/qemu/linux-0.2.img", filepath.Join(dst, "linux-0.2.img"), t)
    82  
    83  	if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
    84  		t.Fatalf("Prestart faild: %v", err)
    85  	}
    86  
    87  	handle, err := d.Start(ctx.ExecCtx, task)
    88  	if err != nil {
    89  		t.Fatalf("err: %v", err)
    90  	}
    91  	if handle == nil {
    92  		t.Fatalf("missing handle")
    93  	}
    94  
    95  	// Ensure that sending a Signal returns an error
    96  	if err := handle.Signal(syscall.SIGINT); err == nil {
    97  		t.Fatalf("Expect an error when signalling")
    98  	}
    99  
   100  	// Attempt to open
   101  	handle2, err := d.Open(ctx.ExecCtx, handle.ID())
   102  	if err != nil {
   103  		t.Fatalf("err: %v", err)
   104  	}
   105  	if handle2 == nil {
   106  		t.Fatalf("missing handle")
   107  	}
   108  
   109  	// Clean up
   110  	if err := handle.Kill(); err != nil {
   111  		fmt.Printf("\nError killing Qemu test: %s", err)
   112  	}
   113  }
   114  
   115  func TestQemuDriverUser(t *testing.T) {
   116  	ctestutils.QemuCompatible(t)
   117  	task := &structs.Task{
   118  		Name:   "linux",
   119  		Driver: "qemu",
   120  		User:   "alice",
   121  		Config: map[string]interface{}{
   122  			"image_path":  "linux-0.2.img",
   123  			"accelerator": "tcg",
   124  			"port_map": []map[string]int{{
   125  				"main": 22,
   126  				"web":  8080,
   127  			}},
   128  			"args": []string{"-nodefconfig", "-nodefaults"},
   129  		},
   130  		LogConfig: &structs.LogConfig{
   131  			MaxFiles:      10,
   132  			MaxFileSizeMB: 10,
   133  		},
   134  		Resources: &structs.Resources{
   135  			CPU:      500,
   136  			MemoryMB: 512,
   137  			Networks: []*structs.NetworkResource{
   138  				&structs.NetworkResource{
   139  					ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
   140  				},
   141  			},
   142  		},
   143  	}
   144  
   145  	ctx := testDriverContexts(t, task)
   146  	defer ctx.AllocDir.Destroy()
   147  	d := NewQemuDriver(ctx.DriverCtx)
   148  
   149  	if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
   150  		t.Fatalf("Prestart faild: %v", err)
   151  	}
   152  
   153  	handle, err := d.Start(ctx.ExecCtx, task)
   154  	if err == nil {
   155  		handle.Kill()
   156  		t.Fatalf("Should've failed")
   157  	}
   158  	msg := "unknown user alice"
   159  	if !strings.Contains(err.Error(), msg) {
   160  		t.Fatalf("Expecting '%v' in '%v'", msg, err)
   161  	}
   162  }