github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/client/driver/driver_test.go (about)

     1  package driver
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"path/filepath"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/nomad/client/allocdir"
    11  	"github.com/hashicorp/nomad/client/config"
    12  	"github.com/hashicorp/nomad/nomad/structs"
    13  )
    14  
    15  var basicResources = &structs.Resources{
    16  	CPU:      250,
    17  	MemoryMB: 256,
    18  	Networks: []*structs.NetworkResource{
    19  		&structs.NetworkResource{
    20  			IP:            "1.2.3.4",
    21  			ReservedPorts: []int{12345},
    22  			DynamicPorts:  []string{"HTTP"},
    23  		},
    24  	},
    25  }
    26  
    27  func testLogger() *log.Logger {
    28  	return log.New(os.Stderr, "", log.LstdFlags)
    29  }
    30  
    31  func testConfig() *config.Config {
    32  	conf := &config.Config{}
    33  	conf.StateDir = os.TempDir()
    34  	conf.AllocDir = os.TempDir()
    35  	return conf
    36  }
    37  
    38  func testDriverContext(task string) *DriverContext {
    39  	cfg := testConfig()
    40  	return NewDriverContext(task, cfg, cfg.Node, testLogger())
    41  }
    42  
    43  func testDriverExecContext(task *structs.Task, driverCtx *DriverContext) *ExecContext {
    44  	allocDir := allocdir.NewAllocDir(filepath.Join(driverCtx.config.AllocDir, structs.GenerateUUID()))
    45  	allocDir.Build([]*structs.Task{task})
    46  	ctx := NewExecContext(allocDir)
    47  	return ctx
    48  }
    49  
    50  func TestDriver_TaskEnvironmentVariables(t *testing.T) {
    51  	ctx := &ExecContext{}
    52  	task := &structs.Task{
    53  		Env: map[string]string{
    54  			"HELLO": "world",
    55  			"lorem": "ipsum",
    56  		},
    57  		Resources: &structs.Resources{
    58  			CPU:      1000,
    59  			MemoryMB: 500,
    60  			Networks: []*structs.NetworkResource{
    61  				&structs.NetworkResource{
    62  					IP:            "1.2.3.4",
    63  					ReservedPorts: []int{80, 443, 8080, 12345},
    64  					DynamicPorts:  []string{"admin", "5000"},
    65  				},
    66  			},
    67  		},
    68  		Meta: map[string]string{
    69  			"chocolate":  "cake",
    70  			"strawberry": "icecream",
    71  		},
    72  	}
    73  
    74  	env := TaskEnvironmentVariables(ctx, task)
    75  	exp := map[string]string{
    76  		"NOMAD_CPU_LIMIT":       "1000",
    77  		"NOMAD_MEMORY_LIMIT":    "500",
    78  		"NOMAD_IP":              "1.2.3.4",
    79  		"NOMAD_PORT_admin":      "8080",
    80  		"NOMAD_PORT_5000":       "12345",
    81  		"NOMAD_META_CHOCOLATE":  "cake",
    82  		"NOMAD_META_STRAWBERRY": "icecream",
    83  		"HELLO":                 "world",
    84  		"lorem":                 "ipsum",
    85  	}
    86  
    87  	act := env.Map()
    88  	if !reflect.DeepEqual(act, exp) {
    89  		t.Fatalf("TaskEnvironmentVariables(%#v, %#v) returned %#v; want %#v", ctx, task, act, exp)
    90  	}
    91  }