Switch tests to use Molecule.

This commit is contained in:
Jeff Geerling 2018-09-27 22:17:50 -05:00
parent 8fb27abcc8
commit 6798a05d0b
16 changed files with 115 additions and 72 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.retry
tests/test.sh
*/__pycache__
*.pyc

View File

@ -1,25 +1,29 @@
---
language: python
services: docker
env:
- distro: centos7
- distro: ubuntu1604
- distro: debian9
global:
- ROLE_NAME: kubernetes
matrix:
- MOLECULE_DISTRO: centos7
MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd
- MOLECULE_DISTRO: ubuntu1604
- MOLECULE_DISTRO: debian9
install:
# Install test dependencies.
- pip install molecule docker
before_script:
# Use actual Ansible Galaxy role name for the project directory.
- cd ../
- mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME
- cd geerlingguy.$ROLE_NAME
script:
# Configure test script so we can run extra tests after playbook is run.
- export container_id=$(date +%s)
- export cleanup=false
# Download test shim.
- wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/
- chmod +x ${PWD}/tests/test.sh
# Run tests.
- ${PWD}/tests/test.sh
# Test whether Kubernetes is running correctly.
# - docker exec --tty ${container_id} command-goes-here
- molecule test
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/

View File

@ -31,15 +31,15 @@ Whether the particular server will serve as a Kubernetes `master` (default) or `
Extra args to pass to `kubelet` during startup. E.g. to allow `kubelet` to start up even if there is swap is enabled on your server, set this to: `"--fail-swap-on=false"`. Or to specify the node-ip advertised by `kubelet`, set this to `"--node-ip={{ ansible_host }}"`.
kubernetes_allow_pods_on_master: True
kubernetes_allow_pods_on_master: true
Whether to remove the taint that denies pods from being deployed to the Kubernetes master. If you have a single-node cluster, this should definitely be `True`. Otherwise, set to `False` if you want a dedicated Kubernetes master which doesn't run any other pods.
kubernetes_enable_web_ui: False
kubernetes_enable_web_ui: false
Whether to enable the Kubernetes web dashboard UI (only accessible on the master itself, or proxied).
kuberenetes_debug: False
kuberenetes_debug: false
Whether to show extra debug info in Ansible's logs (e.g. the output of the `kubeadm init` command).
@ -52,7 +52,7 @@ Options passed to `kubeadm init` when initializing the Kubernetes master. The `a
kubernetes_apt_release_channel: main
kubernetes_apt_repository: "deb http://apt.kubernetes.io/ kubernetes-xenial {{ kubernetes_apt_release_channel }}"
kubernetes_apt_ignore_key_error: False
kubernetes_apt_ignore_key_error: false
Apt repository options for Kubernetes installation.

View File

@ -13,9 +13,9 @@ kubernetes_role: master
kubernetes_kubelet_extra_args: ""
kubernetes_allow_pods_on_master: True
kubernetes_enable_web_ui: True
kuberenetes_debug: False
kubernetes_allow_pods_on_master: true
kubernetes_enable_web_ui: true
kuberenetes_debug: false
kubernetes_pod_network_cidr: '10.0.1.0/16'
kubernetes_apiserver_advertise_address: ''
@ -25,7 +25,7 @@ kubernetes_ignore_preflight_errors: 'all'
kubernetes_apt_release_channel: main
# Note that xenial repo is used for all Debian derivatives at this time.
kubernetes_apt_repository: "deb http://apt.kubernetes.io/ kubernetes-xenial {{ kubernetes_apt_release_channel }}"
kubernetes_apt_ignore_key_error: False
kubernetes_apt_ignore_key_error: false
kubernetes_yum_arch: x86_64

View File

@ -0,0 +1,27 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
options:
config-file: molecule/default/yaml-lint.yml
platforms:
- name: instance
image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible
command: ${MOLECULE_DOCKER_COMMAND:-"sleep infinity"}
privileged: true
pre_build_image: true
provisioner:
name: ansible
lint:
name: ansible-lint
playbooks:
converge: ${MOLECULE_PLAYBOOK:-playbook.yml}
scenario:
name: default
verifier:
name: testinfra
lint:
name: flake8

View File

@ -1,20 +1,23 @@
---
- hosts: all
- name: Converge
hosts: all
become: true
vars:
# Allow swap in test environments (hard to control in some Docker envs).
kubernetes_kubelet_extra_args: "--fail-swap-on=false"
docker_install_compose: False
docker_install_compose: false
pre_tasks:
- name: Update apt cache.
apt: update_cache=yes cache_valid_time=600
apt: update_cache=true cache_valid_time=600
when: ansible_os_family == 'Debian'
- name: Ensure test dependencies are installed.
package: name=iproute state=present
- action: setup
- name: Gather facts.
action: setup
- name: Use cgroupfs cgroup driver instead of systemd (Red Hat).
set_fact:
@ -22,5 +25,5 @@
when: ansible_os_family == 'RedHat'
roles:
- geerlingguy.docker
- role_under_test
- role: geerlingguy.docker
- role: geerlingguy.kubernetes

View File

@ -0,0 +1,14 @@
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_hosts_file(host):
f = host.file('/etc/hosts')
assert f.exists
assert f.user == 'root'
assert f.group == 'root'

View File

@ -0,0 +1,6 @@
---
extends: default
rules:
line-length:
max: 150
level: warning

View File

@ -16,7 +16,7 @@
kubelet_args_path: '/etc/systemd/system/kubelet.service.d/10-kubeadm.conf'
kubelet_args_line: "{{ 'Environment=\"KUBELET_EXTRA_ARGS=' + kubernetes_kubelet_extra_args + '\"' }}"
kubelet_args_regexp: '^Environment='
when: kubelet_environment_file.stat.exists == False
when: kubelet_environment_file.stat.exists == false
- name: Configure KUBELET_EXTRA_ARGS.
lineinfile:
@ -29,6 +29,6 @@
- name: Reload systemd unit if args were changed.
systemd:
state: restarted
daemon_reload: yes
daemon_reload: true
name: kubelet
when: kubelet_config_file is changed

View File

@ -24,7 +24,7 @@
service:
name: kubelet
state: started
enabled: yes
enabled: true
- name: Check if Kubernetes has already been initialized.
stat:
@ -37,8 +37,8 @@
# Set up nodes.
- name: Get the kubeadm join command from the Kubernetes master.
shell: kubeadm token create --print-join-command
changed_when: False
command: kubeadm token create --print-join-command
changed_when: false
when: kubernetes_role == 'master'
register: kubernetes_join_command_result
@ -47,7 +47,7 @@
kubernetes_join_command: "{{ kubernetes_join_command_result.stdout }}"
when: kubernetes_join_command_result.stdout is defined
delegate_to: "{{ item }}"
delegate_facts: True
delegate_facts: true
with_items: "{{ groups['all'] }}"
- include_tasks: node-setup.yml

View File

@ -7,14 +7,14 @@
--kubernetes-version {{ kubernetes_version }}
--ignore-preflight-errors={{ kubernetes_ignore_preflight_errors }}
register: kubeadmin_init
failed_when: False
when: kubernetes_init_stat.stat.exists == False
failed_when: false
when: kubernetes_init_stat.stat.exists == false
- name: Print the init output to screen.
debug: var=kubeadmin_init.stdout
when:
- kuberenetes_debug
- kubernetes_init_stat.stat.exists == False
- kubernetes_init_stat.stat.exists == false
- name: Ensure .kube directory exists.
file:
@ -41,12 +41,12 @@
command: "kubectl taint nodes --all node-role.kubernetes.io/master-"
when:
- kubernetes_allow_pods_on_master
- kubernetes_init_stat.stat.exists == False
- kubernetes_init_stat.stat.exists == false
- name: Check if Kubernetes Dashboard UI service already exists.
shell: kubectl get services --namespace kube-system | grep -q kubernetes-dashboard
changed_when: False
failed_when: False
changed_when: false
failed_when: false
register: kubernetes_dashboard_service
when: kubernetes_enable_web_ui

View File

@ -1,11 +1,10 @@
---
- name: Ensure dependencies are installed.
apt:
name: "{{ item }}"
state: present
with_items:
name:
- apt-transport-https
- ca-certificates
state: present
- name: Add Kubernetes apt key.
apt_key:
@ -18,4 +17,4 @@
apt_repository:
repo: "{{ kubernetes_apt_repository }}"
state: present
update_cache: yes
update_cache: false

View File

@ -3,9 +3,9 @@
yum_repository:
name: kubernetes
description: Kubernetes
enabled: yes
gpgcheck: yes
repo_gpgcheck: yes
enabled: true
gpgcheck: true
repo_gpgcheck: true
baseurl: https://packages.cloud.google.com/yum/repos/kubernetes-el7-{{ kubernetes_yum_arch }}
gpgkey:
- https://packages.cloud.google.com/yum/doc/yum-key.gpg
@ -24,4 +24,4 @@
command: "yum -q makecache -y --disablerepo='*' --enablerepo='kubernetes'"
when: kubernetes_rpm_key is changed
args:
warn: no
warn: false

View File

@ -1,11 +0,0 @@
# Ansible Role tests
To run the test playbook(s) in this directory:
1. Install and start Docker.
1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`:
- `wget -O tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/`
1. Make the test shim executable: `chmod +x tests/test.sh`.
1. Run (from the role root directory) `distro=[distro] playbook=[playbook] ./tests/test.sh`
If you don't want the container to be automatically deleted after the test playbook is run, add the following environment variables: `cleanup=false container_id=$(date +%s)`