github.com/jmitchell/nomad@v0.1.3-0.20151007230021-7ab84c2862d8/client/driver/rkt_test.go (about)

     1  package driver
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  	"time"
     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  func TestRktDriver_Handle(t *testing.T) {
    16  	h := &rktHandle{
    17  		proc:   &os.Process{Pid: 123},
    18  		name:   "foo",
    19  		doneCh: make(chan struct{}),
    20  		waitCh: make(chan error, 1),
    21  	}
    22  
    23  	actual := h.ID()
    24  	expected := `Rkt:{"Pid":123,"Name":"foo"}`
    25  	if actual != expected {
    26  		t.Errorf("Expected `%s`, found `%s`", expected, actual)
    27  	}
    28  }
    29  
    30  // The fingerprinter test should always pass, even if rkt is not installed.
    31  func TestRktDriver_Fingerprint(t *testing.T) {
    32  	ctestutils.RktCompatible(t)
    33  	d := NewRktDriver(testDriverContext(""))
    34  	node := &structs.Node{
    35  		Attributes: make(map[string]string),
    36  	}
    37  	apply, err := d.Fingerprint(&config.Config{}, node)
    38  	if err != nil {
    39  		t.Fatalf("err: %v", err)
    40  	}
    41  	if !apply {
    42  		t.Fatalf("should apply")
    43  	}
    44  	if node.Attributes["driver.rkt"] == "" {
    45  		t.Fatalf("Missing Rkt driver")
    46  	}
    47  	if node.Attributes["driver.rkt.version"] == "" {
    48  		t.Fatalf("Missing Rkt driver version")
    49  	}
    50  	if node.Attributes["driver.rkt.appc.version"] == "" {
    51  		t.Fatalf("Missing appc version for the Rkt driver")
    52  	}
    53  }
    54  
    55  func TestRktDriver_Start(t *testing.T) {
    56  	ctestutils.RktCompatible(t)
    57  	// TODO: use test server to load from a fixture
    58  	task := &structs.Task{
    59  		Name: "etcd",
    60  		Config: map[string]string{
    61  			"trust_prefix": "coreos.com/etcd",
    62  			"name":         "coreos.com/etcd:v2.0.4",
    63  			"exec":         "/etcd --version",
    64  		},
    65  	}
    66  
    67  	driverCtx := testDriverContext(task.Name)
    68  	ctx := testDriverExecContext(task, driverCtx)
    69  	d := NewRktDriver(driverCtx)
    70  	defer ctx.AllocDir.Destroy()
    71  
    72  	handle, err := d.Start(ctx, task)
    73  	if err != nil {
    74  		t.Fatalf("err: %v", err)
    75  	}
    76  	if handle == nil {
    77  		t.Fatalf("missing handle")
    78  	}
    79  
    80  	// Attempt to open
    81  	handle2, err := d.Open(ctx, handle.ID())
    82  	if err != nil {
    83  		t.Fatalf("err: %v", err)
    84  	}
    85  	if handle2 == nil {
    86  		t.Fatalf("missing handle")
    87  	}
    88  
    89  	// Clean up
    90  	if err := handle.Kill(); err != nil {
    91  		fmt.Printf("\nError killing Rkt test: %s", err)
    92  	}
    93  }
    94  
    95  func TestRktDriver_Start_Wait(t *testing.T) {
    96  	ctestutils.RktCompatible(t)
    97  	task := &structs.Task{
    98  		Name: "etcd",
    99  		Config: map[string]string{
   100  			"trust_prefix": "coreos.com/etcd",
   101  			"name":         "coreos.com/etcd:v2.0.4",
   102  			"exec":         "/etcd --version",
   103  		},
   104  	}
   105  
   106  	driverCtx := testDriverContext(task.Name)
   107  	ctx := testDriverExecContext(task, driverCtx)
   108  	d := NewRktDriver(driverCtx)
   109  	defer ctx.AllocDir.Destroy()
   110  
   111  	handle, err := d.Start(ctx, task)
   112  	if err != nil {
   113  		t.Fatalf("err: %v", err)
   114  	}
   115  	if handle == nil {
   116  		t.Fatalf("missing handle")
   117  	}
   118  	defer handle.Kill()
   119  
   120  	// Update should be a no-op
   121  	err = handle.Update(task)
   122  	if err != nil {
   123  		t.Fatalf("err: %v", err)
   124  	}
   125  
   126  	select {
   127  	case err := <-handle.WaitCh():
   128  		if err != nil {
   129  			t.Fatalf("err: %v", err)
   130  		}
   131  	case <-time.After(5 * time.Second):
   132  		t.Fatalf("timeout")
   133  	}
   134  }