[libvirt] [PATCH 1/2] qemuxml2argvmock: Mock time() on non-linux platforms too

The qemuxml2argvtest is run on more platforms than linux. For instance FreeBSD. On these platforms we are, however, not mocking time() which results in current time being fetched from system and hence tests number 32 and 33 failing. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/qemuxml2argvmock.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/qemuxml2argvmock.c b/tests/qemuxml2argvmock.c index ed9fb13..bff3b0f 100644 --- a/tests/qemuxml2argvmock.c +++ b/tests/qemuxml2argvmock.c @@ -20,9 +20,8 @@ #include <config.h> -#ifdef __linux__ -# include "internal.h" -# include <time.h> +#include "internal.h" +#include <time.h> time_t time(time_t *t) { @@ -31,7 +30,3 @@ time_t time(time_t *t) *t = ret; return ret; } - -#else -/* Nothing to override on non-__linux__ platforms */ -#endif -- 1.8.5.3

With my recent work on the test, both time() and localtime() are used. While mocking the former one, we get predictible result for UTC. But since the latter function uses timezone to get local time, the result of localtime() is not so predictive. Therefore, we must set the TZ variable at the beginning of the test. To be able to catch some things that work just by a blind chance, I'm choosing an exotic timezone that (hopefully) no libvirt developer resides in. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- .../qemuxml2argv-clock-localtime-basis-localtime.args | 2 +- tests/qemuxml2argvtest.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/qemuxml2argvdata/qemuxml2argv-clock-localtime-basis-localtime.args b/tests/qemuxml2argvdata/qemuxml2argv-clock-localtime-basis-localtime.args index 24fe89c..7d39125 100644 --- a/tests/qemuxml2argvdata/qemuxml2argv-clock-localtime-basis-localtime.args +++ b/tests/qemuxml2argvdata/qemuxml2argv-clock-localtime-basis-localtime.args @@ -1,5 +1,5 @@ LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test QEMU_AUDIO_DRV=none \ /usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic \ --monitor unix:/tmp/test-monitor,server,nowait -rtc base=2009-02-14T01:31:30 \ +-monitor unix:/tmp/test-monitor,server,nowait -rtc base=2009-02-13T13:31:30 \ -no-acpi -boot c -usb -hda /dev/HostVG/QEMUGuest1 -net none -serial none \ -parallel none diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index c8b3857..1ee4b59 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -486,6 +486,17 @@ mymain(void) if (!abs_top_srcdir) abs_top_srcdir = abs_srcdir "/.."; + /* Set the timezone because we are mocking the time() function. + * If we don't do that, then localtime() may return unpredictable + * results. In order to detect things that just work by a blind + * chance, we need to set an exotic timezone that none libvirt + * developer resides in. So if you go for holiday to Niue, don't + * send patches but enjoy time off! */ + if (setenv("TZ", "Pacific/Niue", 1) < 0) { + perror("setenv"); + return EXIT_FAILURE; + } + driver.config = virQEMUDriverConfigNew(false); VIR_FREE(driver.config->spiceListen); VIR_FREE(driver.config->vncListen); -- 1.8.5.3

On 02/06/2014 06:33 AM, Michal Privoznik wrote:
With my recent work on the test, both time() and localtime() are used. While mocking the former one, we get predictible result for UTC. But
s/predictible/predictable/
since the latter function uses timezone to get local time, the result of localtime() is not so predictive. Therefore, we must set the TZ variable at the beginning of the test. To be able to catch some things that work just by a blind chance, I'm choosing an exotic timezone that (hopefully) no libvirt developer resides in.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> ---
+ /* Set the timezone because we are mocking the time() function. + * If we don't do that, then localtime() may return unpredictable + * results. In order to detect things that just work by a blind + * chance, we need to set an exotic timezone that none libvirt
s/none/no/
+ * developer resides in. So if you go for holiday to Niue, don't + * send patches but enjoy time off! */ + if (setenv("TZ", "Pacific/Niue", 1) < 0) {
This works on glibc, but is not portable to all platforms. I'd prefer we use a POSIX timezone specification: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
The expanded format (for all TZ s whose value does not have a <colon> as the first character) is as follows:
stdoffset[dst[offset][,start[/time],end[/time]]]
In fact, you can abuse offset to list a timezone with a half hour offset from UTC, with no daylight savings, and then guarantee that no developer uses that timezone: $ date --utc Thu Feb 6 13:52:24 UTC 2014 $ TZ=VIR00:30 date Thu Feb 6 13:22:43 VIR 2014 -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

With my recent work on the test, both time() and localtime() are used. While mocking the former one, we get predictable result for UTC. But since the latter function uses timezone to get local time, the result of localtime() is not so predictive. Therefore, we must set the TZ variable at the beginning of the test. To be able to catch some things that work just by a blind chance, I'm choosing a virtual timezone that (hopefully) no libvirt developer resides in. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- .../qemuxml2argv-clock-localtime-basis-localtime.args | 2 +- tests/qemuxml2argvtest.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/qemuxml2argvdata/qemuxml2argv-clock-localtime-basis-localtime.args b/tests/qemuxml2argvdata/qemuxml2argv-clock-localtime-basis-localtime.args index 24fe89c..ab9e979 100644 --- a/tests/qemuxml2argvdata/qemuxml2argv-clock-localtime-basis-localtime.args +++ b/tests/qemuxml2argvdata/qemuxml2argv-clock-localtime-basis-localtime.args @@ -1,5 +1,5 @@ LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test QEMU_AUDIO_DRV=none \ /usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic \ --monitor unix:/tmp/test-monitor,server,nowait -rtc base=2009-02-14T01:31:30 \ +-monitor unix:/tmp/test-monitor,server,nowait -rtc base=2009-02-14T00:01:30 \ -no-acpi -boot c -usb -hda /dev/HostVG/QEMUGuest1 -net none -serial none \ -parallel none diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index c8b3857..dae08d5 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -486,6 +486,16 @@ mymain(void) if (!abs_top_srcdir) abs_top_srcdir = abs_srcdir "/.."; + /* Set the timezone because we are mocking the time() function. + * If we don't do that, then localtime() may return unpredictable + * results. In order to detect things that just work by a blind + * chance, we need to set an virtual timezone that no libvirt + * developer resides in. */ + if (setenv("TZ", "VIR00:30", 1) < 0) { + perror("setenv"); + return EXIT_FAILURE; + } + driver.config = virQEMUDriverConfigNew(false); VIR_FREE(driver.config->spiceListen); VIR_FREE(driver.config->vncListen); -- 1.8.5.3

On 02/06/2014 03:11 PM, Michal Privoznik wrote:
With my recent work on the test, both time() and localtime() are used. While mocking the former one, we get predictable result for UTC. But since the latter function uses timezone to get local time, the result of localtime() is not so predictive. Therefore, we must set the TZ variable at the beginning of the test. To be able to catch some things that work just by a blind chance, I'm choosing a virtual timezone that (hopefully) no libvirt developer resides in.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- .../qemuxml2argv-clock-localtime-basis-localtime.args | 2 +- tests/qemuxml2argvtest.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-)
ACK, please push. Jan

Michal Privoznik wrote:
The qemuxml2argvtest is run on more platforms than linux. For instance FreeBSD. On these platforms we are, however, not mocking time() which results in current time being fetched from system and hence tests number 32 and 33 failing.
This change works on FreeBSD and looks good to me. Now 'make check' works successfully on FreeBSD. Roman Bogorodskiy

On 02/06/2014 02:33 PM, Michal Privoznik wrote:
The qemuxml2argvtest is run on more platforms than linux. For instance FreeBSD. On these platforms we are, however, not mocking time() which results in current time being fetched from system and hence tests number 32 and 33 failing.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/qemuxml2argvmock.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
ACK Jan
participants (4)
-
Eric Blake
-
Ján Tomko
-
Michal Privoznik
-
Roman Bogorodskiy