Procedure from environment construction to operation test of testinfra, a server environment test tool made by Python

Built the environment of Python infrastructure test library Testinfra

background

In-house, server tests seem to be done manually, so I would like to introduce an automation mechanism to improve efficiency. At my previous workplace, I tried to use Ruby's Serverspec and Infrataster as server test tools, but I wanted to unify them with Python's configuration management tools such as Ansible and Fabric. I found a test tool called Testinfra. Even on the official website, Testinfra was written as a tool like Ruby's Serverspec, so I tried using it. This time, I described the procedure of the introduction part from environment construction to operation test.

1. Installation procedure

1-1. Prerequisites

It is assumed that the following environment has been created

  1. pip is installed
  2. There must be at least two servers that can be connected via SSH

1-2. Flow

  1. Install testinfra
  2. ssh connection settings (client side)
  3. ssh connection settings (server side)
  4. Create test code
  5. Run test code (localhost)
  6. Execution of test code (remote host)

2. Procedure

2-1. Installation of testinfra

$ pip install testinfra

2-2. Ssh connection settings (client side)

  1. Create private key and public key
$ cd $HOME/.ssh
$ ssh-keygen -t rsa -f test_server
  1. Transfer the public key to the remote connection destination server with scp
$ scp test_server.pub [email protected]:/var/tmp/
  1. After completing step 2 of "3. ssh connection setting (server side)", try ssh connection for key authentication.
$ ssh -i $HOME/.ssh/test_server [email protected]
  1. Create ssh_config
$ vi $HOME/.ssh/ssh_config

Here, the host name of the remote server is "redmine"

Host redmine
  HostName     192.168.1.31
  Port         22
  User         permit
  IdentityFile ~/.ssh/test_server

2-3. Ssh connection settings (server side)

  1. Create an account for ssh connection This time I created a "permit" user
$ useradd -m permit -G wheel
$ passwd permit
  1. Write the client's public key to authorized_keys
$ mkdir $HOME/.ssh
$ mv /var/tmp/test_server.pub $HOME/.ssh/
$ cd $HOME/.ssh/
$ cat test_server.pub >> authorized_keys
$ chmod 600 authorized_keys
$ rm test_server.pub

2-4. Creating test code

  1. Create test code
$ vi test.py

Test code example

def test_passwd_file(File):
    passwd = File("/etc/passwd")
    assert passwd.contains("root")
    assert passwd.user == "root"
    assert passwd.group == "root"
    assert passwd.mode == 0o644

2-5. Execution of test code (localhost)

  1. localhost test run
$ testinfra -v test.py
  1. Confirmation of execution result
=================================== test session starts ====================================
platform linux2 -- Python 2.7.9, pytest-3.0.1, py-1.4.31, pluggy-0.3.1 -- /usr/local/bin/python2.7
cachedir: .cache
rootdir: /root, inifile: 
plugins: testinfra-1.4.2, pep8-1.0.6
collected 1 items 

test.py::test_passwd_file[local] PASSED

================================== pytest-warning summary ==================================
WP1 None Modules are already imported so can not be re-written: testinfra
======================= 1 passed, 1 pytest-warnings in 0.03 seconds ========================

2-6. Execution of test code (remote host)

  1. Test run of remote host
$ testinfra test.py --connection=ssh --hosts=permit@redmine --ssh-config=/root/.ssh/ssh_config
  1. Confirmation of execution result
=================================== test session starts ====================================
platform linux2 -- Python 2.7.9, pytest-3.0.1, py-1.4.31, pluggy-0.3.1 -- /usr/local/bin/python2.7
cachedir: .cache
rootdir: /root, inifile: 
plugins: testinfra-1.4.2, pep8-1.0.6
collected 1 items 

test.py::test_passwd_file[ssh://redmine] PASSED

================================== pytest-warning summary ==================================
WP1 None Modules are already imported so can not be re-written: testinfra
======================= 1 passed, 1 pytest-warnings in 0.66 seconds ========================

3. Reference

Recommended Posts

Procedure from environment construction to operation test of testinfra, a server environment test tool made by Python
Procedure to exe python file from Ubunts environment construction
Impressions of touching Dash, a data visualization tool made by python
Procedure from AWS CDK (Python) development to AWS resource construction * For beginners of development
Send a message from Slack to a Python server
From 0 to Django development environment construction to basic operation
From Python environment construction to virtual environment construction with anaconda
I made a tool to easily display data as a graph by GUI operation.
Python development environment construction 2020 [From Python installation to poetry introduction]
Add a function to tell the weather of today to slack bot (made by python)
[Python] How to make a list of character strings character by character
Summary from building Python 3.4. * From source to building a scientific computing environment
I want to start a lot of processes from python
I made a tool to create a word cloud from wikipedia
Anaconda3 python environment construction procedure
Procedure from HTML to JSON Ajax communication of API server
How to develop in a virtual environment of Python [Memo]
Build a Python environment and transfer data to the server
[Python] How to create a table from list (basic operation of table creation / change of matrix name)
A Python script that allows you to check the status of the server from your browser
Environment construction of python2 & 3 (OSX)
Build and test a CI environment for multiple versions of Python
From building a Python environment for inexperienced people to Hello world
From installing Ansible to building a Python environment in Vagrant's virtual environment
Prepare a distributed load test environment with the Python load test tool Locust
Pass a list by reference from Python to C ++ with pybind11
Everything from building a Python environment to running it on Windows
Environment construction of python and opencv
[Django] Memorandum of environment construction procedure
Environment construction of python3.8 on mac
From Ubuntu 20.04 introduction to environment construction
Behavior of python3 by Sakura's server
Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer
Build a Postfix mail server, start Python code triggered by mail reception, and post mail to Slack (local environment)
I tried to communicate with a remote server by Socket communication with Python.
I made a tool to automatically browse multiple sites with Selenium (Python)
Create a tool to automatically furigana with html using Mecab from Python3
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
Try to set up a Vim test environment quite seriously (for Python)
From re-environment construction of Python to graph drawing (on visual studio code)
I made a program to check the size of a file in Python
I tried to verify the result of A / B test by chi-square test
I made a function to see the movement of a two-dimensional array (Python)
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
Python project environment construction procedure (for windows)
Send a message from Python to Slack
[Python] Road to snake charmer (1) Environment construction
I want to build a Python environment
Environment construction procedure: Ubuntu + Apache2 + Python + Pyramid
Python development server construction procedure (AWS + Anaconda)
Add a Python virtual environment to VSCode
A textbook for beginners made by Python beginners
A story made by a person who has no knowledge of Python or Json
I made a tool to estimate the execution time of cron (+ PyPI debut)
Environment construction procedure to operate chrome without installing python on Windows (using selenium)
I made a library to operate AWS CloudFormation stack from CUI (Python Fabric)
I made a tool to notify Slack of Connpass events and made it Terraform
[python] How to sort by the Nth Mth element of a multidimensional array
SSH login to the target server from Windows with a click of a shortcut
Build a python environment to learn the theory and implementation of deep learning
I made a module in C language to filter images loaded by Python