github.com/scorpionis/docker@v1.6.0-rc7/engine/env_test.go (about)

     1  package engine
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/docker/docker/pkg/testutils"
    10  )
    11  
    12  func TestEnvLenZero(t *testing.T) {
    13  	env := &Env{}
    14  	if env.Len() != 0 {
    15  		t.Fatalf("%d", env.Len())
    16  	}
    17  }
    18  
    19  func TestEnvLenNotZero(t *testing.T) {
    20  	env := &Env{}
    21  	env.Set("foo", "bar")
    22  	env.Set("ga", "bu")
    23  	if env.Len() != 2 {
    24  		t.Fatalf("%d", env.Len())
    25  	}
    26  }
    27  
    28  func TestEnvLenDup(t *testing.T) {
    29  	env := &Env{
    30  		"foo=bar",
    31  		"foo=baz",
    32  		"a=b",
    33  	}
    34  	// len(env) != env.Len()
    35  	if env.Len() != 2 {
    36  		t.Fatalf("%d", env.Len())
    37  	}
    38  }
    39  
    40  func TestEnvGetDup(t *testing.T) {
    41  	env := &Env{
    42  		"foo=bar",
    43  		"foo=baz",
    44  		"foo=bif",
    45  	}
    46  	expected := "bif"
    47  	if v := env.Get("foo"); v != expected {
    48  		t.Fatalf("expect %q, got %q", expected, v)
    49  	}
    50  }
    51  
    52  func TestNewJob(t *testing.T) {
    53  	job := mkJob(t, "dummy", "--level=awesome")
    54  	if job.Name != "dummy" {
    55  		t.Fatalf("Wrong job name: %s", job.Name)
    56  	}
    57  	if len(job.Args) != 1 {
    58  		t.Fatalf("Wrong number of job arguments: %d", len(job.Args))
    59  	}
    60  	if job.Args[0] != "--level=awesome" {
    61  		t.Fatalf("Wrong job arguments: %s", job.Args[0])
    62  	}
    63  }
    64  
    65  func TestSetenv(t *testing.T) {
    66  	job := mkJob(t, "dummy")
    67  	job.Setenv("foo", "bar")
    68  	if val := job.Getenv("foo"); val != "bar" {
    69  		t.Fatalf("Getenv returns incorrect value: %s", val)
    70  	}
    71  
    72  	job.Setenv("bar", "")
    73  	if val := job.Getenv("bar"); val != "" {
    74  		t.Fatalf("Getenv returns incorrect value: %s", val)
    75  	}
    76  	if val := job.Getenv("nonexistent"); val != "" {
    77  		t.Fatalf("Getenv returns incorrect value: %s", val)
    78  	}
    79  }
    80  
    81  func TestSetenvBool(t *testing.T) {
    82  	job := mkJob(t, "dummy")
    83  	job.SetenvBool("foo", true)
    84  	if val := job.GetenvBool("foo"); !val {
    85  		t.Fatalf("GetenvBool returns incorrect value: %t", val)
    86  	}
    87  
    88  	job.SetenvBool("bar", false)
    89  	if val := job.GetenvBool("bar"); val {
    90  		t.Fatalf("GetenvBool returns incorrect value: %t", val)
    91  	}
    92  
    93  	if val := job.GetenvBool("nonexistent"); val {
    94  		t.Fatalf("GetenvBool returns incorrect value: %t", val)
    95  	}
    96  }
    97  
    98  func TestSetenvTime(t *testing.T) {
    99  	job := mkJob(t, "dummy")
   100  
   101  	now := time.Now()
   102  	job.SetenvTime("foo", now)
   103  	if val, err := job.GetenvTime("foo"); err != nil {
   104  		t.Fatalf("GetenvTime failed to parse: %v", err)
   105  	} else {
   106  		nowStr := now.Format(time.RFC3339)
   107  		valStr := val.Format(time.RFC3339)
   108  		if nowStr != valStr {
   109  			t.Fatalf("GetenvTime returns incorrect value: %s, Expected: %s", valStr, nowStr)
   110  		}
   111  	}
   112  
   113  	job.Setenv("bar", "Obviously I'm not a date")
   114  	if val, err := job.GetenvTime("bar"); err == nil {
   115  		t.Fatalf("GetenvTime was supposed to fail, instead returned: %s", val)
   116  	}
   117  }
   118  
   119  func TestSetenvInt(t *testing.T) {
   120  	job := mkJob(t, "dummy")
   121  
   122  	job.SetenvInt("foo", -42)
   123  	if val := job.GetenvInt("foo"); val != -42 {
   124  		t.Fatalf("GetenvInt returns incorrect value: %d", val)
   125  	}
   126  
   127  	job.SetenvInt("bar", 42)
   128  	if val := job.GetenvInt("bar"); val != 42 {
   129  		t.Fatalf("GetenvInt returns incorrect value: %d", val)
   130  	}
   131  	if val := job.GetenvInt("nonexistent"); val != 0 {
   132  		t.Fatalf("GetenvInt returns incorrect value: %d", val)
   133  	}
   134  }
   135  
   136  func TestSetenvList(t *testing.T) {
   137  	job := mkJob(t, "dummy")
   138  
   139  	job.SetenvList("foo", []string{"bar"})
   140  	if val := job.GetenvList("foo"); len(val) != 1 || val[0] != "bar" {
   141  		t.Fatalf("GetenvList returns incorrect value: %v", val)
   142  	}
   143  
   144  	job.SetenvList("bar", nil)
   145  	if val := job.GetenvList("bar"); val != nil {
   146  		t.Fatalf("GetenvList returns incorrect value: %v", val)
   147  	}
   148  	if val := job.GetenvList("nonexistent"); val != nil {
   149  		t.Fatalf("GetenvList returns incorrect value: %v", val)
   150  	}
   151  }
   152  
   153  func TestEnviron(t *testing.T) {
   154  	job := mkJob(t, "dummy")
   155  	job.Setenv("foo", "bar")
   156  	val, exists := job.Environ()["foo"]
   157  	if !exists {
   158  		t.Fatalf("foo not found in the environ")
   159  	}
   160  	if val != "bar" {
   161  		t.Fatalf("bar not found in the environ")
   162  	}
   163  }
   164  
   165  func TestMultiMap(t *testing.T) {
   166  	e := &Env{}
   167  	e.Set("foo", "bar")
   168  	e.Set("bar", "baz")
   169  	e.Set("hello", "world")
   170  	m := e.MultiMap()
   171  	e2 := &Env{}
   172  	e2.Set("old_key", "something something something")
   173  	e2.InitMultiMap(m)
   174  	if v := e2.Get("old_key"); v != "" {
   175  		t.Fatalf("%#v", v)
   176  	}
   177  	if v := e2.Get("bar"); v != "baz" {
   178  		t.Fatalf("%#v", v)
   179  	}
   180  	if v := e2.Get("hello"); v != "world" {
   181  		t.Fatalf("%#v", v)
   182  	}
   183  }
   184  
   185  func testMap(l int) [][2]string {
   186  	res := make([][2]string, l)
   187  	for i := 0; i < l; i++ {
   188  		t := [2]string{testutils.RandomString(5), testutils.RandomString(20)}
   189  		res[i] = t
   190  	}
   191  	return res
   192  }
   193  
   194  func BenchmarkSet(b *testing.B) {
   195  	fix := testMap(100)
   196  	b.ResetTimer()
   197  	for i := 0; i < b.N; i++ {
   198  		env := &Env{}
   199  		for _, kv := range fix {
   200  			env.Set(kv[0], kv[1])
   201  		}
   202  	}
   203  }
   204  
   205  func BenchmarkSetJson(b *testing.B) {
   206  	fix := testMap(100)
   207  	type X struct {
   208  		f string
   209  	}
   210  	b.ResetTimer()
   211  	for i := 0; i < b.N; i++ {
   212  		env := &Env{}
   213  		for _, kv := range fix {
   214  			if err := env.SetJson(kv[0], X{kv[1]}); err != nil {
   215  				b.Fatal(err)
   216  			}
   217  		}
   218  	}
   219  }
   220  
   221  func BenchmarkGet(b *testing.B) {
   222  	fix := testMap(100)
   223  	env := &Env{}
   224  	for _, kv := range fix {
   225  		env.Set(kv[0], kv[1])
   226  	}
   227  	b.ResetTimer()
   228  	for i := 0; i < b.N; i++ {
   229  		for _, kv := range fix {
   230  			env.Get(kv[0])
   231  		}
   232  	}
   233  }
   234  
   235  func BenchmarkGetJson(b *testing.B) {
   236  	fix := testMap(100)
   237  	env := &Env{}
   238  	type X struct {
   239  		f string
   240  	}
   241  	for _, kv := range fix {
   242  		env.SetJson(kv[0], X{kv[1]})
   243  	}
   244  	b.ResetTimer()
   245  	for i := 0; i < b.N; i++ {
   246  		for _, kv := range fix {
   247  			if err := env.GetJson(kv[0], &X{}); err != nil {
   248  				b.Fatal(err)
   249  			}
   250  		}
   251  	}
   252  }
   253  
   254  func BenchmarkEncode(b *testing.B) {
   255  	fix := testMap(100)
   256  	env := &Env{}
   257  	type X struct {
   258  		f string
   259  	}
   260  	// half a json
   261  	for i, kv := range fix {
   262  		if i%2 != 0 {
   263  			if err := env.SetJson(kv[0], X{kv[1]}); err != nil {
   264  				b.Fatal(err)
   265  			}
   266  			continue
   267  		}
   268  		env.Set(kv[0], kv[1])
   269  	}
   270  	var writer bytes.Buffer
   271  	b.ResetTimer()
   272  	for i := 0; i < b.N; i++ {
   273  		env.Encode(&writer)
   274  		writer.Reset()
   275  	}
   276  }
   277  
   278  func BenchmarkDecode(b *testing.B) {
   279  	fix := testMap(100)
   280  	env := &Env{}
   281  	type X struct {
   282  		f string
   283  	}
   284  	// half a json
   285  	for i, kv := range fix {
   286  		if i%2 != 0 {
   287  			if err := env.SetJson(kv[0], X{kv[1]}); err != nil {
   288  				b.Fatal(err)
   289  			}
   290  			continue
   291  		}
   292  		env.Set(kv[0], kv[1])
   293  	}
   294  	var writer bytes.Buffer
   295  	env.Encode(&writer)
   296  	denv := &Env{}
   297  	reader := bytes.NewReader(writer.Bytes())
   298  	b.ResetTimer()
   299  	for i := 0; i < b.N; i++ {
   300  		err := denv.Decode(reader)
   301  		if err != nil {
   302  			b.Fatal(err)
   303  		}
   304  		reader.Seek(0, 0)
   305  	}
   306  }
   307  
   308  func TestLongNumbers(t *testing.T) {
   309  	type T struct {
   310  		TestNum int64
   311  	}
   312  	v := T{67108864}
   313  	var buf bytes.Buffer
   314  	e := &Env{}
   315  	e.SetJson("Test", v)
   316  	if err := e.Encode(&buf); err != nil {
   317  		t.Fatal(err)
   318  	}
   319  	res := make(map[string]T)
   320  	if err := json.Unmarshal(buf.Bytes(), &res); err != nil {
   321  		t.Fatal(err)
   322  	}
   323  	if res["Test"].TestNum != v.TestNum {
   324  		t.Fatalf("TestNum %d, expected %d", res["Test"].TestNum, v.TestNum)
   325  	}
   326  }
   327  
   328  func TestLongNumbersArray(t *testing.T) {
   329  	type T struct {
   330  		TestNum []int64
   331  	}
   332  	v := T{[]int64{67108864}}
   333  	var buf bytes.Buffer
   334  	e := &Env{}
   335  	e.SetJson("Test", v)
   336  	if err := e.Encode(&buf); err != nil {
   337  		t.Fatal(err)
   338  	}
   339  	res := make(map[string]T)
   340  	if err := json.Unmarshal(buf.Bytes(), &res); err != nil {
   341  		t.Fatal(err)
   342  	}
   343  	if res["Test"].TestNum[0] != v.TestNum[0] {
   344  		t.Fatalf("TestNum %d, expected %d", res["Test"].TestNum, v.TestNum)
   345  	}
   346  }