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