github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/client/driver/rkt_test.go (about)

     1  package driver
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/hashicorp/nomad/client/allocdir"
    12  	"github.com/hashicorp/nomad/client/config"
    13  	"github.com/hashicorp/nomad/nomad/structs"
    14  
    15  	cstructs "github.com/hashicorp/nomad/client/driver/structs"
    16  	ctestutils "github.com/hashicorp/nomad/client/testutil"
    17  )
    18  
    19  func TestRktVersionRegex(t *testing.T) {
    20  	input_rkt := "rkt version 0.8.1"
    21  	input_appc := "appc version 1.2.0"
    22  	expected_rkt := "0.8.1"
    23  	expected_appc := "1.2.0"
    24  	rktMatches := reRktVersion.FindStringSubmatch(input_rkt)
    25  	appcMatches := reAppcVersion.FindStringSubmatch(input_appc)
    26  	if rktMatches[1] != expected_rkt {
    27  		fmt.Printf("Test failed; got %q; want %q\n", rktMatches[1], expected_rkt)
    28  	}
    29  	if appcMatches[1] != expected_appc {
    30  		fmt.Printf("Test failed; got %q; want %q\n", appcMatches[1], expected_appc)
    31  	}
    32  }
    33  
    34  func TestRktDriver_Handle(t *testing.T) {
    35  	h := &rktHandle{
    36  		proc:   &os.Process{Pid: 123},
    37  		image:  "foo",
    38  		doneCh: make(chan struct{}),
    39  		waitCh: make(chan *cstructs.WaitResult, 1),
    40  	}
    41  
    42  	actual := h.ID()
    43  	expected := `Rkt:{"Pid":123,"Image":"foo"}`
    44  	if actual != expected {
    45  		t.Errorf("Expected `%s`, found `%s`", expected, actual)
    46  	}
    47  }
    48  
    49  // The fingerprinter test should always pass, even if rkt is not installed.
    50  func TestRktDriver_Fingerprint(t *testing.T) {
    51  	ctestutils.RktCompatible(t)
    52  	d := NewRktDriver(testDriverContext(""))
    53  	node := &structs.Node{
    54  		Attributes: make(map[string]string),
    55  	}
    56  	apply, err := d.Fingerprint(&config.Config{}, node)
    57  	if err != nil {
    58  		t.Fatalf("err: %v", err)
    59  	}
    60  	if !apply {
    61  		t.Fatalf("should apply")
    62  	}
    63  	if node.Attributes["driver.rkt"] != "1" {
    64  		t.Fatalf("Missing Rkt driver")
    65  	}
    66  	if node.Attributes["driver.rkt.version"] == "" {
    67  		t.Fatalf("Missing Rkt driver version")
    68  	}
    69  	if node.Attributes["driver.rkt.appc.version"] == "" {
    70  		t.Fatalf("Missing appc version for the Rkt driver")
    71  	}
    72  }
    73  
    74  func TestRktDriver_Start(t *testing.T) {
    75  	ctestutils.RktCompatible(t)
    76  	// TODO: use test server to load from a fixture
    77  	task := &structs.Task{
    78  		Name: "etcd",
    79  		Config: map[string]interface{}{
    80  			"trust_prefix": "coreos.com/etcd",
    81  			"image":        "coreos.com/etcd:v2.0.4",
    82  			"command":      "/etcd",
    83  		},
    84  	}
    85  
    86  	driverCtx := testDriverContext(task.Name)
    87  	ctx := testDriverExecContext(task, driverCtx)
    88  	d := NewRktDriver(driverCtx)
    89  	defer ctx.AllocDir.Destroy()
    90  
    91  	handle, err := d.Start(ctx, task)
    92  	if err != nil {
    93  		t.Fatalf("err: %v", err)
    94  	}
    95  	if handle == nil {
    96  		t.Fatalf("missing handle")
    97  	}
    98  
    99  	// Attempt to open
   100  	handle2, err := d.Open(ctx, handle.ID())
   101  	if err != nil {
   102  		t.Fatalf("err: %v", err)
   103  	}
   104  	if handle2 == nil {
   105  		t.Fatalf("missing handle")
   106  	}
   107  
   108  	// Clean up
   109  	if err := handle.Kill(); err != nil {
   110  		fmt.Printf("\nError killing Rkt test: %s", err)
   111  	}
   112  }
   113  
   114  func TestRktDriver_Start_Wait(t *testing.T) {
   115  	ctestutils.RktCompatible(t)
   116  	task := &structs.Task{
   117  		Name: "etcd",
   118  		Config: map[string]interface{}{
   119  			"trust_prefix": "coreos.com/etcd",
   120  			"image":        "coreos.com/etcd:v2.0.4",
   121  			"command":      "/etcd",
   122  			"args":         []string{"--version"},
   123  		},
   124  	}
   125  
   126  	driverCtx := testDriverContext(task.Name)
   127  	ctx := testDriverExecContext(task, driverCtx)
   128  	d := NewRktDriver(driverCtx)
   129  	defer ctx.AllocDir.Destroy()
   130  
   131  	handle, err := d.Start(ctx, task)
   132  	if err != nil {
   133  		t.Fatalf("err: %v", err)
   134  	}
   135  	if handle == nil {
   136  		t.Fatalf("missing handle")
   137  	}
   138  	defer handle.Kill()
   139  
   140  	// Update should be a no-op
   141  	err = handle.Update(task)
   142  	if err != nil {
   143  		t.Fatalf("err: %v", err)
   144  	}
   145  
   146  	select {
   147  	case res := <-handle.WaitCh():
   148  		if !res.Successful() {
   149  			t.Fatalf("err: %v", res)
   150  		}
   151  	case <-time.After(5 * time.Second):
   152  		t.Fatalf("timeout")
   153  	}
   154  }
   155  
   156  func TestRktDriver_Start_Wait_Skip_Trust(t *testing.T) {
   157  	ctestutils.RktCompatible(t)
   158  	task := &structs.Task{
   159  		Name: "etcd",
   160  		Config: map[string]interface{}{
   161  			"image":   "coreos.com/etcd:v2.0.4",
   162  			"command": "/etcd",
   163  			"args":    []string{"--version"},
   164  		},
   165  	}
   166  
   167  	driverCtx := testDriverContext(task.Name)
   168  	ctx := testDriverExecContext(task, driverCtx)
   169  	d := NewRktDriver(driverCtx)
   170  	defer ctx.AllocDir.Destroy()
   171  
   172  	handle, err := d.Start(ctx, task)
   173  	if err != nil {
   174  		t.Fatalf("err: %v", err)
   175  	}
   176  	if handle == nil {
   177  		t.Fatalf("missing handle")
   178  	}
   179  	defer handle.Kill()
   180  
   181  	// Update should be a no-op
   182  	err = handle.Update(task)
   183  	if err != nil {
   184  		t.Fatalf("err: %v", err)
   185  	}
   186  
   187  	select {
   188  	case res := <-handle.WaitCh():
   189  		if !res.Successful() {
   190  			t.Fatalf("err: %v", res)
   191  		}
   192  	case <-time.After(5 * time.Second):
   193  		t.Fatalf("timeout")
   194  	}
   195  }
   196  
   197  func TestRktDriver_Start_Wait_Logs(t *testing.T) {
   198  	ctestutils.RktCompatible(t)
   199  	task := &structs.Task{
   200  		Name: "etcd",
   201  		Config: map[string]interface{}{
   202  			"trust_prefix": "coreos.com/etcd",
   203  			"image":        "coreos.com/etcd:v2.0.4",
   204  			"command":      "/etcd",
   205  			"args":         []string{"--version"},
   206  		},
   207  	}
   208  
   209  	driverCtx := testDriverContext(task.Name)
   210  	ctx := testDriverExecContext(task, driverCtx)
   211  	d := NewRktDriver(driverCtx)
   212  	defer ctx.AllocDir.Destroy()
   213  
   214  	handle, err := d.Start(ctx, task)
   215  	if err != nil {
   216  		t.Fatalf("err: %v", err)
   217  	}
   218  	if handle == nil {
   219  		t.Fatalf("missing handle")
   220  	}
   221  	defer handle.Kill()
   222  
   223  	select {
   224  	case res := <-handle.WaitCh():
   225  		if !res.Successful() {
   226  			t.Fatalf("err: %v", res)
   227  		}
   228  	case <-time.After(5 * time.Second):
   229  		t.Fatalf("timeout")
   230  	}
   231  
   232  	taskDir, ok := ctx.AllocDir.TaskDirs[task.Name]
   233  	if !ok {
   234  		t.Fatalf("Could not find task directory for task: %v", task)
   235  	}
   236  	stdout := filepath.Join(taskDir, allocdir.TaskLocal, fmt.Sprintf("%v.stdout", task.Name))
   237  	data, err := ioutil.ReadFile(stdout)
   238  	if err != nil {
   239  		t.Fatalf("Failed to read tasks stdout: %v", err)
   240  	}
   241  
   242  	if len(data) == 0 {
   243  		t.Fatal("Task's stdout is empty")
   244  	}
   245  }