Update tests to support changes to pxe_parser
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>
Mon, 23 May 2016 05:56:22 +0000 (15:56 +1000)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 28 Jun 2016 06:17:21 +0000 (16:17 +1000)
Substitute load_url_async() when running tests to support direct
callers of load_url_async() who will expect to read a file in a
callback.
Stub out device_handler_discover_context_commit() since it will remove
discover_options from the given discover_context, but the tests will
check the discover_context to count boot_options.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
discover/device-handler.c
discover/paths.c
test/parser/utils.c

index 1c69de02e7f85a8f9dfe5166af9d4bb2cbaab95a..9fd9fab5cf5f60db1b8c372b7401cc07b511b176 100644 (file)
@@ -708,50 +708,6 @@ struct discover_context *device_handler_discover_context_create(
        return ctx;
 }
 
        return ctx;
 }
 
-/**
- * context_commit - Commit a temporary discovery context to the handler,
- * and notify the clients about any new options / devices
- */
-void device_handler_discover_context_commit(struct device_handler *handler,
-               struct discover_context *ctx)
-{
-       struct discover_device *dev = ctx->device;
-       struct discover_boot_option *opt, *tmp;
-
-       if (!device_lookup_by_id(handler, dev->device->id))
-               device_handler_add_device(handler, dev);
-
-       /* move boot options from the context to the device */
-       list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
-               list_remove(&opt->list);
-
-               if (boot_option_resolve(opt, handler)) {
-                       pb_log("boot option %s is resolved, "
-                                       "sending to clients\n",
-                                       opt->option->id);
-                       list_add_tail(&dev->boot_options, &opt->list);
-                       talloc_steal(dev, opt);
-                       boot_option_finalise(handler, opt);
-                       notify_boot_option(handler, opt);
-               } else {
-                       if (!opt->source->resolve_resource) {
-                               pb_log("parser %s gave us an unresolved "
-                                       "resource (%s), but no way to "
-                                       "resolve it\n",
-                                       opt->source->name, opt->option->id);
-                               talloc_free(opt);
-                       } else {
-                               pb_log("boot option %s is unresolved, "
-                                               "adding to queue\n",
-                                               opt->option->id);
-                               list_add(&handler->unresolved_boot_options,
-                                               &opt->list);
-                               talloc_steal(handler, opt);
-                       }
-               }
-       }
-}
-
 void device_handler_add_device(struct device_handler *handler,
                struct discover_device *device)
 {
 void device_handler_add_device(struct device_handler *handler,
                struct discover_device *device)
 {
@@ -1218,6 +1174,50 @@ msg:
 
 #ifndef PETITBOOT_TEST
 
 
 #ifndef PETITBOOT_TEST
 
+/**
+ * context_commit - Commit a temporary discovery context to the handler,
+ * and notify the clients about any new options / devices
+ */
+void device_handler_discover_context_commit(struct device_handler *handler,
+               struct discover_context *ctx)
+{
+       struct discover_device *dev = ctx->device;
+       struct discover_boot_option *opt, *tmp;
+
+       if (!device_lookup_by_id(handler, dev->device->id))
+               device_handler_add_device(handler, dev);
+
+       /* move boot options from the context to the device */
+       list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
+               list_remove(&opt->list);
+
+               if (boot_option_resolve(opt, handler)) {
+                       pb_log("boot option %s is resolved, "
+                                       "sending to clients\n",
+                                       opt->option->id);
+                       list_add_tail(&dev->boot_options, &opt->list);
+                       talloc_steal(dev, opt);
+                       boot_option_finalise(handler, opt);
+                       notify_boot_option(handler, opt);
+               } else {
+                       if (!opt->source->resolve_resource) {
+                               pb_log("parser %s gave us an unresolved "
+                                       "resource (%s), but no way to "
+                                       "resolve it\n",
+                                       opt->source->name, opt->option->id);
+                               talloc_free(opt);
+                       } else {
+                               pb_log("boot option %s is unresolved, "
+                                               "adding to queue\n",
+                                               opt->option->id);
+                               list_add(&handler->unresolved_boot_options,
+                                               &opt->list);
+                               talloc_steal(handler, opt);
+                       }
+               }
+       }
+}
+
 static void device_handler_update_lang(const char *lang)
 {
        const char *cur_lang;
 static void device_handler_update_lang(const char *lang)
 {
        const char *cur_lang;
@@ -1566,6 +1566,13 @@ void device_release_write(struct discover_device *dev, bool release)
 
 #else
 
 
 #else
 
+void device_handler_discover_context_commit(
+               struct device_handler *handler __attribute__((unused)),
+               struct discover_context *ctx __attribute__((unused)))
+{
+       pb_log("%s stubbed out for test cases\n", __func__);
+}
+
 static void device_handler_update_lang(const char *lang __attribute__((unused)))
 {
 }
 static void device_handler_update_lang(const char *lang __attribute__((unused)))
 {
 }
@@ -1605,4 +1612,3 @@ void device_release_write(struct discover_device *dev __attribute__((unused)),
 }
 
 #endif
 }
 
 #endif
-
index 1269dded35b73960c6b3d51c00efcd1b6e6aaafa..7fcff9e0eaf3d8cc1ceb2e66cb68bbc279fd647f 100644 (file)
@@ -47,6 +47,7 @@ char *join_paths(void *alloc_ctx, const char *a, const char *b)
        return full_path;
 }
 
        return full_path;
 }
 
+#ifndef PETITBOOT_TEST
 
 static char *local_name(void *ctx)
 {
 
 static char *local_name(void *ctx)
 {
@@ -449,3 +450,33 @@ void load_url_async_cancel(struct load_url_result *res)
        res->status = LOAD_CANCELLED;
        process_stop_async(task->process);
 }
        res->status = LOAD_CANCELLED;
        process_stop_async(task->process);
 }
+
+#else
+
+static void __attribute__((unused)) load_local(
+               struct load_task *task __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_wget(
+               struct load_task *task __attribute__((unused)),
+               int flags __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_tftp(
+               struct load_task *task __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_sftp(
+               struct load_task *task __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_nfs(
+               struct load_task *task __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_url_process_exit(
+               struct process *process __attribute__((unused)))
+{
+}
+
+#endif
index 2891969791c882da1df1fe6742eb5f1bf0cfcece..5cebc99062ad23a06d6217804cf57ae88a298f74 100644 (file)
@@ -17,6 +17,8 @@
 #include "resource.h"
 #include "event.h"
 #include "platform.h"
 #include "resource.h"
 #include "event.h"
 #include "platform.h"
+#include "paths.h"
+#include "parser-conf.h"
 
 #include "parser-test.h"
 
 
 #include "parser-test.h"
 
@@ -306,6 +308,61 @@ int parser_replace_file(struct discover_context *ctx,
        return 0;
 }
 
        return 0;
 }
 
+struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
+               load_url_complete async_cb, void *async_data)
+{
+       struct conf_context *conf = async_data;
+       struct parser_test *test = conf->dc->test_data;
+       struct load_url_result *result;
+       char tmp[] = "/tmp/pb-XXXXXX";
+       ssize_t rc = -1, sz = 0;
+       struct test_file *file;
+       int fd;
+
+       fd = mkstemp(tmp);
+
+       if (fd < 0)
+               return NULL;
+
+       /* Some parsers will expect to need to read a file, so write the
+        * specified file to a temporary file */
+       list_for_each_entry(&test->files, file, list) {
+               if (file->dev)
+                       continue;
+
+               if (strcmp(file->name, url->full))
+                       continue;
+
+               while (sz < file->size) {
+                       rc = write(fd, file->data, file->size);
+                       if (rc < 0) {
+                               fprintf(stderr,
+                                       "Failed to write to tmpfile, %m\n");
+                               break;
+                       }
+                       sz += rc;
+               }
+               break;
+       }
+
+       close(fd);
+
+       result = talloc_zero(ctx, struct load_url_result);
+       if (!result)
+               return NULL;
+
+       result->local = talloc_strdup(result, tmp);
+       if (rc < 0)
+               result->status = LOAD_ERROR;
+       else
+               result->status = result->local ? LOAD_OK : LOAD_ERROR;
+       result->cleanup_local = true;
+
+       async_cb(result, conf);
+
+       return result;
+}
+
 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
                char **buf, int *len)
 {
 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
                char **buf, int *len)
 {