github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/conn_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	"github.com/juju/utils"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  	"gopkg.in/mgo.v2"
    12  
    13  	"github.com/juju/juju/provider/dummy"
    14  	"github.com/juju/juju/state"
    15  	statetesting "github.com/juju/juju/state/testing"
    16  	"github.com/juju/juju/storage"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  // ConnSuite provides the infrastructure for all other
    21  // test suites (StateSuite, CharmSuite, MachineSuite, etc).
    22  type ConnSuite struct {
    23  	statetesting.StateSuite
    24  	annotations  *mgo.Collection
    25  	charms       *mgo.Collection
    26  	machines     *mgo.Collection
    27  	instanceData *mgo.Collection
    28  	relations    *mgo.Collection
    29  	services     *mgo.Collection
    30  	units        *mgo.Collection
    31  	controllers  *mgo.Collection
    32  	policy       statetesting.MockPolicy
    33  	modelTag     names.ModelTag
    34  }
    35  
    36  func (cs *ConnSuite) SetUpTest(c *gc.C) {
    37  	c.Log("SetUpTest")
    38  
    39  	cs.policy = statetesting.MockPolicy{
    40  		GetStorageProviderRegistry: func() (storage.ProviderRegistry, error) {
    41  			return dummy.StorageProviders(), nil
    42  		},
    43  	}
    44  	cs.StateSuite.NewPolicy = func(*state.State) state.Policy {
    45  		return &cs.policy
    46  	}
    47  
    48  	cs.StateSuite.SetUpTest(c)
    49  
    50  	cs.modelTag = cs.State.ModelTag()
    51  
    52  	jujuDB := cs.MgoSuite.Session.DB("juju")
    53  	cs.annotations = jujuDB.C("annotations")
    54  	cs.charms = jujuDB.C("charms")
    55  	cs.machines = jujuDB.C("machines")
    56  	cs.instanceData = jujuDB.C("instanceData")
    57  	cs.relations = jujuDB.C("relations")
    58  	cs.services = jujuDB.C("applications")
    59  	cs.units = jujuDB.C("units")
    60  	cs.controllers = jujuDB.C("controllers")
    61  
    62  	c.Log("SetUpTest done")
    63  }
    64  
    65  func (s *ConnSuite) AddTestingCharm(c *gc.C, name string) *state.Charm {
    66  	return state.AddTestingCharm(c, s.State, name)
    67  }
    68  
    69  func (s *ConnSuite) AddTestingService(c *gc.C, name string, ch *state.Charm) *state.Application {
    70  	return state.AddTestingService(c, s.State, name, ch)
    71  }
    72  
    73  func (s *ConnSuite) AddTestingServiceWithStorage(c *gc.C, name string, ch *state.Charm, storage map[string]state.StorageConstraints) *state.Application {
    74  	return state.AddTestingServiceWithStorage(c, s.State, name, ch, storage)
    75  }
    76  
    77  func (s *ConnSuite) AddTestingServiceWithBindings(c *gc.C, name string, ch *state.Charm, bindings map[string]string) *state.Application {
    78  	return state.AddTestingServiceWithBindings(c, s.State, name, ch, bindings)
    79  }
    80  
    81  func (s *ConnSuite) AddSeriesCharm(c *gc.C, name, series string) *state.Charm {
    82  	return state.AddCustomCharm(c, s.State, name, "", "", series, -1)
    83  }
    84  
    85  // AddConfigCharm clones a testing charm, replaces its config with
    86  // the given YAML string and adds it to the state, using the given
    87  // revision.
    88  func (s *ConnSuite) AddConfigCharm(c *gc.C, name, configYaml string, revision int) *state.Charm {
    89  	return state.AddCustomCharm(c, s.State, name, "config.yaml", configYaml, "quantal", revision)
    90  }
    91  
    92  // AddActionsCharm clones a testing charm, replaces its actions schema with
    93  // the given YAML, and adds it to the state, using the given revision.
    94  func (s *ConnSuite) AddActionsCharm(c *gc.C, name, actionsYaml string, revision int) *state.Charm {
    95  	return state.AddCustomCharm(c, s.State, name, "actions.yaml", actionsYaml, "quantal", revision)
    96  }
    97  
    98  // AddMetaCharm clones a testing charm, replaces its metadata with the
    99  // given YAML string and adds it to the state, using the given revision.
   100  func (s *ConnSuite) AddMetaCharm(c *gc.C, name, metaYaml string, revision int) *state.Charm {
   101  	return state.AddCustomCharm(c, s.State, name, "metadata.yaml", metaYaml, "quantal", revision)
   102  }
   103  
   104  // AddMetricsCharm clones a testing charm, replaces its metrics declaration with the
   105  // given YAML string and adds it to the state, using the given revision.
   106  func (s *ConnSuite) AddMetricsCharm(c *gc.C, name, metricsYaml string, revision int) *state.Charm {
   107  	return state.AddCustomCharm(c, s.State, name, "metrics.yaml", metricsYaml, "quantal", revision)
   108  }
   109  
   110  // NewStateForModelNamed returns an new model with the given modelName, which
   111  // has a unique UUID, and does not need to be closed when the test completes.
   112  func (s *ConnSuite) NewStateForModelNamed(c *gc.C, modelName string) *state.State {
   113  	cfg := testing.CustomModelConfig(c, testing.Attrs{
   114  		"name": modelName,
   115  		"uuid": utils.MustNewUUID().String(),
   116  	})
   117  	otherOwner := names.NewLocalUserTag("test-admin")
   118  	_, otherState, err := s.State.NewModel(state.ModelArgs{
   119  		CloudName: "dummy", CloudRegion: "dummy-region", Config: cfg, Owner: otherOwner,
   120  		StorageProviderRegistry: storage.StaticProviderRegistry{},
   121  	})
   122  
   123  	c.Assert(err, jc.ErrorIsNil)
   124  	s.AddCleanup(func(*gc.C) { otherState.Close() })
   125  	return otherState
   126  }