github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/package_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  	"testing"
     8  
     9  	"github.com/juju/mgo/v3/bson"
    10  	"github.com/juju/mgo/v3/txn"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/core/network"
    15  	"github.com/juju/juju/core/secrets"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  // TODO (manadart 2020-04-03)
    20  // The following refactoring should occur over time:
    21  // - Move export_test.go contents to this file.
    22  // - Rearrange packages (see state/testing) so that base suites can be
    23  //   implemented here without import cycling.
    24  // - Replace blanket exports with functions in suites here that supply
    25  //   behaviour to parent suites that require them.
    26  
    27  //go:generate go run go.uber.org/mock/mockgen -package state -destination migration_import_mock_test.go github.com/juju/juju/state TransactionRunner,StateDocumentFactory,DocModelNamespace
    28  //go:generate go run go.uber.org/mock/mockgen -package state -destination migration_import_input_mock_test.go github.com/juju/juju/state RemoteEntitiesInput,RelationNetworksInput,RemoteApplicationsInput,ApplicationOfferStateDocumentFactory,ApplicationOfferInput,ExternalControllerStateDocumentFactory,ExternalControllersInput,FirewallRulesInput,FirewallRulesOutput
    29  //go:generate go run go.uber.org/mock/mockgen -package state -destination migration_description_mock_test.go github.com/juju/description/v5 ApplicationOffer,ExternalController,FirewallRule,RemoteEntity,RelationNetwork,RemoteApplication,RemoteSpace,Status
    30  //go:generate go run go.uber.org/mock/mockgen -package mocks -destination mocks/operation_mock.go github.com/juju/juju/state ModelOperation
    31  //go:generate go run go.uber.org/mock/mockgen -package mocks -destination mocks/application_ports_mock.go github.com/juju/juju/state ApplicationPortRanges
    32  
    33  func TestPackage(t *testing.T) {
    34  	if !runStateTests {
    35  		t.Skip("skipping state tests since the skip_state_tests build tag was set")
    36  	}
    37  	coretesting.MgoTestPackage(t)
    38  }
    39  
    40  // SetModelTypeToCAAS can be called after SetUpTest for state suites.
    41  // It crudely just sets the model type to CAAS so that certain functionality
    42  // relying on the model type can be tested.
    43  func SetModelTypeToCAAS(c *gc.C, st *State, m *Model) {
    44  	ops := []txn.Op{{
    45  		C:      modelsC,
    46  		Id:     m.UUID(),
    47  		Update: bson.D{{"$set", bson.D{{"type", ModelTypeCAAS}}}},
    48  	}}
    49  
    50  	RunTransaction(c, st, ops)
    51  	c.Assert(m.refresh(m.UUID()), jc.ErrorIsNil)
    52  }
    53  
    54  // AddTestingApplicationWithEmptyBindings mimics an application
    55  // from an old version of Juju, with no bindings entry.
    56  func AddTestingApplicationWithEmptyBindings(c *gc.C, st *State, name string, ch *Charm) *Application {
    57  	app := addTestingApplication(c, addTestingApplicationParams{
    58  		st:   st,
    59  		name: name,
    60  		ch:   ch,
    61  	})
    62  
    63  	RunTransaction(c, st, []txn.Op{removeEndpointBindingsOp(app.globalKey())})
    64  	return app
    65  }
    66  
    67  // RunTransaction exposes the transaction running capability of State.
    68  func RunTransaction(c *gc.C, st *State, ops []txn.Op) {
    69  	c.Assert(st.db().RunTransaction(ops), jc.ErrorIsNil)
    70  }
    71  
    72  // MustOpenUnitPortRange ensures that the provided port range is opened
    73  // for the specified unit and endpoint combination on the provided machine.
    74  func MustOpenUnitPortRange(c *gc.C, st *State, machine *Machine, unitName, endpointName string, portRange network.PortRange) {
    75  	MustOpenUnitPortRanges(c, st, machine, unitName, endpointName, []network.PortRange{portRange})
    76  }
    77  
    78  // MustOpenUnitPortRanges ensures that the provided port ranges are opened
    79  // for the specified unit and endpoint combination on the provided machine.
    80  func MustOpenUnitPortRanges(c *gc.C, st *State, machine *Machine, unitName, endpointName string, portRanges []network.PortRange) {
    81  	machPortRanges, err := machine.OpenedPortRanges()
    82  	c.Assert(err, jc.ErrorIsNil)
    83  
    84  	unitPortRanges := machPortRanges.ForUnit(unitName)
    85  	for _, pr := range portRanges {
    86  		unitPortRanges.Open(endpointName, pr)
    87  	}
    88  	c.Assert(st.ApplyOperation(machPortRanges.Changes()), jc.ErrorIsNil)
    89  }
    90  
    91  // MustCloseUnitPortRange ensures that the provided port range is closed
    92  // for the specified unit and endpoint combination on the provided machine.
    93  func MustCloseUnitPortRange(c *gc.C, st *State, machine *Machine, unitName, endpointName string, portRange network.PortRange) {
    94  	MustCloseUnitPortRanges(c, st, machine, unitName, endpointName, []network.PortRange{portRange})
    95  }
    96  
    97  // MustCloseUnitPortRanges ensures that the provided port ranges are closed
    98  // for the specified unit and endpoint combination on the provided machine.
    99  func MustCloseUnitPortRanges(c *gc.C, st *State, machine *Machine, unitName, endpointName string, portRanges []network.PortRange) {
   100  	machPortRanges, err := machine.OpenedPortRanges()
   101  	c.Assert(err, jc.ErrorIsNil)
   102  
   103  	unitPortRanges := machPortRanges.ForUnit(unitName)
   104  	for _, pr := range portRanges {
   105  		unitPortRanges.Close(endpointName, pr)
   106  	}
   107  	c.Assert(st.ApplyOperation(machPortRanges.Changes()), jc.ErrorIsNil)
   108  }
   109  
   110  func (st *State) ReadBackendRefCount(backendID string) (int, error) {
   111  	refCountCollection, ccloser := st.db().GetCollection(globalRefcountsC)
   112  	defer ccloser()
   113  
   114  	key := secretBackendRefCountKey(backendID)
   115  	return nsRefcounts.read(refCountCollection, key)
   116  }
   117  
   118  func (st *State) IsSecretRevisionObsolete(c *gc.C, uri *secrets.URI, rev int) bool {
   119  	col, closer := st.db().GetCollection(secretRevisionsC)
   120  	defer closer()
   121  	var doc secretRevisionDoc
   122  	err := col.FindId(secretRevisionKey(uri, rev)).One(&doc)
   123  	c.Assert(err, jc.ErrorIsNil)
   124  	return doc.Obsolete
   125  }