From bb1d535e5fd98c189d22dbea9bb86bddf4c968f1 Mon Sep 17 00:00:00 2001 From: Radoslav Pitonak Date: Tue, 30 May 2017 14:01:17 +0200 Subject: [PATCH] test suite for nginx --- tests/Makefile | 1 - tests/config.yaml | 12 ++----- tests/configuration.py | 62 ++++++++++++++++++++++++++++++++ tests/rpmvalidator.py | 56 +++++++++++++++++++++++++++++ tests/sanity1.py | 63 +++++++++++++++++++++++++++++++++ tests/sanity2.py | 80 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 264 insertions(+), 10 deletions(-) create mode 100644 tests/configuration.py create mode 100644 tests/rpmvalidator.py create mode 100644 tests/sanity1.py create mode 100644 tests/sanity2.py diff --git a/tests/Makefile b/tests/Makefile index fa94987..8fd8ae7 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -3,5 +3,4 @@ CMD=python -m avocado run --filter-by-tags=-WIP $(MODULE_LINT) *.py # all: - generator $(CMD) diff --git a/tests/config.yaml b/tests/config.yaml index 9b9e3ee..32c9de8 100644 --- a/tests/config.yaml +++ b/tests/config.yaml @@ -12,18 +12,12 @@ testdependecies: - curl module: docker: - start: "docker run --rm -p 80:80 nginx" + start: "docker run -it -p 80:80 -p 443:443" source: https://github.com/container-images/nginx.git - container: docker.io/modularitycontainers/nginx + container: nginx rpm: start: systemctl start nginx stop: systemctl stop nginx status: systemctl status nginx repos: - - http://mirror.vutbr.cz/fedora/releases/25/Everything/x86_64/os/ -test: - processrunning: - - 'ls /proc/*/exe -alh | grep nginx' -testhost: - basic: - - 'curl localhost | grep "Welcome to nginx!"' + - http://mirror.vutbr.cz/fedora/releases/26/Everything/x86_64/os/ diff --git a/tests/configuration.py b/tests/configuration.py new file mode 100644 index 0000000..cc26131 --- /dev/null +++ b/tests/configuration.py @@ -0,0 +1,62 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# This Modularity Testing Framework helps you to write tests for modules +# Copyright (C) 2017 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# he Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Authors: Rado Pitonak +# + +from avocado import main +import time +from avocado.core import exceptions +from moduleframework import module_framework + + +class ConfigurationCheck(module_framework.AvocadoTest): + """ + :avocado: enable + """ + + def testConfChange(self): + """ + Test change the root directory in the configuration file of running nginx. + Then nginx is reloaded and should load website from new root directory. + """ + + self.start() + conf_path = "/etc/nginx/nginx.conf" + + content = "

Nginx is running in container!

" + root = "/tmp/app-root" + + # create website for testing + self.run("mkdir {}".format(root)) + self.run("echo '{}' >> {}/index.html".format(content, root)) + + # change the root directory inside conf file + self.run("sed -i 's/root.*$/root \/tmp\/app-root\/;/g' {}".format(conf_path)) + self.run("nginx -s reload") + + time.sleep(5) + + # request content from host + self.assertIn('{}\n'.format(content), self.runHost("curl 127.0.0.1:80").stdout) + + +if __name__ == '__main__': + main() diff --git a/tests/rpmvalidator.py b/tests/rpmvalidator.py new file mode 100644 index 0000000..8dc7541 --- /dev/null +++ b/tests/rpmvalidator.py @@ -0,0 +1,56 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# This Modularity Testing Framework helps you to write tests for modules +# Copyright (C) 2017 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# he Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Authors: Jan Scotka +# + +from avocado import main +from moduleframework import module_framework + + +class ExampleRpmValidation(module_framework.AvocadoTest): + """ + :avocado: enable + """ + fhs_base_paths = [ + '/bin', + '/boot', + '/dev', + '/etc', + '/home', + '/lib', + '/lib64', + '/media', + '/mnt', + '/opt', + '/proc', + '/root', + '/run', + '/sbin', + '/sys', + '/srv', + '/tmp', + '/usr/bin' + ] + + def testPaths(self): + self.start() + for directory in self.fhs_base_paths: + self.run("test -d %s" % directory) diff --git a/tests/sanity1.py b/tests/sanity1.py new file mode 100644 index 0000000..cefd661 --- /dev/null +++ b/tests/sanity1.py @@ -0,0 +1,63 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# This Modularity Testing Framework helps you to write tests for modules +# Copyright (C) 2017 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# he Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Authors: Rado Pitonak +# + +from avocado import main +from avocado.core import exceptions +from moduleframework import module_framework + + +class SanityCheck1(module_framework.AvocadoTest): + """ + :avocado: enable + """ + + def test1(self): + """ + Simple sanity test + """ + + self.start() + self.run("ls / | grep bin") + + def test2Version(self): + """ + Check if nginx is installed in correct version + """ + + version = "1." + self.start() + + # For some reason, nginx -v spits out the version number to stderr + self.run("nginx -v 2>&1 | grep {}".format(version)) + + def test3ServiceRunning(self): + """ + Check if nginx is running + """ + + self.start() + self.run('ls /proc/*/exe -alh | grep nginx') + + +if __name__ == '__main__': + main() diff --git a/tests/sanity2.py b/tests/sanity2.py new file mode 100644 index 0000000..6794e2a --- /dev/null +++ b/tests/sanity2.py @@ -0,0 +1,80 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# This Modularity Testing Framework helps you to write tests for modules +# Copyright (C) 2017 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# he Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Authors: Rado Pitonak +# + +from avocado import main +from avocado.core import exceptions +from moduleframework import module_framework +import time + + +class SanityCheck2(module_framework.AvocadoTest): + """ + :avocado: enable + """ + + def test1Port80(self): + """ + Request to localhost inside the container to make sure that server running on port 80 + """ + + self.start() + time.sleep(2) + self.run("curl 127.0.0.1:80") + + def test2WebsiteContent(self): + """ + Check if nginx display correct content + """ + + content = "

Nginx is running in container!

" + root = "/usr/share/nginx/" + + self.start() + + # create simple static website inside the container + self.run("touch {}/html/index.html".format(root)) + self.run("echo '{}' >> {}/html/index.html".format(content, root)) + + # compare content returned by request from host with original content + self.assertIn('{}\n'.format(content), self.runHost("curl 127.0.0.1:80").stdout) + + def test3Port443(self): + """ + Test configure server to listen also on port 443 and make request from host. + """ + + conf_path = "/etc/nginx/nginx.conf" + self.start() + time.sleep(2) + + # Make the server listen on port 443. + self.run("sed -i 's/server {}$/ server {} listen 443;/g' {}".format("{", "{", conf_path)) + # reload nginx to apply changes + self.run("nginx -s reload") + time.sleep(5) + + self.runHost("curl 127.0.0.1:443") + + +if __name__ == '__main__': + main()