From d3d7663f54307566093953b87d5871cde53986c2 Mon Sep 17 00:00:00 2001 From: Petr Michalec Date: Tue, 21 Jun 2016 13:38:49 +0200 Subject: [PATCH 1/2] add cd to install path --- tests/run_test.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/run_test.sh b/tests/run_test.sh index 890e89a..783670a 100755 --- a/tests/run_test.sh +++ b/tests/run_test.sh @@ -2,6 +2,8 @@ RET=1 +cd /srv/hello + check_proc() { if [ ! -d /proc/$1 ]; then echo "ERROR: Process did not start" 1>&2 From 21abdd0e762edc365478316a4915925338c6193b Mon Sep 17 00:00:00 2001 From: Petr Michalec Date: Tue, 21 Jun 2016 14:10:24 +0200 Subject: [PATCH 2/2] add demo test kitchen resources --- .kitchen.yml | 63 +++++++++++++++++++ Berksfile | 3 + metadata.rb | 4 ++ recipes/default.rb | 24 +++++++ test | 1 + .../default-recipe/bats/flask.bats | 23 +++++++ .../default-recipe/inspec/hello_spec.rb | 44 +++++++++++++ .../simpliest/inspec/hello_spec.rb | 46 ++++++++++++++ 8 files changed, 208 insertions(+) create mode 100644 .kitchen.yml create mode 100644 Berksfile create mode 100644 metadata.rb create mode 100644 recipes/default.rb create mode 120000 test create mode 100644 tests/integration/default-recipe/bats/flask.bats create mode 100644 tests/integration/default-recipe/inspec/hello_spec.rb create mode 100644 tests/integration/simpliest/inspec/hello_spec.rb diff --git a/.kitchen.yml b/.kitchen.yml new file mode 100644 index 0000000..7ddb7b5 --- /dev/null +++ b/.kitchen.yml @@ -0,0 +1,63 @@ +--- +driver: + name: docker + +provisioner: + #name: shell + +verifier: + name: inspec + sudo: true + # not working + # test_base_root: tests + +platforms: + + - name: ubuntu-12.04 + driver_config: + image: ubuntu:12.04 + platform: ubuntu + driver: + provision_command: + - apt-get install -y python-pip git + - sudo pip install flask + + # Mind the difference 12.04 and 14.04 - both works, but ... + - name: ubuntu-14.04 + driver: + provision_command: + - apt-get install -y python-pip git + - sudo pip install flask + + - name: centos # NOTE, there is an issue if centos-7 as name is used + driver: + provision_command: + - sed -i -e "s/Defaults requiretty.*/#Defaults requiretty/g" /etc/sudoers + - yum install -y git + - curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" + - python get-pip.py + - sudo pip install flask + +suites: + + # assumed to be used with kitchen-docker (but I failed to upload a local/repo directory) + # using shell provisioner, but you have to script install dependencies + - name: simpliest + test_base_root: tests + driver: + volume: + - <%= ENV['PWD'] %>:/srv/hello + provisioner: + name: shell + script: tests/run_test.sh + + # require to add Berksfile in repo + # require to add metadata.rb in repo + # require to add recipes/default.rb in repo + # install platform independent all prerequisites + - name: default-recipe + run_list: + - recipe[python-flask-demo::default] + # NOTE, chef allow to run chef-apply 'recipe.rb' to do the miracle (ie as provisioner part), but in this case we need one external cookbook to get + # env. ready (ie virtualenv; however pip install etc could be system wide and as part of recipe.rb) + diff --git a/Berksfile b/Berksfile new file mode 100644 index 0000000..967b9a7 --- /dev/null +++ b/Berksfile @@ -0,0 +1,3 @@ +source "https://supermarket.chef.io" + +metadata diff --git a/metadata.rb b/metadata.rb new file mode 100644 index 0000000..03f4a24 --- /dev/null +++ b/metadata.rb @@ -0,0 +1,4 @@ +name 'python-flask-demo' +version '0.1.0' + +depends 'poise-python' diff --git a/recipes/default.rb b/recipes/default.rb new file mode 100644 index 0000000..c3e2706 --- /dev/null +++ b/recipes/default.rb @@ -0,0 +1,24 @@ + + +%w(curl git).each do |p| + package p +end + +git '/srv/hello' do + repository 'https://github.com/tcpcloud/python-flask-hello' + action :sync +end + +python_runtime '2' +python_virtualenv '/srv/hello/.env' + +python_package 'flask' do + virtualenv '/srv/hello/.env' + #version '1.8' +end + +pip_requirements '/srv/hello/requirements.txt' if ::File.exist?('srv/hello/requirements.txt') + + + + diff --git a/test b/test new file mode 120000 index 0000000..3598c30 --- /dev/null +++ b/test @@ -0,0 +1 @@ +tests \ No newline at end of file diff --git a/tests/integration/default-recipe/bats/flask.bats b/tests/integration/default-recipe/bats/flask.bats new file mode 100644 index 0000000..d06403d --- /dev/null +++ b/tests/integration/default-recipe/bats/flask.bats @@ -0,0 +1,23 @@ +#!/usr/bin/env bats + + +# NOTE MAY LOAD HELPER FUNCTIONS ../TEST_HELPER.BASH +# example https://github.com/dokku/dokku/blob/fc63061eafffc93ec13503c36dcade0339fe0bff/tests/unit/test_helper.bash + + +#run_app() { + #source /srv/hello/.env/bin/activate + #nohup python /srv/hello/hello.py +#} + + +@test "flask app is running" { + #run_app + #run /bin/bash -c "curl -s -D - localhost:8000 -o /dev/null | egrep '^Server' | egrep '[0-9]+'" + run /bin/bash -c "hostname" + echo "output: "$output + echo "status: "$status + [ "$status" -eq 0 ] +} + + diff --git a/tests/integration/default-recipe/inspec/hello_spec.rb b/tests/integration/default-recipe/inspec/hello_spec.rb new file mode 100644 index 0000000..35f8ed1 --- /dev/null +++ b/tests/integration/default-recipe/inspec/hello_spec.rb @@ -0,0 +1,44 @@ + +# better to start is in background and check it's running only +# better to avoid shell scripting check proc/port/output using inspec as shown below +describe command('bash -c "source /srv/hello/.env/bin/activate && cd /srv/hello && ./tests/run_test.sh"') do + its('exit_status') { should eq 0 } +end + + +#cmd = <<-EOH + ## script something +#EOH + +#describe command(cmd) do + #its('exit_status') { should eq 0 } +#end + +## check port +#describe port(5000) do + #it { should be_listening } +#end + +#describe command('curl -m 10 -s -v -f http://127.0.0.1:5000/') do + #its('stdout') { should eq 'Hello World!' } + #its('exit_status') { should eq 0 } +#end + +## BUT BE AWARE OF NOT YET IMPLEMENTED F ON DOCKER ENV BELOW +#return if ::File.exist?('/.dockerenv') + +## check that ssh runs +#if os.unix? + #describe port(5000) do + #it { should be_listening } + #its('protocols') { should include('tcp') } + #end +#end +## extra test for linux +#if os.linux? + #describe port(5000) do + #its('processes') { should include 'python' } + #its('protocols') { should include 'tcp' } + #its('addresses') {should include '0.0.0.0'} + #end +#end diff --git a/tests/integration/simpliest/inspec/hello_spec.rb b/tests/integration/simpliest/inspec/hello_spec.rb new file mode 100644 index 0000000..7375ddb --- /dev/null +++ b/tests/integration/simpliest/inspec/hello_spec.rb @@ -0,0 +1,46 @@ + +# better to start is in background and check it's running only +# better to avoid shell scripting check proc/port/output using inspec as shown below +describe command('bash -c "cd /srv/hello && ./tests/run_test.sh"') do + its('exit_status') { should eq 0 } +end + + +#cmd = <<-EOH + ## script something + ## script something + ## script something +#EOH + +#describe command(cmd) do + #its('exit_status') { should eq 0 } +#end + +## check port +#describe port(5000) do + #it { should be_listening } +#end + +#describe command('curl -m 10 -s -v -f http://127.0.0.1:5000/') do + #its('stdout') { should eq 'Hello World!' } + #its('exit_status') { should eq 0 } +#end + +## BUT BE AWARE OF NOT YET IMPLEMENTED F ON DOCKER ENV BELOW +#return if ::File.exist?('/.dockerenv') + +## check that ssh runs +#if os.unix? + #describe port(5000) do + #it { should be_listening } + #its('protocols') { should include('tcp') } + #end +#end +## extra test for linux +#if os.linux? + #describe port(5000) do + #its('processes') { should include 'python' } + #its('protocols') { should include 'tcp' } + #its('addresses') {should include '0.0.0.0'} + #end +#end