本文整理汇总了Golang中github.com/juju/juju/network.NewHostPorts函数的典型用法代码示例。如果您正苦于以下问题:Golang NewHostPorts函数的具体用法?Golang NewHostPorts怎么用?Golang NewHostPorts使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewHostPorts函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestAPIAddressesPrivateFirst
func (s *apiAddresserSuite) TestAPIAddressesPrivateFirst(c *gc.C) {
ctlr1, err := network.ParseHostPorts("52.7.1.1:17070", "10.0.2.1:17070")
c.Assert(err, jc.ErrorIsNil)
ctlr2, err := network.ParseHostPorts("53.51.121.17:17070", "10.0.1.17:17070")
c.Assert(err, jc.ErrorIsNil)
s.fake.hostPorts = [][]network.HostPort{
network.NewHostPorts(1, "apiaddresses"),
ctlr1,
ctlr2,
network.NewHostPorts(2, "apiaddresses"),
}
for _, hps := range s.fake.hostPorts {
for _, hp := range hps {
c.Logf("%s - %#v", hp.Scope, hp)
}
}
result, err := s.addresser.APIAddresses()
c.Assert(err, jc.ErrorIsNil)
c.Check(result.Result, gc.DeepEquals, []string{
"apiaddresses:1",
"10.0.2.1:17070",
"52.7.1.1:17070",
"10.0.1.17:17070",
"53.51.121.17:17070",
"apiaddresses:2",
})
}
示例2: TestFilterUnusableHostPorts
func (s *HostPortSuite) TestFilterUnusableHostPorts(c *gc.C) {
// The order is preserved, but machine- and link-local addresses
// are dropped.
expected := append(
network.NewHostPorts(1234,
"localhost",
"example.com",
"example.org",
"2001:db8::2",
"example.net",
"invalid host",
"fd00::22",
"2001:db8::1",
"0.1.2.0",
"2001:db8::1",
"localhost",
"10.0.0.1",
"fc00::1",
"172.16.0.1",
"8.8.8.8",
"7.8.8.8",
),
network.NewHostPorts(9999,
"10.0.0.1",
"2001:db8::1", // public
)...,
)
result := network.FilterUnusableHostPorts(s.makeHostPorts())
c.Assert(result, gc.HasLen, len(expected))
c.Assert(result, jc.DeepEquals, expected)
}
示例3: TestPublisherSetsAPIHostPortsOnce
func (s *publishSuite) TestPublisherSetsAPIHostPortsOnce(c *gc.C) {
var mock mockAPIHostPortsSetter
statePublish := newPublisher(&mock)
hostPorts1 := network.NewHostPorts(1234, "testing1.invalid", "127.0.0.1")
hostPorts2 := network.NewHostPorts(1234, "testing2.invalid", "127.0.0.2")
// statePublish.publishAPIServers should not update state a second time.
apiServers := [][]network.HostPort{hostPorts1}
for i := 0; i < 2; i++ {
err := statePublish.publishAPIServers(apiServers, nil)
c.Assert(err, jc.ErrorIsNil)
}
c.Assert(mock.calls, gc.Equals, 1)
c.Assert(mock.apiHostPorts, gc.DeepEquals, apiServers)
apiServers = append(apiServers, hostPorts2)
for i := 0; i < 2; i++ {
err := statePublish.publishAPIServers(apiServers, nil)
c.Assert(err, jc.ErrorIsNil)
}
c.Assert(mock.calls, gc.Equals, 2)
c.Assert(mock.apiHostPorts, gc.DeepEquals, apiServers)
}
示例4: TestParseHostPortsSuccess
func (*HostPortSuite) TestParseHostPortsSuccess(c *gc.C) {
for i, test := range []struct {
args []string
expect []network.HostPort
}{{
args: nil,
expect: []network.HostPort{},
}, {
args: []string{"1.2.3.4:42"},
expect: network.NewHostPorts(42, "1.2.3.4"),
}, {
args: []string{"[fc00::1]:1234"},
expect: network.NewHostPorts(1234, "fc00::1"),
}, {
args: []string{"[fc00::1]:1234", "127.0.0.1:4321", "example.com:42"},
expect: []network.HostPort{
{network.NewAddress("fc00::1"), 1234},
{network.NewAddress("127.0.0.1"), 4321},
{network.NewAddress("example.com"), 42},
},
}} {
c.Logf("test %d: args %v", i, test.args)
hps, err := network.ParseHostPorts(test.args...)
c.Check(err, jc.ErrorIsNil)
c.Check(hps, jc.DeepEquals, test.expect)
}
}
示例5: SetUpTest
func (s *stateAddresserSuite) SetUpTest(c *gc.C) {
s.addresser = common.NewStateAddresser(fakeAddresses{
hostPorts: [][]network.HostPort{
network.NewHostPorts(1, "apiaddresses"),
network.NewHostPorts(2, "apiaddresses"),
},
})
}
示例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.
listener, err := net.Listen("tcp", ":0")
c.Assert(err, jc.ErrorIsNil)
srv, err := apiserver.NewServer(s.State, listener, apiserver.ServerConfig{
Cert: []byte(coretesting.ServerCert),
Key: []byte(coretesting.ServerKey),
Tag: names.NewMachineTag("0"),
})
c.Assert(err, jc.ErrorIsNil)
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,
EnvironTag: s.State.EnvironTag(),
}
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 = ipv4State.Machiner().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 = ipv6State.Machiner().Machine(machine.MachineTag())
c.Assert(err, jc.ErrorIsNil)
}
示例7: APIHostPorts
func (sb *stubBackend) APIHostPorts() ([][]network.HostPort, error) {
sb.MethodCall(sb, "APIHostPorts")
if err := sb.NextErr(); err != nil {
return nil, err
}
hps := [][]network.HostPort{
network.NewHostPorts(1234, "0.1.2.3"),
network.NewHostPorts(1234, "0.1.2.4"),
network.NewHostPorts(1234, "0.1.2.5"),
}
return hps, nil
}
示例8: TestAfterResolvingUnchangedAddressesNotCached
func (s *CacheAPIEndpointsSuite) TestAfterResolvingUnchangedAddressesNotCached(c *gc.C) {
// Test that if new endpoints hostnames are different than the
// cached hostnames, but after resolving the addresses match the
// cached addresses, the cache is not changed.
// Because Hostnames are sorted before caching, reordering them
// will simulate they have changed.
unsortedHPs := network.NewHostPorts(1234,
"ipv4.example.com",
"8.8.8.8",
"ipv6.example.com",
"10.0.0.1",
)
strUnsorted := network.HostPortsToStrings(unsortedHPs)
sortedHPs := network.NewHostPorts(1234,
"8.8.8.8",
"ipv4.example.com",
"ipv6.example.com",
"10.0.0.1",
)
resolvedHPs := network.NewHostPorts(1234,
"0.1.2.1", // from ipv4.example.com
"8.8.8.8",
"10.0.0.1",
"fc00::2", // from ipv6.example.com
)
strResolved := network.HostPortsToStrings(resolvedHPs)
controllerDetails := jujuclient.ControllerDetails{
ControllerUUID: fakeUUID,
CACert: "certificate",
UnresolvedAPIEndpoints: strUnsorted,
APIEndpoints: strResolved,
}
err := s.ControllerStore.AddController("controller-name", controllerDetails)
c.Assert(err, jc.ErrorIsNil)
addrs, hosts, changed := juju.PrepareEndpointsForCaching(
controllerDetails, [][]network.HostPort{unsortedHPs},
)
c.Assert(addrs, gc.IsNil)
c.Assert(hosts, gc.IsNil)
c.Assert(changed, jc.IsFalse)
c.Assert(s.resolveNumCalls, gc.Equals, 1)
c.Assert(s.numResolved, gc.Equals, 2)
expectLog := fmt.Sprintf("DEBUG juju.juju API hostnames changed from %v to %v - resolving hostnames", unsortedHPs, sortedHPs)
c.Assert(c.GetTestLog(), jc.Contains, expectLog)
expectLog = "DEBUG juju.juju API addresses unchanged"
c.Assert(c.GetTestLog(), jc.Contains, expectLog)
}
示例9: TestResolveCalledWithChangedHostnames
func (s *CacheAPIEndpointsSuite) TestResolveCalledWithChangedHostnames(c *gc.C) {
// Test that if new endpoints hostnames are different than the
// cached hostnames DNS resolution happens and we compare resolved
// addresses.
// Because Hostnames are sorted before caching, reordering them
// will simulate they have changed.
unsortedHPs := network.NewHostPorts(1234,
"ipv4.example.com",
"8.8.8.8",
"ipv6.example.com",
"10.0.0.1",
)
strUnsorted := network.HostPortsToStrings(unsortedHPs)
sortedHPs := network.NewHostPorts(1234,
"8.8.8.8",
"ipv4.example.com",
"ipv6.example.com",
"10.0.0.1",
)
strSorted := network.HostPortsToStrings(sortedHPs)
resolvedHPs := network.NewHostPorts(1234,
"0.1.2.1", // from ipv4.example.com
"8.8.8.8",
"10.0.0.1",
"fc00::2", // from ipv6.example.com
)
strResolved := network.HostPortsToStrings(resolvedHPs)
controllerDetails := jujuclient.ControllerDetails{
ControllerUUID: fakeUUID,
CACert: "certificate",
UnresolvedAPIEndpoints: strUnsorted,
}
err := s.ControllerStore.AddController("controller-name", controllerDetails)
c.Assert(err, jc.ErrorIsNil)
addrs, hosts, changed := juju.PrepareEndpointsForCaching(
controllerDetails, [][]network.HostPort{unsortedHPs},
)
c.Assert(addrs, jc.DeepEquals, strResolved)
c.Assert(hosts, jc.DeepEquals, strSorted)
c.Assert(changed, jc.IsTrue)
c.Assert(s.resolveNumCalls, gc.Equals, 1)
c.Assert(s.numResolved, gc.Equals, 2)
expectLog := fmt.Sprintf("DEBUG juju.juju API hostnames changed from %v to %v - resolving hostnames", unsortedHPs, sortedHPs)
c.Assert(c.GetTestLog(), jc.Contains, expectLog)
expectLog = fmt.Sprintf("INFO juju.juju new API addresses to cache %v", resolvedHPs)
c.Assert(c.GetTestLog(), jc.Contains, expectLog)
}
示例10: TestDropDuplicatedHostPorts
func (s *HostPortSuite) TestDropDuplicatedHostPorts(c *gc.C) {
hps := s.makeHostPorts()
noDups := network.DropDuplicatedHostPorts(hps)
c.Assert(noDups, gc.Not(gc.HasLen), len(hps))
c.Assert(noDups, jc.DeepEquals, network.NewHostPorts(1234,
"127.0.0.1",
"localhost",
"example.com",
"127.0.1.1",
"example.org",
"2001:db8::2",
"169.254.1.1",
"example.net",
"invalid host",
"fd00::22",
"2001:db8::1",
"169.254.1.2",
"ff01::22",
"0.1.2.0",
"10.0.0.1",
"::1",
"fc00::1",
"fe80::2",
"172.16.0.1",
"8.8.8.8",
"7.8.8.8",
))
}
示例11: TestMachineAgentRunsAPIAddressUpdaterWorker
func (s *MachineSuite) TestMachineAgentRunsAPIAddressUpdaterWorker(c *gc.C) {
// Start the machine agent.
m, _, _ := s.primeAgent(c, state.JobHostUnits)
a := s.newAgent(c, m)
go func() { c.Check(a.Run(nil), jc.ErrorIsNil) }()
defer func() { c.Check(a.Stop(), jc.ErrorIsNil) }()
// Update the API addresses.
updatedServers := [][]network.HostPort{
network.NewHostPorts(1234, "localhost"),
}
err := s.BackingState.SetAPIHostPorts(updatedServers)
c.Assert(err, jc.ErrorIsNil)
// Wait for config to be updated.
for attempt := coretesting.LongAttempt.Start(); attempt.Next(); {
s.BackingState.StartSync()
if !attempt.HasNext() {
break
}
addrs, err := a.CurrentConfig().APIAddresses()
c.Assert(err, jc.ErrorIsNil)
if reflect.DeepEqual(addrs, []string{"localhost:1234"}) {
return
}
}
c.Fatalf("timeout while waiting for agent config to change")
}
示例12: testNamespace
// testNamespace starts a worker and ensures that
// the rsyslog config file has the expected filename,
// and the appropriate log dir is used.
func (s *RsyslogSuite) testNamespace(c *gc.C, st *api.State, tag names.Tag, namespace, expectedFilename, expectedLogDir string) {
restarted := make(chan struct{}, 2) // once for create, once for teardown
s.PatchValue(rsyslog.RestartRsyslog, func() error {
restarted <- struct{}{}
return nil
})
err := os.MkdirAll(expectedLogDir, 0755)
c.Assert(err, jc.ErrorIsNil)
worker, err := rsyslog.NewRsyslogConfigWorker(st.Rsyslog(), rsyslog.RsyslogModeAccumulate, tag, namespace, []string{"0.1.2.3"})
c.Assert(err, jc.ErrorIsNil)
defer func() { c.Assert(worker.Wait(), gc.IsNil) }()
defer worker.Kill()
// change the API HostPorts to trigger an rsyslog restart
newHostPorts := network.NewHostPorts(6541, "127.0.0.1")
err = s.State.SetAPIHostPorts([][]network.HostPort{newHostPorts})
c.Assert(err, jc.ErrorIsNil)
// Wait for rsyslog to be restarted, so we can check to see
// what the name of the config file is.
waitForRestart(c, restarted)
// Ensure that ca-cert.pem gets written to the expected log dir.
waitForFile(c, filepath.Join(expectedLogDir, "ca-cert.pem"))
dir, err := os.Open(*rsyslog.RsyslogConfDir)
c.Assert(err, jc.ErrorIsNil)
names, err := dir.Readdirnames(-1)
dir.Close()
c.Assert(err, jc.ErrorIsNil)
c.Assert(names, gc.HasLen, 1)
c.Assert(names[0], gc.Equals, expectedFilename)
}
示例13: TestResolveSkippedWhenHostnamesUnchanged
func (s *CacheAPIEndpointsSuite) TestResolveSkippedWhenHostnamesUnchanged(c *gc.C) {
// Test that if new endpoints hostnames are the same as the
// cached, no DNS resolution happens (i.e. we don't resolve on
// every connection, but as needed).
info := s.store.CreateInfo("env-name")
hps := network.NewHostPorts(1234,
"8.8.8.8",
"example.com",
"10.0.0.1",
)
info.SetAPIEndpoint(configstore.APIEndpoint{
Hostnames: network.HostPortsToStrings(hps),
})
err := info.Write()
c.Assert(err, jc.ErrorIsNil)
addrs, hosts, changed := juju.PrepareEndpointsForCaching(
info, [][]network.HostPort{hps}, network.HostPort{},
)
c.Assert(addrs, gc.IsNil)
c.Assert(hosts, gc.IsNil)
c.Assert(changed, jc.IsFalse)
c.Assert(s.resolveNumCalls, gc.Equals, 0)
c.Assert(
c.GetTestLog(),
jc.Contains,
"DEBUG juju.api API hostnames unchanged - not resolving",
)
}
示例14: TestStateServersArePublished
func (s *workerSuite) TestStateServersArePublished(c *gc.C) {
DoTestForIPv4AndIPv6(func(ipVersion TestIPVersion) {
publishCh := make(chan [][]network.HostPort)
publish := func(apiServers [][]network.HostPort, instanceIds []instance.Id) error {
publishCh <- apiServers
return nil
}
st := NewFakeState()
InitState(c, st, 3, ipVersion)
w := newWorker(st, PublisherFunc(publish))
defer func() {
c.Check(worker.Stop(w), gc.IsNil)
}()
select {
case servers := <-publishCh:
AssertAPIHostPorts(c, servers, ExpectedAPIHostPorts(3, ipVersion))
case <-time.After(coretesting.LongWait):
c.Fatalf("timed out waiting for publish")
}
// Change one of the servers' API addresses and check that it's published.
var newMachine10APIHostPorts []network.HostPort
newMachine10APIHostPorts = network.NewHostPorts(apiPort, ipVersion.extraHost)
st.machine("10").setAPIHostPorts(newMachine10APIHostPorts)
select {
case servers := <-publishCh:
expected := ExpectedAPIHostPorts(3, ipVersion)
expected[0] = newMachine10APIHostPorts
AssertAPIHostPorts(c, servers, expected)
case <-time.After(coretesting.LongWait):
c.Fatalf("timed out waiting for publish")
}
})
}
示例15: TestWatchAPIHostPorts
func (s *APIAddresserTests) TestWatchAPIHostPorts(c *gc.C) {
expectServerAddrs := [][]network.HostPort{
network.NewHostPorts(1234, "0.1.2.3"),
}
err := s.state.SetAPIHostPorts(expectServerAddrs)
c.Assert(err, jc.ErrorIsNil)
w, err := s.facade.WatchAPIHostPorts()
c.Assert(err, jc.ErrorIsNil)
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, jc.ErrorIsNil)
wc.AssertOneChange()
statetesting.AssertStop(c, w)
wc.AssertClosed()
}