From 2fcca957dad3205c2e3d04234da33bb2ca686fce Mon Sep 17 00:00:00 2001 From: Castulo Martinez Date: Wed, 25 Jul 2018 15:26:59 -0700 Subject: [PATCH] Adding new assertions to the test library This commit include two parts. 1) It adds 6 new assertions to the test library: - assert_is_output - assert_is_not_output - assert_regex_is_output - assert_regex_is_not_output - assert_regex_in_output - assert_regex_not_in_output 2) It migrates the tests from the following categories to use the new library: - completion - hashdump - mirror - search Finally this commit changes a few test cases so they use "assert_is_output" instead of "assert_in_output", that way we can have a tighter control of the output we want since assert_is_output requires the whole output to match to a specific text instead of doing a partial match. --- Makefile.am | 6 + test/functional/README.md | 93 +++++++++++---- .../bundleadd_v2/add-bad-hash-state.bats | 2 +- .../functional/bundleadd_v2/add-bad-hash.bats | 2 +- .../bundleadd_v2/add-bad-manifest.bats | 2 +- .../bundleadd_v2/add-boot-file.bats | 17 +-- .../bundleadd_v2/add-boot-skip.bats | 2 +- .../bundleadd_v2/add-directory.bats | 2 +- .../functional/bundleadd_v2/add-existing.bats | 2 +- test/functional/bundleadd_v2/add-include.bats | 2 +- .../functional/bundleadd_v2/add-multiple.bats | 2 +- test/functional/bundleadd_v2/add-rc.bats | 40 ++++--- .../bundleadd_v2/add-skip-scripts.bats | 2 +- .../bundleadd_v2/add-verify-fix-path.bats | 2 +- test/functional/bundlelist_v2/list-all.bats | 2 +- .../bundlelist_v2/list-deps-flat.bats | 2 +- .../list-deps-invalid-bundle.bats | 2 +- .../bundlelist_v2/list-deps-nested.bats | 2 +- .../list-has-dep-nested-not-installed.bats | 2 +- .../list-has-dep-nested-server.bats | 2 +- .../bundlelist_v2/list-has-dep-nested.bats | 2 +- .../bundlelist_v2/list-no-deps.bats | 2 +- .../bundlelist_v2/list-none-has-deps.bats | 2 +- .../bundleremove_v2/remove-boot-file.bats | 2 +- .../bundleremove_v2/remove-file.bats | 2 +- .../remove-include-nested.bats | 2 +- .../bundleremove_v2/remove-include.bats | 2 +- .../bundleremove_v2/remove-multiple.bats | 2 +- .../bundleremove_v2/remove-os-core.bats | 2 +- .../functional/bundleremove_v2/remove-rc.bats | 12 +- .../chk-update-new-version.bats | 2 +- .../chk-update-no-server-content.bats | 2 +- .../chk-update-no-target-content.bats | 2 +- .../chk-update-slow-server.bats | 2 +- .../chk-update-version-match.bats | 2 +- .../completion_v2/completion-basic.bats | 41 +++++++ .../hashdump_v2/hashdump-file-hash.bats | 55 +++++++++ .../mirror_v2/mirror-createdir-negative.bats | 62 ++++++++++ .../mirror_v2/mirror-createdir.bats | 84 ++++++++++++++ .../search-content-check-negative.bats | 63 +++++++++++ .../search-content-check-positive.bats | 107 ++++++++++++++++++ test/functional/testlib.bash | 94 +++++++++++++++ 42 files changed, 649 insertions(+), 85 deletions(-) create mode 100755 test/functional/completion_v2/completion-basic.bats create mode 100755 test/functional/hashdump_v2/hashdump-file-hash.bats create mode 100755 test/functional/mirror_v2/mirror-createdir-negative.bats create mode 100755 test/functional/mirror_v2/mirror-createdir.bats create mode 100755 test/functional/search_v2/search-content-check-negative.bats create mode 100755 test/functional/search_v2/search-content-check-positive.bats diff --git a/Makefile.am b/Makefile.am index 0fae554b..16d5e016 100644 --- a/Makefile.am +++ b/Makefile.am @@ -181,17 +181,23 @@ BATS = \ test/functional/checkupdate/version-match/test.bats \ test/functional/checkupdate_v2/chk-update-version-match.bats \ test/functional/completion/basic/test.bats \ + test/functional/completion_v2/completion-basic.bats \ test/functional/hashdump/file-hash/test.bats \ test/functional/hashdump/file-hash-no-path-prefix/test.bats \ + test/functional/hashdump_v2/hashdump-file-hash.bats \ test/functional/mirror/createdir/test.bats \ + test/functional/mirror_v2/mirror-createdir.bats \ test/functional/mirror/createdir-negative/test.bats \ + test/functional/mirror_v2/mirror-createdir-negative.bats \ test/functional/search/content-check-negfull-path/test.bats \ test/functional/search/content-check-neglibtest/test.bats \ + test/functional/search_v2/search-content-check-negative.bats \ test/functional/search/content-check-posbin/test.bats \ test/functional/search/content-check-posebin/test.bats \ test/functional/search/content-check-posfull-path/test.bats \ test/functional/search/content-check-poslib32/test.bats \ test/functional/search/content-check-poslib64/test.bats \ + test/functional/search_v2/search-content-check-positive.bats \ test/functional/update/apply-full-file-delta/test.bats \ test/functional/update/boot-file/test.bats \ test/functional/update/boot-skip/test.bats \ diff --git a/test/functional/README.md b/test/functional/README.md index a2eb87a3..8f30f20d 100644 --- a/test/functional/README.md +++ b/test/functional/README.md @@ -302,46 +302,99 @@ run assert_file_not_exists /some/file ``` -*assert_in_output* -passes if the provided text is included in the command output, fails otherwise +*assert_is_output* +passes if the provided text matches the whole command output, fails otherwise Examples: ```bash # one line strings run -assert_in_output "Successfully installed 1 bundle" +assert_is_output "Some output" # multi-line strings +run +expected_output=$(cat <<-EOM + Some text that needs + to match exactly with + the whole command output +EOM +) +assert_is_output "$expected_output" +``` + +*assert_is_not_output* +passes if the provided text does not match the whole command output, fails otherwise +Examples: +```bash +run +assert_is_not_output "This should not be the command output" +``` + +*assert_in_output* +passes if the provided text is included in the command output (partial match), fails otherwise +Examples: +```bash run expected_output=$(cat <<-EOM - Some multi-ine text that needs to be present - in the exact order - some more lines, bla bla + Some multi-line output + some more lines, + bla bla EOM ) assert_in_output "$expected_output" - -# combination of both -run -expected_output=$(cat <<-EOM - Some multi-ine text that needs to be present - in the exact order - some more lines, bla bla -EOM -) -assert_in_output "some literall text" -assert_in_output "$expected_output" -assert_in_output "more text to be checked" ``` *assert_not_in_output* -passes if the provided text is not included in the command output, fails otherwise +passes if the provided text is not included in the command output (partial match), fails otherwise Examples: ```bash -# one line strings run assert_not_in_output "Error" ``` +*assert_regex_is_output* +similar to assert_is_output but this assertion receives a regular expression, passes +if the provided regular expression matches the whole command output, fails otherwise +Examples: +```bash +# remember to skip characters that are part of the expected +# output that match special regex characters like .*?()[] +run +expected_output=$(cat <<-EOM + Some expected text that can have + regex characters like .* in it + \(skipping these pharenteses\)\. +EOM +) +assert_regex_is_output "$expected_output" +``` + +*assert_regex_is_not_output* +similar to assert_is_not_output but this assertion receives a regular expression, passes +if the provided regular expression does not match the command output, fails otherwise +Examples: +```bash +run +assert_regex_is_not_output "Error .?" +``` + +*assert_regex_in_output* +similar to assert_in_output but this assertion receives a regular expression, passes +if the provided regular expression is part of the command output (partial match), fails otherwise +Examples: +```bash +run +assert_regex_in_output "This is part .* of the output" +``` + +*assert_regex_not_in_output* +similar to assert_not_in_output but this assertion receives a regular expression, passes +if the provided regular expression is not part of the command output (partial match), fails otherwise +Examples: +```bash +run +assert_regex_not_in_output "Error." +``` + *assert_equal* passes if the two values provided are equal, fails otherwise Example: diff --git a/test/functional/bundleadd_v2/add-bad-hash-state.bats b/test/functional/bundleadd_v2/add-bad-hash-state.bats index c7103466..c788fecc 100755 --- a/test/functional/bundleadd_v2/add-bad-hash-state.bats +++ b/test/functional/bundleadd_v2/add-bad-hash-state.bats @@ -36,6 +36,6 @@ test_setup() { Successfully installed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-bad-hash.bats b/test/functional/bundleadd_v2/add-bad-hash.bats index ff22c0c3..14742d7e 100755 --- a/test/functional/bundleadd_v2/add-bad-hash.bats +++ b/test/functional/bundleadd_v2/add-bad-hash.bats @@ -37,7 +37,7 @@ test_setup() { Error: File content hash mismatch for $TEST_DIRNAME/state/staged/e6d85023c5e619eb43d5cfbfdbdec784afef5a82ffa54e8c93bda3e0883360a3 (bad server data?) EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-bad-manifest.bats b/test/functional/bundleadd_v2/add-bad-manifest.bats index a2ad1ce7..c8cdf061 100755 --- a/test/functional/bundleadd_v2/add-bad-manifest.bats +++ b/test/functional/bundleadd_v2/add-bad-manifest.bats @@ -27,6 +27,6 @@ test_setup() { Failed to install 1 of 1 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-boot-file.bats b/test/functional/bundleadd_v2/add-boot-file.bats index d0160e25..9f5e6388 100755 --- a/test/functional/bundleadd_v2/add-boot-file.bats +++ b/test/functional/bundleadd_v2/add-boot-file.bats @@ -17,15 +17,16 @@ test_setup() { assert_status_is 0 assert_file_exists "$TEST_NAME/target-dir/usr/lib/kernel/test-file" expected_output=$(cat <<-EOM - Starting download of remaining update content. This may take a while... - . - Finishing download of update content... - Installing bundle(s) files... - . - Calling post-update helper scripts. + Starting download of remaining update content. This may take a while\.\.\. + \. + Finishing download of update content\.\.\. + Installing bundle\(s\) files\.\.\. + \. + Calling post-update helper scripts\. + .* + Successfully installed 1 bundle EOM ) - assert_in_output "$expected_output" - assert_in_output "Successfully installed 1 bundle" + assert_regex_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-boot-skip.bats b/test/functional/bundleadd_v2/add-boot-skip.bats index 0987bddb..49290040 100755 --- a/test/functional/bundleadd_v2/add-boot-skip.bats +++ b/test/functional/bundleadd_v2/add-boot-skip.bats @@ -27,6 +27,6 @@ test_setup() { Successfully installed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-directory.bats b/test/functional/bundleadd_v2/add-directory.bats index 4bf85777..8cc78467 100755 --- a/test/functional/bundleadd_v2/add-directory.bats +++ b/test/functional/bundleadd_v2/add-directory.bats @@ -25,6 +25,6 @@ test_setup() { Successfully installed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-existing.bats b/test/functional/bundleadd_v2/add-existing.bats index a59c611b..6ab5994c 100755 --- a/test/functional/bundleadd_v2/add-existing.bats +++ b/test/functional/bundleadd_v2/add-existing.bats @@ -19,6 +19,6 @@ test_setup() { 1 bundle was already installed EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-include.bats b/test/functional/bundleadd_v2/add-include.bats index b120c3bf..7f2ed492 100755 --- a/test/functional/bundleadd_v2/add-include.bats +++ b/test/functional/bundleadd_v2/add-include.bats @@ -31,6 +31,6 @@ test_setup() { Successfully installed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-multiple.bats b/test/functional/bundleadd_v2/add-multiple.bats index b253f15b..1d4aa6c8 100755 --- a/test/functional/bundleadd_v2/add-multiple.bats +++ b/test/functional/bundleadd_v2/add-multiple.bats @@ -36,6 +36,6 @@ test_setup() { Successfully installed 2 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-rc.bats b/test/functional/bundleadd_v2/add-rc.bats index d0c24f0e..44f041be 100755 --- a/test/functional/bundleadd_v2/add-rc.bats +++ b/test/functional/bundleadd_v2/add-rc.bats @@ -52,7 +52,7 @@ global_teardown() { Successfully installed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -70,7 +70,7 @@ global_teardown() { Successfully installed 2 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -88,7 +88,7 @@ global_teardown() { 1 bundle was already installed EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -101,7 +101,7 @@ global_teardown() { Failed to install 1 of 1 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -115,7 +115,7 @@ global_teardown() { Failed to install 2 of 2 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -129,7 +129,7 @@ global_teardown() { 2 bundles were already installed EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -144,7 +144,7 @@ global_teardown() { 1 bundle was already installed EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -168,7 +168,7 @@ global_teardown() { 1 bundle was already installed EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -187,7 +187,7 @@ global_teardown() { Failed to install 1 of 2 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -208,7 +208,7 @@ global_teardown() { 1 bundle was already installed EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -222,21 +222,19 @@ global_teardown() { run sudo sh -c "$SWUPD bundle-add $SWUPD_OPTS test-bundle1 test-bundle2 fake-bundle test-bundle3" assert_status_is "$EBUNDLE_INSTALL" expected_output=$(cat <<-EOM - Warning: Bundle "fake-bundle" is invalid, skipping it... - Warning: Bundle "test-bundle3" is already installed, skipping it... - Starting download of remaining update content. This may take a while... - . - Finishing download of update content... - Error for $file_hash tarfile extraction - EOM - ) - assert_in_output "$expected_output" - expected_output=$(cat <<-EOM + Warning: Bundle "fake-bundle" is invalid, skipping it\.\.\. + Warning: Bundle "test-bundle3" is already installed, skipping it\.\.\. + Starting download of remaining update content\. This may take a while\.\.\. + \. + Finishing download of update content\.\.\. + Error for $file_hash tarfile extraction, .* + Installing bundle\(s\) files\.\.\. + .Path /bar/test-file2 is missing on the file system \.\.\. fixing Error: Failed to download file /bar/test-file2 in verify_fix_path Failed to install 3 of 3 bundles 1 bundle was already installed EOM ) - assert_in_output "$expected_output" + assert_regex_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-skip-scripts.bats b/test/functional/bundleadd_v2/add-skip-scripts.bats index 4cf8a3f3..66e90fee 100755 --- a/test/functional/bundleadd_v2/add-skip-scripts.bats +++ b/test/functional/bundleadd_v2/add-skip-scripts.bats @@ -25,6 +25,6 @@ test_setup() { Successfully installed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleadd_v2/add-verify-fix-path.bats b/test/functional/bundleadd_v2/add-verify-fix-path.bats index 0e4667a3..7b96b81e 100755 --- a/test/functional/bundleadd_v2/add-verify-fix-path.bats +++ b/test/functional/bundleadd_v2/add-verify-fix-path.bats @@ -39,6 +39,6 @@ test_setup() { Successfully installed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundlelist_v2/list-all.bats b/test/functional/bundlelist_v2/list-all.bats index bcccdcdc..b621e6eb 100755 --- a/test/functional/bundlelist_v2/list-all.bats +++ b/test/functional/bundlelist_v2/list-all.bats @@ -20,6 +20,6 @@ test_setup() { test-bundle2 EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundlelist_v2/list-deps-flat.bats b/test/functional/bundlelist_v2/list-deps-flat.bats index ea6ea68b..759528f6 100755 --- a/test/functional/bundlelist_v2/list-deps-flat.bats +++ b/test/functional/bundlelist_v2/list-deps-flat.bats @@ -26,6 +26,6 @@ test_setup() { test-bundle3 EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundlelist_v2/list-deps-invalid-bundle.bats b/test/functional/bundlelist_v2/list-deps-invalid-bundle.bats index a956b510..8f064d31 100755 --- a/test/functional/bundlelist_v2/list-deps-invalid-bundle.bats +++ b/test/functional/bundlelist_v2/list-deps-invalid-bundle.bats @@ -11,6 +11,6 @@ load "../testlib" Error: Bad bundle name detected - Aborting EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundlelist_v2/list-deps-nested.bats b/test/functional/bundlelist_v2/list-deps-nested.bats index c031c7ff..7f02b214 100755 --- a/test/functional/bundlelist_v2/list-deps-nested.bats +++ b/test/functional/bundlelist_v2/list-deps-nested.bats @@ -26,6 +26,6 @@ test_setup() { test-bundle3 EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundlelist_v2/list-has-dep-nested-not-installed.bats b/test/functional/bundlelist_v2/list-has-dep-nested-not-installed.bats index 37c988be..168a1680 100755 --- a/test/functional/bundlelist_v2/list-has-dep-nested-not-installed.bats +++ b/test/functional/bundlelist_v2/list-has-dep-nested-not-installed.bats @@ -25,6 +25,6 @@ test_setup() { Error: Bundle list failed EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundlelist_v2/list-has-dep-nested-server.bats b/test/functional/bundlelist_v2/list-has-dep-nested-server.bats index a54850fd..fb7049be 100755 --- a/test/functional/bundlelist_v2/list-has-dep-nested-server.bats +++ b/test/functional/bundlelist_v2/list-has-dep-nested-server.bats @@ -31,6 +31,6 @@ test_setup() { |-- test-bundle3 EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundlelist_v2/list-has-dep-nested.bats b/test/functional/bundlelist_v2/list-has-dep-nested.bats index 51ee23ba..d87aeeb2 100755 --- a/test/functional/bundlelist_v2/list-has-dep-nested.bats +++ b/test/functional/bundlelist_v2/list-has-dep-nested.bats @@ -31,6 +31,6 @@ test_setup() { |-- test-bundle3 EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundlelist_v2/list-no-deps.bats b/test/functional/bundlelist_v2/list-no-deps.bats index 40c2a182..bb2b65e3 100755 --- a/test/functional/bundlelist_v2/list-no-deps.bats +++ b/test/functional/bundlelist_v2/list-no-deps.bats @@ -18,6 +18,6 @@ test_setup() { No included bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundlelist_v2/list-none-has-deps.bats b/test/functional/bundlelist_v2/list-none-has-deps.bats index 0339c366..14424841 100755 --- a/test/functional/bundlelist_v2/list-none-has-deps.bats +++ b/test/functional/bundlelist_v2/list-none-has-deps.bats @@ -18,6 +18,6 @@ test_setup() { No bundles have test-bundle1 as a dependency EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleremove_v2/remove-boot-file.bats b/test/functional/bundleremove_v2/remove-boot-file.bats index c9965fe7..3ec5cf2a 100755 --- a/test/functional/bundleremove_v2/remove-boot-file.bats +++ b/test/functional/bundleremove_v2/remove-boot-file.bats @@ -20,6 +20,6 @@ test_setup() { Successfully removed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleremove_v2/remove-file.bats b/test/functional/bundleremove_v2/remove-file.bats index df930763..0da2157b 100755 --- a/test/functional/bundleremove_v2/remove-file.bats +++ b/test/functional/bundleremove_v2/remove-file.bats @@ -20,6 +20,6 @@ test_setup() { Successfully removed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleremove_v2/remove-include-nested.bats b/test/functional/bundleremove_v2/remove-include-nested.bats index 9ea5c3af..5ee368ff 100755 --- a/test/functional/bundleremove_v2/remove-include-nested.bats +++ b/test/functional/bundleremove_v2/remove-include-nested.bats @@ -35,6 +35,6 @@ test_setup() { Failed to remove 1 of 1 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleremove_v2/remove-include.bats b/test/functional/bundleremove_v2/remove-include.bats index faf0f5c0..2b033d8e 100755 --- a/test/functional/bundleremove_v2/remove-include.bats +++ b/test/functional/bundleremove_v2/remove-include.bats @@ -31,6 +31,6 @@ test_setup() { Failed to remove 1 of 1 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleremove_v2/remove-multiple.bats b/test/functional/bundleremove_v2/remove-multiple.bats index 0ce78f94..cbbb119e 100755 --- a/test/functional/bundleremove_v2/remove-multiple.bats +++ b/test/functional/bundleremove_v2/remove-multiple.bats @@ -36,6 +36,6 @@ test_setup() { Successfully removed 3 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/bundleremove_v2/remove-os-core.bats b/test/functional/bundleremove_v2/remove-os-core.bats index 57e4581a..98d2677f 100755 --- a/test/functional/bundleremove_v2/remove-os-core.bats +++ b/test/functional/bundleremove_v2/remove-os-core.bats @@ -12,6 +12,6 @@ load "../testlib" Failed to remove 1 of 1 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } \ No newline at end of file diff --git a/test/functional/bundleremove_v2/remove-rc.bats b/test/functional/bundleremove_v2/remove-rc.bats index 37cfdf01..fe8d966d 100755 --- a/test/functional/bundleremove_v2/remove-rc.bats +++ b/test/functional/bundleremove_v2/remove-rc.bats @@ -46,7 +46,7 @@ global_teardown() { Successfully removed 1 bundle EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -64,7 +64,7 @@ global_teardown() { Successfully removed 2 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -81,7 +81,7 @@ global_teardown() { Failed to remove 1 of 1 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -94,7 +94,7 @@ global_teardown() { Failed to remove 1 of 1 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -108,7 +108,7 @@ global_teardown() { Failed to remove 2 of 2 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } @@ -129,6 +129,6 @@ global_teardown() { Failed to remove 2 of 3 bundles EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } \ No newline at end of file diff --git a/test/functional/checkupdate_v2/chk-update-new-version.bats b/test/functional/checkupdate_v2/chk-update-new-version.bats index 9461c49f..fe42b8d5 100755 --- a/test/functional/checkupdate_v2/chk-update-new-version.bats +++ b/test/functional/checkupdate_v2/chk-update-new-version.bats @@ -22,6 +22,6 @@ test_setup() { There is a new OS version available: 100 EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/checkupdate_v2/chk-update-no-server-content.bats b/test/functional/checkupdate_v2/chk-update-no-server-content.bats index 158218b9..7526bc68 100755 --- a/test/functional/checkupdate_v2/chk-update-no-server-content.bats +++ b/test/functional/checkupdate_v2/chk-update-no-server-content.bats @@ -14,6 +14,6 @@ test_setup() { run sudo sh -c "$SWUPD check-update $SWUPD_OPTS_NO_CERT" assert_status_is_not 0 - assert_in_output "Error: server does not report any version" + assert_is_output "Error: server does not report any version" } diff --git a/test/functional/checkupdate_v2/chk-update-no-target-content.bats b/test/functional/checkupdate_v2/chk-update-no-target-content.bats index 847e17eb..ecd7cb79 100755 --- a/test/functional/checkupdate_v2/chk-update-no-target-content.bats +++ b/test/functional/checkupdate_v2/chk-update-no-target-content.bats @@ -14,6 +14,6 @@ test_setup() { run sudo sh -c "$SWUPD check-update $SWUPD_OPTS_NO_CERT" assert_status_is_not 0 - assert_in_output "Unable to determine current OS version" + assert_is_output "Unable to determine current OS version" } diff --git a/test/functional/checkupdate_v2/chk-update-slow-server.bats b/test/functional/checkupdate_v2/chk-update-slow-server.bats index e6e46d6f..497154a0 100755 --- a/test/functional/checkupdate_v2/chk-update-slow-server.bats +++ b/test/functional/checkupdate_v2/chk-update-slow-server.bats @@ -29,7 +29,7 @@ test_teardown() { There is a new OS version available: 99990 EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/checkupdate_v2/chk-update-version-match.bats b/test/functional/checkupdate_v2/chk-update-version-match.bats index 44e5e201..25a974dd 100755 --- a/test/functional/checkupdate_v2/chk-update-version-match.bats +++ b/test/functional/checkupdate_v2/chk-update-version-match.bats @@ -11,6 +11,6 @@ load "../testlib" There are no updates available EOM ) - assert_in_output "$expected_output" + assert_is_output "$expected_output" } diff --git a/test/functional/completion_v2/completion-basic.bats b/test/functional/completion_v2/completion-basic.bats new file mode 100755 index 00000000..02f21543 --- /dev/null +++ b/test/functional/completion_v2/completion-basic.bats @@ -0,0 +1,41 @@ +#!/usr/bin/env bats + +load "../testlib" + +test_setup() { + + # do nothing + return + +} + +test_teardown() { + + # do nothing + return + +} + +@test "autocomplete exists" { + + assert_file_exists "$SWUPD_DIR"/swupd.bash + +} + +@test "autocomplete syntax" { + + bash "$SWUPD_DIR"/swupd.bash + +} + +@test "autocomplete has autoupdate" { + + grep -q '("autoupdate")' "$SWUPD_DIR"/swupd.bash + +} + +@test "autocomplete has expected hashdump opts" { + + grep -q 'opts="--help --no-xattrs --path "' "$SWUPD_DIR"/swupd.bash + +} diff --git a/test/functional/hashdump_v2/hashdump-file-hash.bats b/test/functional/hashdump_v2/hashdump-file-hash.bats new file mode 100755 index 00000000..dbbba9ef --- /dev/null +++ b/test/functional/hashdump_v2/hashdump-file-hash.bats @@ -0,0 +1,55 @@ +#!/usr/bin/env bats + +load "../testlib" + +global_setup() { + + create_test_environment "$TEST_NAME" + printf "test-data" | sudo tee "$TEST_NAME"/target-dir/test-hash > /dev/null + +} + +test_setup() { + + return + +} + +test_teardown() { + + return + +} + +global_teardown() { + + destroy_test_environment "$TEST_NAME" + +} + +@test "hashdump with prefix" { + + run sudo sh -c "$SWUPD hashdump --path=$TEST_NAME/target-dir /test-hash" + assert_status_is 0 + expected_output=$(cat <<-EOM + Calculating hash with xattrs for: .*/target-dir/test-hash + 8286279c93f45c7ffa6b9ed440066de09716527346d9dd0239f50948e0e554f0 + EOM + ) + assert_regex_is_output "$expected_output" + +} + +@test "hashdump with no prefix" { + + run sudo sh -c "$SWUPD hashdump $TEST_NAME/target-dir/test-hash" + + assert_status_is 0 + expected_output=$(cat <<-EOM + Calculating hash with xattrs for: .*/target-dir/test-hash + 8286279c93f45c7ffa6b9ed440066de09716527346d9dd0239f50948e0e554f0 + EOM + ) + assert_regex_is_output "$expected_output" + +} diff --git a/test/functional/mirror_v2/mirror-createdir-negative.bats b/test/functional/mirror_v2/mirror-createdir-negative.bats new file mode 100755 index 00000000..73f02890 --- /dev/null +++ b/test/functional/mirror_v2/mirror-createdir-negative.bats @@ -0,0 +1,62 @@ +#!/usr/bin/env bats + +load "../testlib" + +global_setup() { + + create_test_environment "$TEST_NAME" + +} + +test_setup() { + + # do nothing + return + +} + +test_teardown() { + + sudo rm -rf "$TEST_NAME"/target-dir/etc/swupd + +} + +global_teardown() { + + destroy_test_environment "$TEST_NAME" + +} + +@test "mirror /etc/swupd is a file" { + + sudo touch "$TEST_NAME"/target-dir/etc/swupd + + run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR" + assert_status_is_not 0 + expected_output=$(cat <<-EOM + .*/etc/swupd: not a directory + Unable to set mirror url + + Default version URL not found. Use the -v option instead. + EOM + ) + assert_regex_is_output "$expected_output" + +} + +@test "mirror /etc/swupd is a symlink to a file" { + + sudo touch "$TEST_NAME"/target-dir/foo + sudo ln -s $(realpath "$TEST_NAME"/target-dir/foo) "$TEST_NAME"/target-dir/etc/swupd + run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR" + assert_status_is_not 0 + expected_output=$(cat <<-EOM + .*/etc/swupd: not a directory + Unable to set mirror url + + Default version URL not found. Use the -v option instead. + EOM + ) + assert_regex_is_output "$expected_output" + +} diff --git a/test/functional/mirror_v2/mirror-createdir.bats b/test/functional/mirror_v2/mirror-createdir.bats new file mode 100755 index 00000000..acf745e2 --- /dev/null +++ b/test/functional/mirror_v2/mirror-createdir.bats @@ -0,0 +1,84 @@ +#!/usr/bin/env bats + +load "../testlib" + +global_setup() { + + create_test_environment "$TEST_NAME" + +} + +test_setup() { + + # do nothing + return + +} + +test_teardown() { + + sudo rm -rf "$TEST_NAME"/target-dir/etc/swupd + +} + +global_teardown() { + + destroy_test_environment "$TEST_NAME" + +} + +@test "mirror /etc/swupd does not exist" { + + run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR" + assert_status_is 0 + expected_output=$(cat <<-EOM + Set upstream mirror to http://example.com/swupd-file + Installed version: 10 + Version URL: http://example.com/swupd-file + Content URL: http://example.com/swupd-file + EOM + ) + assert_is_output "$expected_output" + assert_equal "http://example.com/swupd-file" $(<$TEST_NAME/target-dir/etc/swupd/mirror_contenturl) + assert_equal "http://example.com/swupd-file" $(<$TEST_NAME/target-dir/etc/swupd/mirror_versionurl) + +} + +@test "mirror /etc/swupd already exists" { + + sudo mkdir -p "$TEST_NAME"/target-dir/etc/swupd + run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR" + assert_status_is 0 + expected_output=$(cat <<-EOM + Set upstream mirror to http://example.com/swupd-file + Installed version: 10 + Version URL: http://example.com/swupd-file + Content URL: http://example.com/swupd-file + EOM + ) + assert_is_output "$expected_output" + assert_equal "http://example.com/swupd-file" $(<$TEST_NAME/target-dir/etc/swupd/mirror_contenturl) + assert_equal "http://example.com/swupd-file" $(<$TEST_NAME/target-dir/etc/swupd/mirror_versionurl) + +} + +@test "mirror /etc/swupd is a symlink to a directory" { + + sudo mkdir "$TEST_NAME"/target-dir/foo + sudo ln -s $(realpath "$TEST_NAME"/target-dir/foo) "$TEST_NAME"/target-dir/etc/swupd + run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR" + assert_status_is 0 + expected_output=$(cat <<-EOM + Set upstream mirror to http://example.com/swupd-file + Installed version: 10 + Version URL: http://example.com/swupd-file + Content URL: http://example.com/swupd-file + EOM + ) + assert_is_output "$expected_output" + + ! [[ -L "$TEST_NAME/target-dir/etc/swupd" ]] + assert_equal "http://example.com/swupd-file" $(<$TEST_NAME/target-dir/etc/swupd/mirror_contenturl) + assert_equal "http://example.com/swupd-file" $(<$TEST_NAME/target-dir/etc/swupd/mirror_versionurl) + +} diff --git a/test/functional/search_v2/search-content-check-negative.bats b/test/functional/search_v2/search-content-check-negative.bats new file mode 100755 index 00000000..89e926f5 --- /dev/null +++ b/test/functional/search_v2/search-content-check-negative.bats @@ -0,0 +1,63 @@ +#!/usr/bin/env bats + +load "../testlib" + +global_setup() { + + create_test_environment "$TEST_NAME" + create_bundle -n test-bundle -f /foo/test-file1,/usr/lib/test-lib32,/libtest-nohit "$TEST_NAME" + +} + +test_setup() { + + # do nothing + return + +} + +test_teardown() { + + # do nothing + return + +} + +global_teardown() { + + destroy_test_environment "$TEST_NAME" + +} + +@test "search with non existant file, specifying full path" { + + run sudo sh -c "$SWUPD search $SWUPD_OPTS /usr/lib64/test-lib100" + assert_status_is 0 + assert_in_output "Searching for '/usr/lib64/test-lib100'" + expected_output=$(cat <<-EOM + Downloading Clear Linux manifests + .* MB total\.\.\. + + Completed manifests download\. + + Search term not found\. + EOM + ) + assert_regex_in_output "$expected_output" + +} + +@test "search with non existant library" { + + run sudo sh -c "$SWUPD search $SWUPD_OPTS -l libtest-nohit" + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for 'libtest-nohit' + + Search term not found. + EOM + ) + assert_is_output "$expected_output" + +} + diff --git a/test/functional/search_v2/search-content-check-positive.bats b/test/functional/search_v2/search-content-check-positive.bats new file mode 100755 index 00000000..da1ab803 --- /dev/null +++ b/test/functional/search_v2/search-content-check-positive.bats @@ -0,0 +1,107 @@ +#!/usr/bin/env bats + +load "../testlib" + +global_setup() { + + create_test_environment "$TEST_NAME" + create_bundle -n test-bundle -f /foo/test-file1,/usr/bin/test-bin,/usr/lib/test-lib32 "$TEST_NAME" + create_bundle -n test-bundle2 -f /bar/test-file2,/usr/lib64/test-lib64 "$TEST_NAME" + +} + +test_setup() { + + # do nothing + return + +} + +test_teardown() { + + # do nothing + return + +} + +global_teardown() { + + destroy_test_environment "$TEST_NAME" + +} + +@test "search for a binary" { + + run sudo sh -c "$SWUPD search $SWUPD_OPTS -b test-bin" + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for 'test-bin' + .* + Bundle test-bundle \(.* MB to install\) + ./usr/bin/test-bin + EOM + ) + assert_regex_is_output "$expected_output" + +} + +@test "search for a file everywhere" { + + run sudo sh -c "$SWUPD search $SWUPD_OPTS test-bin" + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for 'test-bin' + + Bundle test-bundle \(.* MB to install\) + ./usr/bin/test-bin + EOM + ) + assert_regex_is_output "$expected_output" + +} + +@test "search for a file specifying the full path" { + + run sudo sh -c "$SWUPD search $SWUPD_OPTS /usr/lib64/test-lib64" + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for '/usr/lib64/test-lib64' + + Bundle test-bundle2 \(.* MB to install\) + ./usr/lib64/test-lib64 + EOM + ) + assert_regex_is_output "$expected_output" + +} + +@test "search for a library in lib32" { + + run sudo sh -c "$SWUPD search $SWUPD_OPTS -l test-lib32" + + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for 'test-lib32' + + Bundle test-bundle \(.* MB to install\) + ./usr/lib/test-lib32 + EOM + ) + assert_regex_is_output "$expected_output" + +} + +@test "search for a library in lib64" { + + run sudo sh -c "$SWUPD search $SWUPD_OPTS -l test-lib64" + assert_status_is 0 + expected_output=$(cat <<-EOM + Searching for 'test-lib64' + + Bundle test-bundle2 \(.* MB to install\) + ./usr/lib64/test-lib64 + EOM + ) + assert_regex_is_output "$expected_output" + +} diff --git a/test/functional/testlib.bash b/test/functional/testlib.bash index 9837384a..c702a39d 100644 --- a/test/functional/testlib.bash +++ b/test/functional/testlib.bash @@ -126,6 +126,7 @@ set_env_variables() { export SWUPD_OPTS="-S $path/$env_name/state -p $path/$env_name/target-dir -F staging -u file://$path/$env_name/web-dir -C $FUNC_DIR/Swupd_Root.pem -I" export SWUPD_OPTS_NO_CERT="-S $path/$env_name/state -p $path/$env_name/target-dir -F staging -u file://$path/$env_name/web-dir" + export SWUPD_OPTS_MIRROR="-p $path/$env_name/target-dir" export TEST_DIRNAME="$path"/"$env_name" } @@ -513,6 +514,9 @@ create_test_environment() { printf 'BUG_REPORT_URL="https://bugs.clearlinux.org/jira"\n' } | sudo tee "$env_name"/target-dir/usr/lib/os-release > /dev/null sudo mkdir -p "$env_name"/target-dir/usr/share/clear/bundles + sudo mkdir -p "$env_name"/target-dir/usr/share/defaults/swupd + printf '1' | sudo tee "$env_name"/target-dir/usr/share/defaults/swupd/format > /dev/null + sudo mkdir -p "$env_name"/target-dir/etc # state files & dirs sudo mkdir -p "$env_name"/state/{staged,download,delta,telemetry} @@ -1132,6 +1136,96 @@ assert_not_in_output() { } +assert_is_output() { + + local expected_output=$1 + local sep="------------------------------------------------------------------" + validate_param expected_output + + if [[ ! "$output" == "$expected_output" ]]; then + print_assert_failure "The following text was not the command output:\\n$sep\\n$expected_output\\n$sep" + echo -e "Difference:\\n$sep" + echo "$(diff <(echo "$expected_output") <(echo "$output"))" + return 1 + fi + +} + +assert_is_not_output() { + + local expected_output=$1 + local sep="------------------------------------------------------------------" + validate_param expected_output + + if [[ "$output" == "$expected_output" ]]; then + print_assert_failure "The following text was the command output and should not have:\\n$sep\\n$expected_output\\n$sep" + echo -e "Difference:\\n$sep" + echo "$(diff <(echo "$expected_output") <(echo "$output"))" + return 1 + fi + +} + +assert_regex_in_output() { + + local expected_output=$1 + local sep="------------------------------------------------------------------" + validate_param expected_output + + if [[ ! "$output" =~ $expected_output ]]; then + print_assert_failure "The following text (regex) was not found in the command output:\\n$sep\\n$expected_output\\n$sep" + echo -e "Difference:\\n$sep" + echo "$(diff <(echo "$expected_output") <(echo "$output"))" + return 1 + fi + +} + +assert_regex_not_in_output() { + + local expected_output=$1 + local sep="------------------------------------------------------------------" + validate_param expected_output + + if [[ "$output" =~ $expected_output ]]; then + print_assert_failure "The following text (regex) was found in the command output and should not have:\\n$sep\\n$expected_output\\n$sep" + echo -e "Difference:\\n$sep" + echo "$(diff <(echo "$expected_output") <(echo "$output"))" + return 1 + fi + +} + +assert_regex_is_output() { + + local expected_output=$1 + local sep="------------------------------------------------------------------" + validate_param expected_output + + if [[ ! "$output" =~ ^$expected_output$ ]]; then + print_assert_failure "The following text (regex) was not the command output:\\n$sep\\n$expected_output\\n$sep" + echo -e "Difference:\\n$sep" + echo "$(diff <(echo "$expected_output") <(echo "$output"))" + return 1 + fi + +} + +assert_regex_is_not_output() { + + local expected_output=$1 + local sep="------------------------------------------------------------------" + validate_param expected_output + + if [[ "$output" =~ ^$expected_output$ ]]; then + print_assert_failure "The following text (regex) was the command output and should not have:\\n$sep\\n$expected_output\\n$sep" + echo -e "Difference:\\n$sep" + echo "$(diff <(echo "$expected_output") <(echo "$output"))" + return 1 + fi + +} + assert_equal() { local val1=$1