// Addresses is specified in the Instance interface. func (inst *azureInstance) Addresses() ([]jujunetwork.Address, error) { addresses := make([]jujunetwork.Address, 0, len(inst.networkInterfaces)+len(inst.publicIPAddresses)) for _, nic := range inst.networkInterfaces { if nic.Properties.IPConfigurations == nil { continue } for _, ipConfiguration := range *nic.Properties.IPConfigurations { privateIpAddress := ipConfiguration.Properties.PrivateIPAddress if privateIpAddress == nil { continue } addresses = append(addresses, jujunetwork.NewScopedAddress( to.String(privateIpAddress), jujunetwork.ScopeCloudLocal, )) } } for _, pip := range inst.publicIPAddresses { if pip.Properties.IPAddress == nil { continue } addresses = append(addresses, jujunetwork.NewScopedAddress( to.String(pip.Properties.IPAddress), jujunetwork.ScopePublic, )) } return addresses, nil }
// convertNovaAddresses returns nova addresses in generic format func convertNovaAddresses(publicIP string, addresses map[string][]nova.IPAddress) []network.Address { var machineAddresses []network.Address if publicIP != "" { publicAddr := network.NewScopedAddress(publicIP, network.ScopePublic) machineAddresses = append(machineAddresses, publicAddr) } // TODO(gz) Network ordering may be significant but is not preserved by // the map, see lp:1188126 for example. That could potentially be fixed // in goose, or left to be derived by other means. for netName, ips := range addresses { networkScope := network.ScopeUnknown if netName == "public" { networkScope = network.ScopePublic } for _, address := range ips { // If this address has already been added as a floating IP, skip it. if publicIP == address.Address { continue } // Assume IPv4 unless specified otherwise addrtype := network.IPv4Address if address.Version == 6 { addrtype = network.IPv6Address } machineAddr := network.NewScopedAddress(address.Address, networkScope) if machineAddr.Type != addrtype { logger.Warningf("derived address type %v, nova reports %v", machineAddr.Type, addrtype) } machineAddresses = append(machineAddresses, machineAddr) } } return machineAddresses }
func (*AddressSuite) TestExactScopeMatchHonoursPreferIPv6(c *gc.C) { network.SetPreferIPv6(true) addr := network.NewScopedAddress("10.0.0.2", network.ScopeCloudLocal) match := network.ExactScopeMatch(addr, network.ScopeCloudLocal) c.Assert(match, jc.IsFalse) match = network.ExactScopeMatch(addr, network.ScopePublic) c.Assert(match, jc.IsFalse) addr = network.NewScopedAddress("8.8.8.8", network.ScopePublic) match = network.ExactScopeMatch(addr, network.ScopeCloudLocal) c.Assert(match, jc.IsFalse) match = network.ExactScopeMatch(addr, network.ScopePublic) c.Assert(match, jc.IsFalse) addr = network.NewScopedAddress("2001:db8::ff00:42:8329", network.ScopePublic) match = network.ExactScopeMatch(addr, network.ScopeCloudLocal) c.Assert(match, jc.IsFalse) match = network.ExactScopeMatch(addr, network.ScopePublic) c.Assert(match, jc.IsTrue) addr = network.NewScopedAddress("fc00::1", network.ScopeCloudLocal) match = network.ExactScopeMatch(addr, network.ScopeCloudLocal) c.Assert(match, jc.IsTrue) match = network.ExactScopeMatch(addr, network.ScopePublic) c.Assert(match, jc.IsFalse) }
func (s *MachinerStateSuite) TestMachineAddresses(c *gc.C) { s.setupSetMachineAddresses(c, false) c.Assert(s.machine.MachineAddresses(), jc.DeepEquals, []network.Address{ network.NewAddress("2001:db8::1"), network.NewScopedAddress("10.0.0.1", network.ScopeCloudLocal), network.NewScopedAddress("::1", network.ScopeMachineLocal), network.NewScopedAddress("127.0.0.1", network.ScopeMachineLocal), }) }
func (s *AddressSuite) TestNewScopedAddressHostname(c *gc.C) { addr := network.NewScopedAddress("localhost", network.ScopeUnknown) c.Check(addr.Value, gc.Equals, "localhost") c.Check(addr.Type, gc.Equals, network.HostName) c.Check(addr.Scope, gc.Equals, network.ScopeUnknown) addr = network.NewScopedAddress("example.com", network.ScopeUnknown) c.Check(addr.Value, gc.Equals, "example.com") c.Check(addr.Type, gc.Equals, network.HostName) c.Check(addr.Scope, gc.Equals, network.ScopeUnknown) }
func (s *SSHCommonSuite) setAddresses(m *state.Machine, c *gc.C) { addrPub := network.NewScopedAddress( fmt.Sprintf("admin-%s.dns", m.Id()), network.ScopePublic, ) addrPriv := network.NewScopedAddress( fmt.Sprintf("admin-%s.internal", m.Id()), network.ScopeCloudLocal, ) err := m.SetProviderAddresses(addrPub, addrPriv) c.Assert(err, jc.ErrorIsNil) }
func (s *SSHCommonSuite) setAddresses(c *gc.C, m *state.Machine) { addrPub := network.NewScopedAddress( fmt.Sprintf("%s.public", m.Id()), network.ScopePublic, ) addrPriv := network.NewScopedAddress( fmt.Sprintf("%s.private", m.Id()), network.ScopeCloudLocal, ) err := m.SetProviderAddresses(addrPub, addrPriv) c.Assert(err, jc.ErrorIsNil) }
func (s *AddressSuite) TestNewAddressIPv6(c *gc.C) { value := "2001:db8::1" addr1 := network.NewScopedAddress(value, network.ScopeUnknown) addr2 := network.NewAddress(value) addr3 := network.NewScopedAddress(value, network.ScopeLinkLocal) // NewAddress behaves exactly like NewScopedAddress with ScopeUnknown c.Assert(addr1, jc.DeepEquals, addr2) c.Assert(addr2.Scope, gc.Equals, network.ScopePublic) // derived from value c.Assert(addr2.Value, gc.Equals, value) c.Assert(addr2.Type, gc.Equals, network.IPv6Address) c.Assert(addr2.Scope, gc.Not(gc.Equals), addr3.Scope) // different scope c.Assert(addr3.Scope, gc.Equals, network.ScopeLinkLocal) }
func (*AddressSuite) TestExactScopeMatch(c *gc.C) { addr := network.NewScopedAddress("10.0.0.2", network.ScopeCloudLocal) match := network.ExactScopeMatch(addr, network.ScopeCloudLocal) c.Assert(match, jc.IsTrue) match = network.ExactScopeMatch(addr, network.ScopePublic) c.Assert(match, jc.IsFalse) addr = network.NewScopedAddress("8.8.8.8", network.ScopePublic) match = network.ExactScopeMatch(addr, network.ScopeCloudLocal) c.Assert(match, jc.IsFalse) match = network.ExactScopeMatch(addr, network.ScopePublic) c.Assert(match, jc.IsTrue) }
func (suite *environSuite) TestNetworkInterfacesLegacy(c *gc.C) { testInstance := suite.createSubnets(c, false) netInfo, err := suite.makeEnviron().NetworkInterfaces(testInstance.Id()) c.Assert(err, jc.ErrorIsNil) expectedInfo := []network.InterfaceInfo{{ DeviceIndex: 0, MACAddress: "aa:bb:cc:dd:ee:ff", CIDR: "192.168.1.2/24", ProviderSubnetId: "WLAN", VLANTag: 0, InterfaceName: "wlan0", Disabled: true, NoAutoStart: true, ConfigType: network.ConfigDHCP, ExtraConfig: nil, GatewayAddress: network.Address{}, Address: network.NewScopedAddress("192.168.1.2", network.ScopeCloudLocal), }, { DeviceIndex: 1, MACAddress: "aa:bb:cc:dd:ee:f1", CIDR: "192.168.2.2/24", ProviderSubnetId: "LAN", VLANTag: 42, InterfaceName: "eth0", Disabled: false, NoAutoStart: false, ConfigType: network.ConfigDHCP, ExtraConfig: nil, GatewayAddress: network.NewScopedAddress("192.168.2.1", network.ScopeCloudLocal), Address: network.NewScopedAddress("192.168.2.2", network.ScopeCloudLocal), }, { DeviceIndex: 2, MACAddress: "aa:bb:cc:dd:ee:f2", CIDR: "192.168.3.2/24", ProviderSubnetId: "Virt", VLANTag: 0, InterfaceName: "vnet1", Disabled: false, NoAutoStart: false, ConfigType: network.ConfigDHCP, ExtraConfig: nil, GatewayAddress: network.Address{}, Address: network.NewScopedAddress("192.168.3.2", network.ScopeCloudLocal), }} network.SortInterfaceInfo(netInfo) c.Assert(netInfo, jc.DeepEquals, expectedInfo) }
func (c *client) GetNetworkInterfaces(inst instance.Id, ecfg *environConfig) ([]network.InterfaceInfo, error) { vm, err := c.getVm(string(inst)) if err != nil { return nil, errors.Trace(err) } if vm.Guest == nil { return nil, errors.Errorf("vm guest is not initialized") } res := make([]network.InterfaceInfo, 0) for _, net := range vm.Guest.Net { ipScope := network.ScopeCloudLocal if net.Network == ecfg.externalNetwork() { ipScope = network.ScopePublic } res = append(res, network.InterfaceInfo{ DeviceIndex: net.DeviceConfigId, MACAddress: net.MacAddress, NetworkName: net.Network, Disabled: !net.Connected, ProviderId: network.Id(fmt.Sprintf("net-device%d", net.DeviceConfigId)), ProviderSubnetId: network.Id(net.Network), InterfaceName: fmt.Sprintf("unsupported%d", net.DeviceConfigId), ConfigType: network.ConfigDHCP, Address: network.NewScopedAddress(net.IpAddress[0], ipScope), }) } return res, nil }
func (s *IPAddressSuite) TestAllocateTo(c *gc.C) { machine := s.createMachine(c) addr := network.NewScopedAddress("0.1.2.3", network.ScopePublic) ipAddr, err := s.State.AddIPAddress(addr, "foobar") c.Assert(err, jc.ErrorIsNil) c.Assert(ipAddr.State(), gc.Equals, state.AddressStateUnknown) c.Assert(ipAddr.MachineId(), gc.Equals, "") c.Assert(ipAddr.InterfaceId(), gc.Equals, "") c.Assert(ipAddr.InstanceId(), gc.Equals, instance.UnknownId) err = ipAddr.AllocateTo(machine.Id(), "wobble") c.Assert(err, jc.ErrorIsNil) c.Assert(ipAddr.State(), gc.Equals, state.AddressStateAllocated) c.Assert(ipAddr.MachineId(), gc.Equals, machine.Id()) c.Assert(ipAddr.InterfaceId(), gc.Equals, "wobble") c.Assert(ipAddr.InstanceId(), gc.Equals, instance.Id("foo")) freshCopy, err := s.State.IPAddress("0.1.2.3") c.Assert(err, jc.ErrorIsNil) c.Assert(freshCopy.State(), gc.Equals, state.AddressStateAllocated) c.Assert(freshCopy.MachineId(), gc.Equals, machine.Id()) c.Assert(freshCopy.InterfaceId(), gc.Equals, "wobble") c.Assert(freshCopy.InstanceId(), gc.Equals, instance.Id("foo")) // allocating twice should fail. machine2 := s.createMachine(c) err = ipAddr.AllocateTo(machine2.Id(), "i") msg := fmt.Sprintf( `cannot allocate IP address "public:0.1.2.3" to machine %q, interface "i": `+ `already allocated or unavailable`, machine2.Id()) c.Assert(err, gc.ErrorMatches, msg) }
func (s *IPAddressSuite) TestEnsureDeadRemove(c *gc.C) { addr := network.NewScopedAddress("0.1.2.3", network.ScopePublic) ipAddr, err := s.State.AddIPAddress(addr, "foobar") c.Assert(err, jc.ErrorIsNil) // Should not be able to remove an Alive IP address. c.Assert(ipAddr.Life(), gc.Equals, state.Alive) err = ipAddr.Remove() msg := fmt.Sprintf("cannot remove IP address %q: IP address is not dead", ipAddr.String()) c.Assert(err, gc.ErrorMatches, msg) err = ipAddr.EnsureDead() c.Assert(err, jc.ErrorIsNil) // EnsureDead twice should not be an error err = ipAddr.EnsureDead() c.Assert(err, jc.ErrorIsNil) err = ipAddr.Remove() c.Assert(err, jc.ErrorIsNil) // Remove twice is also fine. err = ipAddr.Remove() c.Assert(err, jc.ErrorIsNil) _, err = s.State.IPAddress("0.1.2.3") c.Assert(err, jc.Satisfies, errors.IsNotFound) c.Assert(err, gc.ErrorMatches, `IP address "0.1.2.3" not found`) }
// internalNetworkAddress returns the instance's jujunetwork.Address for the // internal virtual network. This address is used to identify the machine in // network security rules. func (inst *azureInstance) internalNetworkAddress() (jujunetwork.Address, error) { inst.env.mu.Lock() subscriptionId := inst.env.config.subscriptionId resourceGroup := inst.env.resourceGroup controllerResourceGroup := inst.env.controllerResourceGroup inst.env.mu.Unlock() internalSubnetId := internalSubnetId( resourceGroup, controllerResourceGroup, subscriptionId, ) for _, nic := range inst.networkInterfaces { if nic.Properties.IPConfigurations == nil { continue } for _, ipConfiguration := range *nic.Properties.IPConfigurations { if ipConfiguration.Properties.Subnet == nil { continue } if strings.ToLower(to.String(ipConfiguration.Properties.Subnet.ID)) != strings.ToLower(internalSubnetId) { continue } privateIpAddress := ipConfiguration.Properties.PrivateIPAddress if privateIpAddress == nil { continue } return jujunetwork.NewScopedAddress( to.String(privateIpAddress), jujunetwork.ScopeCloudLocal, ), nil } } return jujunetwork.Address{}, errors.NotFoundf("internal network address") }
func (s *InterfaceSuite) TestUnitCaching(c *gc.C) { ctx := s.GetContext(c, -1, "") pr, err := ctx.PrivateAddress() c.Assert(err, jc.ErrorIsNil) c.Assert(pr, gc.Equals, "u-0.testing.invalid") pa, err := ctx.PublicAddress() c.Assert(err, jc.ErrorIsNil) // Initially the public address is the same as the private address since // the "most public" address is chosen. c.Assert(pr, gc.Equals, pa) // Change remote state. err = s.machine.SetProviderAddresses( network.NewScopedAddress("blah.testing.invalid", network.ScopePublic), ) c.Assert(err, jc.ErrorIsNil) // Local view is unchanged. pr, err = ctx.PrivateAddress() c.Assert(err, jc.ErrorIsNil) c.Assert(pr, gc.Equals, "u-0.testing.invalid") pa, err = ctx.PublicAddress() c.Assert(err, jc.ErrorIsNil) c.Assert(pr, gc.Equals, pa) }
func (s *ContextSuite) AddUnit(c *gc.C, svc *state.Service) *state.Unit { unit, err := svc.AddUnit() c.Assert(err, jc.ErrorIsNil) if s.machine != nil { err = unit.AssignToMachine(s.machine) c.Assert(err, jc.ErrorIsNil) return unit } err = s.State.AssignUnit(unit, state.AssignCleanEmpty) c.Assert(err, jc.ErrorIsNil) machineId, err := unit.AssignedMachineId() c.Assert(err, jc.ErrorIsNil) s.machine, err = s.State.Machine(machineId) c.Assert(err, jc.ErrorIsNil) zone := "a-zone" hwc := instance.HardwareCharacteristics{ AvailabilityZone: &zone, } err = s.machine.SetProvisioned("i-exist", "fake_nonce", &hwc) c.Assert(err, jc.ErrorIsNil) name := strings.Replace(unit.Name(), "/", "-", 1) privateAddr := network.NewScopedAddress(name+".testing.invalid", network.ScopeCloudLocal) err = s.machine.SetProviderAddresses(privateAddr) c.Assert(err, jc.ErrorIsNil) return unit }
func (t *localServerSuite) TestNetworkInterfaces(c *gc.C) { env, instId := t.setUpInstanceWithDefaultVpc(c) interfaces, err := env.NetworkInterfaces(instId) c.Assert(err, jc.ErrorIsNil) // The CIDR isn't predictable, but it is in the 10.10.x.0/24 format // The subnet ID is in the form "subnet-x", where x matches the same // number from the CIDR. The interfaces address is part of the CIDR. // For these reasons we check that the CIDR is in the expected format // and derive the expected values for ProviderSubnetId and Address. c.Assert(interfaces, gc.HasLen, 1) cidr := interfaces[0].CIDR re := regexp.MustCompile(`10\.10\.(\d+)\.0/24`) c.Assert(re.Match([]byte(cidr)), jc.IsTrue) index := re.FindStringSubmatch(cidr)[1] addr := fmt.Sprintf("10.10.%s.5", index) subnetId := network.Id("subnet-" + index) expectedInterfaces := []network.InterfaceInfo{{ DeviceIndex: 0, MACAddress: "20:01:60:cb:27:37", CIDR: cidr, ProviderId: "eni-0", ProviderSubnetId: subnetId, VLANTag: 0, InterfaceName: "unsupported0", Disabled: false, NoAutoStart: false, ConfigType: network.ConfigDHCP, Address: network.NewScopedAddress(addr, network.ScopeCloudLocal), }} c.Assert(interfaces, jc.DeepEquals, expectedInterfaces) }
func (s *MachinerStateSuite) TestMachineAddresses(c *gc.C) { lxcFakeNetConfig := filepath.Join(c.MkDir(), "lxc-net") netConf := []byte(` # comments ignored LXC_BR= ignored LXC_ADDR = "fooo" LXC_BRIDGE="foobar" # detected anything else ignored LXC_BRIDGE="ignored"`[1:]) err := ioutil.WriteFile(lxcFakeNetConfig, netConf, 0644) c.Assert(err, jc.ErrorIsNil) s.PatchValue(machiner.InterfaceAddrs, func() ([]net.Addr, error) { addrs := []net.Addr{ &net.IPAddr{IP: net.IPv4(10, 0, 0, 1)}, &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}, &net.IPAddr{IP: net.IPv4(10, 0, 3, 1)}, // lxc bridge address ignored &net.IPAddr{IP: net.IPv6loopback}, &net.UnixAddr{}, // not IP, ignored &net.IPAddr{IP: net.IPv4(10, 0, 3, 4)}, // lxc bridge address ignored &net.IPNet{IP: net.ParseIP("2001:db8::1")}, &net.IPAddr{IP: net.IPv4(169, 254, 1, 20)}, // LinkLocal Ignored &net.IPNet{IP: net.ParseIP("fe80::1")}, // LinkLocal Ignored } return addrs, nil }) s.PatchValue(&network.InterfaceByNameAddrs, func(name string) ([]net.Addr, error) { c.Assert(name, gc.Equals, "foobar") return []net.Addr{ &net.IPAddr{IP: net.IPv4(10, 0, 3, 1)}, &net.IPAddr{IP: net.IPv4(10, 0, 3, 4)}, }, nil }) s.PatchValue(&network.LXCNetDefaultConfig, lxcFakeNetConfig) mr := s.makeMachiner() defer worker.Stop(mr) c.Assert(s.machine.Destroy(), gc.IsNil) s.State.StartSync() c.Assert(mr.Wait(), gc.Equals, worker.ErrTerminateAgent) c.Assert(s.machine.Refresh(), gc.IsNil) c.Assert(s.machine.MachineAddresses(), jc.DeepEquals, []network.Address{ network.NewAddress("2001:db8::1"), network.NewScopedAddress("10.0.0.1", network.ScopeCloudLocal), network.NewScopedAddress("::1", network.ScopeMachineLocal), network.NewScopedAddress("127.0.0.1", network.ScopeMachineLocal), }) }
func (s *HookContextSuite) AddUnit(c *gc.C, svc *state.Service) *state.Unit { unit := s.addUnit(c, svc) name := strings.Replace(unit.Name(), "/", "-", 1) privateAddr := network.NewScopedAddress(name+".testing.invalid", network.ScopeCloudLocal) err := s.machine.SetProviderAddresses(privateAddr) c.Assert(err, jc.ErrorIsNil) return unit }
func (s *RelationerImplicitSuite) TestImplicitRelationer(c *gc.C) { // Create a relationer for an implicit endpoint (mysql:juju-info). mysql := s.AddTestingService(c, "mysql", s.AddTestingCharm(c, "mysql")) u, err := mysql.AddUnit() c.Assert(err, jc.ErrorIsNil) machine, err := s.State.AddMachine("quantal", state.JobHostUnits) c.Assert(err, jc.ErrorIsNil) err = u.AssignToMachine(machine) c.Assert(err, jc.ErrorIsNil) err = machine.SetProviderAddresses(network.NewScopedAddress("blah", network.ScopeCloudLocal)) c.Assert(err, jc.ErrorIsNil) s.AddTestingService(c, "logging", s.AddTestingCharm(c, "logging")) eps, err := s.State.InferEndpoints("logging", "mysql") c.Assert(err, jc.ErrorIsNil) rel, err := s.State.AddRelation(eps...) c.Assert(err, jc.ErrorIsNil) relsDir := c.MkDir() dir, err := relation.ReadStateDir(relsDir, rel.Id()) c.Assert(err, jc.ErrorIsNil) password, err := utils.RandomPassword() c.Assert(err, jc.ErrorIsNil) err = u.SetPassword(password) c.Assert(err, jc.ErrorIsNil) st := s.OpenAPIAs(c, u.Tag(), password) uniterState, err := st.Uniter() c.Assert(err, jc.ErrorIsNil) c.Assert(uniterState, gc.NotNil) apiUnit, err := uniterState.Unit(u.Tag().(names.UnitTag)) c.Assert(err, jc.ErrorIsNil) apiRel, err := uniterState.Relation(rel.Tag().(names.RelationTag)) c.Assert(err, jc.ErrorIsNil) apiRelUnit, err := apiRel.Unit(apiUnit) c.Assert(err, jc.ErrorIsNil) r := relation.NewRelationer(apiRelUnit, dir) c.Assert(r, jc.Satisfies, (*relation.Relationer).IsImplicit) // Hooks are not allowed. f := func() { r.PrepareHook(hook.Info{}) } c.Assert(f, gc.PanicMatches, "implicit relations must not run hooks") f = func() { r.CommitHook(hook.Info{}) } c.Assert(f, gc.PanicMatches, "implicit relations must not run hooks") // Set it to Dying; check that the dir is removed immediately. err = r.SetDying() c.Assert(err, jc.ErrorIsNil) path := strconv.Itoa(rel.Id()) ft.Removed{path}.Check(c, relsDir) err = rel.Destroy() c.Assert(err, jc.ErrorIsNil) err = rel.Refresh() c.Assert(err, jc.Satisfies, errors.IsNotFound) }
func (s *IPAddressSuite) TestAddIPAddressAlreadyExists(c *gc.C) { addr := network.NewScopedAddress("0.1.2.3", network.ScopePublic) _, err := s.State.AddIPAddress(addr, "foobar") c.Assert(err, jc.ErrorIsNil) _, err = s.State.AddIPAddress(addr, "foobar") c.Assert(err, jc.Satisfies, errors.IsAlreadyExists) c.Assert(err, gc.ErrorMatches, `cannot add IP address "public:0.1.2.3": address already exists`, ) }
func (s *UnitSuite) TestPrivateAddress(c *gc.C) { machine, err := s.State.AddMachine("quantal", state.JobHostUnits) c.Assert(err, jc.ErrorIsNil) err = s.unit.AssignToMachine(machine) c.Assert(err, jc.ErrorIsNil) _, err = s.unit.PrivateAddress() c.Assert(err, jc.Satisfies, network.IsNoAddress) public := network.NewScopedAddress("8.8.8.8", network.ScopePublic) private := network.NewScopedAddress("127.0.0.1", network.ScopeCloudLocal) err = machine.SetProviderAddresses(public, private) c.Assert(err, jc.ErrorIsNil) address, err := s.unit.PrivateAddress() c.Assert(err, jc.ErrorIsNil) c.Check(address.Value, gc.Equals, "127.0.0.1") }
func (s *clientSuite) TestClientPrivateAddress(c *gc.C) { s.setUpScenario(c) // Internally, network.SelectInternalAddress is used; the public // address if no cloud-local one is available. m1, err := s.State.Machine("1") c.Assert(err, jc.ErrorIsNil) cloudLocalAddress := network.NewScopedAddress("cloudlocal", network.ScopeCloudLocal) publicAddress := network.NewScopedAddress("public", network.ScopePublic) err = m1.SetProviderAddresses(publicAddress) c.Assert(err, jc.ErrorIsNil) addr, err := s.APIState.Client().PrivateAddress("1") c.Assert(err, jc.ErrorIsNil) c.Assert(addr, gc.Equals, "public") err = m1.SetProviderAddresses(cloudLocalAddress, publicAddress) addr, err = s.APIState.Client().PrivateAddress("1") c.Assert(err, jc.ErrorIsNil) c.Assert(addr, gc.Equals, "cloudlocal") }
func (s *UnitSuite) TestPublicAddress(c *gc.C) { machine, err := s.State.AddMachine("quantal", state.JobHostUnits) c.Assert(err, jc.ErrorIsNil) err = s.unit.AssignToMachine(machine) c.Assert(err, jc.ErrorIsNil) address, ok := s.unit.PublicAddress() c.Check(address, gc.Equals, "") c.Assert(ok, jc.IsFalse) public := network.NewScopedAddress("8.8.8.8", network.ScopePublic) private := network.NewScopedAddress("127.0.0.1", network.ScopeCloudLocal) err = machine.SetProviderAddresses(public, private) c.Assert(err, jc.ErrorIsNil) address, ok = s.unit.PublicAddress() c.Check(address, gc.Equals, "8.8.8.8") c.Assert(ok, jc.IsTrue) }
func (s *clientSuite) TestClientPrivateAddressUnit(c *gc.C) { s.setUpScenario(c) m1, err := s.State.Machine("1") privateAddress := network.NewScopedAddress("private", network.ScopeCloudLocal) err = m1.SetProviderAddresses(privateAddress) c.Assert(err, jc.ErrorIsNil) addr, err := s.APIState.Client().PrivateAddress("wordpress/0") c.Assert(err, jc.ErrorIsNil) c.Assert(addr, gc.Equals, "private") }
func (s *SCPSuite) TestSCPCommand(c *gc.C) { m := s.makeMachines(4, c, true) ch := testcharms.Repo.CharmDir("dummy") curl := charm.MustParseURL( fmt.Sprintf("local:quantal/%s-%d", ch.Meta().Name, ch.Revision()), ) info := state.CharmInfo{ Charm: ch, ID: curl, StoragePath: "dummy-path", SHA256: "dummy-1-sha256", } dummyCharm, err := s.State.AddCharm(info) c.Assert(err, jc.ErrorIsNil) srv := s.AddTestingService(c, "mysql", dummyCharm) s.addUnit(srv, m[0], c) srv = s.AddTestingService(c, "mongodb", dummyCharm) s.addUnit(srv, m[1], c) s.addUnit(srv, m[2], c) srv = s.AddTestingService(c, "ipv6-svc", dummyCharm) s.addUnit(srv, m[3], c) // Simulate machine 3 has a public IPv6 address. ipv6Addr := network.NewScopedAddress("2001:db8::1", network.ScopePublic) err = m[3].SetProviderAddresses(ipv6Addr) c.Assert(err, jc.ErrorIsNil) for i, t := range scpTests { c.Logf("test %d: %s -> %s\n", i, t.about, t.args) ctx := coretesting.Context(c) scpcmd := &scpCommand{} scpcmd.proxy = t.proxy err := modelcmd.Wrap(scpcmd).Init(t.args) c.Check(err, jc.ErrorIsNil) err = modelcmd.Wrap(scpcmd).Run(ctx) if t.error != "" { c.Check(err, gc.ErrorMatches, t.error) c.Check(t.result, gc.Equals, "") } else { c.Check(err, jc.ErrorIsNil) // we suppress stdout from scp c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "") c.Check(ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, "") data, err := ioutil.ReadFile(filepath.Join(s.bin, "scp.args")) c.Check(err, jc.ErrorIsNil) actual := string(data) if t.proxy { actual = strings.Replace(actual, ".dns", ".internal", 2) } c.Check(actual, gc.Equals, t.result) } } }
func (s *IPAddressSuite) TestIPAddressByTag(c *gc.C) { addr := network.NewScopedAddress("0.1.2.3", network.ScopePublic) added, err := s.State.AddIPAddress(addr, "foobar") c.Assert(err, jc.ErrorIsNil) uuid, err := added.UUID() c.Assert(err, jc.ErrorIsNil) tag := names.NewIPAddressTag(uuid.String()) found, err := s.State.IPAddressByTag(tag) c.Assert(err, jc.ErrorIsNil) c.Assert(found.Id(), gc.Equals, added.Id()) }
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{&obj} expected := []network.Address{ network.NewScopedAddress("testing.invalid", network.ScopePublic), network.NewScopedAddress("testing.invalid", network.ScopeCloudLocal), network.NewAddress("1.2.3.4"), network.NewAddress("fe80::d806:dbff:fe23:1199"), } addr, err := inst.Addresses() c.Assert(err, jc.ErrorIsNil) c.Check(addr, gc.DeepEquals, expected) }
func (s *deployerSuite) TestConnectionInfo(c *gc.C) { err := s.machine0.SetProviderAddresses(network.NewScopedAddress("0.1.2.3", network.ScopePublic), network.NewScopedAddress("1.2.3.4", network.ScopeCloudLocal)) c.Assert(err, jc.ErrorIsNil) // Default host port scope is public, so change the cloud-local one hostPorts := network.NewHostPorts(1234, "0.1.2.3", "1.2.3.4") hostPorts[1].Scope = network.ScopeCloudLocal err = s.State.SetAPIHostPorts([][]network.HostPort{hostPorts}) c.Assert(err, jc.ErrorIsNil) expected := params.DeployerConnectionValues{ StateAddresses: []string{"1.2.3.4:1234"}, APIAddresses: []string{"1.2.3.4:1234", "0.1.2.3:1234"}, } result, err := s.deployer.ConnectionInfo() c.Assert(err, jc.ErrorIsNil) c.Assert(result, gc.DeepEquals, expected) }
func (s *suite) TestReleaseAddress(c *gc.C) { e := s.bootstrapTestEnviron(c, false) defer func() { err := e.Destroy() c.Assert(err, jc.ErrorIsNil) }() inst, _ := jujutesting.AssertStartInstance(c, e, "0") c.Assert(inst, gc.NotNil) subnetId := network.Id("net1") opc := make(chan dummy.Operation, 200) dummy.Listen(opc) // Release a couple of addresses. address := network.NewScopedAddress("0.1.2.1", network.ScopeCloudLocal) macAddress := "foobar" hostname := "myhostname" err := e.ReleaseAddress(inst.Id(), subnetId, address, macAddress, hostname) c.Assert(err, jc.ErrorIsNil) assertReleaseAddress(c, e, opc, inst.Id(), subnetId, address, macAddress, hostname) address = network.NewScopedAddress("0.1.2.2", network.ScopeCloudLocal) err = e.ReleaseAddress(inst.Id(), subnetId, address, macAddress, hostname) c.Assert(err, jc.ErrorIsNil) assertReleaseAddress(c, e, opc, inst.Id(), subnetId, address, macAddress, hostname) // Test we can induce errors. s.breakMethods(c, e, "ReleaseAddress") address = network.NewScopedAddress("0.1.2.3", network.ScopeCloudLocal) err = e.ReleaseAddress(inst.Id(), subnetId, address, macAddress, hostname) c.Assert(err, gc.ErrorMatches, `dummy\.ReleaseAddress is broken`) // Finally, test the method respects the feature flag when // disabled. s.SetFeatureFlags() // clear the flags. err = e.ReleaseAddress(inst.Id(), subnetId, address, macAddress, hostname) c.Assert(err, gc.ErrorMatches, "address allocation not supported") c.Assert(err, jc.Satisfies, errors.IsNotSupported) }