本文整理汇总了Golang中github.com/juju/juju/api/machiner.NewState函数的典型用法代码示例。如果您正苦于以下问题:Golang NewState函数的具体用法?Golang NewState怎么用?Golang NewState使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewState函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestBadLogin
func (s *loginSuite) TestBadLogin(c *gc.C) {
// Start our own server so we can control when the first login
// happens. Otherwise in JujuConnSuite.SetUpTest api.Open is
// called with user-admin permissions automatically.
info, cleanup := s.setupServerWithValidator(c, nil)
defer cleanup()
adminUser := s.AdminUserTag(c)
for i, t := range []struct {
tag names.Tag
password string
err error
code string
}{{
tag: adminUser,
password: "wrong password",
err: &rpc.RequestError{
Message: "invalid entity name or password",
Code: "unauthorized access",
},
code: params.CodeUnauthorized,
}, {
tag: names.NewUserTag("unknown"),
password: "password",
err: &rpc.RequestError{
Message: "invalid entity name or password",
Code: "unauthorized access",
},
code: params.CodeUnauthorized,
}} {
c.Logf("test %d; entity %q; password %q", i, t.tag, t.password)
func() {
// Open the API without logging in, so we can perform
// operations on the connection before calling Login.
st := s.openAPIWithoutLogin(c, info)
defer st.Close()
_, err := apimachiner.NewState(st).Machine(names.NewMachineTag("0"))
c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
Message: `unknown object type "Machiner"`,
Code: "not implemented",
})
// Since these are user login tests, the nonce is empty.
err = st.Login(t.tag, t.password, "", nil)
c.Assert(errors.Cause(err), gc.DeepEquals, t.err)
c.Assert(params.ErrCode(err), gc.Equals, t.code)
_, err = apimachiner.NewState(st).Machine(names.NewMachineTag("0"))
c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
Message: `unknown object type "Machiner"`,
Code: "not implemented",
})
}()
}
}
示例2: TestImportingModel
func (s *migrationSuite) TestImportingModel(c *gc.C) {
m, password := s.Factory.MakeMachineReturningPassword(c, &factory.MachineParams{
Nonce: "nonce",
})
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
err = model.SetMigrationMode(state.MigrationModeImporting)
c.Assert(err, jc.ErrorIsNil)
_, cleanup := s.setupServer(c)
defer cleanup()
// Users should be able to log in but RPC requests should fail.
info := s.APIInfo(c)
userConn := s.OpenAPIAs(c, info.Tag, info.Password)
defer userConn.Close()
_, err = userConn.Client().Status(nil)
c.Check(err, gc.ErrorMatches, "migration in progress, model is importing")
// Machines should be able to use the API.
machineConn := s.OpenAPIAsMachine(c, m.Tag(), password, "nonce")
defer machineConn.Close()
_, err = apimachiner.NewState(machineConn).Machine(m.MachineTag())
c.Check(err, jc.ErrorIsNil)
}
示例3: SetUpTest
func (s *MachinerStateSuite) SetUpTest(c *gc.C) {
s.JujuConnSuite.SetUpTest(c)
s.st, s.machine = s.OpenAPIAsNewMachine(c)
// Create the machiner API facade.
s.machinerState = apimachiner.NewState(s.st)
c.Assert(s.machinerState, gc.NotNil)
// Get the machine through the facade.
var err error
s.apiMachine, err = s.machinerState.Machine(s.machine.Tag().(names.MachineTag))
c.Assert(err, jc.ErrorIsNil)
c.Assert(s.apiMachine.Tag(), gc.Equals, s.machine.Tag())
// Isolate tests better by not using real interface addresses.
s.PatchValue(machiner.InterfaceAddrs, func() ([]net.Addr, error) {
return nil, nil
})
s.PatchValue(&network.InterfaceByNameAddrs, func(string) ([]net.Addr, error) {
return nil, nil
})
s.PatchValue(&network.LXCNetDefaultConfig, "")
s.PatchValue(machiner.GetObservedNetworkConfig, func() ([]params.NetworkConfig, error) {
return nil, nil
})
}
示例4: TestAddressChange
func (s *APIAddressUpdaterSuite) TestAddressChange(c *gc.C) {
setter := &apiAddressSetter{servers: make(chan [][]network.HostPort, 1)}
st, _ := s.OpenAPIAsNewMachine(c, state.JobHostUnits)
worker, err := apiaddressupdater.NewAPIAddressUpdater(apimachiner.NewState(st), setter)
c.Assert(err, jc.ErrorIsNil)
defer func() { c.Assert(worker.Wait(), gc.IsNil) }()
defer worker.Kill()
s.BackingState.StartSync()
updatedServers := [][]network.HostPort{
network.NewHostPorts(1234, "localhost", "127.0.0.1"),
}
// SetAPIHostPorts should be called with the initial value (empty),
// and then the updated value.
select {
case <-time.After(coretesting.LongWait):
c.Fatalf("timed out waiting for SetAPIHostPorts to be called initially")
case servers := <-setter.servers:
c.Assert(servers, gc.HasLen, 0)
}
err = s.State.SetAPIHostPorts(updatedServers)
c.Assert(err, jc.ErrorIsNil)
s.BackingState.StartSync()
select {
case <-time.After(coretesting.LongWait):
c.Fatalf("timed out waiting for SetAPIHostPorts to be called after update")
case servers := <-setter.servers:
c.Assert(servers, gc.DeepEquals, updatedServers)
}
}
示例5: TestStartStop
func (s *APIAddressUpdaterSuite) TestStartStop(c *gc.C) {
st, _ := s.OpenAPIAsNewMachine(c, state.JobHostUnits)
worker, err := apiaddressupdater.NewAPIAddressUpdater(apimachiner.NewState(st), &apiAddressSetter{})
c.Assert(err, jc.ErrorIsNil)
worker.Kill()
c.Assert(worker.Wait(), gc.IsNil)
}
示例6: TestAPIServerCanListenOnBothIPv4AndIPv6
func (s *serverSuite) TestAPIServerCanListenOnBothIPv4AndIPv6(c *gc.C) {
err := s.State.SetAPIHostPorts(nil)
c.Assert(err, jc.ErrorIsNil)
// Start our own instance of the server listening on
// both IPv4 and IPv6 localhost addresses and an ephemeral port.
srv := newServer(c, s.State)
defer srv.Stop()
port := srv.Addr().Port
portString := fmt.Sprintf("%d", port)
machine, password := s.Factory.MakeMachineReturningPassword(
c, &factory.MachineParams{Nonce: "fake_nonce"})
// Now connect twice - using IPv4 and IPv6 endpoints.
apiInfo := &api.Info{
Tag: machine.Tag(),
Password: password,
Nonce: "fake_nonce",
Addrs: []string{net.JoinHostPort("127.0.0.1", portString)},
CACert: coretesting.CACert,
ModelTag: s.State.ModelTag(),
}
ipv4State, err := api.Open(apiInfo, fastDialOpts)
c.Assert(err, jc.ErrorIsNil)
defer ipv4State.Close()
c.Assert(ipv4State.Addr(), gc.Equals, net.JoinHostPort("127.0.0.1", portString))
c.Assert(ipv4State.APIHostPorts(), jc.DeepEquals, [][]network.HostPort{
network.NewHostPorts(port, "127.0.0.1"),
})
_, err = apimachiner.NewState(ipv4State).Machine(machine.MachineTag())
c.Assert(err, jc.ErrorIsNil)
apiInfo.Addrs = []string{net.JoinHostPort("::1", portString)}
ipv6State, err := api.Open(apiInfo, fastDialOpts)
c.Assert(err, jc.ErrorIsNil)
defer ipv6State.Close()
c.Assert(ipv6State.Addr(), gc.Equals, net.JoinHostPort("::1", portString))
c.Assert(ipv6State.APIHostPorts(), jc.DeepEquals, [][]network.HostPort{
network.NewHostPorts(port, "::1"),
})
_, err = apimachiner.NewState(ipv6State).Machine(machine.MachineTag())
c.Assert(err, jc.ErrorIsNil)
}
示例7: TestStop
func (s *serverSuite) TestStop(c *gc.C) {
// Start our own instance of the server so we have
// a handle on it to stop it.
srv := newServer(c, s.State)
defer srv.Stop()
machine, password := s.Factory.MakeMachineReturningPassword(
c, &factory.MachineParams{Nonce: "fake_nonce"})
// A net.TCPAddr cannot be directly stringified into a valid hostname.
address := fmt.Sprintf("localhost:%d", srv.Addr().Port)
// Note we can't use openAs because we're not connecting to
apiInfo := &api.Info{
Tag: machine.Tag(),
Password: password,
Nonce: "fake_nonce",
Addrs: []string{address},
CACert: coretesting.CACert,
ModelTag: s.State.ModelTag(),
}
st, err := api.Open(apiInfo, fastDialOpts)
c.Assert(err, jc.ErrorIsNil)
defer st.Close()
_, err = apimachiner.NewState(st).Machine(machine.MachineTag())
c.Assert(err, jc.ErrorIsNil)
err = srv.Stop()
c.Assert(err, jc.ErrorIsNil)
_, err = apimachiner.NewState(st).Machine(machine.MachineTag())
err = errors.Cause(err)
// The client has not necessarily seen the server shutdown yet,
// so there are two possible errors.
if err != rpc.ErrShutdown && err != io.ErrUnexpectedEOF {
c.Fatalf("unexpected error from request: %#v, expected rpc.ErrShutdown or io.ErrUnexpectedEOF", err)
}
// Check it can be stopped twice.
err = srv.Stop()
c.Assert(err, jc.ErrorIsNil)
}
示例8: TestStop
func (s *serverSuite) TestStop(c *gc.C) {
// Start our own instance of the server so we have
// a handle on it to stop it.
_, srv := newServer(c, s.State)
defer assertStop(c, srv)
machine, password := s.Factory.MakeMachineReturningPassword(
c, &factory.MachineParams{Nonce: "fake_nonce"})
// A net.TCPAddr cannot be directly stringified into a valid hostname.
address := fmt.Sprintf("localhost:%d", srv.Addr().Port)
// Note we can't use openAs because we're not connecting to
apiInfo := &api.Info{
Tag: machine.Tag(),
Password: password,
Nonce: "fake_nonce",
Addrs: []string{address},
CACert: coretesting.CACert,
ModelTag: s.State.ModelTag(),
}
st, err := api.Open(apiInfo, fastDialOpts)
c.Assert(err, jc.ErrorIsNil)
defer st.Close()
_, err = apimachiner.NewState(st).Machine(machine.MachineTag())
c.Assert(err, jc.ErrorIsNil)
err = srv.Stop()
c.Assert(err, jc.ErrorIsNil)
_, err = apimachiner.NewState(st).Machine(machine.MachineTag())
// The client has not necessarily seen the server shutdown yet, so there
// are multiple possible errors. All we should care about is that there is
// an error, not what the error actually is.
c.Assert(err, gc.NotNil)
// Check it can be stopped twice.
err = srv.Stop()
c.Assert(err, jc.ErrorIsNil)
}
示例9: TestManageModelServesAPI
func (s *MachineSuite) TestManageModelServesAPI(c *gc.C) {
s.assertJobWithState(c, state.JobManageModel, func(conf agent.Config, agentState *state.State) {
apiInfo, ok := conf.APIInfo()
c.Assert(ok, jc.IsTrue)
st, err := api.Open(apiInfo, fastDialOpts)
c.Assert(err, jc.ErrorIsNil)
defer st.Close()
m, err := apimachiner.NewState(st).Machine(conf.Tag().(names.MachineTag))
c.Assert(err, jc.ErrorIsNil)
c.Assert(m.Life(), gc.Equals, params.Alive)
})
}
示例10: SetUpTest
func (s *machinerSuite) SetUpTest(c *gc.C) {
s.JujuConnSuite.SetUpTest(c)
m, err := s.State.AddMachine("quantal", state.JobManageModel)
c.Assert(err, jc.ErrorIsNil)
err = m.SetProviderAddresses(network.NewAddress("10.0.0.1"))
c.Assert(err, jc.ErrorIsNil)
s.st, s.machine = s.OpenAPIAsNewMachine(c)
// Create the machiner API facade.
s.machiner = machiner.NewState(s.st)
c.Assert(s.machiner, gc.NotNil)
s.APIAddresserTests = apitesting.NewAPIAddresserTests(s.machiner, s.BackingState)
}
示例11: Manifold
// Manifold returns a dependency manifold that runs a machiner worker, using
// the resource names defined in the supplied config.
func Manifold(config ManifoldConfig) dependency.Manifold {
// TODO(waigani) This function is currently covered by functional tests
// under the machine agent. Add unit tests once infrastructure to do so is
// in place.
// newWorker non-trivially wraps NewMachiner to specialise a PostUpgradeManifold.
var newWorker = func(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
currentConfig := a.CurrentConfig()
// TODO(fwereade): this functionality should be on the
// deployer facade instead.
agentFacade := apiagent.NewState(apiCaller)
envConfig, err := agentFacade.ModelConfig()
if err != nil {
return nil, errors.Errorf("cannot read environment config: %v", err)
}
ignoreMachineAddresses, _ := envConfig.IgnoreMachineAddresses()
// Containers only have machine addresses, so we can't ignore them.
tag := currentConfig.Tag()
if names.IsContainerMachine(tag.Id()) {
ignoreMachineAddresses = false
}
if ignoreMachineAddresses {
logger.Infof("machine addresses not used, only addresses from provider")
}
accessor := APIMachineAccessor{apimachiner.NewState(apiCaller)}
w, err := NewMachiner(Config{
MachineAccessor: accessor,
Tag: tag.(names.MachineTag),
ClearMachineAddressesOnStart: ignoreMachineAddresses,
NotifyMachineDead: func() error {
return agent.SetCanUninstall(a)
},
})
if err != nil {
return nil, errors.Annotate(err, "cannot start machiner worker")
}
return w, err
}
return util.PostUpgradeManifold(config.PostUpgradeManifoldConfig, newWorker)
}
示例12: runLoginSetsLogIdentifier
func (s *baseLoginSuite) runLoginSetsLogIdentifier(c *gc.C) {
info, cleanup := s.setupServerWithValidator(c, nil)
defer cleanup()
machine, password := s.Factory.MakeMachineReturningPassword(
c, &factory.MachineParams{Nonce: "fake_nonce"})
info.Tag = machine.Tag()
info.Password = password
info.Nonce = "fake_nonce"
apiConn, err := api.Open(info, fastDialOpts)
c.Assert(err, jc.ErrorIsNil)
defer apiConn.Close()
apiMachine, err := apimachiner.NewState(apiConn).Machine(machine.MachineTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(apiMachine.Tag(), gc.Equals, machine.Tag())
}
示例13: checkLoginWithValidator
func (s *baseLoginSuite) checkLoginWithValidator(c *gc.C, validator apiserver.LoginValidator, checker validationChecker) {
info, cleanup := s.setupServerWithValidator(c, validator)
defer cleanup()
st := s.openAPIWithoutLogin(c, info)
defer st.Close()
// Ensure not already logged in.
_, err := apimachiner.NewState(st).Machine(names.NewMachineTag("0"))
c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
Message: `unknown object type "Machiner"`,
Code: "not implemented",
})
adminUser := s.AdminUserTag(c)
// Since these are user login tests, the nonce is empty.
err = st.Login(adminUser, "dummy-secret", "", nil)
checker(c, err, st)
}
示例14: checkLoginWithValidator
func (s *baseLoginSuite) checkLoginWithValidator(c *gc.C, validator apiserver.LoginValidator, checker validationChecker) {
cfg := defaultServerConfig(c)
cfg.Validator = validator
info, srv := newServerWithConfig(c, s.State, cfg)
defer assertStop(c, srv)
info.ModelTag = s.State.ModelTag()
st := s.openAPIWithoutLogin(c, info)
// Ensure not already logged in.
_, err := apimachiner.NewState(st).Machine(names.NewMachineTag("0"))
c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
Message: `unknown object type "Machiner"`,
Code: "not implemented",
})
adminUser := s.AdminUserTag(c)
// Since these are user login tests, the nonce is empty.
err = st.Login(adminUser, "dummy-secret", "", nil)
checker(c, err, st)
}
示例15: newWorker
// newWorker non-trivially wraps NewMachiner to specialise a util.AgentApiManifold.
//
// TODO(waigani) This function is currently covered by functional tests
// under the machine agent. Add unit tests once infrastructure to do so is
// in place.
func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
currentConfig := a.CurrentConfig()
// TODO(fwereade): this functionality should be on the
// machiner facade instead -- or, better yet, separate
// the networking concerns from the lifecycle ones and
// have completey separate workers.
//
// (With their own facades.)
agentFacade := apiagent.NewState(apiCaller)
envConfig, err := agentFacade.ModelConfig()
if err != nil {
return nil, errors.Errorf("cannot read environment config: %v", err)
}
ignoreMachineAddresses, _ := envConfig.IgnoreMachineAddresses()
// Containers only have machine addresses, so we can't ignore them.
tag := currentConfig.Tag()
if names.IsContainerMachine(tag.Id()) {
ignoreMachineAddresses = false
}
if ignoreMachineAddresses {
logger.Infof("machine addresses not used, only addresses from provider")
}
accessor := APIMachineAccessor{apimachiner.NewState(apiCaller)}
w, err := NewMachiner(Config{
MachineAccessor: accessor,
Tag: tag.(names.MachineTag),
ClearMachineAddressesOnStart: ignoreMachineAddresses,
NotifyMachineDead: func() error {
return agent.SetCanUninstall(a)
},
})
if err != nil {
return nil, errors.Annotate(err, "cannot start machiner worker")
}
return w, err
}