Skip to content

Commit

Permalink
Merge pull request #51 from zscaler/zia-#51-integration-tests
Browse files Browse the repository at this point in the history
feat: Added ZIA Integration tests
  • Loading branch information
willguibr authored Apr 8, 2024
2 parents 88901ec + 0431fbf commit b679f67
Show file tree
Hide file tree
Showing 70 changed files with 3,844 additions and 5,514 deletions.
116 changes: 116 additions & 0 deletions .github/workflows/zia-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: ZIA Test
on:
pull_request:
types: [opened, synchronize]
merge_group:
types: [checks_requested]
push:
branches:
- master
schedule:
- cron: '0 14 * * 1-5' # UTC
workflow_dispatch:


jobs:
zia-beta-tenants:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10"]
environment:
- ZIA_ZSBETA
environment: ${{ matrix.environment }}
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
make lint:zia
- name: Run Pytest
uses: nick-fields/retry@v2
with:
max_attempts: 3
timeout_minutes: 20
command: |
make test:integration:zia
env:
ZIA_USERNAME: ${{ secrets.ZIA_USERNAME }}
ZIA_PASSWORD: ${{ secrets.ZIA_PASSWORD }}
ZIA_API_KEY: ${{ secrets.ZIA_API_KEY }}
ZIA_CLOUD: ${{ secrets.ZIA_CLOUD }}
ZIA_SANDBOX_TOKEN: ${{ secrets.ZIA_SANDBOX_TOKEN }}
ZPA_CLIENT_ID: ${{ secrets.ZPA_CLIENT_ID }}
ZPA_CLIENT_SECRET: ${{ secrets.ZPA_CLIENT_SECRET }}
ZPA_CUSTOMER_ID: ${{ secrets.ZPA_CUSTOMER_ID }}
ZPA_CLOUD: ${{ secrets.ZPA_CLOUD }}

zia-test-tenants:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10"]
environment:
- ZIA_ZSCLOUD
- ZIA_ZS0
- ZIA_ZS1
- ZIA_ZS2
- ZIA_ZS3
environment: ${{ matrix.environment }}
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 zscaler/ --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 zscaler/ --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Run Pytest
uses: nick-fields/retry@v2
with:
max_attempts: 3
timeout_minutes: 20
command: |
make test:integration:zia
env:
ZIA_USERNAME: ${{ secrets.ZIA_USERNAME }}
ZIA_PASSWORD: ${{ secrets.ZIA_PASSWORD }}
ZIA_API_KEY: ${{ secrets.ZIA_API_KEY }}
ZIA_CLOUD: ${{ secrets.ZIA_CLOUD }}
ZIA_SANDBOX_TOKEN: ${{ secrets.ZIA_SANDBOX_TOKEN }}
ZPA_CLIENT_ID: ${{ secrets.ZPA_CLIENT_ID }}
ZPA_CLIENT_SECRET: ${{ secrets.ZPA_CLIENT_SECRET }}
ZPA_CUSTOMER_ID: ${{ secrets.ZPA_CUSTOMER_ID }}
ZPA_CLOUD: ${{ secrets.ZPA_CLOUD }}
20 changes: 20 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

# Read the Docs configuration file for Sphinx projects

# Required
version: 2

# Set the OS and Python version
build:
os: ubuntu-22.04
tools:
python: "3.12"

# Build documentation in the "docsrc/" directory with Sphinx
sphinx:
configuration: docsrc/conf.py

# pyZscaler doc build requirements
python:
install:
- requirements: dev_requirements.txt
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,23 @@ check-format:

test\:integration\:zpa:
@echo "$(COLOR_ZSCALER)Running zpa integration tests...$(COLOR_NONE)"
pytest tests/integration/zpa
pytest tests/integration/zpa --disable-warnings

test\:integration\:zia:
@echo "$(COLOR_ZSCALER)Running zia integration tests...$(COLOR_NONE)"
pytest tests/integration/zia
pytest tests/integration/zia --disable-warnings

test-simple:
pytest --disable-warnings

coverage:
pytest --cov=zscaler
pytest --cov=zscaler --cov-report term

coverage\:zia:
pytest tests/integration/zia --cov=zscaler/zia --cov-report term

coverage\:zpa:
pytest tests/integration/zpa --cov=zscaler/zpa --cov-report term

docs: clean-docs
$(MAKE) -C docs html
Expand Down
86 changes: 44 additions & 42 deletions tests/integration/zia/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,51 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import os
from zscaler.zia import ZIAClientHelper
from functools import wraps

import pytest
import responses
PYTEST_MOCK_CLIENT = "pytest_mock_client"

from zscaler.zia import ZIAClientHelper

class MockZIAClient(ZIAClientHelper):
def __init__(self, fs):
# Fetch credentials from environment variables
username = os.environ.get("ZIA_USERNAME")
password = os.environ.get("ZIA_PASSWORD")
api_key = os.environ.get("ZIA_API_KEY")
cloud = os.environ.get("ZIA_CLOUD")
sandbox_token = os.environ.get("ZIA_SANDBOX_TOKEN")

if PYTEST_MOCK_CLIENT in os.environ:
fs.pause()
super().__init__()
fs.resume()
else:
super().__init__(
username=username,
password=password,
api_key=api_key,
cloud=cloud,
sandbox_token=sandbox_token,
cache=None,
fail_safe=False,
)


def stub_sleep(func):
"""Decorator to speed up time.sleep function used in any methods under test."""
import time
from time import sleep

def newsleep(seconds):
sleep_speed_factor = 10.0
sleep(seconds / sleep_speed_factor)

time.sleep = newsleep

@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)

@pytest.fixture(name="session")
def fixture_session():
return {
"authType": "ADMIN_LOGIN",
"obfuscateApiKey": False,
"passwordExpiryTime": 0,
"passwordExpiryDays": 0,
}


@pytest.fixture(name="zia")
@responses.activate
def zia(session):
responses.add(
responses.POST,
url="https://zsapi.zscaler.net/api/v1/authenticatedSession",
content_type="application/json",
json=session,
status=200,
)
# THIS IS A FAKE (EXAMPLE) USERNAME AND PASSWORD AND NOT USED IN PRODUCTION
return ZIAClientHelper(
username="test@example.com",
password="hunter2",
cloud="zscaler",
api_key="123456789abcdef",
sandbox_token="SANDBOXTOKEN",
)


@pytest.fixture(name="paginated_items")
def fixture_pagination_items():
def _method(num):
items = []
for x in range(0, num):
items.append({"id": x})
return items

return _method
return wrapper
52 changes: 52 additions & 0 deletions tests/integration/zia/test_activation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2023, Zscaler Inc.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


import pytest
from tests.integration.zia.conftest import MockZIAClient

@pytest.fixture
def fs():
yield

class TestActivation:
"""
Integration Tests for the ZIA Activation
"""

@pytest.mark.asyncio
async def test_activation(self, fs):
client = MockZIAClient(fs)
errors = [] # Initialize an empty list to collect errors

# Test Config Status
try:
config_status = client.activate.status()
# Allow for both "ACTIVE" and "PENDING" statuses
assert config_status in ["ACTIVE", "PENDING"], f"Unexpected configuration status: {config_status}"
except Exception as exc:
errors.append(f"Config status check failed: {exc}")

# Test Config Activation
try:
config_activation = client.activate.activate()
# Assuming the activation process might also return "PENDING" immediately after activation request
assert config_activation in ["ACTIVE", "PENDING"], f"Unexpected configuration activation status: {config_activation}"
except Exception as exc:
errors.append(f"Config activation failed: {exc}")

# Assert that no errors occurred during the test
assert len(errors) == 0, f"Errors occurred during activation operations test: {errors}"
Loading

0 comments on commit b679f67

Please sign in to comment.