From f9fd62a33e99d54190b48dfd062fc5f8da1b647d Mon Sep 17 00:00:00 2001 From: Castulo Martinez Date: Fri, 30 Nov 2018 23:18:44 +0000 Subject: [PATCH] Show when a bundle is experimental in swupd search This commit adds ability to show when a bundle is experimental in the results of a swupd search. Signed-off-by: Castulo Martinez --- Makefile.am | 1 + src/bundle.c | 4 +- src/helpers.c | 7 +- src/search.c | 19 ++- src/swupd.h | 2 +- .../search/search-experimental.bats | 131 ++++++++++++++++++ 6 files changed, 152 insertions(+), 12 deletions(-) create mode 100755 test/functional/search/search-experimental.bats diff --git a/Makefile.am b/Makefile.am index 71ba8b01..cc8ea067 100644 --- a/Makefile.am +++ b/Makefile.am @@ -170,6 +170,7 @@ BATS = \ test/functional/search/search-content-check-negative.bats \ test/functional/search/search-content-check-positive.bats \ test/functional/search/search-client-certificate.bats \ + test/functional/search/search-experimental.bats \ test/functional/update/update-boot-file.bats \ test/functional/update/update-boot-skip.bats \ test/functional/update/update-bundle-removed.bats \ diff --git a/src/bundle.c b/src/bundle.c index 7e855fd3..9b55e57a 100644 --- a/src/bundle.c +++ b/src/bundle.c @@ -68,7 +68,7 @@ int list_installable_bundles() while (list) { file = list->data; list = list->next; - get_bundle_name(&name, file); + name = get_printable_bundle_name(file->filename, file->is_experimental); printf("%s\n", name); free_string(&name); } @@ -1168,7 +1168,7 @@ skip_mom: bundle_manifest = search_bundle_in_manifest(MoM, basename((char *)item->data)); } if (bundle_manifest) { - get_bundle_name(&name, bundle_manifest); + name = get_printable_bundle_name(bundle_manifest->filename, bundle_manifest->is_experimental); } else { string_or_die(&name, basename((char *)item->data)); } diff --git a/src/helpers.c b/src/helpers.c index 35263e8e..0f19ceae 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -1200,7 +1200,10 @@ struct list *get_dir_files_sorted(char *path) } /* Appends the (experimental) label if applicable to the bundle name */ -void get_bundle_name(char **name, struct file *bundle_manifest) +char *get_printable_bundle_name(const char *bundle_name, bool is_experimental) { - string_or_die(name, "%s%s", bundle_manifest->filename, bundle_manifest->is_experimental ? " (experimental)" : ""); + char *printable_name = NULL; + + string_or_die(&printable_name, "%s%s", bundle_name, is_experimental ? " (experimental)" : ""); + return printable_name; } diff --git a/src/search.c b/src/search.c index ac8da595..2accdfbc 100644 --- a/src/search.c +++ b/src/search.c @@ -53,6 +53,7 @@ struct bundle_result { struct list *files; struct list *includes; bool is_tracked; + bool is_experimental; bool seen; }; @@ -65,7 +66,7 @@ struct file_result { static struct list *results; /* add a bundle_result to the results list */ -static void add_bundle_file_result(char *bundlename, char *filename, double score) +static void add_bundle_file_result(char *bundlename, char *filename, double score, bool is_experimental) { struct bundle_result *bundle = NULL; struct file_result *file; @@ -89,6 +90,7 @@ static void add_bundle_file_result(char *bundlename, char *filename, double scor strncpy(bundle->bundle_name, bundlename, BUNDLE_NAME_MAXLEN - 1); /* record if the bundle is tracked on the system */ bundle->is_tracked = is_tracked_bundle(bundlename); + bundle->is_experimental = is_experimental; } file = calloc(sizeof(struct file_result), 1); @@ -276,6 +278,7 @@ static void print_csv_results() static void print_final_results(bool display_size) { + char *name = NULL; struct bundle_result *b; struct list *ptr; int counter = 0; @@ -295,7 +298,9 @@ static void print_final_results(bool display_size) /* do not print the size information when the scope is only one bundle ('o') * because we did not load all bundles and therefore do not have include sizes * for the result */ - printf("Bundle %s\t%s", b->bundle_name, b->is_tracked ? "[installed]\t" : ""); + name = get_printable_bundle_name(b->bundle_name, b->is_experimental); + printf("Bundle %s\t%s", name, b->is_tracked ? "[installed]\t" : ""); + free_string(&name); if (display_size) { printf("(%li MB%s)", b->size / 1000 / 1000, /* convert from bytes->KB->MB */ @@ -532,12 +537,12 @@ static double guess_score(char *bundle, char *file, char *search_term) /* report_finds() * Report out, respecting verbosity */ -static void report_find(char *bundle, char *file, char *search_term) +static void report_find(char *bundle, char *file, char *search_term, bool is_experimental) { double score; score = guess_score(bundle, file, search_term); - add_bundle_file_result(bundle, file, score); + add_bundle_file_result(bundle, file, score, is_experimental); } /* do_search() @@ -604,14 +609,14 @@ static void do_search(struct manifest *MoM, char search_type, char *search_term) } else if (search_type == '0') { /* Search for exact match, not path addition */ if (file_search(subfile->filename, "", search_term)) { - report_find(file->filename, subfile->filename, search_term); + report_find(file->filename, subfile->filename, search_term, file->is_experimental); hit = true; } } else if (search_type == 'l') { /* Check each supported library path for a match */ for (i = 0; lib_paths[i] != NULL; i++) { if (file_search(subfile->filename, lib_paths[i], search_term)) { - report_find(file->filename, subfile->filename, search_term); + report_find(file->filename, subfile->filename, search_term, file->is_experimental); hit = true; } } @@ -619,7 +624,7 @@ static void do_search(struct manifest *MoM, char search_type, char *search_term) /* Check each supported path for binaries */ for (i = 0; bin_paths[i] != NULL; i++) { if (file_search(subfile->filename, bin_paths[i], search_term)) { - report_find(file->filename, subfile->filename, search_term); + report_find(file->filename, subfile->filename, search_term, file->is_experimental); hit = true; } } diff --git a/src/swupd.h b/src/swupd.h index c9d25b60..79ce1584 100644 --- a/src/swupd.h +++ b/src/swupd.h @@ -382,7 +382,7 @@ extern void print_progress(unsigned int count, unsigned int max); extern bool is_compatible_format(int format_num); extern bool is_current_version(int version); extern bool on_new_format(void); -extern void get_bundle_name(char **name, struct file *bundle_manifest); +extern char *get_printable_bundle_name(const char *bundle_name, bool is_experimental); /* subscription.c */ struct list *free_list_file(struct list *item); diff --git a/test/functional/search/search-experimental.bats b/test/functional/search/search-experimental.bats new file mode 100755 index 00000000..b5d2387a --- /dev/null +++ b/test/functional/search/search-experimental.bats @@ -0,0 +1,131 @@ +#!/usr/bin/env bats + +# Author: Castulo Martinez +# Email: castulo.martinez@intel.com + +load "../testlib" + +global_setup() { + + create_test_environment "$TEST_NAME" + create_bundle -e -n test-bundle1 -f /common,/foo/test-file1,/foo/test-file3,/usr/bin/test-bin,/usr/lib/test-lib32 "$TEST_NAME" + create_bundle -L -e -n test-bundle2 -f /common,/bar/test-file2,/bar/test-file3,/usr/lib64/test-lib64 "$TEST_NAME" + +} + +test_setup() { + + # do nothing + return + +} + +test_teardown() { + + # do nothing + return + +} + +global_teardown() { + + destroy_test_environment "$TEST_NAME" + +} + +@test "SRH015: Search for an experimental bundle" { + + # it should find the bundle since the tracking file has the + # same name as the bundle, also the first time we run search + # it needs to download the manifests, so we need to account + # for those messages. If the bundle is experimental it should + # swhow that + + run sudo sh -c "$SWUPD search $SWUPD_OPTS test-bundle1" + + assert_status_is 0 + assert_in_output "Searching for 'test-bundle1'" + # there is going to be a whole lot of content within the line + # above and the lines below so we are excluding those from the + # check + expected_output=$(cat <<-EOM + Downloading Clear Linux manifests + .* MB total... + Completed manifests download. + Bundle test-bundle1 \\(experimental\\) \\(0 MB to install\\) + ./usr/share/clear/bundles/test-bundle1 + EOM + ) + assert_regex_in_output "$expected_output" + +} + +@test "SRH016: Search for an experimental bundle that is already installed" { + + # it should find the bundle since the tracking file has the + # same name as the bundle. If the bundle is experimental it + # should swhow that + + run sudo sh -c "$SWUPD search $SWUPD_OPTS test-bundle2" + + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for 'test-bundle2' + Bundle test-bundle2 \\(experimental\\) \\[installed\\] \\(0 MB on system\\) + ./usr/share/clear/bundles/test-bundle2 + EOM + ) + assert_regex_in_output "$expected_output" + +} + +@test "SRH017: Search for a file that is part of an experimental bundle" { + + # If the bundle is experimental it should swhow that + + run sudo sh -c "$SWUPD search $SWUPD_OPTS test-file1" + + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for 'test-file1' + Bundle test-bundle1 \\(experimental\\) \\(0 MB to install\\) + ./foo/test-file1 + EOM + ) + assert_regex_in_output "$expected_output" + +} + +@test "SRH018: Search for a binary file that is part of an experimental bundle" { + + # If the bundle is experimental it should swhow that + + run sudo sh -c "$SWUPD search --binary $SWUPD_OPTS test-bin" + + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for 'test-bin' + Bundle test-bundle1 \\(experimental\\) \\(0 MB to install\\) + ./usr/bin/test-bin + EOM + ) + assert_regex_in_output "$expected_output" + +} + +@test "SRH019: Search for a library that is part of an experimental bundle" { + + # If the bundle is experimental it should swhow that + + run sudo sh -c "$SWUPD search --library $SWUPD_OPTS test-lib32" + + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for 'test-lib32' + Bundle test-bundle1 \\(experimental\\) \\(0 MB to install\\) + ./usr/lib/test-lib32 + EOM + ) + assert_regex_in_output "$expected_output" + +}