launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/worker/instancepoller/observer_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // TODO(wallyworld) - move to instancepoller_test 5 package instancepoller 6 7 import ( 8 "strings" 9 "time" 10 11 "github.com/loggo/loggo" 12 gc "launchpad.net/gocheck" 13 14 "launchpad.net/juju-core/juju/testing" 15 coretesting "launchpad.net/juju-core/testing" 16 ) 17 18 var _ = gc.Suite(&observerSuite{}) 19 20 type observerSuite struct { 21 testing.JujuConnSuite 22 } 23 24 func (s *observerSuite) TestWaitsForValidEnviron(c *gc.C) { 25 obs, err := newEnvironObserver(s.State, nil) 26 c.Assert(err, gc.IsNil) 27 env := obs.Environ() 28 stateConfig, err := s.State.EnvironConfig() 29 c.Assert(err, gc.IsNil) 30 c.Assert(env.Config().AllAttrs(), gc.DeepEquals, stateConfig.AllAttrs()) 31 } 32 33 func (s *observerSuite) TestEnvironmentChanges(c *gc.C) { 34 originalConfig, err := s.State.EnvironConfig() 35 c.Assert(err, gc.IsNil) 36 37 logc := make(logChan, 1009) 38 c.Assert(loggo.RegisterWriter("testing", logc, loggo.WARNING), gc.IsNil) 39 defer loggo.RemoveWriter("testing") 40 41 obs, err := newEnvironObserver(s.State, nil) 42 c.Assert(err, gc.IsNil) 43 44 env := obs.Environ() 45 c.Assert(env.Config().AllAttrs(), gc.DeepEquals, originalConfig.AllAttrs()) 46 47 var oldType string 48 // Change to an invalid configuration and check 49 // that the observer's environment remains the same. 50 testing.ChangeEnvironConfig(c, s.State, func(attrs coretesting.Attrs) coretesting.Attrs { 51 oldType = attrs["type"].(string) 52 return attrs.Merge(coretesting.Attrs{ 53 "type": "invalid", 54 }) 55 }) 56 s.State.StartSync() 57 58 // Wait for the observer to register the invalid environment 59 timeout := time.After(coretesting.LongWait) 60 loop: 61 for { 62 select { 63 case msg := <-logc: 64 if strings.Contains(msg, "error creating Environ") { 65 break loop 66 } 67 case <-timeout: 68 c.Fatalf("timed out waiting to see broken environment") 69 } 70 } 71 // Check that the returned environ is still the same. 72 env = obs.Environ() 73 c.Assert(env.Config().AllAttrs(), gc.DeepEquals, originalConfig.AllAttrs()) 74 75 // Change the environment back to a valid configuration 76 // with a different name and check that we see it. 77 testing.ChangeEnvironConfig(c, s.State, func(attrs coretesting.Attrs) coretesting.Attrs { 78 return attrs.Merge(coretesting.Attrs{ 79 "type": oldType, 80 "name": "a-new-name", 81 }) 82 }) 83 s.State.StartSync() 84 85 for a := coretesting.LongAttempt.Start(); a.Next(); { 86 env := obs.Environ() 87 if !a.HasNext() { 88 c.Fatalf("timed out waiting for new environ") 89 } 90 if env.Config().Name() == "a-new-name" { 91 break 92 } 93 } 94 } 95 96 type logChan chan string 97 98 func (logc logChan) Write(level loggo.Level, name, filename string, line int, timestamp time.Time, message string) { 99 logc <- message 100 }