Skip to content

Commit

Permalink
Add new functionality to read config from pytest
Browse files Browse the repository at this point in the history
And moved deployment test case to pytest
  • Loading branch information
petr-balogh committed May 21, 2019
1 parent e26af38 commit f855518
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 105 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# OCS-CI
# OpenShift Install

OCS CI is a framework to test Red Hat OCS features and functionality using AWS
and other supported platforms. The framework is based on CephCI
( https://github.com/red-hat-storage/cephci )
The OpenShift installer `openshift-install` makes it easy to get a cluster
running on the public cloud or your local infrastructure.

Documentation is structured and available under docs directory
inside this project. You can click [HERE](docs/README.md) to see
the Table Of Content(TOC) of our documentation.
To learn more about installing OpenShift, visit [docs.openshift.com](https://docs.openshift.com)
and select the version of OpenShift you are using.

## Installing the tools

After extracting this archive, you can move the `openshift-install` binary
to a location on your PATH such as `/usr/local/bin`, or keep it in a temporary
directory and reference it via `./openshift-install`.

## License

OpenShift is licensed under the Apache Public License 2.0. The source code for this
program is [located on github](https://github.com/openshift/installer).
7 changes: 3 additions & 4 deletions conf/ocs_basic_install.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
---
# This is example of the config for deployment which allows you to overwrite
# the default values from templates.
env_data:
ENV_DATA:
# take a look at deployment templates variables, here you is the place you
# can overwrite all the vars you want.
platform: 'aws'
workers: 3 # here you can define diff number of workers
workers: 6 # here you can define diff number of workers
masters: 3
ceph_image: "quay.io/rhceph-dev/rhceph-4.0-rhel-8"
rook_image: "quay.io/rhceph-dev/rook"
rook_image: "quay.io/rhceph-dev/rook" # you can overwrite default rook image
55 changes: 55 additions & 0 deletions conf/ocsci/default_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
# This is the default configuration file which will be merged with file passed
# by:
#
# * --ocs-conf file.yaml parameter.
# * --cluster-conf cluster.yaml parameter - this will be rendered under
# ENV_DATA section
#
# Then it will be rendered with jinja2 to be able to reuse vars defined in
# different sections. The sections which are with capital letters will be
# available directly in ocsci.config module.
#
# Please when you reuse some variable follow this convention:
# ceph_image: '{{ DEFAULTS["CEPH_IMAGE"] }}'
# Order of the quotas is important! Start with apostrophe like in example!
# ------------------------------------------------------------------------
# This is the place for common default variables used in multiple places.
# Values from this section are then available from tests from ocs.defaults
# module.
DEFAULTS:
AWS_REGION: "us-east-2"
ROOK_CLUSTER_NAMESPACE: "openshift-storage"
KUBECONFIG_LOCATION: "auth/kubeconfig" # relative from cluster_dir
API_VERSION: "v1"
CEPH_IMAGE: "quay.io/rhceph-dev/rhceph-4.0-rhel-8"
ROOK_IMAGE: "quay.io/rhceph-dev/rook"
DEPLOYMENT_PLATFORM: "AWS"



# in this RUN section we will keep default parameters for run of OCS-CI
RUN:
log_dir: "/tmp"
run_id: null # this will be redefined in the execution
kubeconfig_location: '{{ DEFAULTS["KUBECONFIG_LOCATION"] }}'
cli_params: {} # this will be filled with CLI parameters data

# In this section we are storing all deployment related configuration
DEPLOYMENT:
installer_version: "4.1.0-rc.3"

# Section for reporting configuration
REPORTING:
email:
address: "ocs-ci@redhat.com"

# This is the default information about environment. Will be overwritten with
# --cluster-conf file.yaml data you will pass to the pytest.
ENV_DATA:
platform: '{{ DEFAULTS["DEPLOYMENT_PLATFORM"] }}'
cluster_name: "ocs-ci-cluster" # needs to be changed in ocscilib plugin
cluster_namespace: '{{ DEFAULTS["ROOK_CLUSTER_NAMESPACE"] }}'
region: '{{ DEFAULTS["AWS_REGION"] }}'
ceph_image: '{{ DEFAULTS["CEPH_IMAGE"] }}'
rook_image: '{{ DEFAULTS["ROOK_IMAGE"] }}'
16 changes: 9 additions & 7 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ It is recommended that you use a python virtual environment to install the neces

### OCS-CI config

Configure your ocs-ci.yaml file:
Configure your ocs-ci.yaml and pass it with --ocs-conf parameter

This file is used to allow configuration around a number of things within ocs-ci.
The template can be found at the top level of the repository, `ocs-ci.yaml.template`.
The default file is in conf/ocsci/default_config.yaml.
TODO: Once moved to pytest update properly this file: `ocs-ci.yaml.template`.

The required keys are in the template. Values are placeholders and should be replaced by legitimate values.
Values for report portal or polarion are only required if you plan on posting to that particular service.

Move a copy of the template to your user directory and edit it from there with the proper values.
Move a copy of the template to your conf directory and edit it from there with
the proper values and pass it with --ocs-conf parameter to pytest.

```bash
cp ocs-ci.yaml.template ~/.ocs-ci.yaml
```

### Pull Secret

Expand All @@ -62,8 +62,10 @@ CLI option
Cluster configuration that defines Openshift/Kubernetes Cluster along with Ceph Configuration
will reside in conf/ folder, This is still a work in progress.

### Test Suites
### Test Suites - deprecated once moved to pytest

Once moved to pytest we have to determine if we still need those suites or how
to utilize them.
All test suite configurations are found inside the `suites` directory.

```yaml
Expand Down
37 changes: 34 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,42 @@ There are a few arguments that are required ocs test execution:

Some non-required arguments that we end up using a lot:

* `--log-level <level>` - set the log level that is output to stdout.
* **--capture=no** - when using pdb or ipdb you have to turn of capture mode
for pytest.

### Parameters already converted to pytest:

* `--cluster-name <name>` - name of cluster.
* `--cluster-path <path>` - path where to create the directory which will
contains all the installation/authentication information about cluster.
`Use this parameter when running on already deployed cluster!` You can
pass the cluster path from previous execution if was created automatically.
* `--conf` - with this configuration you can overwrite the default parameters
for cluster and deployment. See the example of such file
* `--ocsci-conf` - with this configuration you can overwrite the default
OCS-CI parameters defined in `conf/ocsci/default_config.yaml`
* `--cluster-conf` - with this configuration you can overwrite the default
parameters for cluster and deployment. See the example of such file
[here](../conf/ocs_basic_install.yml).

### Parameters for old runner:

>TODO: Delete this section once moved to pytest and move missing parameters above!
* `--cluster-name <name>` - name of cluster.
* `--cluster-path <path>` - path where to create the directory which will
contains all the installation/authentication information about cluster.
`Use this parameter when running on already deployed cluster!` You can
pass the cluster path from previous execution if was created automatically.
* `--conf` - with this configuration you can overwrite the default
parameters for cluster and deployment. See the example of such file
[here](../conf/ocs_basic_install.yml).
* `--no-destroy` - this will prevent you to destroy your cluster at the end of
the execution. (Recommended if you want to re-run on the same cluster)
* `--log-level <level>` - set the log level that is output to stdout.

## Examples

### For old runner

* Run OCS install suite:

```bash
Expand All @@ -55,3 +77,12 @@ python run.py --cluster-name=my-testing-cluster \
--suite=suites/custom-test.yml --cluster-path=/home/your_login/my-testing-cluster \
--no-email
```

### For pytest

```bash
py.test -m deployment --ocsci-conf conf/ocsci/custom_config.yaml \
--cluster-conf conf/ocs_basic_install.yml \
--cluster-name kerberos_ID-ocs-deployment \
--cluster-path /home/my_user/my-ocs-dir tests/
```
13 changes: 11 additions & 2 deletions ocsci/config.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# TODO: Create yaml config file for defaults
ocs_conf_file = "decide_path/default_config.yml"
# Following config variables will be loaded by ocscilib pytest plugin.
# See the conf/ocsci/default_config.yaml for default values.
# The data are combined from multiple sources and then exposed to the following
# variables.
#
# You can see the logic in pytest_customization/plugins/ocscilib.py.

RUN = {} # all runtime related data
DEPLOYMENT = {} # deployment related data
REPORTING = {} # reporting related data
ENV_DATA = {} # environment related data
3 changes: 2 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[pytest]
markers =
run_this: testing marker for run this test
run_this: testing marker for run this test, useful for development
deployment: marker for deployment
tier0: marker for tier0 tests
tier1: marker for tier1 tests
tier2: marker for tier2 tests
Expand Down
126 changes: 113 additions & 13 deletions pytest_customization/plugins/ocscilib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
This plugin allows you to setup all basic configuratio for pytest we need
in our OCS-CI.
"""
import pytest
from getpass import getuser
import os

from ocsci import config as ocsci_conf
import random
import yaml

from oc.openshift_ops import OCP
from ocs import defaults
from ocsci import config as ocsci_config
from utility.utils import update_dict_recursively
from utility.templating import render_yaml_with_j2_context

__all__ = [
"pytest_addoption",
Expand All @@ -13,25 +21,117 @@

def pytest_addoption(parser):
"""
Add necessary options to initialize ART library.
Add necessary options to initialize OCS CI library.
"""
parser.addoption(
'--ocs-conf',
dest='ocs_conf',
help="Path to config file of OCS_CI",
help="Path to config file of OCS CI",
)
parser.addoption(
'--cluster-conf',
dest='cluster_conf',
help="Path to cluster configuration yaml file",
)
parser.addoption(
'--cluster-path',
dest='cluster_path',
help="Path to cluster directory",
)
parser.addoption(
'--cluster-name',
dest='cluster_name',
help="Name of cluster",
)


# To make sure that we call all hooks after all plugins are loaded we run this
# as the last one.
@pytest.mark.trylast
def pytest_configure(config):
"""
Load config files, and initialize ocs-ci library.
Args:
config (pytest.config): Pytest config object
"""
init_ocsci_conf(config)


def get_cli_param(config, name_of_param, default=None):
"""
This is helper function which store cli parameter in RUN section in
cli_params
Args:
config (pytest.config): Pytest config object
name_of_param (str): cli parameter name
default (any): default value of parameter
"""
# TODO:
# Load ocs_conf data and do the rest configuration here.
ocs_conf = config.getoption('ocs_conf')
if not ocs_conf:
return
ocsci_conf.ocs_conf_file = ocs_conf
cli_param = config.getoption(name_of_param, default=default)
ocsci_config.RUN['cli_params'][name_of_param] = cli_param
return cli_param


def process_cluster_cli_params(config):
"""
Process cluster related cli parameters
Args:
config (pytest.config): Pytest config object
"""
cluster_path = get_cli_param(config, 'cluster_path')
if cluster_path:
OCP.set_kubeconfig(
os.path.join(cluster_path, defaults.KUBECONFIG_LOCATION)
)
# TODO: determine better place for parent dir
cluster_dir_parent = "/tmp"
default_cluster_name = (
f"{ocsci_config.ENV_DATA['cluster_name']}-{getuser()}"
)
cluster_name = get_cli_param(config, 'cluster_name')
base_cluster_name = default_cluster_name
cid = random.randint(10000, 99999)
if not (cluster_name and cluster_path):
cluster_name = f"{base_cluster_name}-{cid}"
if not cluster_path:
cluster_path = os.path.join(cluster_dir_parent, cluster_name)
ocsci_config.ENV_DATA['cluster_name'] = cluster_name
ocsci_config.ENV_DATA['cluster_path'] = cluster_path


def init_ocsci_conf(
config,
default_config="conf/ocsci/default_config.yaml",
):
"""
Function to init the default config for OCS CI
Args:
config (pytest.config): Pytest config object
default_config (str): Default config data (default:
conf/ocsci/default_config.yaml)
"""
custom_config = config.getoption('ocs_conf')
cluster_config = config.getoption('cluster_conf')
with open(default_config) as file_stream:
default_config_data = yaml.safe_load(file_stream)
if custom_config:
with open(custom_config) as file_stream:
custom_config_data = yaml.safe_load(file_stream)
update_dict_recursively(default_config_data, custom_config_data)
if cluster_config:
with open(cluster_config) as file_stream:
cluster_config_data = yaml.safe_load(file_stream)
update_dict_recursively(default_config_data, cluster_config_data)
rendered_config = render_yaml_with_j2_context(default_config_data)

for key, value in rendered_config.items():
if key == "DEFAULTS":
for default_key, default_value in value.items():
setattr(defaults, default_key, default_value)
elif key.isupper():
setattr(ocsci_config, key, value)

process_cluster_cli_params(config)
19 changes: 5 additions & 14 deletions tests/test_example.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import logging
import pytest

from ocsci import config
from ocsci.config import ENV_DATA

logger = logging.getLogger(__name__)


@pytest.mark.run_this
def test_not_run_me_fail_pass():
logger.info("Hey from test which should pass")
# This is just example, we will need to load config data and access them
# directly, this is just example how we can access and share this config
# in all the tests
logger.info(f"Conf file is: {config.ocs_conf_file}")
logger.info(
"You can easily access data from ENV_DATA like cluster_name: %s",
ENV_DATA['cluster_name']
)
assert 1 == 1, "This will not reach this message"


# Not applying this marker if you call py.test -m run_this this test won't be
# executed
# @pytest.mark.run_this
def test_not_run_me_fail():
logger.info("Hey from test which should pass")
assert 1 == 0, "This will fail and print this message cause of 1 != 0"
Loading

0 comments on commit f855518

Please sign in to comment.