examples.rst revision e10d56ed715df64fbb0883aa73563fed56bd0238
b039dabf4c53f72454e795930e7643b6e0e625f9Timo SirainenExamples of using Multihost Plugin with Fixture for SSSD
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen========================================================
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* pytest multihost plugin uses paramiko/OpenSSHTransport to connect to hosts and provides methods to
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen run commands and copy files.
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo SirainenNamespace hook
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen--------------
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* With pytest multihost plugin we define the hosts under which the actual commands will be
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen running in a YAML/JSON file. This file is then read by multihost plugin. Each of the
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen host specified in the YAML file have a role, username/password, IP address.
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* pytest multihost provides modules and functions which takes the description of hosts in the
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen YAML file, connect to the hosts and provides some common functions to run commands, copy/get
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen files etc. The main modules provided by multihost plugin are config, domain, host. To use
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen these modules we have to subclass them and change their behaviour to suite our needs.
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* For SSSD QE we created subclasses of the config, domain and host functions to suite our needs and
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen expanded them. This is available through
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen `qe_class.py <http://git.app.eng.bos.redhat.com/git/sssd-qe-tests.git/plain/python/sssd/testlib/common/qe_class.py>`_.
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* qe_class.py also provides a global fixture called session_multihost which provides a session
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen scope fixture. This fixture can read the YAML file and provide a global multihost fixture
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen to all the tests. This fixture assumes that the YAML file has hosts defined with any of these roles:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - master
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - client
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - ad
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen - atomic
220e21750948941dc6e33b8f11b552fa21d7f81eTimo Sirainen - others
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* Hosts in the above roles are read and a list is created for hosts in each role and provided using the generator (yield) feature of python. All this is exported in the pytest namespace and can be accessed in the individual tests using namespace hook
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* Below are the examples of using namespace hook:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - Single host:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen * create a multihost config file mhc.yaml as below::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen root_password: 'redhat'
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen domains:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - name: example.test
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen type: sssd
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen hosts:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - name: client1
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen external_hostname: client1.example.test
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen ip: 10.65.223.16
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen role: client
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen * since we have 1 host with role client we can create a namespace hook in conftest.py
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen to access the host as a list
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen * create a conftest.py as below::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def pytest_namespace():
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return { 'num_masters': 0, 'num_ad':0, 'num_atomic': 0, 'num_replicas': 0, 'num_clients':1, 'num_others': 0}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen * in the actual testcase it can be accessed as below::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen from sssd.testlib.common.qe_class import session_multihost
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen class TestCase:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def test1(self, session_multihost):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen session_multihost.client[0].<method>
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen * client[0] is the handle for the client1 system defined in the mhc.yaml
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - Multiple hosts of same role:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen * create a multihost config file as below::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen root_password: 'redhat'
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen domains:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - name: example.test
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen type: sssd
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen hosts:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - name: client1
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen external_hostname: client1.example.test
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen ip: 10.65.223.16
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen role: client
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen - name: client2
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen external_hostname: client2.example.test
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen ip: 10.65.223.17
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen role: client
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen * since we have 2 hosts with role client we can create a namespace hook in conftest.py
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen to access the hosts as a list
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
2dd39e478269d6fb0bb26d12b394aa30ee965e38Timo Sirainen * create a conftest.py as below::
2dd39e478269d6fb0bb26d12b394aa30ee965e38Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def pytest_namespace():
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return { 'num_masters': 0, 'num_ad':0, 'num_atomic': 0, 'num_replicas': 0, 'num_clients':2, 'num_others': 0}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen * in the actual testcase it can be accessed as below::
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen from sssd.testlib.common.qe_class import session_multihost
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen class TestCase:
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen def test1(self, session_multihost):
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen session_multihost.client[0].<method>
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen session_multihost.client[1].<method>
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen * client[0] is the handle for the client1 system defined in mhc.yaml
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen * client[1] is the handle for the client2 system defined in mhc.yaml
740dfc753f1f087f0c41082e4c551053129e875dTimo Sirainen
740dfc753f1f087f0c41082e4c551053129e875dTimo Sirainen - Multiple hosts of different roles:
740dfc753f1f087f0c41082e4c551053129e875dTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen * create a multihost config file where we have 2 clients and 1 server::
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen root_password: 'redhat'
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen domains:
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen - name: example.test
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen type: sssd
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen hosts:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - name: client1
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen external_hostname: client1.example.test
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen ip: 10.65.223.16
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen role: client
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - name: client2
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen external_hostname: client2.example.test
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen ip: 10.65.223.17
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen role: client
8fbb781d749be44e70434f4f70eff09956d58163Timo Sirainen - name: server1
8fbb781d749be44e70434f4f70eff09956d58163Timo Sirainen external_hostname: master1.example.test
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen ip: 10.65.223.18
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen role: master
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen * since we have 2 hosts with with role client and 1 host with role master we can create a namespace hook in conftest.py to access the hosts as a list
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen * create a conftest.py as below::
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen def pytest_namespace():
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen return { 'num_masters': 1, 'num_ad':0, 'num_atomic': 0, 'num_replicas': 0, 'num_clients':2, 'num_others': 0}
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen * in the actual testcase it can be accessed as below::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen from sssd.testlib.common.qe_class import session_multihost
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen class TestCase:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def test1(session_multihost):
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen session_multihost.client[0].<method>
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen session_multihost.client[1].<method>
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen def test2(session_multihost):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen session_multihost.master[0].<method>
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
1701e3f91107051b1704721bf1dc1e32491faaf9Timo SirainenExample-1: Single host tests
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen----------------------------
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* create a multihost config file mhc.yaml with 1 host as below::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen root_password: 'redhat'
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen domains:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - name: example.test
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen type: sssd
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen hosts:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen - name: client1
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen external_hostname: client1.example.test
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen ip: 10.65.223.16
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen role: client
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* create a file called conftest.py with below contents::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def pytest_namespace():
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen return { 'num_masters': 0, 'num_ad':0, 'num_atomic': 0, 'num_replicas': 0, 'num_clients':1, 'num_others':0 }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* create file called test1.py which contains testcases::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen from sssd.testlib.common.qe_class import session_multihost
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen class TestCase:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def test1(self, session_multihost):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen session_multihost.client[0].run_command(['ls', '-l'])
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* running the test::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen $ py.test --multihost-config=mhc.yaml test1.py -s -v
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo SirainenExample-2: Multiple hosts tests
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen-------------------------------
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen* create a multihost config file mhc.yaml with 2 hosts with roles master and client::
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen root_password: 'redhat'
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen domains:
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen - name: example.test
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen type: sssd
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen hosts:
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen - name: client1
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen external_hostname: client1.example.test
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen ip: 10.65.223.16
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen role: client
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen - name: master1
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen external_hostname: master1.example.test
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen ip: 10.65.223.35
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen role: master
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* create a file called conftest.py with below contents::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def pytest_namespace():
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return { 'num_masters': 1, 'num_ad':0, 'num_atomic': 0, 'num_replicas': 0, 'num_clients':1, 'num_others':0 }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* create file called test1.py which contains testcases::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen from sssd.testlib.common.qe_class import session_multihost
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen class TestCase:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def test1(self, session_multihost):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen session_multihost.client[0].run_command(['ls', '-l'])
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def test2(self, session_multihost):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen session_multihost.master[0].run_command(['ls', '-l'])
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen* running the test::
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen $ py.test --multihost-config=mhc.yaml test1.py -s -v
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo SirainenExample-3: Multiple hosts test with setup and teardown
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen------------------------------------------------------
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen* create a multihost config file mhc.yaml with 2 hosts with roles master and client::
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen root_password: 'redhat'
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen domains:
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen - name: example.test
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen type: sssd
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen hosts:
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen - name: client1
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen external_hostname: client1.example.test
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen ip: 10.65.223.16
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen role: client
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen - name: master1
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen external_hostname: master1.example.test
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen ip: 10.65.223.35
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen role: master
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen* create a file called conftest.py with below contents::
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen from sssd.testlib.common.qe_class import session_multihost
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen def pytest_namespace():
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen return { 'num_masters': 1, 'num_ad':0, 'num_atomic': 0, 'num_replicas': 0, 'num_clients':1, 'num_others':0 }
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen @pytest.fixture(scope="class")
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen def multihost(session_multihost, request):
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen if hasattr(request.cls(), 'class_setup'):
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen request.cls().class_setup(session_multihost)
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen request.addfinalizer(lambda: request.cls().class_teardown(session_multihost))
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen return session_multihost
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen* create file called test1.py which contains testcases::
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen class TestCase:
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen def class_setup(self, session_multihost):
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen cmd = session_multihost.client[0].run_command(['useradd', 'foobar'])
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen assert cmd.returncode == 0
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def test1(self, session_multihost):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen session_multihost.client[0].run_command(['id', 'foobar'])
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def class_teardown(self, session_multihost):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen session_multihost.master[0].run_command(['userdel', 'foobar'])
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen* running the test::
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen $ py.test --multihost-config=mhc.yaml test1.py -s -v
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen
5160580b0ec3f3288a320987abdf12a990f09df5Timo SirainenExample-4: Copying files to hosts using multihost plugin
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen--------------------------------------------------------
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen* scenario: Create an sssd.conf file with specific configuration parameters. We create a local file on the
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen system from which we are running py.test command (jslave/laptop/testsystem) and copy it
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen to the actual hosts using transport.put_file method
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen* create test1.py with below contents::
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen import ConfigParser
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen class TestCase:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def class_setup(self, session_multihost):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig = ConfigParser.RawConfigParser()
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen sssdconfig.optionxform = str
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen sssdconfig.add_section('sssd')
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen sssdconfig.set("sssd", "config_file_version", '2')
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen sssdconfig.set("sssd", "domains", 'example.com')
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen sssdconfig.set("sssd", "services", "nss, pam")
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen domain_section = '%s/%s' % ('domain', 'example.com')
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen sssdconfig.add_section(domain_section)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set(domain_section, "id_provider", "ad")
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set(domain_section, "auth_provider", "ad")
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set(domain_section, "access_provider", "ad")
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set(domain_section, "fallback_homedir", "/home/%d/%u")
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen sssdconfig.set(domain_section, "use_fully_qualified_names", "True")
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set(domain_section, "ad_maximum_machine_account_password_age", "1")
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set(domain_section, "ad_machine_account_password_renewal_opts", "300:15")
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen sssdconfig.set(domain_section, "debug_level", "9")
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen sssdconfig.set(domain_section, "enumerate", "True")
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen temp_file = '/tmp/sssd.conf'
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen with open(temp_file, 'wb') as fd:
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen sssdconfig.write(fd)
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen session_multihost.client[0].transport.put_file(temp_file, '/etc/sssd/sssd.conf')
f97cacf16251b42f530c6a28686cc8c9aa7df3a2Timo Sirainen session_multihost.client[0].run_command(['chmod', '600', '/etc/sssd/sssd.conf'],
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen set_env=False, raiseonerr=False)
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo SirainenExample-5: Creating a fixture and calling a fixture
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen---------------------------------------------------
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen* scenario: We want to configure sssd.conf before our test runs. we can create a
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen function which configures sssd.conf and we call this function before our test runs
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen* create a file called conftest.py with below contents::
d92f33f13830ba23d814342bf3ea8db721a15bb1Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen from sssd.testlib.common.qe_class import session_multihost
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen import ConfigParser
61e84692827b6a64912343f515c984853021483aTimo Sirainen def pytest_namespace():
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return { 'num_masters': 1, 'num_ad':0, 'num_atomic': 0, 'num_replicas': 0, 'num_clients':1, 'num_others':0 }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen @pytest.fixture(scope="class")
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def multihost(session_multihost, request):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if hasattr(request.cls(), 'class_setup'):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen request.cls().class_setup(session_multihost)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen request.addfinalizer(lambda: request.cls().class_teardown(session_multihost))
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return session_multihost
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen @pytest.fixture(scope="class")
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def config_sssd(session_multihost, request):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig = ConfigParser.RawConfigParser()
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.optionxform = str
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.add_section('sssd')
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set("sssd", "config_file_version", '2')
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set("sssd", "domains", 'example.com')
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set("sssd", "services", "nss, pam")
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen domain_section = '%s/%s' % ('domain', 'example.com')
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen sssdconfig.add_section(domain_section)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set(domain_section, "id_provider", "ad")
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set(domain_section, "auth_provider", "ad")
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sssdconfig.set(domain_section, "fallback_homedir", "/home/%d/%u")
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen sssdconfig.set(domain_section, "use_fully_qualified_names", "True")
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen sssdconfig.set(domain_section, "debug_level", "9")
25d9db0c00e8041165540e0829d7eab7548cbaa5Timo Sirainen sssdconfig.set(domain_section, "enumerate", "True")
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen temp_file = '/tmp/sssd.conf'
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen with open(temp_file, 'wb') as fd:
25d9db0c00e8041165540e0829d7eab7548cbaa5Timo Sirainen sssdconfig.write(fd)
25d9db0c00e8041165540e0829d7eab7548cbaa5Timo Sirainen session_multihost.client[0].transport.put_file(temp_file, '/etc/sssd/sssd.conf')
7d7917f3b54749f667d8c98e881a965ae84f033eTimo Sirainen
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen* create a file test1.py with below contents::
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen
7d7917f3b54749f667d8c98e881a965ae84f033eTimo Sirainen class Testcase:
7d7917f3b54749f667d8c98e881a965ae84f033eTimo Sirainen def class_setup(self, multihost, config_sssd):
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen cmd = session_multihost.client[0].run_command(['service', 'sssd', 'restart'])
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen assert cmd.returncode == 0
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen def test1(self, multihost):
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen print("I am in test1")
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen def class_teardown(self, multihost):
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen cmd = session_multihost.client[0].run_command(['service', 'sssd', 'stop'])
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen assert cmd.returncode == 0
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen
5160580b0ec3f3288a320987abdf12a990f09df5Timo SirainenExample-6: Connecting to Windows system and running AD specific commands
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen------------------------------------------------------------------------
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen* scenario: If the test requirement requires running any specific native commands on
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen windows which cannot be fulfilled by adcli.
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen Note: Connecting to Windows using multihost plugin requires ssh be running on Windows system.
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen For this multihost plugin has been tested only with OpenSSH provided using CYGWIN. So before
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen using multihost plugin please install CYGWIN and OpenSSH package. Configure OpenSSH on Windows
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen and make sure its firewall is allowing ssh port.
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen* create a multihost config file mhc.yaml with 2 hosts with roles master and client::
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen root_password: 'redhat'
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen domains:
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen - name: example.test
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen type: sssd
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen hosts:
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen - name: client1
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen external_hostname: client1.example.test
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen ip: 10.65.223.16
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen role: client
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen - name: srv1
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen external_hostname: srv1.example.test
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen ip: 10.65.223.35
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen role: ad
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen username: Administrator
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen password: Secret123
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen* create a file called conftest.py with below contents::
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen def pytest_namespace():
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen return { 'num_masters': 0, 'num_ad':1, 'num_atomic': 0, 'num_replicas': 0, 'num_clients':1, 'num_others':0 }
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen* create file called test1.py which contains testcases::
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen from sssd.testlib.common.qe_class import session_multihost
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen class TestCase:
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen def test1(self, session_multihost):
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen session_multihost.client[0].run_command(['ls', '-l'])
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen def test2(self, session_multihost):
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen session_multihost.ad[0].run_command(['date'])
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen* Running the test::
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen $ py.test --multihost-config=mhc.yaml test1.py -s -v
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen