github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/internal_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/juju/clock/testclock"
    11  	"github.com/juju/errors"
    12  	mgotesting "github.com/juju/mgo/v3/testing"
    13  	"github.com/juju/names/v5"
    14  	jc "github.com/juju/testing/checkers"
    15  	"github.com/juju/utils/v3"
    16  	gc "gopkg.in/check.v1"
    17  
    18  	"github.com/juju/juju/cloud"
    19  	"github.com/juju/juju/core/constraints"
    20  	"github.com/juju/juju/environs"
    21  	"github.com/juju/juju/environs/config"
    22  	"github.com/juju/juju/environs/context"
    23  	"github.com/juju/juju/storage"
    24  	"github.com/juju/juju/storage/provider"
    25  	"github.com/juju/juju/storage/provider/dummy"
    26  	"github.com/juju/juju/testing"
    27  )
    28  
    29  var _ = gc.Suite(&internalStateSuite{})
    30  
    31  // internalStateSuite manages a *State instance for tests in the state
    32  // package (i.e. internal tests) that need it. It is similar to
    33  // state.testing.StateSuite but is duplicated to avoid cyclic imports.
    34  type internalStateSuite struct {
    35  	mgotesting.MgoSuite
    36  	testing.BaseSuite
    37  	controller *Controller
    38  	pool       *StatePool
    39  	state      *State
    40  	owner      names.UserTag
    41  	modelCount int
    42  }
    43  
    44  func (s *internalStateSuite) SetUpSuite(c *gc.C) {
    45  	s.MgoSuite.SetUpSuite(c)
    46  	s.BaseSuite.SetUpSuite(c)
    47  }
    48  
    49  func (s *internalStateSuite) TearDownSuite(c *gc.C) {
    50  	s.BaseSuite.TearDownSuite(c)
    51  	s.MgoSuite.TearDownSuite(c)
    52  }
    53  
    54  func (s *internalStateSuite) SetUpTest(c *gc.C) {
    55  	s.MgoSuite.SetUpTest(c)
    56  	s.BaseSuite.SetUpTest(c)
    57  
    58  	s.owner = names.NewLocalUserTag("test-admin")
    59  	modelCfg := testing.ModelConfig(c)
    60  	controllerCfg := testing.FakeControllerConfig()
    61  	ctlr, err := Initialize(InitializeParams{
    62  		Clock:            testclock.NewClock(testing.NonZeroTime()),
    63  		ControllerConfig: controllerCfg,
    64  		ControllerModelArgs: ModelArgs{
    65  			Type:        ModelTypeIAAS,
    66  			CloudName:   "dummy",
    67  			CloudRegion: "dummy-region",
    68  			Owner:       s.owner,
    69  			Config:      modelCfg,
    70  			StorageProviderRegistry: storage.ChainedProviderRegistry{
    71  				dummy.StorageProviders(),
    72  				provider.CommonStorageProviders(),
    73  			},
    74  		},
    75  		Cloud: cloud.Cloud{
    76  			Name:      "dummy",
    77  			Type:      "dummy",
    78  			AuthTypes: []cloud.AuthType{cloud.EmptyAuthType},
    79  			Regions: []cloud.Region{
    80  				{
    81  					Name: "dummy-region",
    82  				},
    83  			},
    84  		},
    85  		MongoSession:        s.Session,
    86  		WatcherPollInterval: 10 * time.Millisecond,
    87  		AdminPassword:       "dummy-secret",
    88  		NewPolicy: func(*State) Policy {
    89  			return internalStatePolicy{}
    90  		},
    91  	})
    92  	c.Assert(err, jc.ErrorIsNil)
    93  	s.controller = ctlr
    94  	s.pool = ctlr.StatePool()
    95  	s.state, err = ctlr.SystemState()
    96  	c.Assert(err, jc.ErrorIsNil)
    97  	s.AddCleanup(func(*gc.C) {
    98  		// Controller closes pool, pool closes all states.
    99  		s.controller.Close()
   100  	})
   101  }
   102  
   103  func (s *internalStateSuite) TearDownTest(c *gc.C) {
   104  	s.BaseSuite.TearDownTest(c)
   105  	s.MgoSuite.TearDownTest(c)
   106  }
   107  
   108  func (s *internalStateSuite) newState(c *gc.C) *State {
   109  	s.modelCount++
   110  	cfg := testing.CustomModelConfig(c, testing.Attrs{
   111  		"name": fmt.Sprintf("testmodel%d", s.modelCount),
   112  		"uuid": utils.MustNewUUID().String(),
   113  	})
   114  	_, st, err := s.controller.NewModel(ModelArgs{
   115  		Type:        ModelTypeIAAS,
   116  		CloudName:   "dummy",
   117  		CloudRegion: "dummy-region",
   118  		Config:      cfg,
   119  		Owner:       s.owner,
   120  		StorageProviderRegistry: storage.ChainedProviderRegistry{
   121  			dummy.StorageProviders(),
   122  			provider.CommonStorageProviders(),
   123  		},
   124  	})
   125  	c.Assert(err, jc.ErrorIsNil)
   126  	s.AddCleanup(func(*gc.C) { st.Close() })
   127  	return st
   128  }
   129  
   130  func (s *internalStateSuite) newCAASState(c *gc.C) *State {
   131  	s.modelCount++
   132  	cfg := testing.CustomModelConfig(c, testing.Attrs{
   133  		"name": fmt.Sprintf("testmodel%d", s.modelCount),
   134  		"uuid": utils.MustNewUUID().String(),
   135  	})
   136  	_, st, err := s.controller.NewModel(ModelArgs{
   137  		Type:        ModelTypeCAAS,
   138  		CloudName:   "dummy",
   139  		CloudRegion: "dummy-region",
   140  		Config:      cfg,
   141  		Owner:       s.owner,
   142  		StorageProviderRegistry: storage.ChainedProviderRegistry{
   143  			dummy.StorageProviders(),
   144  			provider.CommonStorageProviders(),
   145  		},
   146  	})
   147  	c.Assert(err, jc.ErrorIsNil)
   148  	s.AddCleanup(func(*gc.C) { st.Close() })
   149  	return st
   150  }
   151  
   152  type internalStatePolicy struct{}
   153  
   154  func (internalStatePolicy) Prechecker() (environs.InstancePrechecker, error) {
   155  	return nil, errors.NotImplementedf("Prechecker")
   156  }
   157  
   158  func (internalStatePolicy) ConfigValidator() (config.Validator, error) {
   159  	return nil, errors.NotImplementedf("ConfigValidator")
   160  }
   161  
   162  func (internalStatePolicy) ConstraintsValidator(context.ProviderCallContext) (constraints.Validator, error) {
   163  	return nil, errors.NotImplementedf("ConstraintsValidator")
   164  }
   165  
   166  func (internalStatePolicy) InstanceDistributor() (context.Distributor, error) {
   167  	return nil, errors.NotImplementedf("InstanceDistributor")
   168  }
   169  
   170  func (internalStatePolicy) StorageProviderRegistry() (storage.ProviderRegistry, error) {
   171  	return storage.ChainedProviderRegistry{
   172  		dummy.StorageProviders(),
   173  		provider.CommonStorageProviders(),
   174  	}, nil
   175  }
   176  
   177  func (internalStatePolicy) ProviderConfigSchemaSource(cloudName string) (config.ConfigSchemaSource, error) {
   178  	return nil, errors.NotImplementedf("ConfigSchemaSource")
   179  }