[V2,2/3] Update tests to support changes to pxe_parser
diff mbox

Message ID 20160621032139.9390-2-sam@mendozajonas.com
State Accepted
Headers show

Commit Message

Samuel Mendoza-Jonas June 21, 2016, 3:21 a.m. UTC
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 | 96 +++++++++++++++++++++++++----------------------
 discover/paths.c          | 31 +++++++++++++++
 test/parser/utils.c       | 57 ++++++++++++++++++++++++++++
 3 files changed, 139 insertions(+), 45 deletions(-)

Patch
diff mbox

diff --git a/discover/device-handler.c b/discover/device-handler.c
index 1c69de0..9fd9fab 100644
--- a/discover/device-handler.c
+++ b/discover/device-handler.c
@@ -708,50 +708,6 @@  struct discover_context *device_handler_discover_context_create(
 	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)
 {
@@ -1218,6 +1174,50 @@  msg:
 
 #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;
@@ -1566,6 +1566,13 @@  void device_release_write(struct discover_device *dev, bool release)
 
 #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)))
 {
 }
@@ -1605,4 +1612,3 @@  void device_release_write(struct discover_device *dev __attribute__((unused)),
 }
 
 #endif
-
diff --git a/discover/paths.c b/discover/paths.c
index 1269dde..7fcff9e 100644
--- a/discover/paths.c
+++ b/discover/paths.c
@@ -47,6 +47,7 @@  char *join_paths(void *alloc_ctx, const char *a, const char *b)
 	return full_path;
 }
 
+#ifndef PETITBOOT_TEST
 
 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);
 }
+
+#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
diff --git a/test/parser/utils.c b/test/parser/utils.c
index 2891969..5cebc99 100644
--- a/test/parser/utils.c
+++ b/test/parser/utils.c
@@ -17,6 +17,8 @@ 
 #include "resource.h"
 #include "event.h"
 #include "platform.h"
+#include "paths.h"
+#include "parser-conf.h"
 
 #include "parser-test.h"
 
@@ -306,6 +308,61 @@  int parser_replace_file(struct discover_context *ctx,
 	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)
 {