From: Mathias Gibbens <mathias@calenhad.com>
Date: Fri, 20 Jan 2023 11:25:52 +0800
Subject: Use an actual IP address;
 in some build environments, "localhost" is resolved to an IPv6 address,
 while the code is bound to an IPv4 address

Forwarded: not-needed
---
 client/apiversion_test.go                 | 24 ++++++++++++------------
 client/local_test.go                      | 12 ++++++------
 testservices/swiftservice/service_test.go |  2 +-
 3 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/client/apiversion_test.go b/client/apiversion_test.go
index 8e9f332..9a32844 100644
--- a/client/apiversion_test.go
+++ b/client/apiversion_test.go
@@ -32,42 +32,42 @@ func (s *localLiveSuite) makeServiceURLTests() []makeServiceURLTest {
 			version:     "",
 			parts:       []string{},
 			success:     true,
-			URL:         "http://localhost:%s",
+			URL:         "http://127.0.0.1:%s",
 		},
 		{
 			serviceType: "compute",
 			version:     "v2.1",
 			parts:       []string{"foo", "bar/"},
 			success:     true,
-			URL:         "http://localhost:%s/v2.1/foo/bar/",
+			URL:         "http://127.0.0.1:%s/v2.1/foo/bar/",
 		},
 		{
 			serviceType: "compute",
 			version:     "v2.0",
 			parts:       []string{},
 			success:     true,
-			URL:         "http://localhost:%s/v2.0",
+			URL:         "http://127.0.0.1:%s/v2.0",
 		},
 		{
 			serviceType: "compute",
 			version:     "v2.0",
 			parts:       []string{"foo", "bar/"},
 			success:     true,
-			URL:         "http://localhost:%s/v2.0/foo/bar/",
+			URL:         "http://127.0.0.1:%s/v2.0/foo/bar/",
 		},
 		{
 			serviceType: "compute",
 			version:     "v2",
 			parts:       []string{"foo", "bar/"},
 			success:     true,
-			URL:         "http://localhost:%s/v2.1/foo/bar/",
+			URL:         "http://127.0.0.1:%s/v2.1/foo/bar/",
 		},
 		{
 			serviceType: "object-store",
 			version:     "",
 			parts:       []string{"foo", "bar"},
 			success:     true,
-			URL:         "http://localhost:%s/swift/v1/foo/bar",
+			URL:         "http://127.0.0.1:%s/swift/v1/foo/bar",
 		},
 		{
 			serviceType: "object-store",
@@ -88,7 +88,7 @@ func (s *localLiveSuite) makeServiceURLTests() []makeServiceURLTest {
 			version:     "v1",
 			parts:       []string{"foo", "bar/"},
 			success:     true,
-			URL:         "http://localhost:%s/swift/v1/foo/bar/",
+			URL:         "http://127.0.0.1:%s/swift/v1/foo/bar/",
 		},
 		{
 			serviceType: "juju-container-test",
@@ -102,7 +102,7 @@ func (s *localLiveSuite) makeServiceURLTests() []makeServiceURLTest {
 			version:     "",
 			parts:       []string{"foo", "bar/"},
 			success:     true,
-			URL:         "http://localhost:%s/swift/v1/foo/bar/",
+			URL:         "http://127.0.0.1:%s/swift/v1/foo/bar/",
 		},
 		{
 			serviceType: "compute",
@@ -150,7 +150,7 @@ func (s *localLiveSuite) TestMakeServiceURLAPIVersionDiscoveryDisabled(c *gc.C)
 
 	url, err := cl.MakeServiceURL("compute", "v2.1", []string{"foo", "bar/"})
 	c.Assert(err, gc.IsNil)
-	c.Assert(url, gc.Equals, fmt.Sprintf("http://localhost:%s/foo/bar/", port))
+	c.Assert(url, gc.Equals, fmt.Sprintf("http://127.0.0.1:%s/foo/bar/", port))
 }
 
 func (s *localLiveSuite) TestMakeServiceURLNoAPIVersionEndpoint(c *gc.C) {
@@ -162,7 +162,7 @@ func (s *localLiveSuite) TestMakeServiceURLNoAPIVersionEndpoint(c *gc.C) {
 
 	url, err := cl.MakeServiceURL("compute", "v2.1", []string{"foo", "bar/"})
 	c.Assert(err, gc.IsNil)
-	c.Assert(url, gc.Equals, fmt.Sprintf("http://localhost:%s/foo/bar/", port))
+	c.Assert(url, gc.Equals, fmt.Sprintf("http://127.0.0.1:%s/foo/bar/", port))
 }
 
 func (s *localLiveSuite) TestMakeServiceURLValues(c *gc.C) {
@@ -206,7 +206,7 @@ func (vh *versionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		w.Write([]byte(body))
 		return
 	}
-	body := []byte(fmt.Sprintf(vh.authBody, "http://localhost:"+vh.port, "http://localhost:"+vh.port, "http://localhost:"+vh.port))
+	body := []byte(fmt.Sprintf(vh.authBody, "http://127.0.0.1:"+vh.port, "http://127.0.0.1:"+vh.port, "http://127.0.0.1:"+vh.port))
 	// workaround for https://code.google.com/p/go/issues/detail?id=4454
 	w.Header().Set("Content-Length", strconv.Itoa(len(body)))
 	w.WriteHeader(http.StatusMultipleChoices)
@@ -219,5 +219,5 @@ func startApiVersionMux(vh versionHandler) string {
 	mux.Handle("/", &vh)
 
 	go http.ListenAndServe(":"+vh.port, mux)
-	return fmt.Sprintf("Listening on localhost:%s...\n", vh.port)
+	return fmt.Sprintf("Listening on 127.0.0.1:%s...\n", vh.port)
 }
diff --git a/client/local_test.go b/client/local_test.go
index 69f4911..13c1ec7 100644
--- a/client/local_test.go
+++ b/client/local_test.go
@@ -181,7 +181,7 @@ func newAuthenticator(bufsize int, port string) *fakeAuthenticator {
 	}
 }
 
-// doNewAuthenticator sets up the HTTP listener on localhost:<port> for testing
+// doNewAuthenticator sets up the HTTP listener on 127.0.0.1:<port> for testing
 // api version functionality within MakeServiceURL if we're using a fakeAuthenticator.
 func (s *localLiveSuite) doNewAuthenticator(c *gc.C, bufsize int, port string) *fakeAuthenticator {
 	newAuth := newAuthenticator(bufsize, port)
@@ -224,9 +224,9 @@ func (auth *fakeAuthenticator) Auth(creds *identity.Credentials) (*identity.Auth
 	}
 	URLs := make(map[string]identity.ServiceURLs)
 	endpoints := make(map[string]string)
-	endpoints["compute"] = fmt.Sprintf("http://localhost:%s", auth.port)
-	endpoints["object-store"] = fmt.Sprintf("http://localhost:%s/swift/v1", auth.port)
-	endpoints["juju-container-test"] = fmt.Sprintf("http://localhost:%s/swift/v1", auth.port)
+	endpoints["compute"] = fmt.Sprintf("http://127.0.0.1:%s", auth.port)
+	endpoints["object-store"] = fmt.Sprintf("http://127.0.0.1:%s/swift/v1", auth.port)
+	endpoints["juju-container-test"] = fmt.Sprintf("http://127.0.0.1:%s/swift/v1", auth.port)
 	URLs[creds.Region] = endpoints
 	return &identity.AuthDetails{
 		Token:             "token",
@@ -269,7 +269,7 @@ func (s *localLiveSuite) TestAuthenticationSuccess(c *gc.C) {
 	cl := s.assertAuthenticationSuccess(c, "3000")
 	URL, err := cl.MakeServiceURL("compute", "v2.0", nil)
 	c.Assert(err, gc.IsNil)
-	c.Assert(URL, gc.Equals, "http://localhost:3000/v2.0")
+	c.Assert(URL, gc.Equals, "http://127.0.0.1:3000/v2.0")
 }
 
 func checkAuthentication(cl client.AuthenticatingClient) error {
@@ -281,7 +281,7 @@ func checkAuthentication(cl client.AuthenticatingClient) error {
 	if err != nil {
 		return err
 	}
-	if URL != "http://localhost:3000/v3" {
+	if URL != "http://127.0.0.1:3000/v3" {
 		return fmt.Errorf("Unexpected URL: %s", URL)
 	}
 	return nil
diff --git a/testservices/swiftservice/service_test.go b/testservices/swiftservice/service_test.go
index be24919..8955c00 100644
--- a/testservices/swiftservice/service_test.go
+++ b/testservices/swiftservice/service_test.go
@@ -13,7 +13,7 @@ type SwiftServiceSuite struct {
 }
 
 var region = "region"             // not really used here
-var hostname = "http://localhost" // not really used here
+var hostname = "http://127.0.0.1" // not really used here
 var versionPath = "v2"            // not really used here
 var tenantId = "tenant"           // not really used here
 
