Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testing framework #1

Merged
merged 1 commit into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# See: http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.json]
indent_size = 2
indent_style = space

[*.py]
indent_style = space
indent_size = 4

[*.yml]
indent_style = space
indent_size = 2

[Makefile]
indent_style = tab

[Vagrantfile]
indent_size = 2
indent_style = space
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
*.swp
*.o
.idea

.vagrant
_obj

*TEST.out
main.retry
testing/ssh_config
testing/ve
34 changes: 34 additions & 0 deletions testing/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Control ansible's verbosity by setting this with -v or -vvvvv (more verbose).
ANSIBLE_VERBOSE?=
# Exluding localhost, as localhost is assumed to be Mac OS X with ssh enabled.
ANSIBLE_LIMIT?=all:!localhost
# Extra flags to pass to Ansible (e.g. --skip-tags filebeat).
ANSIBLE_EXTRA_FLAGS?=--tags run_tests

.PHONY:ansible
ansible: ve
ANSIBLE_HOST_KEY_CHECKING=false \
ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' \
time ve/bin/ansible-playbook \
${ANSIBLE_VERBOSE} \
-i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory \
--limit "${ANSIBLE_LIMIT}" \
${ANSIBLE_EXTRA_FLAGS} \
ansible/main.yml

# Create a virtualenv to run Ansible.
ve: ve/bin/activate
ve/bin/activate: requirements.txt
@test -d ve || virtualenv ve
@ve/bin/pip install -Ur requirements.txt
@touch ve/bin/activate

.PHONY:setup
setup:
vagrant up

# This destroys all vagrant machines and removes the vagrant related data
clean:
-vagrant destroy -f
-rm -r .vagrant
-rm -f ssh_config
87 changes: 87 additions & 0 deletions testing/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
boxes = {
"freebsd-10" => {
:box => "bento/freebsd-10.4",
:ip => '192.70.21.2',
:cpu => "100",
:ram => "512",
:sync_type => "rsync",
:shell_provision => "pkg install -qy lang/python",
:host_vars => {
"ansible_python_interpreter" => "/usr/local/bin/python",
},
},
"debian-9" => {
:box => "bento/debian-9.3",
:ip => '192.70.21.3',
:cpu => "100",
:ram => "512",
},
"windows-2012" => {
:box => "elastic/windows-2012_r2-x86_64",
:ip => '192.70.21.4',
:cpu => "100",
:ram => "1024",
:host_vars => {
"ansible_winrm_scheme" => "http",
},
},
"windows-2016" => {
:box => "elastic/windows-2016-x86_64",
:ip => '192.70.21.5',
:cpu => "100",
:ram => "1024",
:host_vars => {
"ansible_winrm_scheme" => "http",
},
},
}

groups = {
"windows" => ["windows-2012", "windows-2016"],
"unix" => ["freebsd-10", "debian-9"],
}

Vagrant.configure("2") do |config|
host_vars = Hash.new
boxes.each do |box_name, box|
host_vars[box_name] = box[:host_vars]
end

boxes.each do |box_name, box|
config.vm.define box_name do |machine|
machine.vm.box = box[:box]
machine.vm.hostname = "%s" % box_name

machine.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--cpuexecutioncap", box[:cpu]]
v.customize ["modifyvm", :id, "--memory", box[:ram]]
end

machine.vm.network :private_network, ip: box[:ip]

host_dir = "../"
mount_point = "/home/vagrant/go/src/github.com/elastic/go-sysinfo"
if box_name =~ /^windows/
host_dir = ".gopath_mount/"
mount_point = "c:\\Users\\vagrant\\go"
end
machine.vm.synced_folder host_dir, mount_point,
type: box[:sync_type], owner: "vagrant", group: "vagrant", create: true,
rsync__exclude: ["testing", ".git"]

if box.has_key?(:shell_provision)
machine.vm.provision "shell",
inline: box[:shell_provision]
end

machine.vm.provision :ansible do |ansible|
ansible.playbook_command = "ve/bin/ansible-playbook"
ansible.playbook = "ansible/main.yml"
ansible.verbose = ENV['ANSIBLE_VERBOSE'] ||= "v"
ansible.tags = ENV['ANSIBLE_TAGS'] ||= "setup"
ansible.host_vars = host_vars
ansible.groups = groups
end
end
end
end
4 changes: 4 additions & 0 deletions testing/ansible/group_vars/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---

project_dir: '{{ ansible_user_dir }}/go/src/github.com/elastic/go-sysinfo'

93 changes: 93 additions & 0 deletions testing/ansible/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
- name: setup - dump vars
hosts: all
tags: setup

tasks:
- name: Display all variables/facts known for a host
debug:
var: hostvars[inventory_hostname]

- name: setup - go
hosts: all
tags: setup

roles:
- gvm
- golang

- name: tests - run tests
hosts: unix
tags: run_tests
roles:
- gvm
- golang

environment:
GOPATH: "{{ ansible_user_dir }}/go"
PATH: "{{ golang_goroot }}/bin:{{ ansible_env.GOPATH }}/bin:{{ ansible_env.PATH }}"

tasks:
- name: tests - fix gopath ownership
file:
path: '{{ ansible_user_dir }}/go'
owner: '{{ ansible_user_id }}'
group: '{{ ansible_user_gid }}'
recurse: true
state: directory
become: true

- name: tests - run tests
shell: 'go test -v ./... 2>&1 > {{ ansible_distribution }}-{{ ansible_distribution_major_version}}.TEST.out'
args:
chdir: '{{ project_dir }}'
register: go_test_cmd
ignore_errors: yes

- name: tests - save output to host
fetch:
src: '{{ project_dir }}/{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.TEST.out'
dest: '../{{ ansible_distribution|lower }}-{{ ansible_distribution_major_version }}.TEST.out'
flat: yes

- name: tests - delete output
file:
path: '{{ project_dir }}/{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.TEST.out'
state: absent

- name: tests - assert
assert:
that: '{{ go_test_cmd | success }}'

- name: tests - run tests (windows)
hosts: windows
tags: run_tests
roles:
- gvm
- golang

vars:
project_dir_win: '{{ ansible_user_dir }}\go\src\github.com\elastic\go-sysinfo'
test_output_file: '{{ project_dir_win }}\{{ ansible_distribution | regex_replace(" ", "_") }}.TEST.out'

tasks:
- name: tests - run tests
win_shell: 'go test -v ./... | Out-File -Encoding UTF8 {{ test_output_file }}'
args:
chdir: '{{ project_dir_win }}'
register: go_test_cmd
ignore_errors: yes

- name: tests - save output to host
fetch:
src: '{{ test_output_file }}'
dest: '../{{ ansible_distribution | regex_replace(" ", "_") }}.TEST.out'
flat: yes

- name: tests - delete output
win_file:
path: '{{ test_output_file }}'
state: absent

- name: tests - assert
assert:
that: '{{ go_test_cmd | success }}'
5 changes: 5 additions & 0 deletions testing/ansible/roles/golang/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

gvm_go_version: 1.9.3
global_profile_file: "/etc/profile.d/gvm.sh"
user_profile_file: ".profile"
36 changes: 36 additions & 0 deletions testing/ansible/roles/golang/tasks/common/unix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---

- name: Install Go
command: gvm '{{ gvm_go_version }}'

- name: Set golang_goroot var
set_fact:
golang_goroot: '{{ ansible_user_dir }}/.gvm/versions/go{{ gvm_go_version }}.{{ gvm_arch | regex_replace("-", ".") }}'

- name: tests - update bashrc with goroot
lineinfile:
path: '{{ global_profile_file }}'
line: 'eval "$(gvm {{ gvm_go_version }})"'
regexp: '^eval.*gvm'
create: true
become: true

- name: tests - update bashrc to add gopath
lineinfile:
path: "{{ ansible_user_dir }}/{{ user_profile_file }}"
line: "export GOPATH=/home/{{ ansible_user_id }}/go"
regexp: '^export GOPATH'
create: true

- name: tests - update bashrc to add gopath/bin to path
lineinfile:
path: "{{ ansible_user_dir }}/{{ user_profile_file }}"
line: 'export PATH="${GOPATH}/bin:${PATH}"'
regexp: '^export PATH.*GOPATH'
create: true

- name: tests - setup gopath
file:
path: "{{ ansible_user_dir }}/go/src/github.com/elastic"
state: directory

15 changes: 15 additions & 0 deletions testing/ansible/roles/golang/tasks/freebsd/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---

- include: ../common/unix.yml

- name: Install build tools (git, make)
pkgng:
name: git,gmake
become: true

- name: Create make - gmake symlink
file:
src: /usr/local/bin/gmake
dest: /usr/local/bin/make
state: link
become: true
8 changes: 8 additions & 0 deletions testing/ansible/roles/golang/tasks/linux/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---

- include: ../common/unix.yml

- name: Install git
apt:
name: git
become: true
20 changes: 20 additions & 0 deletions testing/ansible/roles/golang/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---

- name: golang - include_vars
include_vars: '{{ item }}'
with_first_found:
- files:
- '{{ ansible_distribution_id }}_vars.yml'
- '{{ ansible_distribution }}_vars.yml'
- '{{ ansible_os_family }}_vars.yml'
- '{{ ansible_system }}_vars.yml'
skip: true

- name: golang - include task
include: '{{ system_item }}'
with_first_found:
- '{{ ansible_os_family }}/main.yml'
- '{{ ansible_system }}/main.yml'
- unsupported.yml
loop_control:
loop_var: system_item
35 changes: 35 additions & 0 deletions testing/ansible/roles/golang/tasks/win32nt/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---

- name: Install git
win_chocolatey:
name: git
state: present

- name: Install Go
win_command: 'gvm.exe {{ gvm_go_version }}'

- name: Set golang_goroot var
set_fact:
golang_goroot: '{{ ansible_user_dir }}\.gvm\versions\go{{ gvm_go_version }}.{{ gvm_arch | regex_replace("-", ".") }}'

- name: 'Set GOROOT for {{ ansible_user_id }}'
win_environment:
state: present
name: GOROOT
value: '{{ golang_goroot }}'
level: user

- name: 'Set GOPATH for {{ ansible_user_id }}'
win_environment:
state: present
name: GOPATH
value: '{{ ansible_user_dir }}\go'
level: user

- name: 'Add GOROOT and GOPATH to PATH for {{ ansible_user_id }}'
win_path:
name: PATH
elements:
- '%GOPATH%\bin'
- '%GOROOT%\bin'
scope: user
3 changes: 3 additions & 0 deletions testing/ansible/roles/golang/vars/freebsd_vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

global_profile_file: "/etc/profile"
5 changes: 5 additions & 0 deletions testing/ansible/roles/gvm/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

gvm_version: 0.0.5
gvm_unix_path: /usr/local/bin/gvm
gvm_windows_path: 'c:\windows\system32\gvm.exe'
Loading