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", }) }
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) }
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) }
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) } }
func (s *stateAddresserSuite) SetUpTest(c *gc.C) { s.addresser = common.NewStateAddresser(fakeAddresses{ hostPorts: [][]network.HostPort{ network.NewHostPorts(1, "apiaddresses"), network.NewHostPorts(2, "apiaddresses"), }, }) }
func (s *apiAddresserSuite) SetUpTest(c *gc.C) { s.fake = &fakeAddresses{ hostPorts: [][]network.HostPort{ network.NewHostPorts(1, "apiaddresses"), network.NewHostPorts(2, "apiaddresses"), }, } s.addresser = common.NewAPIAddresser(s.fake, common.NewResources()) }
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) }
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 }
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) }
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) }
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", )) }
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") }
// 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) }
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", ) }
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") } }) }
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() }
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(st.Machiner(), 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) } }
func (*HostPortSuite) makeHostPorts() []network.HostPort { return network.NewHostPorts(1234, "127.0.0.1", // machine-local "localhost", // hostname "example.com", // hostname "127.0.1.1", // machine-local "example.org", // hostname "2001:db8::2", // public "169.254.1.1", // link-local "example.net", // hostname "invalid host", // hostname "fd00::22", // cloud-local "127.0.0.1", // machine-local "2001:db8::1", // public "169.254.1.2", // link-local "ff01::22", // link-local "0.1.2.0", // public "2001:db8::1", // public "localhost", // hostname "10.0.0.1", // cloud-local "::1", // machine-local "fc00::1", // cloud-local "fe80::2", // link-local "172.16.0.1", // cloud-local "::1", // machine-local "8.8.8.8", // public "7.8.8.8", // public ) }
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). hps := network.NewHostPorts(1234, "8.8.8.8", "example.com", "10.0.0.1", ) controllerDetails := jujuclient.ControllerDetails{ ControllerUUID: fakeUUID, CACert: "certificate", UnresolvedAPIEndpoints: network.HostPortsToStrings(hps), } err := s.ControllerStore.AddController("controller-name", controllerDetails) c.Assert(err, jc.ErrorIsNil) addrs, hosts, changed := juju.PrepareEndpointsForCaching( controllerDetails, [][]network.HostPort{hps}, ) 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.juju API hostnames unchanged - not resolving", ) }
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. info := s.store.CreateInfo("env-name") // 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) info.SetAPIEndpoint(configstore.APIEndpoint{ Hostnames: strUnsorted, Addresses: strResolved, }) err := info.Write() c.Assert(err, jc.ErrorIsNil) addrs, hosts, changed := juju.PrepareEndpointsForCaching( info, [][]network.HostPort{unsortedHPs}, network.HostPort{}, ) 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.api API hostnames changed from %v to %v - resolving hostnames", unsortedHPs, sortedHPs) c.Assert(c.GetTestLog(), jc.Contains, expectLog) expectLog = "DEBUG juju.api API addresses unchanged" c.Assert(c.GetTestLog(), jc.Contains, expectLog) }
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. info := s.store.CreateInfo("env-name") // 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) info.SetAPIEndpoint(configstore.APIEndpoint{ Hostnames: strUnsorted, }) err := info.Write() c.Assert(err, jc.ErrorIsNil) addrs, hosts, changed := juju.PrepareEndpointsForCaching( info, [][]network.HostPort{unsortedHPs}, network.HostPort{}, ) 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.api API hostnames changed from %v to %v - resolving hostnames", unsortedHPs, sortedHPs) c.Assert(c.GetTestLog(), jc.Contains, expectLog) expectLog = fmt.Sprintf("INFO juju.api new API addresses to cache %v", resolvedHPs) c.Assert(c.GetTestLog(), jc.Contains, expectLog) }
func (s *CacheAPIEndpointsSuite) TestResolveCalledWithInitialEndpoints(c *gc.C) { // Test that if no hostnames exist cached we call resolve (i.e. // simulate the behavior right after bootstrap) // 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", ) 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", } 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 %v - resolving hostnames", 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) }
func (s *APIAddresserTests) TestAPIHostPorts(c *gc.C) { ipv6Addr := network.NewScopedAddress( "2001:DB8::1", network.ScopeCloudLocal, ) expectServerAddrs := [][]network.HostPort{ network.NewHostPorts(999, "0.1.2.24"), network.NewHostPorts(1234, "example.com"), network.AddressesWithPort([]network.Address{ipv6Addr}, 999), } err := s.state.SetAPIHostPorts(expectServerAddrs) c.Assert(err, jc.ErrorIsNil) serverAddrs, err := s.facade.APIHostPorts() c.Assert(err, jc.ErrorIsNil) c.Assert(serverAddrs, gc.DeepEquals, expectServerAddrs) }
func (*HostPortSuite) TestCollapseHostPorts(c *gc.C) { servers := [][]network.HostPort{ network.NewHostPorts(1234, "0.1.2.3", "10.0.1.2", "fc00::1", "2001:db8::1", "::1", "127.0.0.1", "localhost", "fe80::123", "example.com", ), network.NewHostPorts(4321, "8.8.8.8", "1.2.3.4", "fc00::2", "127.0.0.1", "foo", ), network.NewHostPorts(9999, "localhost", "127.0.0.1", ), } expected := append(servers[0], append(servers[1], servers[2]...)...) result := network.CollapseHostPorts(servers) c.Assert(result, gc.HasLen, len(servers[0])+len(servers[1])+len(servers[2])) c.Assert(result, jc.DeepEquals, expected) }
func (s *publishSuite) TestPublisherSortsHostPorts(c *gc.C) { ipV4First := network.NewHostPorts(1234, "testing1.invalid", "127.0.0.1", "::1") ipV6First := network.NewHostPorts(1234, "testing1.invalid", "::1", "127.0.0.1") check := func(publish, expect []network.HostPort) { var mock mockAPIHostPortsSetter statePublish := newPublisher(&mock) for i := 0; i < 2; i++ { err := statePublish.publishAPIServers([][]network.HostPort{publish}, nil) c.Assert(err, jc.ErrorIsNil) } c.Assert(mock.calls, gc.Equals, 1) c.Assert(mock.apiHostPorts, gc.DeepEquals, [][]network.HostPort{expect}) } check(ipV6First, ipV4First) check(ipV4First, ipV4First) }
func (*HostPortSuite) TestNewHostPorts(c *gc.C) { addrs := []string{"0.1.2.3", "fc00::1", "::1", "example.com"} expected := network.AddressesWithPort( network.NewAddresses(addrs...), 42, ) result := network.NewHostPorts(42, addrs...) c.Assert(result, gc.HasLen, len(addrs)) c.Assert(result, jc.DeepEquals, expected) }
func (s *toolsSuite) TestToolsURLGetter(c *gc.C) { g := common.NewToolsURLGetter("my-uuid", mockAPIHostPortsGetter{ hostPorts: [][]network.HostPort{ network.NewHostPorts(1234, "0.1.2.3"), }, }) url, err := g.ToolsURL(current) c.Assert(err, jc.ErrorIsNil) c.Assert(url, gc.Equals, "https://0.1.2.3:1234/model/my-uuid/tools/"+current.String()) }
// ExpectedAPIHostPorts returns the expected addresses // of the machines as created by InitState. func ExpectedAPIHostPorts(n int, ipVersion TestIPVersion) [][]network.HostPort { servers := make([][]network.HostPort, n) for i := range servers { servers[i] = network.NewHostPorts( apiPort, fmt.Sprintf(ipVersion.formatHost, i+10), ) } return servers }
func (s *CacheAPIEndpointsSuite) SetUpTest(c *gc.C) { s.PatchValue(juju.ResolveOrDropHostnames, s.mockResolveOrDropHostnames) s.hostPorts = [][]network.HostPort{ network.NewHostPorts(1234, "1.0.0.1", "192.0.0.1", "127.0.0.1", "ipv4+6.example.com", "localhost", "169.254.1.1", "ipv4.example.com", "invalid host", "ipv6+6.example.com", "ipv4+4.example.com", "::1", "fe80::1", "ipv6.example.com", "fc00::111", "2001:db8::1", ), network.NewHostPorts(1235, "1.0.0.2", "2001:db8::2", "::1", "127.0.0.1", "ipv6+4.example.com", "localhost", ), } s.gocheckC = c s.resolveSeq = 1 s.resolveNumCalls = 0 s.numResolved = 0 s.envTag = names.NewEnvironTag(fakeUUID) s.store = configstore.NewMem() s.JujuConnSuite.SetUpTest(c) apiHostPort, err := network.ParseHostPorts(s.APIState.Addr()) c.Assert(err, jc.ErrorIsNil) s.apiHostPort = apiHostPort[0] }
func (s *CacheAPIEndpointsSuite) TestResolveCalledWithInitialEndpoints(c *gc.C) { // Test that if no hostnames exist cached we call resolve (i.e. // simulate the behavior right after bootstrap) info := s.store.CreateInfo("env-name") // 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", ) 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) info.SetAPIEndpoint(configstore.APIEndpoint{}) err := info.Write() c.Assert(err, jc.ErrorIsNil) addrs, hosts, changed := juju.PrepareEndpointsForCaching( info, [][]network.HostPort{unsortedHPs}, network.HostPort{}, ) 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.api API hostnames %v - resolving hostnames", sortedHPs) c.Assert(c.GetTestLog(), jc.Contains, expectLog) expectLog = fmt.Sprintf("INFO juju.api new API addresses to cache %v", resolvedHPs) c.Assert(c.GetTestLog(), jc.Contains, expectLog) }