~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/worker/environ_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package worker_test
 
2
 
 
3
import (
 
4
        . "launchpad.net/gocheck"
 
5
        "launchpad.net/juju-core/environs"
 
6
        "launchpad.net/juju-core/environs/config"
 
7
        "launchpad.net/juju-core/juju/testing"
 
8
        "launchpad.net/juju-core/state"
 
9
        coretesting "launchpad.net/juju-core/testing"
 
10
        "launchpad.net/juju-core/worker"
 
11
        "launchpad.net/tomb"
 
12
        stdtesting "testing"
 
13
)
 
14
 
 
15
type suite struct {
 
16
        testing.JujuConnSuite
 
17
}
 
18
 
 
19
var _ = Suite(&suite{})
 
20
 
 
21
func TestPackage(t *stdtesting.T) {
 
22
        coretesting.MgoTestPackage(t)
 
23
}
 
24
 
 
25
func (s *suite) TestStop(c *C) {
 
26
        w := s.State.WatchEnvironConfig()
 
27
        defer stopWatcher(c, w)
 
28
        stop := make(chan struct{})
 
29
        done := make(chan error)
 
30
        go func() {
 
31
                env, err := worker.WaitForEnviron(w, stop)
 
32
                c.Check(env, IsNil)
 
33
                done <- err
 
34
        }()
 
35
        close(stop)
 
36
        c.Assert(<-done, Equals, tomb.ErrDying)
 
37
}
 
38
 
 
39
func stopWatcher(c *C, w *state.EnvironConfigWatcher) {
 
40
        err := w.Stop()
 
41
        c.Check(err, IsNil)
 
42
}
 
43
 
 
44
func (s *suite) TestInvalidConfig(c *C) {
 
45
        // Create an invalid config by taking the current config and
 
46
        // tweaking the provider type.
 
47
        cfg, err := s.State.EnvironConfig()
 
48
        c.Assert(err, IsNil)
 
49
        m := cfg.AllAttrs()
 
50
        m["type"] = "unknown"
 
51
        invalidCfg, err := config.New(m)
 
52
        c.Assert(err, IsNil)
 
53
 
 
54
        err = s.State.SetEnvironConfig(invalidCfg)
 
55
        c.Assert(err, IsNil)
 
56
 
 
57
        w := s.State.WatchEnvironConfig()
 
58
        defer stopWatcher(c, w)
 
59
        done := make(chan environs.Environ)
 
60
        go func() {
 
61
                env, err := worker.WaitForEnviron(w, nil)
 
62
                c.Check(err, IsNil)
 
63
                done <- env
 
64
        }()
 
65
        // Wait for the loop to process the invalid configuratrion
 
66
        <-worker.LoadedInvalid
 
67
 
 
68
        // Then load a valid configuration back in.
 
69
        m = cfg.AllAttrs()
 
70
        m["secret"] = "environ_test"
 
71
        validCfg, err := config.New(m)
 
72
        c.Assert(err, IsNil)
 
73
 
 
74
        err = s.State.SetEnvironConfig(validCfg)
 
75
        c.Assert(err, IsNil)
 
76
        s.State.StartSync()
 
77
 
 
78
        env := <-done
 
79
        c.Assert(env, NotNil)
 
80
        c.Assert(env.Config().AllAttrs()["secret"], Equals, "environ_test")
 
81
}