[PATCH 3/6] tests/hd-geo-test: Skip test when images can not be created

Thomas Huth posted 6 patches 6 years, 2 months ago
Maintainers: Thomas Huth <thuth@redhat.com>, "Philippe Mathieu-Daudé" <philmd@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Fam Zheng <fam@euphon.net>, Laurent Vivier <lvivier@redhat.com>, Max Reitz <mreitz@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Cornelia Huck <cohuck@redhat.com>, Kevin Wolf <kwolf@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
There is a newer version of this series
[PATCH 3/6] tests/hd-geo-test: Skip test when images can not be created
Posted by Thomas Huth 6 years, 2 months ago
In certain environments like restricted containers, we can not create
huge test images. To be able to use "make check" in such container
environments, too, let's skip the hd-geo-test instead of failing when
the test images could not be created.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 tests/hd-geo-test.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/tests/hd-geo-test.c b/tests/hd-geo-test.c
index 7e86c5416c..a249800544 100644
--- a/tests/hd-geo-test.c
+++ b/tests/hd-geo-test.c
@@ -34,8 +34,13 @@ static char *create_test_img(int secs)
     fd = mkstemp(template);
     g_assert(fd >= 0);
     ret = ftruncate(fd, (off_t)secs * 512);
-    g_assert(ret == 0);
     close(fd);
+
+    if (ret) {
+        free(template);
+        template = NULL;
+    }
+
     return template;
 }
 
@@ -934,6 +939,10 @@ int main(int argc, char **argv)
     for (i = 0; i < backend_last; i++) {
         if (img_secs[i] >= 0) {
             img_file_name[i] = create_test_img(img_secs[i]);
+            if (!img_file_name[i]) {
+                g_test_message("Could not create test images.");
+                goto test_add_done;
+            }
         } else {
             img_file_name[i] = NULL;
         }
@@ -965,6 +974,7 @@ int main(int argc, char **argv)
                        "skipping hd-geo/override/* tests");
     }
 
+test_add_done:
     ret = g_test_run();
 
     for (i = 0; i < backend_last; i++) {
-- 
2.23.0


Re: [PATCH 3/6] tests/hd-geo-test: Skip test when images can not be created
Posted by Philippe Mathieu-Daudé 6 years, 2 months ago
On 11/19/19 6:08 PM, Thomas Huth wrote:
> In certain environments like restricted containers, we can not create
> huge test images. To be able to use "make check" in such container
> environments, too, let's skip the hd-geo-test instead of failing when
> the test images could not be created.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>   tests/hd-geo-test.c | 12 +++++++++++-
>   1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/tests/hd-geo-test.c b/tests/hd-geo-test.c
> index 7e86c5416c..a249800544 100644
> --- a/tests/hd-geo-test.c
> +++ b/tests/hd-geo-test.c
> @@ -34,8 +34,13 @@ static char *create_test_img(int secs)
>       fd = mkstemp(template);
>       g_assert(fd >= 0);
>       ret = ftruncate(fd, (off_t)secs * 512);
> -    g_assert(ret == 0);
>       close(fd);
> +
> +    if (ret) {
> +        free(template);
> +        template = NULL;
> +    }
> +
>       return template;
>   }
>   
> @@ -934,6 +939,10 @@ int main(int argc, char **argv)
>       for (i = 0; i < backend_last; i++) {
>           if (img_secs[i] >= 0) {
>               img_file_name[i] = create_test_img(img_secs[i]);
> +            if (!img_file_name[i]) {
> +                g_test_message("Could not create test images.");
> +                goto test_add_done;
> +            }
>           } else {
>               img_file_name[i] = NULL;
>           }
> @@ -965,6 +974,7 @@ int main(int argc, char **argv)
>                          "skipping hd-geo/override/* tests");
>       }
>   
> +test_add_done:
>       ret = g_test_run();
>   
>       for (i = 0; i < backend_last; i++) {
> 

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>


Re: [PATCH 3/6] tests/hd-geo-test: Skip test when images can not be created
Posted by Alex Bennée 6 years, 2 months ago
Thomas Huth <thuth@redhat.com> writes:

> In certain environments like restricted containers, we can not create
> huge test images. To be able to use "make check" in such container
> environments, too, let's skip the hd-geo-test instead of failing when
> the test images could not be created.
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  tests/hd-geo-test.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/tests/hd-geo-test.c b/tests/hd-geo-test.c
> index 7e86c5416c..a249800544 100644
> --- a/tests/hd-geo-test.c
> +++ b/tests/hd-geo-test.c
> @@ -34,8 +34,13 @@ static char *create_test_img(int secs)
>      fd = mkstemp(template);
>      g_assert(fd >= 0);
>      ret = ftruncate(fd, (off_t)secs * 512);
> -    g_assert(ret == 0);
>      close(fd);
> +
> +    if (ret) {
> +        free(template);
> +        template = NULL;
> +    }
> +
>      return template;
>  }
>  
> @@ -934,6 +939,10 @@ int main(int argc, char **argv)
>      for (i = 0; i < backend_last; i++) {
>          if (img_secs[i] >= 0) {
>              img_file_name[i] = create_test_img(img_secs[i]);
> +            if (!img_file_name[i]) {
> +                g_test_message("Could not create test images.");
> +                goto test_add_done;
> +            }
>          } else {
>              img_file_name[i] = NULL;
>          }
> @@ -965,6 +974,7 @@ int main(int argc, char **argv)
>                         "skipping hd-geo/override/* tests");
>      }
>  
> +test_add_done:
>      ret = g_test_run();

It does seem a bit odd to call g_test_run if we have explicitly not set
any up. Personally I'd hoist all the test creation into a new function
so you could do:

  if (setup_images()) {
     setup_tests();
     ret = run_tests();
  } else {
     ret = 0; /* pass if we have no images */
  }

  cleanup_images();

but that's just me going above and beyond to avoid goto's ;-)

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

>  
>      for (i = 0; i < backend_last; i++) {


-- 
Alex Bennée