Skip to content

Commit

Permalink
Add tests for NSIS installer
Browse files Browse the repository at this point in the history
  • Loading branch information
twangboy committed May 23, 2024
1 parent 41ef078 commit bb94d15
Show file tree
Hide file tree
Showing 74 changed files with 3,385 additions and 0 deletions.
38 changes: 38 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,3 +1981,41 @@ def ci_test_onedir_pkgs(session):
on_rerun=True,
)
sys.exit(0)


@nox.session(python="3", name="ci-test-installer")
def ci_test_installer(session):
"""
Test a dummy installer to make sure the installer logic is working correctly
"""
installers = ["nsis", "msi"]
if not session.posargs or session.posargs[0] not in installers:
installer = "nsis"
session.log("Choosing default 'nsis' installer type")
else:
installer = session.posargs.pop(0)

# Build dummy installer
tests_root = REPO_ROOT / "tests" / "pytests" / "pkg" / "installer" / installer
setup_file = tests_root / "setup.ps1"
session.run("PowerShell", "-ExecutionPolicy", "RemoteSigned", "-File", setup_file)

session.install("--progress-bar=off", "psutil", silent=PIP_INSTALL_SILENT)

requirements_file = os.path.join("requirements", "pytest.txt")
install_command = ["--progress-bar=off", "-r", requirements_file]
session.install(*install_command, silent=PIP_INSTALL_SILENT)

# Install the Salt requirements, though they're not needed for these tests
requirements_file = _get_pip_requirements_file(session, requirements_type="ci")
install_command = ["--progress-bar=off", "-r", requirements_file]
session.install(*install_command, silent=PIP_INSTALL_SILENT)

# Run installer tests
cmd_args = ["-vvv", "--rootdir", str(tests_root), "--"]
if not session.posargs:
cmd_args.append(str(tests_root / "config_tests"))
else:
cmd_args.extend(session.posargs)
# os.chdir(tests_root)
session.run("python", "-m", "pytest", *cmd_args)
8 changes: 8 additions & 0 deletions tests/pytests/pkg/installer/nsis/_files/minion
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Default config from test suite line 1/6
#master: salt
# Default config from test suite line 2/6
#id:
# Default config from test suite line 3/6
# Default config from test suite line 4/6
# Default config from test suite line 5/6
# Default config from test suite line 6/6
3 changes: 3 additions & 0 deletions tests/pytests/pkg/installer/nsis/clean.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ echo off
Set "CurDir=%~dp0"
PowerShell -ExecutionPolicy RemoteSigned -File "%CurDir%\clean.ps1" %*
83 changes: 83 additions & 0 deletions tests/pytests/pkg/installer/nsis/clean.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<#
.SYNOPSIS
Clean the NSIS Tests directory
.DESCRIPTION
This script removes the venv directory and the test-setup.exe
.EXAMPLE
clean.ps1
.EXAMPLE
clean.ps1
#>

# Script Preferences
$ProgressPreference = "SilentlyContinue"
$ErrorActionPreference = "Stop"

#-------------------------------------------------------------------------------
# Script Variables
#-------------------------------------------------------------------------------

$SCRIPT_DIR = (Get-ChildItem "$($myInvocation.MyCommand.Definition)").DirectoryName

#-------------------------------------------------------------------------------
# Script Functions
#-------------------------------------------------------------------------------

function Write-Result($result, $ForegroundColor="Green") {
$position = 80 - $result.Length - [System.Console]::CursorLeft
Write-Host -ForegroundColor $ForegroundColor ("{0,$position}$result" -f "")
}

#-------------------------------------------------------------------------------
# Start the Script
#-------------------------------------------------------------------------------
Write-Host $("=" * 80)
Write-Host "Clean NSIS Test Environment" -ForegroundColor Cyan
Write-Host $("-" * 80)

#-------------------------------------------------------------------------------
# Make sure we're not in a virtual environment
#-------------------------------------------------------------------------------
if ( $env:VIRTUAL_ENV ) {
# I've tried deactivating from the script, but it doesn't work
Write-Host "Please deactive the virtual environment"
exit
}

#-------------------------------------------------------------------------------
# Remove venv directory
#-------------------------------------------------------------------------------
if ( Test-Path -Path "$SCRIPT_DIR\venv" ) {
Write-Host "Removing venv directory: " -NoNewline
Remove-Item -Path "$SCRIPT_DIR\venv" -Recurse -Force
if ( Test-Path -Path "$SCRIPT_DIR\venv" ) {
Write-Result "Failed" -ForegroundColor Red
exit 1
} else {
Write-Result "Success" -ForegroundColor Green
}
}
#-------------------------------------------------------------------------------
# Remove test-setup.exe
#-------------------------------------------------------------------------------
if ( Test-Path -Path "$SCRIPT_DIR\test-setup.exe" ) {
Write-Host "Removing test-setup.exe: " -NoNewline
Remove-Item -Path "$SCRIPT_DIR\test-setup.exe" -Recurse -Force
if ( Test-Path -Path "$SCRIPT_DIR\test-setup.exe" ) {
Write-Result "Failed" -ForegroundColor Red
exit 1
} else {
Write-Result "Success" -ForegroundColor Green
}
}

#-------------------------------------------------------------------------------
# Script Completed
#-------------------------------------------------------------------------------
Write-Host $("-" * 80)
Write-Host "Clean NSIS Test Environment Completed" -ForegroundColor Cyan
Write-Host $("=" * 80)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os

import pytest


@pytest.fixture(scope="module")
def install():
pytest.helpers.clean_env()

# Create a custom config
pytest.helpers.custom_config()

full_path_conf = rf"{pytest.REPO_DIR}\custom_conf"

pytest.helpers.run_command(
[pytest.INST_BIN, "/S", f"/custom-config={full_path_conf}"]
)
yield
pytest.helpers.clean_env()


def test_binaries_present(install):
assert os.path.exists(rf"{pytest.INST_DIR}\ssm.exe")


def test_config_present(install):
assert os.path.exists(rf"{pytest.DATA_DIR}\conf\minion")


def test_config_correct(install):
# The config file should be the custom config, unchanged
with open(rf"{pytest.REPO_DIR}\custom_conf") as f:
expected = f.readlines()

with open(rf"{pytest.DATA_DIR}\conf\minion") as f:
result = f.readlines()

assert result == expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os

import pytest


@pytest.fixture(scope="module")
def install():
pytest.helpers.clean_env()

# Create a custom config
pytest.helpers.custom_config()

pytest.helpers.run_command(
[pytest.INST_BIN, "/S", "/custom-config=custom_conf", "/master=cli_master"]
)
yield
pytest.helpers.clean_env()


def test_binaries_present(install):
assert os.path.exists(rf"{pytest.INST_DIR}\ssm.exe")


def test_config_present(install):
assert os.path.exists(rf"{pytest.DATA_DIR}\conf\minion")


def test_config_correct(install):
# The config file should be the custom config with only master set
expected = [
"# Custom config from test suite line 1/6\n",
"master: cli_master\n",
"# Custom config from test suite line 2/6\n",
"id: custom_minion\n",
"# Custom config from test suite line 3/6\n",
"# Custom config from test suite line 4/6\n",
"# Custom config from test suite line 5/6\n",
"# Custom config from test suite line 6/6\n",
]

with open(rf"{pytest.DATA_DIR}\conf\minion") as f:
result = f.readlines()

assert result == expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os

import pytest


@pytest.fixture(scope="module")
def install():
pytest.helpers.clean_env()

# Create a custom config
pytest.helpers.custom_config()

pytest.helpers.run_command(
[
pytest.INST_BIN,
"/S",
"/custom-config=custom_conf",
"/master=cli_master",
"/minion-name=cli_minion",
]
)
yield
pytest.helpers.clean_env()


def test_binaries_present(install):
assert os.path.exists(rf"{pytest.INST_DIR}\ssm.exe")


def test_config_present(install):
assert os.path.exists(rf"{pytest.DATA_DIR}\conf\minion")


def test_config_correct(install):
# The config file should be the custom config with master and minion set
expected = [
"# Custom config from test suite line 1/6\n",
"master: cli_master\n",
"# Custom config from test suite line 2/6\n",
"id: cli_minion\n",
"# Custom config from test suite line 3/6\n",
"# Custom config from test suite line 4/6\n",
"# Custom config from test suite line 5/6\n",
"# Custom config from test suite line 6/6\n",
]

with open(f"{pytest.DATA_DIR}\\conf\\minion") as f:
result = f.readlines()

assert result == expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os

import pytest


@pytest.fixture(scope="module")
def install():
pytest.helpers.clean_env()

# Create a custom config
pytest.helpers.custom_config()

pytest.helpers.run_command(
[
pytest.INST_BIN,
"/S",
"/custom-config=custom_conf",
"/minion-name=cli_minion",
]
)
yield
pytest.helpers.clean_env()


def test_binaries_present(install):
assert os.path.exists(rf"{pytest.INST_DIR}\ssm.exe")


def test_config_present(install):
assert os.path.exists(rf"{pytest.DATA_DIR}\conf\minion")


def test_config_correct(install):
# The config file should be the custom config with only minion set
expected = [
"# Custom config from test suite line 1/6\n",
"master: custom_master\n",
"# Custom config from test suite line 2/6\n",
"id: cli_minion\n",
"# Custom config from test suite line 3/6\n",
"# Custom config from test suite line 4/6\n",
"# Custom config from test suite line 5/6\n",
"# Custom config from test suite line 6/6\n",
]

with open(rf"{pytest.DATA_DIR}\conf\minion") as f:
result = f.readlines()

assert result == expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os

import pytest


@pytest.fixture(scope="module")
def install():
pytest.helpers.clean_env()

# Create a custom config
pytest.helpers.custom_config()

pytest.helpers.run_command([pytest.INST_BIN, "/S", "/custom-config=custom_conf"])
yield
pytest.helpers.clean_env()


def test_binaries_present(install):
assert os.path.exists(rf"{pytest.INST_DIR}\ssm.exe")


def test_config_present(install):
assert os.path.exists(rf"{pytest.DATA_DIR}\conf\minion")


def test_config_correct(install):
# The config file should be the custom config, unchanged
with open(rf"{pytest.REPO_DIR}\custom_conf") as f:
expected = f.readlines()

with open(rf"{pytest.DATA_DIR}\conf\minion") as f:
result = f.readlines()

assert result == expected
30 changes: 30 additions & 0 deletions tests/pytests/pkg/installer/nsis/config_tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os

import pytest


@pytest.fixture(scope="module")
def install():
pytest.helpers.clean_env()
pytest.helpers.run_command([pytest.INST_BIN, "/S"])
yield
pytest.helpers.clean_env()


def test_binaries_present(install):
assert os.path.exists(rf"{pytest.INST_DIR}\ssm.exe")


def test_config_present(install):
assert os.path.exists(rf"{pytest.DATA_DIR}\conf\minion")


def test_config_correct(install):
# The config file should be the default config, unchanged
with open(rf"{pytest.REPO_DIR}\_files\minion") as f:
expected = f.readlines()

with open(rf"{pytest.DATA_DIR}\conf\minion") as f:
result = f.readlines()

assert result == expected
Loading

0 comments on commit bb94d15

Please sign in to comment.