Skip to content

Commit

Permalink
updates to molecule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Anderson committed Mar 5, 2024
1 parent 6b99a9f commit be451d6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 254 deletions.
112 changes: 1 addition & 111 deletions molecule/kompose/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,111 +1 @@
# Molecule managed

{% if item.registry is defined %}
FROM {{ item.registry.url }}/{{ item.image }}
{% else %}
FROM {{ item.image }}
{% endif %}
ENV container=docker

{# Initial Package Installs and Container Prep #}
{% if item.image.split(':', 1)[0] in ["ubuntu"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
locales software-properties-common rsyslog systemd systemd-cron sudo \
iproute2 gnupg
RUN sed -i 's/^\($ModLoad imklog\)/#\1/' /etc/rsyslog.conf
{% elif item.image.split(':', 1)[0] in ["debian"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
sudo systemd systemd-sysv \
build-essential wget gnupg
{% elif item.image.split(':', 1)[0] in ["centos"] %}
{% if item.image in ["centos:7"] %}
RUN yum makecache fast && yum -y install deltarpm \
{% elif item.image in ["centos:8"] %}
RUN yum makecache --timer \
{% endif %}
&& yum -y install epel-release \
&& yum -y update \
&& yum -y install sudo which
{% endif %}


{# Install of Python2 #}
{% if item.image in ["ubuntu:16.04"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends python-setuptools wget \
&& wget https://bootstrap.pypa.io/get-pip.py \
&& python get-pip.py
{% elif item.image in ["debian:9"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends libffi-dev libssl-dev \
python-pip python-dev python-setuptools python-wheel
{% elif item.image in ["centos:7"] %}
RUN yum -y install python-pip
{% endif %}

{# Install of Python3 #}
{% if item.image in ["ubuntu:18.04", "ubuntu:20.04", "debian:10"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
apt-utils python3-setuptools python3-pip
{% endif %}
{% if item.image in ["centos:8"] %}
RUN yum -y install hostname python3 python3-pip
{% endif %}

{# Steps for cleanup #}
{% if item.image.split(':', 1)[0] in ["ubuntu", "debian"] %}
RUN rm -Rf /var/lib/apt/lists/* \
&& rm -Rf /usr/share/doc && rm -Rf /usr/share/man \
&& apt-get clean
{% elif item.image.split(':', 1)[0] in ["centos"] %}
RUN yum clean all
{% endif %}

{# Steps for clenaup of systemd #}
{% if item.image in ["centos:7", "centos:8", "debian:9"] %}
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*; \
mkdir -p /run/systemd/system
{% endif %}
{% if item.image in ["ubuntu:18.04", "ubuntu:20.04"] %}
# Remove unnecessary getty and udev targets that result in high CPU usage when using
# multiple containers with Molecule (https://github.com/ansible/molecule/issues/1104)
RUN rm -f /lib/systemd/system/systemd*udev* \
&& rm -f /lib/systemd/system/getty.target
{% endif %}


{% if item.image in ["centos:7", "centos:8"] %}
# Disable requiretty.
RUN sed -i -e 's/^\(Defaults\s*requiretty\)/#--- \1/' /etc/sudoers
{% endif %}

{% if item.image.split(':', 1)[0] not in ["centos", "debian"] %}
# Fix potential UTF-8 errors with ansible-test.
RUN locale-gen en_US.UTF-8
{% endif %}

# Install Ansible inventory file.
RUN mkdir -p /etc/ansible
RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts

{% if item.image in ["centos:7", "centos:8", "debian:9", "debian:10"] %}
VOLUME ["/sys/fs/cgroup"]
{% elif item.image in ["ubuntu:16.04", "ubuntu:18.04", "ubuntu:20.04"] %}
VOLUME ["/sys/fs/cgroup", "/tmp", "/run"]
{% endif %}
{% if item.image in ["centos:7", "centos:8"] %}
CMD ["/usr/sbin/init"]
{% elif item.image in ["ubuntu:16.04", "ubuntu:18.04", "ubuntu:20.04", "debian:9", "debian:10"] %}
CMD ["/lib/systemd/systemd"]
{% endif %}
{{ lookup('url', 'https://raw.githubusercontent.com/ericsysmin/ansible-molecule-dockerfiles/main/' ~ item.image ~ '/Dockerfile', split_lines=False) }}
25 changes: 13 additions & 12 deletions molecule/kompose/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
- name: Converge
hosts: all
pre_tasks:
- name: Wait for systemd to complete initialization. # noqa command-instead-of-module
ansible.builtin.command: systemctl is-system-running
register: systemctl_status
until: >-
"running" in systemctl_status.stdout or "degraded" in
systemctl_status.stdout
retries: 30
delay: 5
when:
- ansible_service_mgr == "systemd"
- ansible_distribution == "Fedora"
changed_when: false
- name: Update package cache
ansible.builtin.package:
update_cache: "yes"
cmd: ""
update_cache: true
changed_when: false
register: task_result
until: task_result is success
retries: 10
delay: 2
- name: Create containerd folder
ansible.builtin.file:
path: /etc/systemd/system/containerd.service.d
state: directory
when: ansible_service_mgr == "systemd"
- name: Override file for containerd
ansible.builtin.copy:
src: files/override.conf
dest: /etc/systemd/system/containerd.service.d/override.conf
when: ansible_service_mgr == "systemd"
roles:
- role: ericsysmin.kubernetes.kompose
9 changes: 5 additions & 4 deletions molecule/kompose/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ lint: |
ansible-lint
platforms:
- name: instance
image: ${MOLECULE_DISTRO:-ubuntu:xenial}
image: ${MOLECULE_DISTRO:-ubuntu-xenial}
privileged: true
command: ${MOLECULE_COMMAND:-"sleep infinity"}
command: ${MOLECULE_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- /sys/fs/cgroup:/sys/fs/cgroup:rw
cgroupns_mode: host
provisioner:
name: ansible
playbooks:
converge: ${MOLECULE_PLAYBOOK:-converge.yml}
converge: ${MOLECULE_PLAYBOOK:-converge.yml}
112 changes: 1 addition & 111 deletions molecule/kubectl/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,111 +1 @@
# Molecule managed

{% if item.registry is defined %}
FROM {{ item.registry.url }}/{{ item.image }}
{% else %}
FROM {{ item.image }}
{% endif %}
ENV container=docker

{# Initial Package Installs and Container Prep #}
{% if item.image.split(':', 1)[0] in ["ubuntu"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
locales software-properties-common rsyslog systemd systemd-cron sudo \
iproute2 gnupg
RUN sed -i 's/^\($ModLoad imklog\)/#\1/' /etc/rsyslog.conf
{% elif item.image.split(':', 1)[0] in ["debian"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
sudo systemd systemd-sysv \
build-essential wget gnupg
{% elif item.image.split(':', 1)[0] in ["centos"] %}
{% if item.image in ["centos:7"] %}
RUN yum makecache fast && yum -y install deltarpm \
{% elif item.image in ["centos:8"] %}
RUN yum makecache --timer \
{% endif %}
&& yum -y install epel-release \
&& yum -y update \
&& yum -y install sudo which
{% endif %}


{# Install of Python2 #}
{% if item.image in ["ubuntu:16.04"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends python-setuptools wget \
&& wget https://bootstrap.pypa.io/get-pip.py \
&& python get-pip.py
{% elif item.image in ["debian:9"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends libffi-dev libssl-dev \
python-pip python-dev python-setuptools python-wheel
{% elif item.image in ["centos:7"] %}
RUN yum -y install python-pip
{% endif %}

{# Install of Python3 #}
{% if item.image in ["ubuntu:18.04", "ubuntu:20.04", "debian:10"] %}
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
apt-utils python3-setuptools python3-pip
{% endif %}
{% if item.image in ["centos:8"] %}
RUN yum -y install hostname python3 python3-pip
{% endif %}

{# Steps for cleanup #}
{% if item.image.split(':', 1)[0] in ["ubuntu", "debian"] %}
RUN rm -Rf /var/lib/apt/lists/* \
&& rm -Rf /usr/share/doc && rm -Rf /usr/share/man \
&& apt-get clean
{% elif item.image.split(':', 1)[0] in ["centos"] %}
RUN yum clean all
{% endif %}

{# Steps for clenaup of systemd #}
{% if item.image in ["centos:7", "centos:8", "debian:9"] %}
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*; \
mkdir -p /run/systemd/system
{% endif %}
{% if item.image in ["ubuntu:18.04", "ubuntu:20.04"] %}
# Remove unnecessary getty and udev targets that result in high CPU usage when using
# multiple containers with Molecule (https://github.com/ansible/molecule/issues/1104)
RUN rm -f /lib/systemd/system/systemd*udev* \
&& rm -f /lib/systemd/system/getty.target
{% endif %}


{% if item.image in ["centos:7", "centos:8"] %}
# Disable requiretty.
RUN sed -i -e 's/^\(Defaults\s*requiretty\)/#--- \1/' /etc/sudoers
{% endif %}

{% if item.image.split(':', 1)[0] not in ["centos", "debian"] %}
# Fix potential UTF-8 errors with ansible-test.
RUN locale-gen en_US.UTF-8
{% endif %}

# Install Ansible inventory file.
RUN mkdir -p /etc/ansible
RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts

{% if item.image in ["centos:7", "centos:8", "debian:9", "debian:10"] %}
VOLUME ["/sys/fs/cgroup"]
{% elif item.image in ["ubuntu:16.04", "ubuntu:18.04", "ubuntu:20.04"] %}
VOLUME ["/sys/fs/cgroup", "/tmp", "/run"]
{% endif %}
{% if item.image in ["centos:7", "centos:8"] %}
CMD ["/usr/sbin/init"]
{% elif item.image in ["ubuntu:16.04", "ubuntu:18.04", "ubuntu:20.04", "debian:9", "debian:10"] %}
CMD ["/lib/systemd/systemd"]
{% endif %}
{{ lookup('url', 'https://raw.githubusercontent.com/ericsysmin/ansible-molecule-dockerfiles/main/' ~ item.image ~ '/Dockerfile', split_lines=False) }}
25 changes: 13 additions & 12 deletions molecule/kubectl/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
- name: Converge
hosts: all
pre_tasks:
- name: Wait for systemd to complete initialization. # noqa command-instead-of-module
ansible.builtin.command: systemctl is-system-running
register: systemctl_status
until: >-
"running" in systemctl_status.stdout or "degraded" in
systemctl_status.stdout
retries: 30
delay: 5
when:
- ansible_service_mgr == "systemd"
- ansible_distribution == "Fedora"
changed_when: false
- name: Update package cache
ansible.builtin.package:
update_cache: "yes"
cmd: ""
update_cache: true
changed_when: false
register: task_result
until: task_result is success
retries: 10
delay: 2
- name: Create containerd folder
ansible.builtin.file:
path: /etc/systemd/system/containerd.service.d
state: directory
when: ansible_service_mgr == "systemd"
- name: Override file for containerd
ansible.builtin.copy:
src: files/override.conf
dest: /etc/systemd/system/containerd.service.d/override.conf
when: ansible_service_mgr == "systemd"
roles:
- role: ericsysmin.kubernetes.kubectl
9 changes: 5 additions & 4 deletions molecule/kubectl/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ lint: |
ansible-lint
platforms:
- name: instance
image: ${MOLECULE_DISTRO:-ubuntu:xenial}
image: ${MOLECULE_DISTRO:-ubuntu-xenial}
privileged: true
command: ${MOLECULE_COMMAND:-"sleep infinity"}
command: ${MOLECULE_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- /sys/fs/cgroup:/sys/fs/cgroup:rw
cgroupns_mode: host
provisioner:
name: ansible
playbooks:
converge: ${MOLECULE_PLAYBOOK:-converge.yml}
converge: ${MOLECULE_PLAYBOOK:-converge.yml}

0 comments on commit be451d6

Please sign in to comment.