Skip to content

Instantly share code, notes, and snippets.

@maurorappa
Created November 30, 2017 16:22
Show Gist options
  • Save maurorappa/85ea85e1708ea79115b5b1c7bb83bf0c to your computer and use it in GitHub Desktop.
Save maurorappa/85ea85e1708ea79115b5b1c7bb83bf0c to your computer and use it in GitHub Desktop.
Testinfra generic validation script
# scenario: you have a container which has a programming environment (node,python,java) and you want to run Testinfra (or Molecule) to verify is set up properly
# This script will copy the test code in the container, run it and compare its output
# invocation:
# INTERPRETER=node TEST_PROGRAM=test.js EXPECTED_OUTPUT="Hello World" testinfra -v --connection docker --hosts=nodejs
# or
# INTERPRETER=/tools-copy/java/bin/java TEST_PROGRAM=HelloWorld.java EXPECTED_OUTPUT="Hello, World" testinfra -v --connection docker --hosts=maven
import os
import pytest
import testinfra
global codefile, expected_output, interpreter, testfile
interpreter = os.environ.get('INTERPRETER')
codefile = os.environ.get('TEST_PROGRAM')
expected_output = os.environ.get('EXPECTED_OUTPUT')
def init():
assert os.path.isfile(codefile), "Test program non present!"
assert interpreter is not None
assert expected_output is not None
global testfile
f = open(codefile, 'r')
testfile = f.read()
f.close
testfile = testfile.replace('"', r'\"')
def test_run_program(host):
init()
global codefile
host.run("echo \""+ testfile + "\" > /tmp/" + codefile)
if "java" in interpreter:
assert ".java" in codefile
compiled = host.run_test(interpreter + "c /tmp/" + codefile).rc
if compiled != 0:
assert False, "Compiling failed!"
codefile = codefile.strip('.java')
output = host.run(interpreter + " -cp /tmp/ " + codefile).stdout
else:
output = host.run(interpreter+" /tmp/" + codefile).stdout
output = output.strip('\n')
assert output == expected_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment