func (s *machinerSuite) TestSetMachineAddresses(c *gc.C) { c.Assert(s.machine0.Addresses(), gc.HasLen, 0) c.Assert(s.machine1.Addresses(), gc.HasLen, 0) addresses := []instance.Address{ instance.NewAddress("127.0.0.1", instance.NetworkUnknown), instance.NewAddress("8.8.8.8", instance.NetworkUnknown), } args := params.SetMachinesAddresses{MachineAddresses: []params.MachineAddresses{ {Tag: "machine-1", Addresses: addresses}, {Tag: "machine-0", Addresses: addresses}, {Tag: "machine-42", Addresses: addresses}, }} result, err := s.machiner.SetMachineAddresses(args) c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.ErrorResults{ Results: []params.ErrorResult{ {nil}, {apiservertesting.ErrUnauthorized}, {apiservertesting.ErrUnauthorized}, }, }) err = s.machine1.Refresh() c.Assert(err, gc.IsNil) c.Assert(s.machine1.MachineAddresses(), gc.DeepEquals, addresses) err = s.machine0.Refresh() c.Assert(err, gc.IsNil) c.Assert(s.machine0.MachineAddresses(), gc.HasLen, 0) }
func (s *suite) TestAllocateAddress(c *gc.C) { cfg, err := config.New(config.NoDefaults, s.TestConfig) c.Assert(err, gc.IsNil) e, err := environs.Prepare(cfg, testing.Context(c), s.ConfigStore) c.Assert(err, gc.IsNil, gc.Commentf("preparing environ %#v", s.TestConfig)) c.Assert(e, gc.NotNil) envtesting.UploadFakeTools(c, e.Storage()) err = bootstrap.EnsureNotBootstrapped(e) c.Assert(err, gc.IsNil) err = bootstrap.Bootstrap(testing.Context(c), e, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) inst, _ := jujutesting.AssertStartInstance(c, e, "0") c.Assert(inst, gc.NotNil) netId := network.Id("net1") opc := make(chan dummy.Operation, 200) dummy.Listen(opc) expectAddress := instance.NewAddress("0.1.2.1", instance.NetworkCloudLocal) address, err := e.AllocateAddress(inst.Id(), netId) c.Assert(err, gc.IsNil) c.Assert(address, gc.DeepEquals, expectAddress) assertAllocateAddress(c, e, opc, inst.Id(), netId, expectAddress) expectAddress = instance.NewAddress("0.1.2.2", instance.NetworkCloudLocal) address, err = e.AllocateAddress(inst.Id(), netId) c.Assert(err, gc.IsNil) c.Assert(address, gc.DeepEquals, expectAddress) assertAllocateAddress(c, e, opc, inst.Id(), netId, expectAddress) }
func (s *lxcProvisionerSuite) SetUpTest(c *gc.C) { s.CommonProvisionerSuite.SetUpTest(c) s.lxcSuite.SetUpTest(c) // The lxc provisioner actually needs the machine it is being created on // to be in state, in order to get the watcher. m, err := s.State.AddMachine(coretesting.FakeDefaultSeries, state.JobHostUnits, state.JobManageEnviron) c.Assert(err, gc.IsNil) err = m.SetAddresses(instance.NewAddress("0.1.2.3", instance.NetworkUnknown)) c.Assert(err, gc.IsNil) hostPorts := [][]instance.HostPort{{{ Address: instance.NewAddress("0.1.2.3", instance.NetworkUnknown), Port: 1234, }}} err = s.State.SetAPIHostPorts(hostPorts) c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil) s.parentMachineId = m.Id() s.APILogin(c, m) err = m.SetAgentVersion(version.Current) c.Assert(err, gc.IsNil) s.events = make(chan mock.Event, 25) s.Factory.AddListener(s.events) }
func (fakeAddresses) APIHostPorts() ([][]instance.HostPort, error) { return [][]instance.HostPort{{{ Address: instance.NewAddress("apiaddresses", instance.NetworkUnknown), Port: 1, }}, {{ Address: instance.NewAddress("apiaddresses", instance.NetworkUnknown), Port: 2, }}}, nil }
func (s *APIEndpointForEnvSuite) TestAPIEndpointNotMachineLocal(c *gc.C) { defer coretesting.MakeEmptyFakeHome(c).Restore() store := newConfigStore("env-name", dummyStoreInfo) called := 0 hostPorts := [][]instance.HostPort{ instance.AddressesWithPort([]instance.Address{ instance.NewAddress("1.0.0.1", instance.NetworkPublic), instance.NewAddress("192.0.0.1", instance.NetworkCloudLocal), instance.NewAddress("127.0.0.1", instance.NetworkMachineLocal), instance.NewAddress("localhost", instance.NetworkMachineLocal), }, 1234), instance.AddressesWithPort([]instance.Address{ instance.NewAddress("1.0.0.2", instance.NetworkUnknown), instance.NewAddress("2002:0:0:0:0:0:100:2", instance.NetworkUnknown), instance.NewAddress("::1", instance.NetworkMachineLocal), instance.NewAddress("127.0.0.1", instance.NetworkMachineLocal), instance.NewAddress("localhost", instance.NetworkMachineLocal), }, 1235), } expectState := &mockAPIState{apiHostPorts: hostPorts} apiOpen := func(_ *api.Info, _ api.DialOpts) (juju.APIState, error) { called++ return expectState, nil } endpoint, err := juju.APIEndpointInStore("env-name", true, store, apiOpen) c.Assert(err, gc.IsNil) c.Check(called, gc.Equals, 1) c.Check(endpoint.Addresses, gc.DeepEquals, []string{ "1.0.0.1:1234", "192.0.0.1:1234", "1.0.0.2:1235", }) }
// AddStateServerMachine adds a "state server" machine to the state so // that State.Addresses and State.APIAddresses will work. It returns the // added machine. The addresses that those methods will return bear no // relation to the addresses actually used by the state and API servers. // It returns the addresses that will be returned by the State.Addresses // and State.APIAddresses methods, which will not bear any relation to // the be the addresses used by the state servers. func AddStateServerMachine(c *gc.C, st *state.State) *state.Machine { machine, err := st.AddMachine("quantal", state.JobManageEnviron) c.Assert(err, gc.IsNil) err = machine.SetAddresses(instance.NewAddress("0.1.2.3", instance.NetworkUnknown)) c.Assert(err, gc.IsNil) hostPorts := [][]instance.HostPort{{{ Address: instance.NewAddress("0.1.2.3", instance.NetworkUnknown), Port: 1234, }}} err = st.SetAPIHostPorts(hostPorts) c.Assert(err, gc.IsNil) return machine }
func (s *machineConfigSuite) TestMachineConfig(c *gc.C) { addrs := []instance.Address{instance.NewAddress("1.2.3.4", instance.NetworkUnknown)} hc := instance.MustParseHardware("mem=4G arch=amd64") apiParams := params.AddMachineParams{ Jobs: []params.MachineJob{params.JobHostUnits}, InstanceId: instance.Id("1234"), Nonce: "foo", HardwareCharacteristics: hc, Addrs: addrs, } machines, err := s.APIState.Client().AddMachines([]params.AddMachineParams{apiParams}) c.Assert(err, gc.IsNil) c.Assert(len(machines), gc.Equals, 1) machineId := machines[0].Machine machineConfig, err := client.MachineConfig(s.State, machineId, apiParams.Nonce, "") c.Assert(err, gc.IsNil) envConfig, err := s.State.EnvironConfig() c.Assert(err, gc.IsNil) env, err := environs.New(envConfig) c.Assert(err, gc.IsNil) stateInfo, apiInfo, err := env.StateInfo() c.Assert(err, gc.IsNil) c.Check(machineConfig.StateInfo.Addrs, gc.DeepEquals, stateInfo.Addrs) c.Check(machineConfig.APIInfo.Addrs, gc.DeepEquals, apiInfo.Addrs) c.Assert(machineConfig.Tools.URL, gc.Not(gc.Equals), "") }
// setMachineAddresses sets the addresses for this machine to all of the // host's non-loopback interface IP addresses. func setMachineAddresses(m *machiner.Machine) error { addrs, err := interfaceAddrs() if err != nil { return err } var hostAddresses []instance.Address for _, addr := range addrs { var ip net.IP switch addr := addr.(type) { case *net.IPAddr: ip = addr.IP case *net.IPNet: ip = addr.IP default: continue } address := instance.NewAddress(ip.String(), instance.NetworkUnknown) hostAddresses = append(hostAddresses, address) } if len(hostAddresses) == 0 { return nil } logger.Infof("setting addresses for %v to %q", m.Tag(), hostAddresses) return m.SetMachineAddresses(hostAddresses) }
func (s *APIEndpointForEnvSuite) TestAPIEndpointRefresh(c *gc.C) { defer coretesting.MakeEmptyFakeHome(c).Restore() store := newConfigStore("env-name", dummyStoreInfo) called := 0 expectState := &mockAPIState{ apiHostPorts: [][]instance.HostPort{ instance.AddressesWithPort([]instance.Address{instance.NewAddress("0.1.2.3", instance.NetworkUnknown)}, 1234), }, } apiOpen := func(apiInfo *api.Info, opts api.DialOpts) (juju.APIState, error) { c.Check(apiInfo.Tag, gc.Equals, "user-foo") c.Check(string(apiInfo.CACert), gc.Equals, "certificated") c.Check(apiInfo.Password, gc.Equals, "foopass") c.Check(opts, gc.DeepEquals, api.DefaultDialOpts()) called++ return expectState, nil } endpoint, err := juju.APIEndpointInStore("env-name", false, store, apiOpen) c.Assert(err, gc.IsNil) c.Assert(called, gc.Equals, 0) c.Check(endpoint.Addresses, gc.DeepEquals, []string{"foo.invalid"}) // However, if we ask to refresh them, we'll connect to the API and get // the freshest set endpoint, err = juju.APIEndpointInStore("env-name", true, store, apiOpen) c.Assert(err, gc.IsNil) c.Check(called, gc.Equals, 1) // This refresh now gives us the values return by APIHostPorts c.Check(endpoint.Addresses, gc.DeepEquals, []string{"0.1.2.3:1234"}) }
func (s *APIAddresserTests) TestWatchAPIHostPorts(c *gc.C) { expectServerAddrs := [][]instance.HostPort{{{ Address: instance.NewAddress("0.1.2.3", instance.NetworkUnknown), Port: 1234, }}} err := s.state.SetAPIHostPorts(expectServerAddrs) c.Assert(err, gc.IsNil) w, err := s.facade.WatchAPIHostPorts() c.Assert(err, gc.IsNil) defer statetesting.AssertStop(c, w) wc := statetesting.NewNotifyWatcherC(c, s.state, w) // Initial event. wc.AssertOneChange() // Change the state addresses and check that we get a notification expectServerAddrs[0][0].Value = "0.1.99.99" err = s.state.SetAPIHostPorts(expectServerAddrs) c.Assert(err, gc.IsNil) wc.AssertOneChange() statetesting.AssertStop(c, w) wc.AssertClosed() }
func (s *deployerSuite) SetUpTest(c *gc.C) { s.JujuConnSuite.SetUpTest(c) s.stateAPI, s.machine = s.OpenAPIAsNewMachine(c, state.JobManageEnviron, state.JobHostUnits) err := s.machine.SetAddresses(instance.NewAddress("0.1.2.3", instance.NetworkUnknown)) c.Assert(err, gc.IsNil) // Create the needed services and relate them. s.service0 = s.AddTestingService(c, "mysql", s.AddTestingCharm(c, "mysql")) s.service1 = s.AddTestingService(c, "logging", s.AddTestingCharm(c, "logging")) eps, err := s.State.InferEndpoints([]string{"mysql", "logging"}) c.Assert(err, gc.IsNil) rel, err := s.State.AddRelation(eps...) c.Assert(err, gc.IsNil) // Create principal and subordinate units and assign them. s.principal, err = s.service0.AddUnit() c.Assert(err, gc.IsNil) err = s.principal.AssignToMachine(s.machine) c.Assert(err, gc.IsNil) relUnit, err := rel.Unit(s.principal) c.Assert(err, gc.IsNil) err = relUnit.EnterScope(nil) c.Assert(err, gc.IsNil) s.subordinate, err = s.service1.Unit("logging/0") c.Assert(err, gc.IsNil) // Create the deployer facade. s.st = s.stateAPI.Deployer() c.Assert(s.st, gc.NotNil) s.APIAddresserTests = apitesting.NewAPIAddresserTests(s.st, s.BackingState) }
// AllocateAddress requests a new address to be allocated for the // given instance on the given network. func (env *environ) AllocateAddress(instId instance.Id, netId network.Id) (instance.Address, error) { if err := env.checkBroken("AllocateAddress"); err != nil { return instance.Address{}, err } estate, err := env.state() if err != nil { return instance.Address{}, err } estate.mu.Lock() defer estate.mu.Unlock() estate.maxAddr++ // TODO(dimitern) Once we have integrated networks // and addresses, make sure we return a valid address // for the given network, and we also have the network // already registered. newAddress := instance.NewAddress( fmt.Sprintf("0.1.2.%d", estate.maxAddr), instance.NetworkCloudLocal, ) estate.ops <- OpAllocateAddress{ Env: env.name, InstanceId: instId, NetworkId: netId, Address: newAddress, } return newAddress, nil }
// addAddress appends a new server derived from the given // address to servers if the address is not already found // there. func addAddress(servers [][]instance.HostPort, addr string) ([][]instance.HostPort, error) { for i, server := range servers { for j, hostPort := range server { if hostPort.NetAddr() == addr { slideAddressToFront(servers, i, j) return servers, nil } } } host, portString, err := net.SplitHostPort(addr) if err != nil { return nil, err } port, err := strconv.Atoi(portString) if err != nil { return nil, err } hostPort := instance.HostPort{ Address: instance.NewAddress(host, instance.NetworkUnknown), Port: port, } result := make([][]instance.HostPort, 0, len(servers)+1) result = append(result, []instance.HostPort{hostPort}) result = append(result, servers...) return result, nil }
func (ac *addressesChange) Addresses() ([]instance.Address, error) { var addrs []instance.Address for _, addr := range ac.addrs[0] { addrs = append(addrs, instance.NewAddress(addr, instance.NetworkUnknown)) } return addrs, nil }
// setupEnvironmentManager adds an environment manager machine and login to the API. func (s *CommonProvisionerSuite) setupEnvironmentManager(c *gc.C) { machine, err := s.State.AddMachine("quantal", state.JobManageEnviron) c.Assert(err, gc.IsNil) c.Assert(machine.Id(), gc.Equals, "0") err = machine.SetAddresses(instance.NewAddress("0.1.2.3", instance.NetworkUnknown)) c.Assert(err, gc.IsNil) s.APILogin(c, machine) }
func (i *interruptOnDial) Addresses() ([]instance.Address, error) { // kill the tomb the second time Addresses is called if !i.returned { i.returned = true } else { i.interrupted <- os.Interrupt } return []instance.Address{instance.NewAddress(i.name, instance.NetworkUnknown)}, nil }
// expectedAPIHostPorts returns the expected addresses // of the machines as created by initState. func expectedAPIHostPorts(n int) [][]instance.HostPort { servers := make([][]instance.HostPort, n) for i := range servers { servers[i] = []instance.HostPort{{ Address: instance.NewAddress(fmt.Sprintf("0.1.2.%d", i+10), instance.NetworkUnknown), Port: apiPort, }} } return servers }
func (s *machinerSuite) TestSetMachineAddresses(c *gc.C) { machine, err := s.machiner.Machine("machine-1") c.Assert(err, gc.IsNil) addr := s.machine.Addresses() c.Assert(addr, gc.HasLen, 0) addresses := []instance.Address{ instance.NewAddress("127.0.0.1", instance.NetworkUnknown), instance.NewAddress("10.0.0.1", instance.NetworkUnknown), instance.NewAddress("8.8.8.8", instance.NetworkUnknown), } err = machine.SetMachineAddresses(addresses) c.Assert(err, gc.IsNil) err = s.machine.Refresh() c.Assert(err, gc.IsNil) c.Assert(s.machine.MachineAddresses(), gc.DeepEquals, addresses) }
func (s *UnitSuite) TestPrivateAddress(c *gc.C) { machine, err := s.State.AddMachine("quantal", state.JobHostUnits) c.Assert(err, gc.IsNil) err = s.unit.AssignToMachine(machine) c.Assert(err, gc.IsNil) address, ok := s.unit.PrivateAddress() c.Check(address, gc.Equals, "") c.Assert(ok, gc.Equals, false) public := instance.NewAddress("8.8.8.8", instance.NetworkPublic) private := instance.NewAddress("127.0.0.1", instance.NetworkCloudLocal) err = machine.SetAddresses(public, private) c.Assert(err, gc.IsNil) address, ok = s.unit.PrivateAddress() c.Check(address, gc.Equals, "127.0.0.1") c.Assert(ok, gc.Equals, true) }
func (s *unitSuite) TestPrivateAddress(c *gc.C) { address, err := s.apiUnit.PrivateAddress() c.Assert(err, gc.ErrorMatches, `"unit-wordpress-0" has no private address set`) err = s.wordpressMachine.SetAddresses(instance.NewAddress("1.2.3.4", instance.NetworkCloudLocal)) c.Assert(err, gc.IsNil) address, err = s.apiUnit.PrivateAddress() c.Assert(err, gc.IsNil) c.Assert(address, gc.Equals, "1.2.3.4") }
func (s *provisionerSuite) TestStateAddresses(c *gc.C) { err := s.machine.SetAddresses(instance.NewAddress("0.1.2.3", instance.NetworkUnknown)) c.Assert(err, gc.IsNil) stateAddresses, err := s.State.Addresses() c.Assert(err, gc.IsNil) addresses, err := s.provisioner.StateAddresses() c.Assert(err, gc.IsNil) c.Assert(addresses, gc.DeepEquals, stateAddresses) }
func (s *instanceTest) TestAddresses(c *gc.C) { jsonValue := `{ "hostname": "testing.invalid", "system_id": "system_id", "ip_addresses": [ "1.2.3.4", "fe80::d806:dbff:fe23:1199" ] }` obj := s.testMAASObject.TestServer.NewNode(jsonValue) inst := maasInstance{maasObject: &obj, environ: s.makeEnviron()} expected := []instance.Address{ {Value: "testing.invalid", Type: instance.HostName, NetworkScope: instance.NetworkPublic}, instance.NewAddress("1.2.3.4", instance.NetworkUnknown), instance.NewAddress("fe80::d806:dbff:fe23:1199", instance.NetworkUnknown), } addr, err := inst.Addresses() c.Assert(err, gc.IsNil) c.Check(addr, gc.DeepEquals, expected) }
func (s *runSuite) addUnit(c *gc.C, service *state.Service) *state.Unit { unit, err := service.AddUnit() c.Assert(err, gc.IsNil) err = unit.AssignToNewMachine() c.Assert(err, gc.IsNil) mId, err := unit.AssignedMachineId() c.Assert(err, gc.IsNil) machine, err := s.State.Machine(mId) c.Assert(err, gc.IsNil) machine.SetAddresses(instance.NewAddress("10.3.2.1", instance.NetworkUnknown)) return unit }
func (s *deployerSuite) TestStateAddresses(c *gc.C) { err := s.machine.SetAddresses(instance.NewAddress("0.1.2.3", instance.NetworkUnknown)) c.Assert(err, gc.IsNil) stateAddresses, err := s.State.Addresses() c.Assert(err, gc.IsNil) c.Assert(len(stateAddresses), gc.Equals, 1) addresses, err := s.st.StateAddresses() c.Assert(err, gc.IsNil) c.Assert(addresses, gc.DeepEquals, stateAddresses) }
func (s *HookContextSuite) AddUnit(c *gc.C, svc *state.Service) *state.Unit { unit, err := svc.AddUnit() c.Assert(err, gc.IsNil) s.machine, err = s.State.AddMachine("quantal", state.JobHostUnits) c.Assert(err, gc.IsNil) err = unit.AssignToMachine(s.machine) c.Assert(err, gc.IsNil) name := strings.Replace(unit.Name(), "/", "-", 1) privateAddr := instance.NewAddress(name+".testing.invalid", instance.NetworkCloudLocal) err = s.machine.SetAddresses(privateAddr) c.Assert(err, gc.IsNil) return unit }
func (s *APIAddresserTests) TestAPIAddresses(c *gc.C) { hostPorts := [][]instance.HostPort{{{ Address: instance.NewAddress("0.1.2.3", instance.NetworkUnknown), Port: 1234, }}} err := s.state.SetAPIHostPorts(hostPorts) c.Assert(err, gc.IsNil) addresses, err := s.facade.APIAddresses() c.Assert(err, gc.IsNil) c.Assert(addresses, gc.DeepEquals, []string{"0.1.2.3:1234"}) }
func (s *deployerSuite) TestStateAddresses(c *gc.C) { err := s.machine0.SetAddresses(instance.NewAddress("0.1.2.3", instance.NetworkUnknown)) c.Assert(err, gc.IsNil) addresses, err := s.State.Addresses() c.Assert(err, gc.IsNil) result, err := s.deployer.StateAddresses() c.Assert(err, gc.IsNil) c.Assert(result, gc.DeepEquals, params.StringsResult{ Result: addresses, }) }
func (s *machinerSuite) SetUpTest(c *gc.C) { s.JujuConnSuite.SetUpTest(c) m, err := s.State.AddMachine("quantal", state.JobManageEnviron) c.Assert(err, gc.IsNil) err = m.SetAddresses(instance.NewAddress("10.0.0.1", instance.NetworkUnknown)) c.Assert(err, gc.IsNil) s.st, s.machine = s.OpenAPIAsNewMachine(c) // Create the machiner API facade. s.machiner = s.st.Machiner() c.Assert(s.machiner, gc.NotNil) s.APIAddresserTests = apitesting.NewAPIAddresserTests(s.machiner, s.BackingState) }
func (s *relationUnitSuite) TestPrivateAddress(c *gc.C) { _, apiRelUnit := s.getRelationUnits(c) // Try getting it first without an address set. address, err := apiRelUnit.PrivateAddress() c.Assert(err, gc.ErrorMatches, `"unit-wordpress-0" has no private address set`) // Set an address and try again. err = s.wordpressMachine.SetAddresses(instance.NewAddress("1.2.3.4", instance.NetworkCloudLocal)) c.Assert(err, gc.IsNil) address, err = apiRelUnit.PrivateAddress() c.Assert(err, gc.IsNil) c.Assert(address, gc.Equals, "1.2.3.4") }
func (s *commonMachineSuite) setFakeMachineAddresses(c *gc.C, machine *state.Machine) { addrs := []instance.Address{ instance.NewAddress("0.1.2.3", instance.NetworkUnknown), } err := machine.SetAddresses(addrs...) c.Assert(err, gc.IsNil) // Set the addresses in the environ instance as well so that if the instance poller // runs it won't overwrite them. instId, err := machine.InstanceId() c.Assert(err, gc.IsNil) insts, err := s.Conn.Environ.Instances([]instance.Id{instId}) c.Assert(err, gc.IsNil) dummy.SetInstanceAddresses(insts[0], addrs) }