From db974361f74e8e782f81c60a473f6adf051cbd56 Mon Sep 17 00:00:00 2001 From: David Murphy Date: Mon, 4 Nov 2024 15:40:40 -0700 Subject: [PATCH] Initial Shell pytest --- .github/workflows/test-linux.yml | 40 ++++++---- .github/workflows/test-macos.yml | 47 +++++++----- .github/workflows/test-windows.yml | 100 +++++++++++++++---------- .gitignore | 1 + .pre-commit-config.yaml | 2 +- tests/conftest.py | 37 +-------- tests/integration/test_installation.py | 59 ++++++++++----- tests/requirements.txt | 12 +-- 8 files changed, 166 insertions(+), 132 deletions(-) diff --git a/.github/workflows/test-linux.yml b/.github/workflows/test-linux.yml index b224e0eb3..5f49a6661 100644 --- a/.github/workflows/test-linux.yml +++ b/.github/workflows/test-linux.yml @@ -37,22 +37,22 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 2.6.10 - ## ruby-version: 3.3.4 - bundler-cache: true + ## DGM - name: Setup Ruby + ## DGM uses: ruby/setup-ruby@v1 + ## DGM with: + ## DGM ruby-version: 2.6.10 + ## DGM ## ruby-version: 3.3.4 + ## DGM bundler-cache: true - - name: Set up Python 3.10 - uses: actions/setup-python@v5 - with: - python-version: "3.10" + ## DGM - name: Set up Python 3.10 + ## DGM uses: actions/setup-python@v5 + ## DGM with: + ## DGM python-version: "3.10" - - name: Install Python Dependencies - run: | - python3 -m pip install -U pip - python3 -m pip install -r tests/requirements.txt + ## DGM - name: Install Python Dependencies + ## DGM run: | + ## DGM python3 -m pip install -U pip + ## DGM python3 -m pip install -r tests/requirements.txt ## DGM - name: Create Test Instance ## DGM run: | @@ -71,6 +71,18 @@ jobs: ## DGM bundle exec kitchen destroy ${{ matrix.instance }}-${{ inputs.distro-slug }} ## DGM sleep 2 + - name: Install Pytest + run: | + pip install -U pytest + + - name: Bootstrap Salt + run: | + . ./bootstrap.sh + + - name: Test Bootstrap + run: | + pytest --cache-clear -v -s -ra --log-cli-level=debug tests/integration/ + - name: Set Exit Status if: always() run: | diff --git a/.github/workflows/test-macos.yml b/.github/workflows/test-macos.yml index 113e2063c..190ec2463 100644 --- a/.github/workflows/test-macos.yml +++ b/.github/workflows/test-macos.yml @@ -28,13 +28,14 @@ on: default: 20 description: The timeout(in minutes) for the workflow -env: - KITCHEN_LOCAL_YAML: 'kitchen.macos.yml' +## DGM env: +## DGM KITCHEN_LOCAL_YAML: 'kitchen.macos.yml' jobs: Test: name: ${{ matrix.instance }} - runs-on: ${{ inputs.runs-on }} + ## DGM runs-on: ${{ inputs.runs-on }} + runs-on: macos-12 timeout-minutes: ${{ inputs.timeout }} strategy: fail-fast: false @@ -44,22 +45,22 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 2.6.10 - ## ruby-version: 3.3.4 - bundler-cache: true + ## DGM - name: Setup Ruby + ## DGM uses: ruby/setup-ruby@v1 + ## DGM with: + ## DGM ruby-version: 2.6.10 + ## DGM ## ruby-version: 3.3.4 + ## DGM bundler-cache: true - - name: Set up Python 3.10 - uses: actions/setup-python@v5 - with: - python-version: "3.10" + ## DGM - name: Set up Python 3.10 + ## DGM uses: actions/setup-python@v5 + ## DGM with: + ## DGM python-version: "3.10" - - name: Install Python Dependencies - run: | - pip install -U pip - pip install -r tests/requirements.txt + ## DGM - name: Install Python Dependencies + ## DGM run: | + ## DGM pip install -U pip + ## DGM pip install -r tests/requirements.txt ## DGM - name: Create Test Instance ## DGM run: | @@ -78,6 +79,18 @@ jobs: ## DGM bundle exec kitchen destroy ${{ matrix.instance }}-${{ inputs.distro-slug }} ## DGM sleep 2 + - name: Install Pytest + run: | + pip install -U pytest + + - name: Bootstrap Salt + run: | + . ./bootstrap.sh + + - name: Test Bootstrap + run: | + pytest --cache-clear -v -s -ra --log-cli-level=debug tests/integration/ + - name: Set Exit Status if: always() run: | diff --git a/.github/workflows/test-windows.yml b/.github/workflows/test-windows.yml index 8a6bfe4ec..8613744eb 100644 --- a/.github/workflows/test-windows.yml +++ b/.github/workflows/test-windows.yml @@ -28,16 +28,17 @@ on: default: 20 description: The timeout(in minutes) for the workflow -env: - machine_user: kitchen - machine_pass: Password1 - machine_port: 5985 - KITCHEN_LOCAL_YAML: 'kitchen.windows.yml' +## DGM env: +## DGM machine_user: kitchen +## DGM machine_pass: Password1 +## DGM machine_port: 5985 +## DGM KITCHEN_LOCAL_YAML: 'kitchen.windows.yml' jobs: Test: name: ${{ matrix.instance }} - runs-on: ${{ inputs.runs-on }} + ## DGM runs-on: ${{ inputs.runs-on }} + runs-on: windows-latest timeout-minutes: ${{ inputs.timeout }} strategy: fail-fast: false @@ -47,48 +48,50 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 2.6.10 - ## ruby-version: 3.3.4 - bundler-cache: true - - - name: Install Chef - uses: actionshub/chef-install@1.1.0 - with: - project: chef - version: 16.10.8 - - - name: Add Chef bindir to PATH - uses: myci-actions/export-env-var-powershell@1 - with: - name: PATH - value: "C:\\opscode\\chef\\bin;C:\\opscode\\chef\\embedded\\bin;$env:PATH" - - - name: Setup test user - run: | - $password = ConvertTo-SecureString $env:machine_pass -AsPlainText -Force - New-LocalUser $env:machine_user -Password $password - Add-LocalGroupMember -Group "Administrators" -Member $env:machine_user - Get-LocalUser - Get-LocalGroupMember -Group "Administrators" + ## DGM - name: Setup Ruby + ## DGM uses: ruby/setup-ruby@v1 + ## DGM with: + ## DGM ruby-version: 2.6.10 + ## DGM ## ruby-version: 3.3.4 + ## DGM bundler-cache: true + + ## DGM - name: Install Chef + ## DGM uses: actionshub/chef-install@1.1.0 + ## DGM with: + ## DGM project: chef + ## DGM version: 16.10.8 + + ## DGM - name: Add Chef bindir to PATH + ## DGM uses: myci-actions/export-env-var-powershell@1 + ## DGM with: + ## DGM name: PATH + ## DGM value: "C:\\opscode\\chef\\bin;C:\\opscode\\chef\\embedded\\bin;$env:PATH" + + ## DGM - name: Setup test user + ## DGM run: | + ## DGM $password = ConvertTo-SecureString $env:machine_pass -AsPlainText -Force + ## DGM New-LocalUser $env:machine_user -Password $password + ## DGM Add-LocalGroupMember -Group "Administrators" -Member $env:machine_user + ## DGM Get-LocalUser + ## DGM Get-LocalGroupMember -Group "Administrators" - - name: Set up WinRM - run: | - Set-WSManQuickConfig -Force - winrm set winrm/config/service '@{AllowUnencrypted="True"}' - env + ## DGM - name: Set up WinRM + ## DGM run: | + ## DGM Set-WSManQuickConfig -Force + ## DGM winrm set winrm/config/service '@{AllowUnencrypted="True"}' + ## DGM env - name: Set up Python 3.10 uses: actions/setup-python@v5 with: python-version: "3.10" - - name: Install Python Dependencies + ## DGM - name: Install Python Dependencies + - name: Install Pytest run: | - pip install -U pip - pip install -r tests/requirements.txt + ## DGM pip install -U pip + ## DGM pip install -r tests/requirements.txt + pip install -U pytest ## DGM - name: Create Test Instance ## DGM run: | @@ -107,6 +110,23 @@ jobs: ## DGM bundle exec kitchen destroy ${{ matrix.instance }}-${{ inputs.distro-slug }} ## DGM sleep 2 + - name: Bootstrap Salt + run: | + . ./bootstrap-salt.ps1 -RunService $false + + - name: Set Environment Variables + run: | + $env:Path = "$env:Path;C:\Program Files\Salt Project\Salt" + $env:Instance = ${{ matrix.instance }} + Write-Output "Path:" + Write-Output $env:Path + Write-Output "Environment Variables" + Get-ChildItem Env: + + - name: Test Bootstrap + run: | + pytest --cache-clear -v -s -ra --log-cli-level=debug tests/integration/ + - name: Set Exit Status if: always() run: | diff --git a/.gitignore b/.gitignore index 9b7ebc0b0..f1af41c03 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.sw? *.un~ .vagrant +*__pycache__ tools venv diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9fb6a2607..033ac67b4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -58,7 +58,7 @@ repos: args: [--py310-plus] - repo: https://github.com/psf/black - rev: 24.1.0 + rev: 24.10.0 hooks: - id: black args: [] diff --git a/tests/conftest.py b/tests/conftest.py index 864307c03..3779d377d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,45 +1,11 @@ import logging import os -import pprint import pytest -import testinfra log = logging.getLogger(__name__) -@pytest.fixture(scope="session") -def host(): - if ( - os.environ.get("RUNNER_OS", "") == "macOS" - and os.environ.get("KITCHEN_LOCAL_YAML", "") == "kitchen.macos.yml" - ): - # Adjust the `PATH` so that the `salt-call` executable can be found - os.environ["PATH"] = "/opt/salt/bin{}{}".format(os.pathsep, os.environ["PATH"]) - return testinfra.get_host("local://", sudo=True) - - if os.environ.get("KITCHEN_USERNAME") == "vagrant" or "windows" in os.environ.get( - "KITCHEN_INSTANCE" - ): - if "windows" in os.environ.get("KITCHEN_INSTANCE"): - _url = "winrm://{KITCHEN_USERNAME}:{KITCHEN_PASSWORD}@{KITCHEN_HOSTNAME}:{KITCHEN_PORT}".format( - **os.environ - ) - return testinfra.get_host( - _url, - no_ssl=True, - ) - return testinfra.get_host( - "paramiko://{KITCHEN_USERNAME}@{KITCHEN_HOSTNAME}:{KITCHEN_PORT}".format( - **os.environ - ), - ssh_identity_file=os.environ.get("KITCHEN_SSH_KEY"), - ) - return testinfra.get_host( - "docker://{KITCHEN_USERNAME}@{KITCHEN_CONTAINER_ID}".format(**os.environ) - ) - - @pytest.fixture(scope="session") def target_python_version(): return 3 @@ -53,7 +19,8 @@ def target_salt_version(): target_salt = ".".join( [ item - for item in os.environ["KITCHEN_SUITE"].split("-") + ## DGM for item in os.environ["KITCHEN_SUITE"].split("-") + for item in os.environ.get("KITCHEN_SUITE", "").split("-") if item not in bootstrap_types ] ) diff --git a/tests/integration/test_installation.py b/tests/integration/test_installation.py index 9ee18d49c..6623fc613 100644 --- a/tests/integration/test_installation.py +++ b/tests/integration/test_installation.py @@ -1,31 +1,50 @@ +import json import logging -import os -from contextlib import nullcontext +import platform +import subprocess + +import pytest +from Demoa.winc32cred_demo import target log = logging.getLogger(__name__) -def selected_context_manager(host): - if "windows" in os.environ.get("KITCHEN_INSTANCE"): - return nullcontext() - return host.sudo() +@pytest.fixture +def path(): + if platform.system() == "Windows": + salt_path = "C:\\Program Files\\Salt Project\\Salt" + if salt_path not in os.environ["path"]: + os.environ["path"] = f'{os.environ["path"]};{salt_path}' + yield os.environ["path"] + + +def run_salt_call(cmd): + """ + Runs salt call command and returns a dictionary + Accepts cmd as a list + """ + cmd.append("--out=json") + result = subprocess.run(cmd, capture_output=True, text=True) + json_data = json.loads(result.stdout) + return json_data["local"] -def test_ping(host): - with selected_context_manager(host): - assert host.salt("test.ping", "--timeout=120") +def test_ping(): + cmd = ["salt-call", "--local", "test.ping"] + result = run_salt_call(cmd) + assert result == True -def test_target_python_version(host, target_python_version): - with selected_context_manager(host): - ret = host.salt("grains.item", "pythonversion", "--timeout=120") - assert ret["pythonversion"][0] == target_python_version +def test_target_python_version(target_python_version): + cmd = ["salt-call", "--local", "grains.item", "pythonversion", "--timeout=120"] + result = run_salt_call(cmd) + # Returns: {'pythonversion': [3, 10, 11, 'final', 0]} + py_maj_ver = result["pythonversion"][0] + assert py_maj_ver == target_python_version -def test_target_salt_version(host, target_salt_version): - with selected_context_manager(host): - ret = host.salt("grains.item", "saltversion", "--timeout=120") - if target_salt_version.endswith(".0") or target_salt_version.endswith(".x"): - assert ret["saltversion"] == ".".join(target_salt_version.split(".")[:-1]) - else: - assert ret["saltversion"].startswith(target_salt_version) +def test_target_salt_version(target_salt_version): + cmd = ["salt-call", "--local", "grains.item", "saltversion", "--timeout=120"] + result = run_salt_call(cmd) + # Returns: {'saltversion': '3006.9+217.g53cfa53040'} + assert result["saltversion"] == target_salt_version diff --git a/tests/requirements.txt b/tests/requirements.txt index 21d133da8..64223821a 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,5 +1,7 @@ -pytest-testinfra -paramiko -requests-ntlm==1.1.0; sys.platform == 'win32' -pywinrm; sys.platform == 'win32' -six>=1.10.0 +## DGM pytest-testinfra +## DGM paramiko +## DGM requests-ntlm==1.1.0; sys.platform == 'win32' +## DGM pywinrm; sys.platform == 'win32' +## DGM six>=1.10.0 + +pytest