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