[libvirt] [PATCH] tests: don't assume "localhost" only resolves to 1/2 IPs

Daniel P. Berrangé posted 1 patch 4 years, 9 months ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20190715155858.26178-1-berrange@redhat.com
tests/virsystemdtest.c | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
[libvirt] [PATCH] tests: don't assume "localhost" only resolves to 1/2 IPs
Posted by Daniel P. Berrangé 4 years, 9 months ago
On Debian derived distros "localhost" can resolve to the normal
"127.0.0.1" and "::1", but it can also resolve to "127.0.1.1"

Rewrite the code so that it doesn't assume a fixed number of IPs.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 tests/virsystemdtest.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
index cd031914ab..dd87caeebf 100644
--- a/tests/virsystemdtest.c
+++ b/tests/virsystemdtest.c
@@ -548,22 +548,21 @@ testActivation(bool useNames)
     size_t nsockIP;
     int ret = -1;
     size_t i;
-    const char *names2 = "demo-unix.socket:demo-ip.socket";
-    const char *names3 = "demo-unix.socket:demo-ip.socket:demo-ip.socket";
     char nfdstr[INT_BUFSIZE_BOUND(size_t)];
     char pidstr[INT_BUFSIZE_BOUND(pid_t)];
     virSystemdActivationMap map[2];
     int *fds = NULL;
     size_t nfds = 0;
     VIR_AUTOPTR(virSystemdActivation) act = NULL;
+    virBuffer names = VIR_BUFFER_INITIALIZER;
+
+    virBufferAddLit(&names, "demo-unix.socket");
 
     if (testActivationCreateFDs(&sockUNIX, &sockIP, &nsockIP) < 0)
         return -1;
 
-    if (nsockIP != 1 && nsockIP != 2) {
-        fprintf(stderr, "Got %zu IP sockets but expected only 1 or 2\n", nsockIP);
-        goto cleanup;
-    }
+    for (i = 0; i < nsockIP; i++)
+        virBufferAddLit(&names, ":demo-ip.socket");
 
     snprintf(nfdstr, sizeof(nfdstr), "%zu", 1 + nsockIP);
     snprintf(pidstr, sizeof(pidstr), "%lld", (long long)getpid());
@@ -571,8 +570,11 @@ testActivation(bool useNames)
     setenv("LISTEN_FDS", nfdstr, 1);
     setenv("LISTEN_PID", pidstr, 1);
 
+    if (virBufferError(&names))
+        goto cleanup;
+
     if (useNames)
-        setenv("LISTEN_FDNAMES", nsockIP == 1 ? names2 : names3, 1);
+        setenv("LISTEN_FDNAMES", virBufferCurrentContent(&names), 1);
     else
         unsetenv("LISTEN_FDNAMES");
 
@@ -627,6 +629,7 @@ testActivation(bool useNames)
 
     ret = 0;
  cleanup:
+    virBufferFreeAndReset(&names);
     virObjectUnref(sockUNIX);
     for (i = 0; i < nsockIP; i++)
         virObjectUnref(sockIP[i]);
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] tests: don't assume "localhost" only resolves to 1/2 IPs
Posted by Michal Privoznik 4 years, 9 months ago
On 7/15/19 5:58 PM, Daniel P. Berrangé wrote:
> On Debian derived distros "localhost" can resolve to the normal
> "127.0.0.1" and "::1", but it can also resolve to "127.0.1.1"
> 
> Rewrite the code so that it doesn't assume a fixed number of IPs.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   tests/virsystemdtest.c | 17 ++++++++++-------
>   1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
> index cd031914ab..dd87caeebf 100644
> --- a/tests/virsystemdtest.c
> +++ b/tests/virsystemdtest.c
> @@ -548,22 +548,21 @@ testActivation(bool useNames)
>       size_t nsockIP;
>       int ret = -1;
>       size_t i;
> -    const char *names2 = "demo-unix.socket:demo-ip.socket";
> -    const char *names3 = "demo-unix.socket:demo-ip.socket:demo-ip.socket";
>       char nfdstr[INT_BUFSIZE_BOUND(size_t)];
>       char pidstr[INT_BUFSIZE_BOUND(pid_t)];
>       virSystemdActivationMap map[2];
>       int *fds = NULL;
>       size_t nfds = 0;
>       VIR_AUTOPTR(virSystemdActivation) act = NULL;
> +    virBuffer names = VIR_BUFFER_INITIALIZER;

VIR_AUTOCLEAN(virBuffer) names = VIR_BUFFER_INITIALIZER;

> +
> +    virBufferAddLit(&names, "demo-unix.socket");
>   
>       if (testActivationCreateFDs(&sockUNIX, &sockIP, &nsockIP) < 0)
>           return -1;
>   
> -    if (nsockIP != 1 && nsockIP != 2) {
> -        fprintf(stderr, "Got %zu IP sockets but expected only 1 or 2\n", nsockIP);
> -        goto cleanup;
> -    }
> +    for (i = 0; i < nsockIP; i++)
> +        virBufferAddLit(&names, ":demo-ip.socket");
>   
>       snprintf(nfdstr, sizeof(nfdstr), "%zu", 1 + nsockIP);
>       snprintf(pidstr, sizeof(pidstr), "%lld", (long long)getpid());
> @@ -571,8 +570,11 @@ testActivation(bool useNames)
>       setenv("LISTEN_FDS", nfdstr, 1);
>       setenv("LISTEN_PID", pidstr, 1);
>   
> +    if (virBufferError(&names))
> +        goto cleanup;
> +
>       if (useNames)
> -        setenv("LISTEN_FDNAMES", nsockIP == 1 ? names2 : names3, 1);
> +        setenv("LISTEN_FDNAMES", virBufferCurrentContent(&names), 1);
>       else
>           unsetenv("LISTEN_FDNAMES");
>   
> @@ -627,6 +629,7 @@ testActivation(bool useNames)
>   
>       ret = 0;
>    cleanup:
> +    virBufferFreeAndReset(&names);

And drop this line.

>       virObjectUnref(sockUNIX);
>       for (i = 0; i < nsockIP; i++)
>           virObjectUnref(sockIP[i]);
> 

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list