Skip to content

Commit

Permalink
Merge branch 'be-able-to-whitelist-relays-in-an-end-to-end-test-run-d…
Browse files Browse the repository at this point in the history
…es-1052'
  • Loading branch information
Serock3 committed Jan 22, 2025
2 parents 5987277 + b198ecd commit bbfc9c8
Show file tree
Hide file tree
Showing 26 changed files with 743 additions and 619 deletions.
4 changes: 2 additions & 2 deletions mullvad-api/src/availability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ApiAvailability {
/// starting it if it's not currently running.
pub fn reset_inactivity_timer(&self) {
let mut inner = self.acquire();
log::debug!("Restarting API inactivity check");
log::trace!("Restarting API inactivity check");
inner.stop_inactivity_timer();
let availability_handle = self.clone();
inner.inactivity_timer = Some(tokio::spawn(async move {
Expand Down Expand Up @@ -252,7 +252,7 @@ impl ApiAvailabilityState {
}

fn stop_inactivity_timer(&mut self) {
log::debug!("Stopping API inactivity check");
log::trace!("Stopping API inactivity check");
if let Some(timer) = self.inactivity_timer.take() {
timer.abort();
}
Expand Down
1 change: 0 additions & 1 deletion mullvad-cli/src/cmds/relay_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ impl From<LocationArgs> for Constraint<GeographicLocationConstraint> {
(country, Some(city), Some(hostname)) => {
GeographicLocationConstraint::Hostname(country, city, hostname)
}

_ => unreachable!("invalid location arguments"),
})
}
Expand Down
53 changes: 53 additions & 0 deletions mullvad-types/src/relay_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ pub enum GeographicLocationConstraint {
Hostname(CountryCode, CityCode, Hostname),
}

#[derive(thiserror::Error, Debug)]
#[error("Failed to parse {input} into a geographic location constraint")]
pub struct ParseGeoLocationError {
input: String,
}

impl GeographicLocationConstraint {
/// Create a new [`GeographicLocationConstraint`] given a country.
pub fn country(country: impl Into<String>) -> Self {
Expand Down Expand Up @@ -227,6 +233,27 @@ impl Match<Relay> for GeographicLocationConstraint {
}
}

impl FromStr for GeographicLocationConstraint {
type Err = ParseGeoLocationError;

// TODO: Implement for country and city as well?
fn from_str(input: &str) -> Result<Self, Self::Err> {
// A host name, such as "se-got-wg-101" maps to
// Country: se
// City: got
// hostname: se-got-wg-101
let x = input.split("-").collect::<Vec<_>>();
match x[..] {
[country] => Ok(GeographicLocationConstraint::country(country)),
[country, city] => Ok(GeographicLocationConstraint::city(country, city)),
[country, city, ..] => Ok(GeographicLocationConstraint::hostname(country, city, input)),
_ => Err(ParseGeoLocationError {
input: input.to_string(),
}),
}
}
}

/// Limits the set of servers to choose based on ownership.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
Expand Down Expand Up @@ -677,3 +704,29 @@ impl RelayOverride {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn parse_hostname() {
// Parse a country
assert_eq!(
"se".parse::<GeographicLocationConstraint>().unwrap(),
GeographicLocationConstraint::country("se")
);
// Parse a city
assert_eq!(
"se-got".parse::<GeographicLocationConstraint>().unwrap(),
GeographicLocationConstraint::city("se", "got")
);
// Parse a hostname
assert_eq!(
"se-got-wg-101"
.parse::<GeographicLocationConstraint>()
.unwrap(),
GeographicLocationConstraint::hostname("se", "got", "se-got-wg-101")
);
}
}
1 change: 1 addition & 0 deletions test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

136 changes: 68 additions & 68 deletions test/scripts/test-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function get_test_utls_dir {
local script_path="${BASH_SOURCE[0]}"
local script_dir
if [[ -n "$script_path" ]]; then
script_dir="$(cd "$(dirname "$script_path")" > /dev/null && pwd)"
script_dir="$(cd "$(dirname "$script_path")" >/dev/null && pwd)"
else
script_dir="$(cd "$(dirname "$0")" > /dev/null && pwd)"
script_dir="$(cd "$(dirname "$0")" >/dev/null && pwd)"
fi
echo "$script_dir"
}
Expand Down Expand Up @@ -54,7 +54,7 @@ export CURRENT_VERSION
export LATEST_STABLE_RELEASE

function print_available_releases {
for release in $(jq -r '.[].tag_name'<<<"$RELEASES"); do
for release in $(jq -r '.[].tag_name' <<<"$RELEASES"); do
echo "$release"
done
}
Expand All @@ -73,7 +73,7 @@ function get_package_dir {
exit 1
fi

mkdir -p "$package_dir" || exit 1
mkdir -p "$package_dir" || exit 1
# Clean up old packages
find "$package_dir" -type f -mtime +5 -delete || true

Expand All @@ -89,7 +89,7 @@ function nice_time {
result=$?
fi
s=$SECONDS
echo "\"$*\" completed in $((s/60))m:$((s%60))s"
echo "\"$*\" completed in $((s / 60))m:$((s % 60))s"
return $result
}
# Matches $1 with a build version string and sets the following exported variables:
Expand Down Expand Up @@ -122,22 +122,22 @@ function get_app_filename {
version="${BUILD_VERSION}${COMMIT_HASH}${TAG:-}"
fi
case $os in
debian*|ubuntu*)
echo "MullvadVPN-${version}_amd64.deb"
;;
fedora*)
echo "MullvadVPN-${version}_x86_64.rpm"
;;
windows*)
echo "MullvadVPN-${version}.exe"
;;
macos*)
echo "MullvadVPN-${version}.pkg"
;;
*)
echo "Unsupported target: $os" 1>&2
return 1
;;
debian* | ubuntu*)
echo "MullvadVPN-${version}_amd64.deb"
;;
fedora*)
echo "MullvadVPN-${version}_x86_64.rpm"
;;
windows*)
echo "MullvadVPN-${version}.exe"
;;
macos*)
echo "MullvadVPN-${version}.pkg"
;;
*)
echo "Unsupported target: $os" 1>&2
return 1
;;
esac
}

Expand Down Expand Up @@ -177,19 +177,19 @@ function get_e2e_filename {
version="${BUILD_VERSION}${COMMIT_HASH}"
fi
case $os in
debian*|ubuntu*|fedora*)
echo "app-e2e-tests-${version}-x86_64-unknown-linux-gnu"
;;
windows*)
echo "app-e2e-tests-${version}-x86_64-pc-windows-msvc.exe"
;;
macos*)
echo "app-e2e-tests-${version}-aarch64-apple-darwin"
;;
*)
echo "Unsupported target: $os" 1>&2
return 1
;;
debian* | ubuntu* | fedora*)
echo "app-e2e-tests-${version}-x86_64-unknown-linux-gnu"
;;
windows*)
echo "app-e2e-tests-${version}-x86_64-pc-windows-msvc.exe"
;;
macos*)
echo "app-e2e-tests-${version}-aarch64-apple-darwin"
;;
*)
echo "Unsupported target: $os" 1>&2
return 1
;;
esac
}

Expand Down Expand Up @@ -282,38 +282,38 @@ function run_tests_for_os {
test_dir=$(get_test_utls_dir)/..
read -ra test_filters_arg <<<"${TEST_FILTERS:-}" # Split the string by words into an array
pushd "$test_dir"
if [ -n "${TEST_DIST_DIR+x}" ]; then
if [ ! -x "${TEST_DIST_DIR%/}/test-manager" ]; then
executable_not_found_in_dist_error test-manager
fi
test_manager="${TEST_DIST_DIR%/}/test-manager"
runner_dir_flag=("--runner-dir" "$TEST_DIST_DIR")
else
test_manager="cargo run --bin test-manager"
runner_dir_flag=()
if [ -n "${TEST_DIST_DIR+x}" ]; then
if [ ! -x "${TEST_DIST_DIR%/}/test-manager" ]; then
executable_not_found_in_dist_error test-manager
fi
test_manager="${TEST_DIST_DIR%/}/test-manager"
runner_dir_flag=("--runner-dir" "$TEST_DIST_DIR")
else
test_manager="cargo run --bin test-manager"
runner_dir_flag=()
fi

if [ -n "${MULLVAD_HOST+x}" ]; then
mullvad_host_arg=("--mullvad-host" "$MULLVAD_HOST")
else
mullvad_host_arg=()
fi
if [ -n "${MULLVAD_HOST+x}" ]; then
mullvad_host_arg=("--mullvad-host" "$MULLVAD_HOST")
else
mullvad_host_arg=()
fi

if ! RUST_LOG_STYLE=always $test_manager run-tests \
--account "${ACCOUNT_TOKEN:?Error: ACCOUNT_TOKEN not set}" \
--app-package "${APP_PACKAGE:?Error: APP_PACKAGE not set}" \
"${upgrade_package_arg[@]}" \
"${test_report_arg[@]}" \
--package-dir "${package_dir}" \
--vm "$vm" \
--openvpn-certificate "${OPENVPN_CERTIFICATE:-"assets/openvpn.ca.crt"}" \
"${mullvad_host_arg[@]}" \
"${test_filters_arg[@]}" \
"${runner_dir_flag[@]}" \
2>&1 | sed -r "s/${ACCOUNT_TOKEN}/\{ACCOUNT_TOKEN\}/g"; then
echo "Test run failed"
exit 1
fi
if ! RUST_LOG_STYLE=always $test_manager run-tests \
--account "${ACCOUNT_TOKEN:?Error: ACCOUNT_TOKEN not set}" \
--app-package "${APP_PACKAGE:?Error: APP_PACKAGE not set}" \
"${upgrade_package_arg[@]}" \
"${test_report_arg[@]}" \
--package-dir "${package_dir}" \
--vm "$vm" \
--openvpn-certificate "${OPENVPN_CERTIFICATE:-"assets/openvpn.ca.crt"}" \
"${mullvad_host_arg[@]}" \
"${test_filters_arg[@]}" \
"${runner_dir_flag[@]}" \
2>&1 | sed -r "s/${ACCOUNT_TOKEN}/\{ACCOUNT_TOKEN\}/g"; then
echo "Test run failed"
exit 1
fi
popd
}

Expand All @@ -335,10 +335,10 @@ function build_current_version {

if [ ! -f "$app_package" ]; then
pushd "$app_dir"
if [[ $(git diff --quiet) ]]; then
echo "WARNING: the app repository contains uncommitted changes, this script will only rebuild the app package when the git hash changes"
fi
./build.sh
if [[ $(git diff --quiet) ]]; then
echo "WARNING: the app repository contains uncommitted changes, this script will only rebuild the app package when the git hash changes"
fi
./build.sh
popd
echo "Moving '$(realpath "$app_dir/dist/$app_filename")' to '$(realpath "$app_package")'"
mv -n "$app_dir"/dist/"$app_filename" "$app_package"
Expand All @@ -348,7 +348,7 @@ function build_current_version {

if [ ! -f "$gui_test_bin" ]; then
pushd "$app_dir"/gui
npm run build-test-executable
npm run build-test-executable
popd
echo "Moving '$(realpath "$app_dir/dist/$gui_test_filename")' to '$(realpath "$gui_test_bin")'"
mv -n "$app_dir"/dist/"$gui_test_filename" "$gui_test_bin"
Expand Down
6 changes: 3 additions & 3 deletions test/test-by-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ usage() {
echo
echo "Required environment variables:"
echo " - ACCOUNT_TOKEN: Valid MullvadVPN account number"
echo " - TEST_OS: Name of the VM configuration to use. List available configurations with 'cargo run --bin test-manager list'"
echo " - TEST_OS: Name of the VM configuration to use. List available configurations with 'cargo run --bin test-manager config vm list'"
echo "Optional environment variables:"
echo " - APP_VERSION: The version of the app to test (defaults to the latest stable release)"
echo " - APP_PACKAGE_TO_UPGRADE_FROM: The package version to upgrade from (defaults to none)"
Expand All @@ -18,13 +18,13 @@ usage() {
echo " - TEST_REPORT : path to save the test results in a structured format"
}

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# shellcheck source=test/scripts/test-utils.sh
source "scripts/test-utils.sh"

if [[ ( "$*" == "--help") || "$*" == "-h" ]]; then
if [[ ("$*" == "--help") || "$*" == "-h" ]]; then
usage
exit 0
fi
Expand Down
1 change: 1 addition & 0 deletions test/test-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async-trait = { workspace = true }
uuid = "1.3"
dirs = "5.0.1"
scopeguard = "1.2"
glob = "0.3"

serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
Loading

0 comments on commit bbfc9c8

Please sign in to comment.