diff mbox

KVM test: Add subtest clock_getres

Message ID 1289477087-4460-1-git-send-email-lmr@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Lucas Meneghel Rodrigues Nov. 11, 2010, 12:04 p.m. UTC
None
diff mbox

Patch

diff --git a/client/tests/kvm/deps/test_clock_getres/Makefile b/client/tests/kvm/deps/test_clock_getres/Makefile
new file mode 100644
index 0000000..b4f73c7
--- /dev/null
+++ b/client/tests/kvm/deps/test_clock_getres/Makefile
@@ -0,0 +1,11 @@ 
+CC = gcc
+PROG = test_clock_getres
+SRC = test_clock_getres.c
+LIBS = -lrt
+
+all: $(PROG)
+
+$(PROG):
+	$(CC) $(LIBS) -o $(PROG) $(SRC)
+clean:
+	rm -f $(PROG)
diff --git a/client/tests/kvm/deps/test_clock_getres/test_clock_getres.c b/client/tests/kvm/deps/test_clock_getres/test_clock_getres.c
new file mode 100644
index 0000000..81d3b9c
--- /dev/null
+++ b/client/tests/kvm/deps/test_clock_getres/test_clock_getres.c
@@ -0,0 +1,58 @@ 
+/*
+ *  Test clock resolution for KVM guests that have kvm-clock as clock source
+ *
+ *  Copyright (c) 2010 Red Hat, Inc
+ *  Author: Lucas Meneghel Rodrigues <lmr@redhat.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdio.h>
+#include <time.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(void) {
+	struct timespec res;
+	int clock_return = clock_getres(CLOCK_MONOTONIC, &res);
+	char clocksource[50];
+	char line[80];
+	FILE *fr;
+	if ((fr = fopen(
+			"/sys/devices/system/clocksource/clocksource0/current_clocksource",
+			"rt")) == NULL) {
+		perror("fopen");
+		return EXIT_FAILURE;
+	}
+	while (fgets(line, 80, fr) != NULL) {
+		sscanf(line, "%s", &clocksource);
+	}
+	fclose(fr);
+	if (!strncmp(clocksource, "kvm-clock", strlen("kvm-clock"))) {
+		if (clock_return == 0) {
+			if (res.tv_sec > 1 || res.tv_nsec > 100) {
+				printf("FAIL: clock_getres returned bad clock resolution\n");
+				return EXIT_FAILURE;
+			} else {
+				printf("PASS: check successful\n");
+				return EXIT_SUCCESS;
+			}
+		} else {
+			printf("FAIL: clock_getres failed\n");
+			return EXIT_FAILURE;
+		}
+	} else {
+		printf("FAIL: invalid clock source: %s\n", clocksource);
+		return EXIT_FAILURE;
+	}
+}
diff --git a/client/tests/kvm/tests/clock_getres.py b/client/tests/kvm/tests/clock_getres.py
new file mode 100644
index 0000000..7cadae8
--- /dev/null
+++ b/client/tests/kvm/tests/clock_getres.py
@@ -0,0 +1,44 @@ 
+import logging, time, os
+from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib import utils
+import kvm_test_utils, kvm_utils
+
+
+def run_clock_getres(test, params, env):
+    """
+    Verify if guests using kvm-clock as the time source have a sane clock
+    resolution.
+
+    @param test: kvm test object.
+    @param params: Dictionary with test parameters.
+    @param env: Dictionary with the test environment.
+    """
+    t_name = "test_clock_getres"
+    base_dir = "/tmp"
+
+    deps_dir = os.path.join(test.bindir, "deps", t_name)
+    os.chdir(deps_dir)
+    try:
+        utils.system("make clean")
+        utils.system("make")
+    except:
+        raise error.TestError("Failed to compile %s" % t_name)
+
+    test_clock = os.path.join(deps_dir, t_name)
+    if not os.path.isfile(test_clock):
+        raise error.TestError("Could not find %s" % t_name)
+
+    vm = kvm_test_utils.get_living_vm(env, params.get("main_vm"))
+    timeout = int(params.get("login_timeout", 360))
+    session = kvm_test_utils.wait_for_login(vm, timeout=timeout)
+    if not vm.copy_files_to(test_clock, base_dir):
+        raise error.TestError("Failed to copy %s to VM" % t_name)
+    s, o = session.get_command_status_output(os.path.join(base_dir, t_name))
+    if s:
+        raise error.TestError("Execution of %s failed: %s" % (t_name, o))
+    else:
+        logging.info("PASS: Guest reported an appropriate clock source "
+                     "resolution")
+    s, o = session.get_command_status_output("dmesg")
+    logging.info("guest's dmesg:")
+    logging.info(o)
diff --git a/client/tests/kvm/tests_base.cfg.sample b/client/tests/kvm/tests_base.cfg.sample
index 40ecf4a..7e36841 100644
--- a/client/tests/kvm/tests_base.cfg.sample
+++ b/client/tests/kvm/tests_base.cfg.sample
@@ -663,6 +663,9 @@  variants:
                 image_name_snapshot1 = sn1
                 image_name_snapshot2 = sn2
 
+    - clock_getres: install setup unattended_install.cdrom
+        type = clock_getres
+
     # system_powerdown, system_reset and shutdown *must* be the last ones
     # defined (in this order), since the effect of such tests can leave
     # the VM on a bad state.