~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/uniter/runner/env_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012-2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package runner_test
 
5
 
 
6
import (
 
7
        "os"
 
8
        "strings"
 
9
 
 
10
        envtesting "github.com/juju/testing"
 
11
        jc "github.com/juju/testing/checkers"
 
12
        gc "gopkg.in/check.v1"
 
13
 
 
14
        "github.com/juju/juju/worker/uniter/runner"
 
15
)
 
16
 
 
17
type MergeEnvSuite struct {
 
18
        envtesting.IsolationSuite
 
19
}
 
20
 
 
21
var _ = gc.Suite(&MergeEnvSuite{})
 
22
 
 
23
func (e *MergeEnvSuite) TestMergeEnviron(c *gc.C) {
 
24
        // environment does not get fully cleared on Windows
 
25
        // when using testing.IsolationSuite
 
26
        origEnv := os.Environ()
 
27
        extraExpected := []string{
 
28
                "DUMMYVAR=foo",
 
29
                "DUMMYVAR2=bar",
 
30
                "NEWVAR=ImNew",
 
31
        }
 
32
        expectEnv := make([]string, 0, len(origEnv)+len(extraExpected))
 
33
 
 
34
        // os.Environ prepends some garbage on Windows that we need to strip out.
 
35
        // All the garbage starts and ends with = (for example "=C:=").
 
36
        for _, v := range origEnv {
 
37
                if !(strings.HasPrefix(v, "=") && strings.HasSuffix(v, "=")) {
 
38
                        expectEnv = append(expectEnv, v)
 
39
                }
 
40
        }
 
41
        expectEnv = append(expectEnv, extraExpected...)
 
42
        os.Setenv("DUMMYVAR2", "ChangeMe")
 
43
        os.Setenv("DUMMYVAR", "foo")
 
44
 
 
45
        newEnv := make([]string, 0, len(expectEnv))
 
46
        for _, v := range runner.MergeWindowsEnvironment([]string{"dummyvar2=bar", "NEWVAR=ImNew"}, os.Environ()) {
 
47
                if !(strings.HasPrefix(v, "=") && strings.HasSuffix(v, "=")) {
 
48
                        newEnv = append(newEnv, v)
 
49
                }
 
50
        }
 
51
        c.Assert(expectEnv, jc.SameContents, newEnv)
 
52
}
 
53
 
 
54
func (s *MergeEnvSuite) TestMergeEnvWin(c *gc.C) {
 
55
        initial := []string{"a=foo", "b=bar", "foo=val"}
 
56
        newValues := []string{"a=baz", "c=omg", "FOO=val2", "d=another"}
 
57
 
 
58
        created := runner.MergeWindowsEnvironment(newValues, initial)
 
59
        expected := []string{"a=baz", "b=bar", "c=omg", "foo=val2", "d=another"}
 
60
        c.Check(created, jc.SameContents, expected)
 
61
}