本文整理汇总了Golang中github.com/juju/juju/api/uniter.NewState函数的典型用法代码示例。如果您正苦于以下问题:Golang NewState函数的具体用法?Golang NewState怎么用?Golang NewState使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewState函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestUnitStorageAttachments
func (s *storageSuite) TestUnitStorageAttachments(c *gc.C) {
storageAttachmentIds := []params.StorageAttachmentId{{
StorageTag: "storage-whatever-0",
UnitTag: "unit-mysql-0",
}}
var called bool
apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
c.Check(objType, gc.Equals, "Uniter")
c.Check(version, gc.Equals, 2)
c.Check(id, gc.Equals, "")
c.Check(request, gc.Equals, "UnitStorageAttachments")
c.Check(arg, gc.DeepEquals, params.Entities{
Entities: []params.Entity{{Tag: "unit-mysql-0"}},
})
c.Assert(result, gc.FitsTypeOf, ¶ms.StorageAttachmentIdsResults{})
*(result.(*params.StorageAttachmentIdsResults)) = params.StorageAttachmentIdsResults{
Results: []params.StorageAttachmentIdsResult{{
Result: params.StorageAttachmentIds{storageAttachmentIds},
}},
}
called = true
return nil
})
st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
attachmentIds, err := st.UnitStorageAttachments(names.NewUnitTag("mysql/0"))
c.Check(err, jc.ErrorIsNil)
c.Check(called, jc.IsTrue)
c.Assert(attachmentIds, gc.DeepEquals, storageAttachmentIds)
}
示例2: TestStorageAttachmentLife
func (s *storageSuite) TestStorageAttachmentLife(c *gc.C) {
apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
c.Check(objType, gc.Equals, "Uniter")
c.Check(version, gc.Equals, 2)
c.Check(id, gc.Equals, "")
c.Check(request, gc.Equals, "StorageAttachmentLife")
c.Check(arg, gc.DeepEquals, params.StorageAttachmentIds{
Ids: []params.StorageAttachmentId{{
StorageTag: "storage-data-0",
UnitTag: "unit-mysql-0",
}},
})
c.Assert(result, gc.FitsTypeOf, ¶ms.LifeResults{})
*(result.(*params.LifeResults)) = params.LifeResults{
Results: []params.LifeResult{{
Life: params.Dying,
}},
}
return nil
})
st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
results, err := st.StorageAttachmentLife([]params.StorageAttachmentId{{
StorageTag: "storage-data-0",
UnitTag: "unit-mysql-0",
}})
c.Check(err, jc.ErrorIsNil)
c.Assert(results, jc.DeepEquals, []params.LifeResult{{Life: params.Dying}})
}
示例3: Uniter
// Uniter returns a version of the state that provides functionality
// required by the uniter worker.
func (st *State) Uniter() (*uniter.State, error) {
unitTag, ok := st.authTag.(names.UnitTag)
if !ok {
return nil, errors.Errorf("expected UnitTag, got %T %v", st.authTag, st.authTag)
}
return uniter.NewState(st, unitTag), nil
}
示例4: TestWatchStorageAttachments
func (s *storageSuite) TestWatchStorageAttachments(c *gc.C) {
var called bool
apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
c.Check(objType, gc.Equals, "Uniter")
c.Check(version, gc.Equals, 2)
c.Check(id, gc.Equals, "")
c.Check(request, gc.Equals, "WatchStorageAttachments")
c.Check(arg, gc.DeepEquals, params.StorageAttachmentIds{
Ids: []params.StorageAttachmentId{{
StorageTag: "storage-data-0",
UnitTag: "unit-mysql-0",
}},
})
c.Assert(result, gc.FitsTypeOf, ¶ms.NotifyWatchResults{})
*(result.(*params.NotifyWatchResults)) = params.NotifyWatchResults{
Results: []params.NotifyWatchResult{{
Error: ¶ms.Error{Message: "FAIL"},
}},
}
called = true
return nil
})
st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
_, err := st.WatchStorageAttachment(names.NewStorageTag("data/0"), names.NewUnitTag("mysql/0"))
c.Check(err, gc.ErrorMatches, "FAIL")
c.Check(called, jc.IsTrue)
}
示例5: TestNextOpNothing
func (s *relationsSuite) TestNextOpNothing(c *gc.C) {
unitTag := names.NewUnitTag("wordpress/0")
abort := make(chan struct{})
var numCalls int32
unitEntity := params.Entities{Entities: []params.Entity{params.Entity{Tag: "unit-wordpress-0"}}}
apiCaller := mockAPICaller(c, &numCalls,
uniterApiCall("Life", unitEntity, params.LifeResults{Results: []params.LifeResult{{Life: params.Alive}}}, nil),
uniterApiCall("JoinedRelations", unitEntity, params.StringsResults{Results: []params.StringsResult{{Result: []string{}}}}, nil),
uniterApiCall("GetPrincipal", unitEntity, params.StringBoolResults{Results: []params.StringBoolResult{{Result: "", Ok: false}}}, nil),
)
st := uniter.NewState(apiCaller, unitTag)
r, err := relation.NewRelations(st, unitTag, s.stateDir, s.relationsDir, abort)
c.Assert(err, jc.ErrorIsNil)
assertNumCalls(c, &numCalls, 2)
localState := resolver.LocalState{
State: operation.State{
Kind: operation.Continue,
},
}
remoteState := remotestate.Snapshot{}
relationsResolver := relation.NewRelationsResolver(r)
_, err = relationsResolver.NextOp(localState, remoteState, &mockOperations{})
c.Assert(errors.Cause(err), gc.Equals, resolver.ErrNoOperation)
}
示例6: TestMergeLeadershipSettings
func (s *uniterLeadershipSuite) TestMergeLeadershipSettings(c *gc.C) {
// First, the unit must be elected leader; otherwise merges will be denied.
leaderClient := leadership.NewClient(s.apiState)
err := leaderClient.ClaimLeadership(s.serviceId, s.unitId, 10*time.Second)
c.Assert(err, jc.ErrorIsNil)
client := uniter.NewState(s.apiState, names.NewUnitTag(s.unitId))
// Grab what settings exist.
settings, err := client.LeadershipSettings.Read(s.serviceId)
c.Assert(err, jc.ErrorIsNil)
// Double check that it's empty so that we don't pass the test by
// happenstance.
c.Assert(settings, gc.HasLen, 0)
// Toss a few settings in.
settings["foo"] = "bar"
settings["baz"] = "biz"
err = client.LeadershipSettings.Merge(s.serviceId, settings)
c.Assert(err, jc.ErrorIsNil)
settings, err = client.LeadershipSettings.Read(s.serviceId)
c.Assert(err, jc.ErrorIsNil)
c.Check(settings["foo"], gc.Equals, "bar")
c.Check(settings["baz"], gc.Equals, "biz")
}
示例7: TestRemoveStorageAttachment
func (s *storageSuite) TestRemoveStorageAttachment(c *gc.C) {
apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
c.Check(objType, gc.Equals, "Uniter")
c.Check(version, gc.Equals, 2)
c.Check(id, gc.Equals, "")
c.Check(request, gc.Equals, "RemoveStorageAttachments")
c.Check(arg, gc.DeepEquals, params.StorageAttachmentIds{
Ids: []params.StorageAttachmentId{{
StorageTag: "storage-data-0",
UnitTag: "unit-mysql-0",
}},
})
c.Assert(result, gc.FitsTypeOf, ¶ms.ErrorResults{})
*(result.(*params.ErrorResults)) = params.ErrorResults{
Results: []params.ErrorResult{{
Error: ¶ms.Error{Message: "yoink"},
}},
}
return nil
})
st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
err := st.RemoveStorageAttachment(names.NewStorageTag("data/0"), names.NewUnitTag("mysql/0"))
c.Check(err, gc.ErrorMatches, "yoink")
}
示例8: TestAPIErrors
func (s *storageSuite) TestAPIErrors(c *gc.C) {
apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
return errors.New("bad")
})
st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
_, err := st.UnitStorageAttachments(names.NewUnitTag("mysql/0"))
c.Check(err, gc.ErrorMatches, "bad")
}
示例9: Manifold
// Manifold returns a dependency manifold that runs a uniter worker,
// using the resource names defined in the supplied config.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{
config.AgentName,
config.APICallerName,
config.LeadershipTrackerName,
config.MachineLockName,
config.CharmDirName,
},
Start: func(getResource dependency.GetResourceFunc) (worker.Worker, error) {
// Collect all required resources.
var agent agent.Agent
if err := getResource(config.AgentName, &agent); err != nil {
return nil, err
}
var apiCaller base.APICaller
if err := getResource(config.APICallerName, &apiCaller); err != nil {
// TODO(fwereade): absence of an APICaller shouldn't be the end of
// the world -- we ought to return a type that can at least run the
// leader-deposed hook -- but that's not done yet.
return nil, err
}
var machineLock *fslock.Lock
if err := getResource(config.MachineLockName, &machineLock); err != nil {
return nil, err
}
var leadershipTracker leadership.Tracker
if err := getResource(config.LeadershipTrackerName, &leadershipTracker); err != nil {
return nil, err
}
var charmDirGuard fortress.Guard
if err := getResource(config.CharmDirName, &charmDirGuard); err != nil {
return nil, err
}
// Configure and start the uniter.
config := agent.CurrentConfig()
tag := config.Tag()
unitTag, ok := tag.(names.UnitTag)
if !ok {
return nil, errors.Errorf("expected a unit tag, got %v", tag)
}
uniterFacade := uniter.NewState(apiCaller, unitTag)
return NewUniter(&UniterParams{
UniterFacade: uniterFacade,
UnitTag: unitTag,
LeadershipTracker: leadershipTracker,
DataDir: config.DataDir(),
MachineLock: machineLock,
CharmDirGuard: charmDirGuard,
UpdateStatusSignal: NewUpdateStatusTimer(),
NewOperationExecutor: operation.NewExecutor,
Clock: clock.WallClock,
}), nil
},
}
}
示例10: TestImplicitRelationNoHooks
func (s *relationsSuite) TestImplicitRelationNoHooks(c *gc.C) {
unitTag := names.NewUnitTag("wordpress/0")
abort := make(chan struct{})
unitEntity := params.Entities{Entities: []params.Entity{params.Entity{Tag: "unit-wordpress-0"}}}
relationResults := params.RelationResults{
Results: []params.RelationResult{
{
Id: 1,
Key: "wordpress:juju-info juju-info:juju-info",
Life: params.Alive,
Endpoint: multiwatcher.Endpoint{
ApplicationName: "wordpress",
Relation: multiwatcher.CharmRelation{Name: "juju-info", Role: string(charm.RoleProvider), Interface: "juju-info", Scope: "global"},
}},
},
}
relationUnits := params.RelationUnits{RelationUnits: []params.RelationUnit{
{Relation: "relation-wordpress.juju-info#juju-info.juju-info", Unit: "unit-wordpress-0"},
}}
apiCalls := []apiCall{
uniterApiCall("Life", unitEntity, params.LifeResults{Results: []params.LifeResult{{Life: params.Alive}}}, nil),
uniterApiCall("JoinedRelations", unitEntity, params.StringsResults{Results: []params.StringsResult{{Result: []string{}}}}, nil),
uniterApiCall("RelationById", params.RelationIds{RelationIds: []int{1}}, relationResults, nil),
uniterApiCall("Relation", relationUnits, relationResults, nil),
uniterApiCall("Relation", relationUnits, relationResults, nil),
uniterApiCall("Watch", unitEntity, params.NotifyWatchResults{Results: []params.NotifyWatchResult{{NotifyWatcherId: "1"}}}, nil),
uniterApiCall("EnterScope", relationUnits, params.ErrorResults{Results: []params.ErrorResult{{}}}, nil),
uniterApiCall("GetPrincipal", unitEntity, params.StringBoolResults{Results: []params.StringBoolResult{{Result: "", Ok: false}}}, nil),
}
var numCalls int32
apiCaller := mockAPICaller(c, &numCalls, apiCalls...)
st := uniter.NewState(apiCaller, unitTag)
r, err := relation.NewRelations(st, unitTag, s.stateDir, s.relationsDir, abort)
c.Assert(err, jc.ErrorIsNil)
localState := resolver.LocalState{
State: operation.State{
Kind: operation.Continue,
},
}
remoteState := remotestate.Snapshot{
Relations: map[int]remotestate.RelationSnapshot{
1: remotestate.RelationSnapshot{
Life: params.Alive,
Members: map[string]int64{
"wordpress": 1,
},
},
},
}
relationsResolver := relation.NewRelationsResolver(r)
_, err = relationsResolver.NextOp(localState, remoteState, &mockOperations{})
c.Assert(errors.Cause(err), gc.Equals, resolver.ErrNoOperation)
}
示例11: TestStorageAttachmentResultCountMismatch
func (s *storageSuite) TestStorageAttachmentResultCountMismatch(c *gc.C) {
apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
*(result.(*params.StorageAttachmentIdsResults)) = params.StorageAttachmentIdsResults{
[]params.StorageAttachmentIdsResult{{}, {}},
}
return nil
})
st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
c.Assert(func() {
st.UnitStorageAttachments(names.NewUnitTag("mysql/0"))
}, gc.PanicMatches, "expected 1 result, got 2")
}
示例12: setupRelations
func (s *relationsSuite) setupRelations(c *gc.C) relation.Relations {
unitTag := names.NewUnitTag("wordpress/0")
abort := make(chan struct{})
var numCalls int32
unitEntity := params.Entities{Entities: []params.Entity{params.Entity{Tag: "unit-wordpress-0"}}}
apiCaller := mockAPICaller(c, &numCalls,
uniterApiCall("Life", unitEntity, params.LifeResults{Results: []params.LifeResult{{Life: params.Alive}}}, nil),
uniterApiCall("JoinedRelations", unitEntity, params.StringsResults{Results: []params.StringsResult{{Result: []string{}}}}, nil),
)
st := uniter.NewState(apiCaller, unitTag)
r, err := relation.NewRelations(st, unitTag, s.stateDir, s.relationsDir, abort)
c.Assert(err, jc.ErrorIsNil)
assertNumCalls(c, &numCalls, 2)
return r
}
示例13: TestNewRelationsWithExistingRelations
func (s *relationsSuite) TestNewRelationsWithExistingRelations(c *gc.C) {
unitTag := names.NewUnitTag("wordpress/0")
abort := make(chan struct{})
var numCalls int32
unitEntity := params.Entities{Entities: []params.Entity{params.Entity{Tag: "unit-wordpress-0"}}}
relationUnits := params.RelationUnits{RelationUnits: []params.RelationUnit{
{Relation: "relation-wordpress.db#mysql.db", Unit: "unit-wordpress-0"},
}}
relationResults := params.RelationResults{
Results: []params.RelationResult{
{
Id: 1,
Key: "wordpress:db mysql:db",
Life: params.Alive,
Endpoint: multiwatcher.Endpoint{
ApplicationName: "wordpress",
Relation: multiwatcher.CharmRelation{Name: "mysql", Role: string(charm.RoleProvider), Interface: "db"},
}},
},
}
apiCaller := mockAPICaller(c, &numCalls,
uniterApiCall("Life", unitEntity, params.LifeResults{Results: []params.LifeResult{{Life: params.Alive}}}, nil),
uniterApiCall("JoinedRelations", unitEntity, params.StringsResults{Results: []params.StringsResult{{Result: []string{"relation-wordpress:db mysql:db"}}}}, nil),
uniterApiCall("Relation", relationUnits, relationResults, nil),
uniterApiCall("Relation", relationUnits, relationResults, nil),
uniterApiCall("Watch", unitEntity, params.NotifyWatchResults{Results: []params.NotifyWatchResult{{NotifyWatcherId: "1"}}}, nil),
uniterApiCall("EnterScope", relationUnits, params.ErrorResults{Results: []params.ErrorResult{{}}}, nil),
)
st := uniter.NewState(apiCaller, unitTag)
r, err := relation.NewRelations(st, unitTag, s.stateDir, s.relationsDir, abort)
c.Assert(err, jc.ErrorIsNil)
assertNumCalls(c, &numCalls, 6)
info := r.GetInfo()
c.Assert(info, gc.HasLen, 1)
oneInfo := info[1]
c.Assert(oneInfo.RelationUnit.Relation().Tag(), gc.Equals, names.NewRelationTag("wordpress:db mysql:db"))
c.Assert(oneInfo.RelationUnit.Endpoint(), jc.DeepEquals, uniter.Endpoint{
Relation: charm.Relation{Name: "mysql", Role: "provider", Interface: "db", Optional: false, Limit: 0, Scope: ""},
})
c.Assert(oneInfo.MemberNames, gc.HasLen, 0)
}
示例14: validateMigration
// validateMigration is called by the migrationminion to help check
// that the agent will be ok when connected to a new controller.
func (a *UnitAgent) validateMigration(apiCaller base.APICaller) error {
// TODO(mjs) - more extensive checks to come.
unitTag := names.NewUnitTag(a.UnitName)
facade := uniter.NewState(apiCaller, unitTag)
_, err := facade.Unit(unitTag)
if err != nil {
return errors.Trace(err)
}
model, err := facade.Model()
if err != nil {
return errors.Trace(err)
}
curModelUUID := a.CurrentConfig().Model().Id()
newModelUUID := model.UUID()
if newModelUUID != curModelUUID {
return errors.Errorf("model mismatch when validating: got %q, expected %q",
newModelUUID, curModelUUID)
}
return nil
}
示例15: TestReadLeadershipSettings
func (s *uniterLeadershipSuite) TestReadLeadershipSettings(c *gc.C) {
// First, the unit must be elected leader; otherwise merges will be denied.
leaderClient := leadership.NewClient(s.apiState)
err := leaderClient.ClaimLeadership(s.serviceId, s.unitId, 10*time.Second)
c.Assert(err, jc.ErrorIsNil)
client := uniter.NewState(s.apiState, names.NewUnitTag(s.unitId))
// Toss a few settings in.
desiredSettings := map[string]string{
"foo": "bar",
"baz": "biz",
}
err = client.LeadershipSettings.Merge(s.serviceId, desiredSettings)
c.Assert(err, jc.ErrorIsNil)
settings, err := client.LeadershipSettings.Read(s.serviceId)
c.Assert(err, jc.ErrorIsNil)
c.Check(settings, gc.DeepEquals, desiredSettings)
}