github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/cmd/jujud/deploy_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "reflect" 8 "sort" 9 "sync" 10 "time" 11 12 gc "launchpad.net/gocheck" 13 14 "launchpad.net/juju-core/agent" 15 "launchpad.net/juju-core/state" 16 "launchpad.net/juju-core/testing" 17 "launchpad.net/juju-core/utils/set" 18 ) 19 20 // fakeManager allows us to test deployments without actually deploying units 21 // to the local system. It's slightly uncomfortably complex because it needs 22 // to use the *state.State opened within the agent's runOnce -- not the one 23 // created in the test -- to StartSync and cause the task to actually start 24 // a sync and observe changes to the set of desired units (and thereby run 25 // deployment tests in a reasonable amount of time). 26 type fakeContext struct { 27 mu sync.Mutex 28 deployed set.Strings 29 st *state.State 30 agentConfig agent.Config 31 inited chan struct{} 32 } 33 34 func (ctx *fakeContext) DeployUnit(unitName, _ string) error { 35 ctx.mu.Lock() 36 ctx.deployed.Add(unitName) 37 ctx.mu.Unlock() 38 return nil 39 } 40 41 func (ctx *fakeContext) RecallUnit(unitName string) error { 42 ctx.mu.Lock() 43 ctx.deployed.Remove(unitName) 44 ctx.mu.Unlock() 45 return nil 46 } 47 48 func (ctx *fakeContext) DeployedUnits() ([]string, error) { 49 ctx.mu.Lock() 50 defer ctx.mu.Unlock() 51 if ctx.deployed.IsEmpty() { 52 return nil, nil 53 } 54 return ctx.deployed.SortedValues(), nil 55 } 56 57 func (ctx *fakeContext) waitDeployed(c *gc.C, want ...string) { 58 sort.Strings(want) 59 timeout := time.After(testing.LongWait) 60 select { 61 case <-timeout: 62 c.Fatalf("manager never initialized") 63 case <-ctx.inited: 64 for { 65 ctx.st.StartSync() 66 select { 67 case <-timeout: 68 got, err := ctx.DeployedUnits() 69 c.Assert(err, gc.IsNil) 70 c.Fatalf("unexpected units: %#v", got) 71 case <-time.After(testing.ShortWait): 72 got, err := ctx.DeployedUnits() 73 c.Assert(err, gc.IsNil) 74 if reflect.DeepEqual(got, want) { 75 return 76 } 77 } 78 } 79 } 80 } 81 82 func (ctx *fakeContext) AgentConfig() agent.Config { 83 return ctx.agentConfig 84 }