From 943bed62f1ae06e7e35443756547fd0210f637cd Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Fri, 9 Jun 2017 10:41:43 +0200 Subject: [PATCH] First set of tests. Signed-off-by: Petr "Stone" Hracek --- .gitignore | 1 + Dockerfile | 25 +++++++++++++++ Makefile | 16 ++++++++++ repos/nodejs.repo | 6 ++++ tests/Makefile | 6 ++++ tests/config.yaml | 26 ++++++++++++++++ tests/resources/hello.txt | 1 + tests/resources/server1.js | 13 ++++++++ tests/resources/server2.js | 27 ++++++++++++++++ tests/sanity_check.py | 63 ++++++++++++++++++++++++++++++++++++++ 10 files changed, 184 insertions(+) create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 repos/nodejs.repo create mode 100644 tests/Makefile create mode 100644 tests/config.yaml create mode 100644 tests/resources/hello.txt create mode 100644 tests/resources/server1.js create mode 100644 tests/resources/server2.js create mode 100644 tests/sanity_check.py diff --git a/.gitignore b/.gitignore index e69de29..c2956c7 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +*.py[oc] diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..12cf904 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM baseruntime/baseruntime:latest + +ENV NAME=nodejs \ + VERSION=0 \ + RELEASE=1 \ + ARCH=x86_64 + +LABEL summary="Javascript runtime." \ + name="$FGC/$NAME" \ + version="$VERSION" \ + release="$RELEASE.$DISTTAG" \ + architecture="$ARCH" \ + usage="docker run nodejs node" \ + description="Node.js is a platform built on V8 JavaScript Engine for easily building fast, scalable network applications." \ + io.k8s.description="Node.js is a platform built on V8 JavaScript Engine for easily building fast, scalable network applications." + + +COPY repos/* /etc/yum.repos.d/ + +RUN sed -i 's|/jkaluza/|/ralph/|g' /etc/yum.repos.d/build.repo && \ + microdnf --nodocs --enablerepo nodejs install nodejs npm + +EXPOSE 8080 + +CMD /bin/sh/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3d9a1b8 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +IMAGE_NAME=nodejs + +MODULEMDURL=http://pkgs.fedoraproject.org/cgit/modules/nodejs.git/plain/nodejs.yaml + +default: run + +build: + docker build --tag=$(IMAGE_NAME) . + +run: build + docker run -d $(IMAGE_NAME) + +test: build + cd tests; MODULE=docker MODULEMD=$(MODULEMDURL) URL="docker=$(IMAGE_NAME)" make all + cd tests; MODULE=rpm MODULEMD=$(MODULEMDURL) make all + diff --git a/repos/nodejs.repo b/repos/nodejs.repo new file mode 100644 index 0000000..2b6bfaf --- /dev/null +++ b/repos/nodejs.repo @@ -0,0 +1,6 @@ +[nodejs] +name=nodejs +baseurl=https://kojipkgs.fedoraproject.org/compose/latest-Fedora-Modular-26/compose/Server/x86_64/os/ +enabled=0 +gpgcheck=false +modules=1 diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..8fd8ae7 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,6 @@ +MODULE_LINT=/usr/share/moduleframework/tools/modulelint.py +CMD=python -m avocado run --filter-by-tags=-WIP $(MODULE_LINT) *.py + +# +all: + $(CMD) diff --git a/tests/config.yaml b/tests/config.yaml new file mode 100644 index 0000000..ab3b041 --- /dev/null +++ b/tests/config.yaml @@ -0,0 +1,26 @@ +document: modularity-testing +version: 1 +name: nodejs +modulemd-url: http://pkgs.fedoraproject.org/cgit/modules/nodejs.git/plain/nodejs.yaml +service: + port: 8080 +packages: + rpms: + - nodejs + - npm +testdependecies: + rpms: + - curl +module: + docker: + start: "docker run -itd -u=0 --entrypoint /usr/bin/bash -p 8080:8080" + labels: + description: "Node.js is a platform built on V8 JavaScript Engine for easily building fast, scalable network applications." + io.k8s.description: "Node.js is a platform built on V8 JavaScript Engine for easily building fast, scalable network applications." + source: https://github.com/container-images/nodejs + container: docker.io/modularitycontainers/nodejs + rpm: + start: bash + stop: "forever stopall" + repos: + - http://mirror.vutbr.cz/fedora/releases/26/Everything/x86_64/os/ diff --git a/tests/resources/hello.txt b/tests/resources/hello.txt new file mode 100644 index 0000000..cd08755 --- /dev/null +++ b/tests/resources/hello.txt @@ -0,0 +1 @@ +Hello world! diff --git a/tests/resources/server1.js b/tests/resources/server1.js new file mode 100644 index 0000000..993b51b --- /dev/null +++ b/tests/resources/server1.js @@ -0,0 +1,13 @@ +const http = require('http'); + +const port = 8080; + +const server = http.createServer((req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.end('Hello World\n'); +}); + +server.listen(port, () => { + console.log(`Server running at http://127.0.0.1:${port}/`); +}); diff --git a/tests/resources/server2.js b/tests/resources/server2.js new file mode 100644 index 0000000..67cab3f --- /dev/null +++ b/tests/resources/server2.js @@ -0,0 +1,27 @@ +const http = require('http'); +const fs = require('fs'); + +const port = 8080; + +string = "ERROR" + +fs.readFile('/tmp/test/hello.txt','utf-8', function (err,data) { + if (err) { + console.log("Error"); + } + else + { + string = data; + + } + +}); +const server = http.createServer((req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.end(string); +}); + +server.listen(port, () => { + console.log(`Server running at http://127.0.0.1:${port}/`); +}); diff --git a/tests/sanity_check.py b/tests/sanity_check.py new file mode 100644 index 0000000..6b3aaa4 --- /dev/null +++ b/tests/sanity_check.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: Jan Koscielniak +# +import socket +from avocado import main +from avocado.core import exceptions +from moduleframework import module_framework +import urllib, os, time + + +class SanityCheckTemplate(module_framework.AvocadoTest): + """ + :avocado: enable + """ + def setUp(self): + super(self.__class__, self).setUp() + self.start() + self.run("mkdir -p /tmp/test") + self.run('cd /tmp/test/ && npm install -g forever') + + def testServer(self): + self.copyTo(os.path.abspath("resources/server1.js"), "/tmp/test/server1.js") + self.run('cd /tmp/test/ && forever start server1.js') + time.sleep(2) + conn = urllib.urlopen("http://127.0.0.1:8080") + code = conn.getcode() + print ">>> Testing a simple server" + print ">>> Returned code: " + str(code) + assert code == 200 + + def testSyscall(self): + self.copyTo(os.path.abspath("resources/server2.js"), "/tmp/test/server2.js") + self.copyTo(os.path.abspath("resources/hello.txt"), "/tmp/test/hello.txt") + self.run('cd /tmp/test/ && npm install -g fs') + self.run('cd /tmp/test/ && forever start server2.js') + time.sleep(2) + conn = urllib.urlopen("http://127.0.0.1:8080") + response = conn.read() + print ">>> Response: " + response + assert response == "Hello world!" + +if __name__ == '__main__': + main() +