ソースを参照

Added base infrastructure for test kitchen

Till Klocke 9 年 前
コミット
8e3d965d7f

+ 30 - 0
.kitchen.yml

@@ -0,0 +1,30 @@
+---
+driver:
+  name: vagrant
+
+provisioner:
+    name               : ansible_push
+    verbose            : "vvvv"
+    ansible_config     : "test/ansible.cfg"
+    idempotency_test   : True
+    #chef_bootstrap_url : False
+    #extra_vars        : "@kitchen_vars.yml"
+
+platforms:
+    - name: Debian-jessie
+      driver:
+        box: ARTACK/debian-jessie
+    - name: Debian-7.8
+      driver:
+        box: debian-7.8.0-amd64
+        box_url: https://github.com/kraksoft/vagrant-box-debian/releases/download/7.8.0/debian-7.8.0-amd64.box
+
+suites:
+  - name: fastd
+    provisioner:
+      playbook: "test/application/fastd.yml"
+      extra_vars: {'kitchen_connection': 'smart'}
+#  - name            : group
+#    provisioner     :
+#        playbook    : "test/application/group.yml"
+#        extra_vars  : { 'kitchen_connection': 'smart' }

+ 2 - 0
test/ansible.cfg

@@ -0,0 +1,2 @@
+[defaults]
+roles_path=../:../../:/spec/:./roles

+ 10 - 0
test/application/fastd.yml

@@ -0,0 +1,10 @@
+---
+- name         : Fastd via pkg manager
+  hosts        : all
+  gather_facts : yes
+  sudo         : True
+  connection   : "{{ kitchen_connection | default('local') }}"
+  vars_files   :
+                 - "fastd_pkg_vars.yml"
+  roles        :
+                 - "fastd"

+ 1 - 0
test/application/fastd_pkg_vars.yml

@@ -0,0 +1 @@
+fastd_install_from_source: false

+ 8 - 0
test/integration/fastd/serverspec/test_spec.rb

@@ -0,0 +1,8 @@
+require 'serverspec'
+
+# Required by serverspec
+set :backend, :exec
+
+describe package('fastd') do
+  it { should be_installed }
+end

+ 1 - 0
test/local.ini

@@ -0,0 +1 @@
+localhost ansible_connection='local'

+ 17 - 0
test/run_vagrant_kitchen.sh

@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+set -e
+echo "**** Box setup ***"
+
+echo "* mkdir /kitchen"
+mkdir -p /kitchen
+
+#echo "* cp -ar /mnt/shared /kitchen"
+#cp -r /mnt/shared/. /kitchen
+echo "* ln -sf /mnt/shared /kitchen"
+ln -sf /mnt/shared/* /kitchen/
+
+echo "* cd /kitchen"
+cd /kitchen/*
+
+echo "* python test/travis_run.py"
+python test/travis_run.py

+ 98 - 0
test/travis_run.py

@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+# https://github.com/ahelal/travis-in-box
+
+import yaml
+import subprocess
+import sys
+import os.path
+
+
+class TravisExec(object):
+    def __init__(self, filename="travis.yml"):
+        self.fail = False
+        stream = open(filename, 'r')
+        yaml_file = yaml.load(stream)
+        # language
+        self.language = yaml_file.get("language", None)
+
+        # Section
+        self.section_before_install = yaml_file.get("before_install", None)
+        self.section_install = yaml_file.get("install", None)
+        self.section_before_script = yaml_file.get("before_script", None)
+        self.section_script = yaml_file.get("script", None)
+
+        #self.section_after_script = yaml_file.get("after_script", None)
+        self.section_after_failure = yaml_file.get("after_failure", None)
+        self.section_after_success = yaml_file.get("after_success", None)
+
+    def _setup(self):
+        if self.language == "python":
+            print "********** Setup Python  **********"
+            print ""
+            # Since we are not using container we have to install various lang our self
+            # So this is probably not the best way to do it
+            self._execute_command(["sudo apt-get install python-setuptools python-pip -y"])
+        else:
+            print "Errors unsupported language {}".format(self.language)
+            exit(1)
+
+    def life_cycle(self):
+        # See http://docs.travis-ci.com/user/build-configuration/
+
+            # 1. setup language
+            self._setup()
+            # 4. Run before_install commands
+            self.run_command("before_install", self.section_before_install, self.section_after_failure)
+            # 5. Run install commands
+            self.run_command("install", self.section_install, self.section_after_failure)
+            # 6. Run before_script commands
+            self.run_command("before_script", self.section_before_script, self.section_after_failure)
+            # 7. Run test script commands
+            self.run_command("script", self.section_script, self.section_after_failure)
+            # 8 . if we reach this point we made it run after_success
+            self.run_command("after_success", self.section_after_success, None)
+
+    @staticmethod
+    def _execute_command(command):
+        new_command = ["echo '> " + item.rstrip('\n') + "' && { " + item.rstrip('\n') + " ; }" for item in command]
+        new_command = " && ".join(new_command)
+
+        p = subprocess.Popen(new_command, shell=True, stderr=subprocess.PIPE)
+        while True:
+            out = p.stderr.read(1)
+            if out == '' and p.poll() is not None:
+                break
+            if out != '':
+                sys.stdout.write(out)
+                sys.stdout.flush()
+        print ""
+        return p.returncode
+
+    def run_command(self, section_name=None, command=None, execute_on_failure=None):
+        if command:
+            print ""
+            print "********** Running '{}' **********".format(section_name)
+            return_code = self._execute_command(command)
+            if return_code != 0:
+                print ""
+                print "********** Failed in '{}' **********".format(section_name)
+                if execute_on_failure:
+                    print ""
+                    print "********** Running after_failure  **********".format(section_name)
+                    self._execute_command(execute_on_failure)
+                    exit(1)
+
+filename = None
+if len(sys.argv) == 1:
+    filename = ".travis.yml"
+elif len(sys.argv) == 2:
+    filename = sys.argv[1]
+else:
+    print "Invalid number of arguments"
+    exit(1)
+
+if os.path.exists(filename):
+    TravisExec(filename).life_cycle()
+else:
+    print "Could not file travis file '{}'".format(filename)
+    exit(1)