~nskaggs/+junk/xenial-test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package runner_test

import (
	"os"
	"strings"

	envtesting "github.com/juju/testing"
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/juju/worker/uniter/runner"
)

type MergeEnvSuite struct {
	envtesting.IsolationSuite
}

var _ = gc.Suite(&MergeEnvSuite{})

func (e *MergeEnvSuite) TestMergeEnviron(c *gc.C) {
	// environment does not get fully cleared on Windows
	// when using testing.IsolationSuite
	origEnv := os.Environ()
	extraExpected := []string{
		"DUMMYVAR=foo",
		"DUMMYVAR2=bar",
		"NEWVAR=ImNew",
	}
	expectEnv := make([]string, 0, len(origEnv)+len(extraExpected))

	// os.Environ prepends some garbage on Windows that we need to strip out.
	// All the garbage starts and ends with = (for example "=C:=").
	for _, v := range origEnv {
		if !(strings.HasPrefix(v, "=") && strings.HasSuffix(v, "=")) {
			expectEnv = append(expectEnv, v)
		}
	}
	expectEnv = append(expectEnv, extraExpected...)
	os.Setenv("DUMMYVAR2", "ChangeMe")
	os.Setenv("DUMMYVAR", "foo")

	newEnv := make([]string, 0, len(expectEnv))
	for _, v := range runner.MergeWindowsEnvironment([]string{"dummyvar2=bar", "NEWVAR=ImNew"}, os.Environ()) {
		if !(strings.HasPrefix(v, "=") && strings.HasSuffix(v, "=")) {
			newEnv = append(newEnv, v)
		}
	}
	c.Assert(expectEnv, jc.SameContents, newEnv)
}

func (s *MergeEnvSuite) TestMergeEnvWin(c *gc.C) {
	initial := []string{"a=foo", "b=bar", "foo=val"}
	newValues := []string{"a=baz", "c=omg", "FOO=val2", "d=another"}

	created := runner.MergeWindowsEnvironment(newValues, initial)
	expected := []string{"a=baz", "b=bar", "c=omg", "foo=val2", "d=another"}
	c.Check(created, jc.SameContents, expected)
}