func (*suite) TestSetAPIHostPorts(c *gc.C) { conf, err := agent.NewAgentConfig(attributeParams) c.Assert(err, gc.IsNil) addrs, err := conf.APIAddresses() c.Assert(err, gc.IsNil) c.Assert(addrs, gc.DeepEquals, attributeParams.APIAddresses) // The first cloud-local address for each server is used, // else if there are none then the first public- or unknown- // scope address. // // If a server has only machine-local addresses, or none // at all, then it will be excluded. server1 := instance.NewAddresses("0.1.2.3", "0.1.2.4", "zeroonetwothree") server1[0].NetworkScope = instance.NetworkCloudLocal server1[1].NetworkScope = instance.NetworkCloudLocal server1[2].NetworkScope = instance.NetworkPublic server2 := instance.NewAddresses("127.0.0.1") server2[0].NetworkScope = instance.NetworkMachineLocal server3 := instance.NewAddresses("0.1.2.5", "zeroonetwofive") server3[0].NetworkScope = instance.NetworkUnknown server3[1].NetworkScope = instance.NetworkUnknown conf.SetAPIHostPorts([][]instance.HostPort{ instance.AddressesWithPort(server1, 123), instance.AddressesWithPort(server2, 124), instance.AddressesWithPort(server3, 125), }) addrs, err = conf.APIAddresses() c.Assert(err, gc.IsNil) c.Assert(addrs, gc.DeepEquals, []string{"0.1.2.3:123", "0.1.2.5:125"}) }
func (s *APIAddressUpdaterSuite) TestAddressChange(c *gc.C) { setter := &apiAddressSetter{servers: make(chan [][]instance.HostPort, 1)} st, _ := s.OpenAPIAsNewMachine(c, state.JobHostUnits) worker := apiaddressupdater.NewAPIAddressUpdater(st.Machiner(), setter) defer func() { c.Assert(worker.Wait(), gc.IsNil) }() defer worker.Kill() s.BackingState.StartSync() updatedServers := [][]instance.HostPort{instance.AddressesWithPort( instance.NewAddresses("localhost", "127.0.0.1"), 1234, )} // 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 first") case servers := <-setter.servers: c.Assert(servers, gc.HasLen, 0) } err := s.State.SetAPIHostPorts(updatedServers) c.Assert(err, gc.IsNil) s.BackingState.StartSync() select { case <-time.After(coretesting.LongWait): c.Fatalf("timed out waiting for SetAPIHostPorts to be called second") case servers := <-setter.servers: c.Assert(servers, gc.DeepEquals, updatedServers) } }
func (s *MachineSuite) TestMachineAgentRunsAPIAddressUpdaterWorker(c *gc.C) { // Start the machine agent. m, _, _ := s.primeAgent(c, version.Current, state.JobHostUnits) a := s.newAgent(c, m) go func() { c.Check(a.Run(nil), gc.IsNil) }() defer func() { c.Check(a.Stop(), gc.IsNil) }() // Update the API addresses. updatedServers := [][]instance.HostPort{instance.AddressesWithPort( instance.NewAddresses("localhost"), 1234, )} err := s.BackingState.SetAPIHostPorts(updatedServers) c.Assert(err, gc.IsNil) // Wait for config to be updated. s.BackingState.StartSync() for attempt := coretesting.LongAttempt.Start(); attempt.Next(); { addrs, err := a.CurrentConfig().APIAddresses() c.Assert(err, gc.IsNil) if reflect.DeepEqual(addrs, []string{"localhost:1234"}) { return } } c.Fatalf("timeout while waiting for agent config to change") }
func parseHostPort(s string) (instance.HostPort, error) { addr, port, err := net.SplitHostPort(s) if err != nil { return instance.HostPort{}, err } portNum, err := strconv.Atoi(port) if err != nil { return instance.HostPort{}, fmt.Errorf("bad port number %q", port) } addrs := instance.NewAddresses(addr) hostPorts := instance.AddressesWithPort(addrs, portNum) return hostPorts[0], nil }
func (m *fakeMachine) setStateHostPort(hostPort string) { var mongoHostPorts []instance.HostPort if hostPort != "" { host, portStr, err := net.SplitHostPort(hostPort) if err != nil { panic(err) } port, err := strconv.Atoi(portStr) if err != nil { panic(err) } mongoHostPorts = instance.AddressesWithPort(instance.NewAddresses(host), port) mongoHostPorts[0].NetworkScope = instance.NetworkCloudLocal } m.mutate(func(doc *machineDoc) { doc.mongoHostPorts = mongoHostPorts }) }
func (s *bootstrapSuite) TestInitializeState(c *gc.C) { dataDir := c.MkDir() pwHash := utils.UserPasswordHash(testing.DefaultMongoPassword, utils.CompatSalt) configParams := agent.AgentConfigParams{ DataDir: dataDir, Tag: "machine-0", UpgradedToVersion: version.Current.Number, StateAddresses: []string{testing.MgoServer.Addr()}, CACert: testing.CACert, Password: pwHash, } servingInfo := params.StateServingInfo{ Cert: testing.ServerCert, PrivateKey: testing.ServerKey, APIPort: 1234, StatePort: testing.MgoServer.Port(), SystemIdentity: "def456", } cfg, err := agent.NewStateMachineConfig(configParams, servingInfo) c.Assert(err, gc.IsNil) _, available := cfg.StateServingInfo() c.Assert(available, gc.Equals, true) expectConstraints := constraints.MustParse("mem=1024M") expectHW := instance.MustParseHardware("mem=2048M") mcfg := agent.BootstrapMachineConfig{ Addresses: instance.NewAddresses("0.1.2.3", "zeroonetwothree"), Constraints: expectConstraints, Jobs: []params.MachineJob{params.JobHostUnits}, InstanceId: "i-bootstrap", Characteristics: expectHW, SharedSecret: "abc123", } envAttrs := dummy.SampleConfig().Delete("admin-secret").Merge(testing.Attrs{ "agent-version": version.Current.Number.String(), "state-id": "1", // needed so policy can Open config }) envCfg, err := config.New(config.NoDefaults, envAttrs) c.Assert(err, gc.IsNil) st, m, err := agent.InitializeState(cfg, envCfg, mcfg, state.DialOpts{}, environs.NewStatePolicy()) c.Assert(err, gc.IsNil) defer st.Close() err = cfg.Write() c.Assert(err, gc.IsNil) // Check that initial admin user has been set up correctly. s.assertCanLogInAsAdmin(c, pwHash) user, err := st.User("admin") c.Assert(err, gc.IsNil) c.Assert(user.PasswordValid(testing.DefaultMongoPassword), jc.IsTrue) // Check that environment configuration has been added. newEnvCfg, err := st.EnvironConfig() c.Assert(err, gc.IsNil) c.Assert(newEnvCfg.AllAttrs(), gc.DeepEquals, envCfg.AllAttrs()) // Check that the bootstrap machine looks correct. c.Assert(m.Id(), gc.Equals, "0") c.Assert(m.Jobs(), gc.DeepEquals, []state.MachineJob{state.JobHostUnits}) c.Assert(m.Series(), gc.Equals, version.Current.Series) c.Assert(m.CheckProvisioned(state.BootstrapNonce), jc.IsTrue) c.Assert(m.Addresses(), gc.DeepEquals, mcfg.Addresses) gotConstraints, err := m.Constraints() c.Assert(err, gc.IsNil) c.Assert(gotConstraints, gc.DeepEquals, expectConstraints) c.Assert(err, gc.IsNil) gotHW, err := m.HardwareCharacteristics() c.Assert(err, gc.IsNil) c.Assert(*gotHW, gc.DeepEquals, expectHW) gotAddrs := m.Addresses() c.Assert(gotAddrs, gc.DeepEquals, mcfg.Addresses) // Check that the API host ports are initialised correctly. apiHostPorts, err := st.APIHostPorts() c.Assert(err, gc.IsNil) c.Assert(apiHostPorts, gc.DeepEquals, [][]instance.HostPort{ instance.AddressesWithPort(mcfg.Addresses, 1234), }) // Check that the state serving info is initialised correctly. stateServingInfo, err := st.StateServingInfo() c.Assert(err, gc.IsNil) c.Assert(stateServingInfo, jc.DeepEquals, params.StateServingInfo{ APIPort: 1234, StatePort: testing.MgoServer.Port(), Cert: testing.ServerCert, PrivateKey: testing.ServerKey, SharedSecret: "abc123", SystemIdentity: "def456", }) // Check that the machine agent's config has been written // and that we can use it to connect to the state. newCfg, err := agent.ReadConfig(agent.ConfigPath(dataDir, "machine-0")) c.Assert(err, gc.IsNil) c.Assert(newCfg.Tag(), gc.Equals, "machine-0") c.Assert(agent.Password(newCfg), gc.Not(gc.Equals), pwHash) c.Assert(agent.Password(newCfg), gc.Not(gc.Equals), testing.DefaultMongoPassword) info, ok := cfg.StateInfo() c.Assert(ok, jc.IsTrue) st1, err := state.Open(info, state.DialOpts{}, environs.NewStatePolicy()) c.Assert(err, gc.IsNil) defer st1.Close() }
func addressesWithPort(port int, addrs ...string) []instance.HostPort { return instance.AddressesWithPort(instance.NewAddresses(addrs...), port) }
// StartInstance is specified in the InstanceBroker interface. func (e *environ) StartInstance(args environs.StartInstanceParams) (instance.Instance, *instance.HardwareCharacteristics, []network.Info, error) { defer delay() machineId := args.MachineConfig.MachineId logger.Infof("dummy startinstance, machine %s", machineId) if err := e.checkBroken("StartInstance"); err != nil { return nil, nil, nil, err } estate, err := e.state() if err != nil { return nil, nil, nil, err } estate.mu.Lock() defer estate.mu.Unlock() if args.MachineConfig.MachineNonce == "" { return nil, nil, nil, fmt.Errorf("cannot start instance: missing machine nonce") } if _, ok := e.Config().CACert(); !ok { return nil, nil, nil, fmt.Errorf("no CA certificate in environment configuration") } if args.MachineConfig.StateInfo.Tag != names.MachineTag(machineId) { return nil, nil, nil, fmt.Errorf("entity tag must match started machine") } if args.MachineConfig.APIInfo.Tag != names.MachineTag(machineId) { return nil, nil, nil, fmt.Errorf("entity tag must match started machine") } logger.Infof("would pick tools from %s", args.Tools) series := args.Tools.OneSeries() idString := fmt.Sprintf("%s-%d", e.name, estate.maxId) i := &dummyInstance{ id: instance.Id(idString), addresses: instance.NewAddresses(idString + ".dns"), ports: make(map[instance.Port]bool), machineId: machineId, series: series, firewallMode: e.Config().FirewallMode(), state: estate, } var hc *instance.HardwareCharacteristics // To match current system capability, only provide hardware characteristics for // environ machines, not containers. if state.ParentId(machineId) == "" { // We will just assume the instance hardware characteristics exactly matches // the supplied constraints (if specified). hc = &instance.HardwareCharacteristics{ Arch: args.Constraints.Arch, Mem: args.Constraints.Mem, RootDisk: args.Constraints.RootDisk, CpuCores: args.Constraints.CpuCores, CpuPower: args.Constraints.CpuPower, Tags: args.Constraints.Tags, } // Fill in some expected instance hardware characteristics if constraints not specified. if hc.Arch == nil { arch := "amd64" hc.Arch = &arch } if hc.Mem == nil { mem := uint64(1024) hc.Mem = &mem } if hc.RootDisk == nil { disk := uint64(8192) hc.RootDisk = &disk } if hc.CpuCores == nil { cores := uint64(1) hc.CpuCores = &cores } } // Simulate networks added when requested. networkInfo := make([]network.Info, len(args.MachineConfig.IncludeNetworks)) for i, netName := range args.MachineConfig.IncludeNetworks { if strings.HasPrefix(netName, "bad-") { // Simulate we didn't get correct information for the network. networkInfo[i] = network.Info{ ProviderId: network.Id(netName), NetworkName: netName, CIDR: "invalid", } } else { networkInfo[i] = network.Info{ ProviderId: network.Id(netName), NetworkName: netName, CIDR: fmt.Sprintf("0.%d.2.0/24", i+1), InterfaceName: fmt.Sprintf("eth%d", i), VLANTag: i, MACAddress: fmt.Sprintf("aa:bb:cc:dd:ee:f%d", i), IsVirtual: i > 0, } } } estate.insts[i.id] = i estate.maxId++ estate.ops <- OpStartInstance{ Env: e.name, MachineId: machineId, MachineNonce: args.MachineConfig.MachineNonce, Constraints: args.Constraints, IncludeNetworks: args.MachineConfig.IncludeNetworks, ExcludeNetworks: args.MachineConfig.ExcludeNetworks, NetworkInfo: networkInfo, Instance: i, Info: args.MachineConfig.StateInfo, APIInfo: args.MachineConfig.APIInfo, Secret: e.ecfg().secret(), } return i, hc, networkInfo, nil }
"github.com/wallyworld/core/errors" "github.com/wallyworld/core/instance" "github.com/wallyworld/core/state" "github.com/wallyworld/core/state/api/params" coretesting "github.com/wallyworld/core/testing" "github.com/wallyworld/core/testing/testbase" ) var _ = gc.Suite(&machineSuite{}) type machineSuite struct { testbase.LoggingSuite } var testAddrs = instance.NewAddresses("127.0.0.1") func (s *machineSuite) TestSetsInstanceInfoInitially(c *gc.C) { context := &testMachineContext{ getInstanceInfo: instanceInfoGetter(c, "i1234", testAddrs, "running", nil), dyingc: make(chan struct{}), } m := &testMachine{ id: "99", instanceId: "i1234", refresh: func() error { return nil }, life: state.Alive, } died := make(chan machine) // Change the poll intervals to be short, so that we know // that we've polled (probably) at least a few times.
func newTestInstance(status string, addresses []string) *testInstance { thisInstance := testInstance{status: status} thisInstance.addresses = instance.NewAddresses(addresses...) return &thisInstance }
func (*workerSuite) addressesForIndex(i int) []instance.Address { return instance.NewAddresses(fmt.Sprintf("127.0.0.%d", i)) }
func (n *neverOpensPort) Addresses() ([]instance.Address, error) { return instance.NewAddresses(n.addr), nil }