github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/jujud/agent/deploy_test.go (about)

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