diff options
author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2024-06-20 15:10:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-20 15:10:56 -0400 |
commit | 931ee28eb9c5b5e3c2b0d3008f5f65d810dc9b0c (patch) | |
tree | 5dbcccffd509fa71a99c351ed4628ed0841e1e46 /tests/test_docker.c | |
parent | 11aa1d44d95da221073e512fbec3bbccc0f1a46b (diff) | |
download | stasis-931ee28eb9c5b5e3c2b0d3008f5f65d810dc9b0c.tar.gz |
Unit tests (#6)
* Initial commit of unit tests [WIP]
* Address shortcomings and bugs flushed out by unit tests
* Enable unit testing in CI workflow
* Enable verbose ctests
* Handle lack of __FILE_NAME__ define
* Only podman support `run --arch` argument
* Skip docker build testing if CI system cannot pull an image
* Remove errant call to puts()
* Identify local repo user
* Fix missing xmllint
* NULL terminate arrays
* Fix filename assignment in is_url mode
* Break loop when expected lines are exhausted
* strcmp_array expects NULL terminated array. Iterating by size in this case passes NULL to strcmp leading to an invalid read
* Remove debug printf statements
* Disable a few warnings for tests
* Workaround for ctest junit xml truncation
* Update checkout@v4
* Prevent false-positive result
* Return zero on error
* Fix strlist_remove function
* Value argument can be constant
* Fix test to match changes to startswith and endswith
* Add test_ini.c
* Fix redaction code to accept NULL pointers in array
* And let the caller specify the length of the array of strings to redact.
* Redactions now occur directly on authentication strings rather than their command line arguments
* Fix BUILD_TESTING_DEBUG
* Adds missing -D argument
Diffstat (limited to 'tests/test_docker.c')
-rw-r--r-- | tests/test_docker.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/test_docker.c b/tests/test_docker.c new file mode 100644 index 0000000..eac385e --- /dev/null +++ b/tests/test_docker.c @@ -0,0 +1,73 @@ +#include "testing.h" +struct DockerCapabilities cap_suite; + +void test_docker_capable() { + struct DockerCapabilities cap; + int result = docker_capable(&cap); + OMC_ASSERT(result == true, "docker installation unusable"); + OMC_ASSERT(cap.available, "docker is not available"); + OMC_ASSERT(cap.build != 0, "docker cannot build images"); + // no cap.podman assertion. doesn't matter if we have docker or podman + OMC_ASSERT(cap.usable, "docker is unusable"); +} + +void test_docker_exec() { + OMC_ASSERT(docker_exec("system info", 0) == 0, "unable to print docker information"); + OMC_ASSERT(docker_exec("system info", OMC_DOCKER_QUIET) == 0, "unable to be quiet"); +} + +void test_docker_sanitize_tag() { + const char *data = " !\"#$%&'()*+,-;<=>?@[\\]^_`{|}~"; + char *input = strdup(data); + docker_sanitize_tag(input); + int result = 0; + for (size_t i = 0; i < strlen(data); i++) { + if (input[i] != '-') { + result = 1; + break; + } + } + OMC_ASSERT(result == 0, "proper tag characters were not replaced correctly"); + guard_free(input); +} + +void test_docker_build_and_script_and_save() { + OMC_SKIP_IF(docker_exec("pull alpine:latest", OMC_DOCKER_QUIET), "unable to pull an image"); + + const char *dockerfile_contents = "FROM alpine:latest\nCMD [\"sh\", \"-l\"]\n"; + mkdir("test_docker_build", 0755); + if (!pushd("test_docker_build")) { + omc_testing_write_ascii("Dockerfile", dockerfile_contents); + OMC_ASSERT(docker_build(".", "-t test_docker_build", cap_suite.build) == 0, "docker build test failed"); + OMC_ASSERT(docker_script("test_docker_build", "uname -a", 0) == 0, "simple docker container script execution failed"); + OMC_ASSERT(docker_save("test_docker_build", ".", OMC_DOCKER_IMAGE_COMPRESSION) == 0, "saving a simple image failed"); + OMC_ASSERT(docker_exec("load < test_docker_build.tar.*", 0) == 0, "loading a simple image failed"); + docker_exec("image rm -f test_docker_build", 0); + remove("Dockerfile"); + popd(); + remove("test_docker_build"); + } else { + OMC_ASSERT(false, "dir change failed"); + return; + } +} + +void test_docker_validate_compression_program() { + OMC_ASSERT(docker_validate_compression_program(OMC_DOCKER_IMAGE_COMPRESSION) == 0, "baked-in compression program does not exist"); +} + +int main(int argc, char *argv[]) { + OMC_TEST_BEGIN_MAIN(); + if (!docker_capable(&cap_suite)) { + return OMC_TEST_SUITE_SKIP; + } + OMC_TEST_FUNC *tests[] = { + test_docker_capable, + test_docker_exec, + test_docker_build_and_script_and_save, + test_docker_sanitize_tag, + test_docker_validate_compression_program, + }; + OMC_TEST_RUN(tests); + OMC_TEST_END_MAIN(); +}
\ No newline at end of file |