Skip some I2C tests if stub driver cannot be found 01/287201/3
authorAntoni Adaszkiewicz <a.adaszkiewi@samsung.com>
Tue, 24 Jan 2023 16:31:22 +0000 (17:31 +0100)
committerAntoni Adaszkiewicz <a.adaszkiewi@samsung.com>
Mon, 13 Feb 2023 13:33:11 +0000 (14:33 +0100)
Some I2C tests can behave as expected even if stub driver cannot be
found. Prevoiusly, these tests would not be run, now they will (with
suitable info displayed).

Change-Id: I48a439dd7d99f3d18b71878618f1b034a414e017

test/include/test_peripheral_i2c.h
test/peripheral-io-test.c
test/src/test_peripheral_i2c.c

index 0c032ae..73a3636 100644 (file)
@@ -19,6 +19,8 @@
 
 int test_peripheral_io_i2c_initialize(char *model, bool feature);
 
+int test_peripheral_io_i2c_find_stub_driver(void);
+
 int test_peripheral_io_i2c_peripheral_i2c_open_p(void);
 int test_peripheral_io_i2c_peripheral_i2c_open_n1(void);
 int test_peripheral_io_i2c_peripheral_i2c_open_n2(void);
index 65dacb3..364858e 100644 (file)
@@ -60,6 +60,23 @@ static skip_test_t skip_test_list[] = {
        {"test_peripheral_io_spi_peripheral_spi_set_bit_order_p2", "rpi3"}
 };
 
+static bool i2c_no_stub_driver = false;
+
+static char* skip_test_list_i2c_no_stub_driver[] = {
+       "test_peripheral_io_i2c_peripheral_i2c_open_p",
+       "test_peripheral_io_i2c_peripheral_i2c_close_p",
+       "test_peripheral_io_i2c_peripheral_i2c_read_p",
+       "test_peripheral_io_i2c_peripheral_i2c_read_n2",
+       "test_peripheral_io_i2c_peripheral_i2c_write_p",
+       "test_peripheral_io_i2c_peripheral_i2c_write_n2",
+       "test_peripheral_io_i2c_peripheral_i2c_read_register_byte_p",
+       "test_peripheral_io_i2c_peripheral_i2c_read_register_byte_n2",
+       "test_peripheral_io_i2c_peripheral_i2c_write_register_byte_p",
+       "test_peripheral_io_i2c_peripheral_i2c_read_register_word_p",
+       "test_peripheral_io_i2c_peripheral_i2c_read_register_word_n2",
+       "test_peripheral_io_i2c_peripheral_i2c_write_register_word_p"
+};
+
 static int __get_model_name(char **model_name)
 {
        int ret = SYSTEM_INFO_ERROR_NONE;
@@ -88,6 +105,13 @@ static bool __skip_check(char *name)
        for (int i = 0; i < n; ++i)
                if (!strcmp(skip_test_list[i].model_name, model_name) && !strcmp(skip_test_list[i].test_name, name))
                        return true;
+
+       if (i2c_no_stub_driver) {
+               n = sizeof(skip_test_list_i2c_no_stub_driver) / sizeof(skip_test_list_i2c_no_stub_driver[0]);
+               for (int i = 0; i < n; i++)
+                       if (!strcmp(skip_test_list_i2c_no_stub_driver[i], name))
+                               return true;
+       }
        return false;
 }
 
@@ -136,6 +160,11 @@ static int __test_peripheral_init(const char *test_name, const char *key_feature
 
 static void __test_performance_for_function(const char *name, int (*function)(void))
 {
+       if (i2c_no_stub_driver) {
+               printf(CYEL "[SKIP]" RESET " performance check for %s skipped.\n", name);
+               return;
+       }
+
        struct timespec start, stop, elapsed;
 
        clock_gettime(CLOCK_MONOTONIC, &start);
@@ -235,6 +264,14 @@ static void __test_peripheral_i2c_run()
        if (ret != PERIPHERAL_ERROR_NONE)
                return;
 
+       ret = test_peripheral_io_i2c_find_stub_driver();
+       if (ret == PERIPHERAL_ERROR_NO_DEVICE) {
+               printf("Warning - stub driver not found. Tests requiring stub driver will be skipped. (To load the driver run `modprobe i2c-stub chip_addr=<addr>`)\n");
+               i2c_no_stub_driver = true;
+       } else if (ret != PERIPHERAL_ERROR_NONE) {
+               return;
+       }
+
        ret = test_peripheral_io_i2c_peripheral_i2c_open_p();
        __error_check(ret, "test_peripheral_io_i2c_peripheral_i2c_open_p");
        ret = test_peripheral_io_i2c_peripheral_i2c_open_n1();
@@ -280,6 +317,8 @@ static void __test_peripheral_i2c_run()
        ret = test_peripheral_io_i2c_peripheral_i2c_write_register_word_n();
        __error_check(ret, "test_peripheral_io_i2c_peripheral_i2c_write_register_word_n");
        __test_performance_for_function("open/close", test_peripheral_io_i2c_peripheral_i2c_open_p);
+
+       i2c_no_stub_driver = false;
 }
 
 static void __test_peripheral_pwm_run()
index e330ca8..d973fa1 100644 (file)
@@ -22,6 +22,7 @@
 #include "test_peripheral_i2c.h"
 
 #define I2C_BUS_INVALID -99
+#define I2C_BUS_MOCK 0 // value for when stub driver is not found, to allow tests that don't require the driver to run
 #define I2C_ADDRESS 0x39
 #define I2C_ADDRESS_INVALID -99
 #define I2C_BUFFER_LEN 10
@@ -78,11 +79,18 @@ int test_peripheral_io_i2c_initialize(char *model, bool feature)
        if (strcmp(model, "rpi3") != 0 && strcmp(model, "rpi4") != 0 && strcmp(model, "artik") != 0)
                return PERIPHERAL_ERROR_NO_DEVICE;
 
+       address = I2C_ADDRESS;
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_i2c_find_stub_driver(void)
+{
        bus = __find_stub_driver();
-       if (bus == -1)
+       if (bus == -1) {
+               bus = I2C_BUS_MOCK;
                return PERIPHERAL_ERROR_NO_DEVICE;
-
-       address = I2C_ADDRESS;
+       }
 
        return PERIPHERAL_ERROR_NONE;
 }