Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): setupos unit tests #3661

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cce32b2
normalize config.sh
andrewbattat Jan 21, 2025
9bc7442
Merge branch 'master' into andrew/setupos-unit-tests
andrewbattat Jan 28, 2025
a0fef8f
Add test-setupos.sh unit tests
andrewbattat Jan 28, 2025
997e3a8
Remove unnecessary comment
andrewbattat Jan 28, 2025
e298eb6
Fix pre-commit
andrewbattat Jan 28, 2025
3f99fa2
Fix buildifier
andrewbattat Jan 29, 2025
5d91b85
Merge branch 'master' into andrew/setupos-unit-tests
andrewbattat Feb 18, 2025
5523074
Make test_setupos non-manual
andrewbattat Feb 18, 2025
9f6c81f
Add check-hardware unit tests
andrewbattat Feb 18, 2025
b43f8fe
Refactor test-setupos.sh
andrewbattat Feb 18, 2025
f39cc75
Remove log_ mocked functions
andrewbattat Feb 18, 2025
fff9e91
Refactor log_and_halt_installation_on_error
andrewbattat Feb 18, 2025
e17dff3
Add test_verify_cpu_gen1_failure
andrewbattat Feb 18, 2025
9ff9ad1
Add missing unit tests
andrewbattat Feb 18, 2025
89901d1
Remove unnecessary || return statements
andrewbattat Feb 18, 2025
0c5ce69
Fix buldifier and pre-commit
andrewbattat Feb 18, 2025
ee87cd0
Fix test_validate_domain_name logs
andrewbattat Feb 19, 2025
e824195
Refactor test_validate_domain_name to separate test cases and the act…
andrewbattat Feb 19, 2025
a02af99
Separate test cases and test logic for test_detect_hardware_generation
andrewbattat Feb 19, 2025
df5e11b
Separate test cases and test logic fortest_verify_cpu
andrewbattat Feb 19, 2025
c5d0e65
Separate test cases and test logic for test_verify_memory
andrewbattat Feb 19, 2025
f4305df
Catch failure errors more explicitly
andrewbattat Feb 19, 2025
ce36455
Remove unnecessary comment
andrewbattat Feb 19, 2025
25e1511
Fix pre-commit
andrewbattat Feb 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ic-os/components/setupos-scripts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,15 @@ sh_test(
srcs = ["test-kernel-cmdline-function-runner"],
data = [":functions.sh"],
)

sh_test(
name = "test_setupos",
srcs = ["test-setupos.sh"],
args = [
"$(execpath check-network.sh)",
],
data = [
"check-network.sh",
],
tags = ["manual"],
)
8 changes: 4 additions & 4 deletions ic-os/components/setupos-scripts/check-network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,20 @@ function validate_domain_name() {
IFS='.' read -ra domain_parts <<<"${domain_name}"

if [ ${#domain_parts[@]} -lt 2 ]; then
log_and_halt_installation_on_error 1 "Domain validation error: less than two domain parts in domain: ${domain_name}"
log_and_halt_installation_on_error 1 "Domain validation error: less than two domain parts in domain: ${domain_name}" || return $?
andrewbattat marked this conversation as resolved.
Show resolved Hide resolved
fi

for domain_part in "${domain_parts[@]}"; do
if [ -z "$domain_part" ] || [ ${#domain_part} -gt 63 ]; then
log_and_halt_installation_on_error 1 "Domain validation error: domain part length violation: ${domain_part}"
log_and_halt_installation_on_error 1 "Domain validation error: domain part length violation: ${domain_part}" || return $?
fi

if [[ $domain_part == -* ]] || [[ $domain_part == *- ]]; then
log_and_halt_installation_on_error 1 "Domain validation error: domain part starts or ends with a hyphen: ${domain_part}"
log_and_halt_installation_on_error 1 "Domain validation error: domain part starts or ends with a hyphen: ${domain_part}" || return $?
fi

if ! [[ $domain_part =~ ^[a-zA-Z0-9-]+$ ]]; then
log_and_halt_installation_on_error 1 "Domain validation error: invalid characters in domain part: ${domain_part}"
log_and_halt_installation_on_error 1 "Domain validation error: invalid characters in domain part: ${domain_part}" || return $?
fi
done
}
Expand Down
81 changes: 81 additions & 0 deletions ic-os/components/setupos-scripts/test-setupos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash

set -euo pipefail

# ------------------------------------------------------------------------------
# Test harness for check-network.sh
# ------------------------------------------------------------------------------

CHECK_NETWORK_SCRIPT="${1:-./check-network.sh}"

# ------------------------------------------------------------------------------
# Override "source" so that sourcing /opt/ic/bin/config.sh and /opt/ic/bin/functions.sh
# does not fail in our test environment.
# ------------------------------------------------------------------------------
function source() {
if [[ "$1" == "/opt/ic/bin/config.sh" || "$1" == "/opt/ic/bin/functions.sh" ]]; then
echo "MOCKED: ignoring source of '$1' (file not present in test environment)"
return
fi
builtin source "$1"
}

# ------------------------------------------------------------------------------
# Mocked out functions
# ------------------------------------------------------------------------------

function log_and_halt_installation_on_error() {
local exit_code="$1"
return "${exit_code}"
}

function log_start() {
local script="${1}"
}

function log_end() {
local script="${1}"
}

# ------------------------------------------------------------------------------
# Unit tests
# ------------------------------------------------------------------------------

function test_validate_domain_name() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about separating the test cases and the actual tests. Like that we can easily add more tests (e.g., for subdomains). Something like this (not sure it actually runs):

function test_validate_domain_name() {
    declare -A test_cases=(
        ["example.com"]=0
        ["example."]=1
        ["&BadDOMAIN.com"]=1
    )

    for domain_name in "${!test_cases[@]}"; do
        expected_fail=${test_cases[$domain_name]}
        echo "Running test for domain: $domain_name"

        if validate_domain_name; then
            if [[ $expected_fail -eq 0 ]]; then
                echo "  PASS: valid domain: $domain_name"
            else
                echo "  FAIL: domain $domain_name validation was expected to fail but didn't"
                exit 1
            fi
        else
            if [[ $expected_fail -eq 1 ]]; then
                echo "  PASS: domain $domain_name validation failed as expected"
            else
                echo "  FAIL: valid domain $domain_name was incorrectly marked as invalid"
                exit 1
            fi
        fi
    done
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! I've done this for all the unit tests. Much cleaner this way!
e824195
a02af99
df5e11b
c5d0e65

echo "Running test: test_validate_domain_name_valid"
domain_name="example.com"
# If domain_name is valid, validate_domain_name should never call
# log_and_halt_installation_on_error, so it should exit 0.
if validate_domain_name; then
echo " PASS: valid domain: $domain_name"
else
echo " FAIL: valid domain: domain_name"
exit 1
fi

echo "Running test: test_validate_domain_name_invalid"
domain_name="example."
if ! validate_domain_name; then
echo " PASS: domain $domain_name validation failed as expected"
else
echo " FAIL: domain $domain_name validation was expected to fail but didn't"
exit 1
fi
domain_name="&BadDOMAIN.com"
if ! validate_domain_name; then
echo " PASS: domain $domain_name validation failed as expected"
else
echo " FAIL: domain $domain_name validation was expected to fail but didn't"
exit 1
fi
}

# ------------------------------------------------------------------------------
# Run tests
# ------------------------------------------------------------------------------
source "${CHECK_NETWORK_SCRIPT}"

test_validate_domain_name

echo
echo "All tests passed."
Loading