Repository: ansible-community/ansible-bender (language: python)

Issue:
Can't build simple-playbook on vagrant up
Here are some things that I found on attempting to try out ansible-bender.

I found the presence of a Vagrantfile great, as this could be useful not just for a dev environment but also for someone to try it out.

Adding this line to the Vagrantfile
```
config.vm.synced_folder ".", "/src"
```

In order to successfully build I had to:

- disable SELinux to rectify what appeared to be a `podman` issue.  
- run ansible-bender as root

Here is a [gist](https://gist.github.com/00willo/c6770e90a967d475c7dae410193cb777) for that error with SELinux enabled, as the Fedora default.

Here is the [gist](https://gist.github.com/00willo/c6444793a8ba37742544bb79891fc61a) for the non-root ansible-bender build.

Perhaps we could put in the appropriate config to in a separate playbook for the Vagrant provision process, move the common things between that and the recipe.yml playbook into a task file to be included by both playbooks.  

Happy to do up a PR on any of this.




Discussion / hints:
Thanks for taking time to look into this so thoroughly. I recall I added the vagrantfile when I tested something and needed a clean VM.

I completely agree with you that the fact we have the vagrantfile could be a great opportunity for everyone to try bender in a clean environment.

Please, if you have time and motivation to do all the work you described, I'd gladly accept such pull request. Especially when there will be a clear list of steps how to do so. Not everyone is as experienced and skilful as you are!

As for the SELinux issue, I have it in the enforcing mode on my laptop and haven't noticed any issues recently. Would to be nice to get it fixed, feel free to create a dedicated issue for that, or even link to an issue in podman's tracker.

Thank you!


Definitely keen to do the work on this.  I'll make the changes for the Vagrantfile first.

Question @TomasTomecek on this comment from the Vagrantfile. 
>     # FIXME: it will fail since the playbook expects a container environment
>     #        we should fix it
Anything else that would fail, apart from the `/src` folder containing the git repo code?  As I think the `config.vm.synced_folder` option resolves that issue.
> Definitely keen to do the work on this. I'll make the changes for the Vagrantfile first.
> 
> Question @TomasTomecek on this comment from the Vagrantfile.
> 
> > ```
> > # FIXME: it will fail since the playbook expects a container environment
> > #        we should fix it
> > ```
> 
> Anything else that would fail, apart from the `/src` folder containing the git repo code? As I think the `config.vm.synced_folder` option resolves that issue.

Yup, sounds like that's exactly it.

Fix patch:
diff --git a/.gitignore b/.gitignore
index 2e3fcea..a2c8fcc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,3 +20,5 @@ pip-delete-this-directory.txt
 dmypy.json
 .pyre/
 
+# Vagrant artifacts
+.vagrant/**
diff --git a/Makefile b/Makefile
index eecdff4..93be301 100644
--- a/Makefile
+++ b/Makefile
@@ -6,8 +6,8 @@ CONT_IMG := $(PY_PACKAGE)
 ANSIBLE_BENDER := python3 -m ansible_bender.cli
 PYTEST_EXEC := pytest
 
-build-ab-img: contrib/pre-setup.yml
-	$(ANSIBLE_BENDER) build -- ./contrib/pre-setup.yml $(BASE_IMAGE) $(CONT_IMG)
+build-ab-img: contrib/setup.yml
+	$(ANSIBLE_BENDER) build -- ./contrib/setup.yml $(BASE_IMAGE) $(CONT_IMG)
 
 check:
 	PYTHONPATH=$(CURDIR) PYTHONDONTWRITEBYTECODE=yes $(PYTEST_EXEC) --cov=ansible_bender -l -v $(TEST_TARGET)
@@ -65,8 +65,7 @@ check-in-docker:
 		bash -c " \
 			set -x \
 			&& dnf install -y ansible make \
-			&& ansible-playbook -i 'localhost,' -e ansible_python_interpreter=/usr/bin/python3 -e test_mode=yes -c local ./contrib/pre-setup.yml \
-			&& ansible-playbook -i 'localhost,' -e ansible_python_interpreter=/usr/bin/python3 -e test_mode=yes -c local ./contrib/post-setup.yml \
+			&& ansible-playbook -i 'localhost,' -e ansible_python_interpreter=/usr/bin/python3 -e test_mode=yes -c local ./contrib/setup.yml \
 			&& id \
 			&& pwd \
 			&& podman info \
diff --git a/Vagrantfile b/Vagrantfile
index d5b4337..3114a9e 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -1,48 +1,31 @@
 # -*- mode: ruby -*-
 # vi: set ft=ruby :
 
-Vagrant.configure("2") do |config|
-  with_tests = ENV["WITH_TESTS"] || "no"
-  if with_tests == "yes"
-    config.vm.define "f30" do |f30|
-      f30.vm.box = "fedora/30-cloud-base"
-    end
-  end
-
-  config.vm.define "f31" do |f31|
-    f31.vm.box = "fedora/31-cloud-base"
-  end
+with_tests = ENV["WITH_TESTS"] || "no"
 
+Vagrant.configure("2") do |config|
   config.vm.provider "libvirt" do |vb|
     vb.memory = "1024"
   end
 
-  config.vm.provision "ansible" do |a|
-    a.playbook = "contrib/pre-setup.yml"
-    a.raw_arguments = [
-      "-vv",
-      "-e", "ansible_python_interpreter=/usr/bin/python3",
-      "-e", "project_dir=/vagrant",
-      "-e", "with_tests=#{with_tests}",
-      "--become"
-    ]
-  end
-  config.vm.provision "shell", :inline => "grep unified_cgroup_hierarchy /proc/cmdline || reboot"
-  config.vm.provision "ansible" do |a|
-    a.playbook = "contrib/post-setup.yml"
-    a.raw_arguments = [
-      "-vv",
-      "-e", "ansible_python_interpreter=/usr/bin/python3",
-      "-e", "project_dir=/vagrant",
-      "-e", "with_tests=#{with_tests}",
-      "--become"
-    ]
-  end
+  config.vm.define "f37" do |f37|
+    f37.vm.box = "fedora/37-cloud-base"
+
+    f37.vm.provision "ansible" do |a|
+      a.playbook = "contrib/setup.yml"
+      a.raw_arguments = [
+        "-vv",
+        "-e", "ansible_python_interpreter=/usr/bin/python3",
+        "-e", "project_dir=/src",
+        "-e", "with_tests=#{with_tests}",
+        "--become"
+      ]
+    end
+
+    f37.vm.synced_folder ".", "/src"
 
-  if with_tests == "yes"
-    config.vm.provision "shell" do |s|
-      s.name = "test-script"
-      s.inline = "cd /vagrant && sudo make check"
+    if with_tests == "yes"
+      f37.vm.provision "shell", :inline => "cd /src && sudo make check"
     end
   end
 end
diff --git a/ci.yaml b/ci.yaml
index a2e1a9e..dc4705a 100644
--- a/ci.yaml
+++ b/ci.yaml
@@ -1,13 +1,3 @@
 ---
 - name: Provision environment with dependncies to run Bender's test suite
-  vars:
-    project_dir: "."
-    with_bender_install: no
-    with_tests: no
-  import_playbook: contrib/pre-setup.yml
-- name: Provision environment with dependncies to run Bender's test suite
-  vars:
-    project_dir: "."
-    with_bender_install: no
-    with_tests: no
-  import_playbook: contrib/post-setup.yml
+  import_playbook: contrib/setup.yml
diff --git a/contrib/post-setup.yml b/contrib/post-setup.yml
deleted file mode 100644
index 8320955..0000000
--- a/contrib/post-setup.yml
+++ /dev/null
@@ -1,45 +0,0 @@
----
-- name: Set up environment for ansible-bender (after reboot)
-  hosts: all
-  vars:
-    test_mode: no  # yes - test in CI, no - build an image
-    project_dir: /src
-    with_tests: no  # yes - run test suite
-    with_bender_install: yes
-    docker_package_name: docker
-    ansible_bender:
-      target_image:
-        workging_dir: /src
-        cmd: /entry.sh
-      working_container:
-        volumes:
-        - "{{ playbook_dir }}:/src"
-  tasks:
-  - name: Start dockerd
-    systemd:
-      name: docker
-      state: started
-    when: with_tests == "yes"
-  - name: stat /src
-    stat:
-      path: /src
-    register: src_path
-    when: test_mode == "no"
-  - name: Let's make sure /src is present
-    assert:
-      that:
-      - 'src_path.stat.isdir'
-    when: test_mode == "no"
-  - name: copy entrypoint script
-    copy:
-      src: contrib/entry.sh
-      dest: /entry.sh
-    when: test_mode == "no"
-  # this requires sources mounted inside at /src
-  - name: Install ansible-bender from the current working directory
-    pip:
-      name: '{{ project_dir }}'
-    tags:
-    - no-cache
-    when: with_bender_install == "yes"
-
diff --git a/contrib/pre-setup.yml b/contrib/pre-setup.yml
deleted file mode 100644
index d6cc1cd..0000000
--- a/contrib/pre-setup.yml
+++ /dev/null
@@ -1,83 +0,0 @@
----
-- name: Set up environment for ansible-bender (before reboot)
-  hosts: all
-  vars:
-    test_mode: no  # yes - test in CI, no - build an image
-    project_dir: /src
-    with_tests: no  # yes - run test suite
-    with_bender_install: yes
-    docker_package_name: docker
-    ansible_bender:
-      target_image:
-        workging_dir: /src
-        cmd: /entry.sh
-      working_container:
-        volumes:
-        - "{{ playbook_dir }}:/src"
-  tasks:
-  - name: upgrade all packages
-    dnf:
-      name: "*"
-      state: latest
-    when: with_tests == "yes"
-  - name: "figure out docker package name: F31+ has moby, not docker"
-    set_fact:
-      docker_package_name: moby-engine
-    when: ansible_distribution_major_version >= '31'
-  - name: use cgroups v1
-    lineinfile:
-      state: present
-      path: /etc/default/grub
-      regexp: "^(GRUB_CMDLINE_LINUX=\")^(?!systemd.unified)(.+)"
-      # double escape is yaml's fault
-      line: "\\1systemd.unified_cgroup_hierarchy=0 \\2"
-      backrefs: yes
-    become: true
-  - name: regen grub config
-    command: grub2-mkconfig -o /boot/grub2/grub.cfg
-    become: true
-  - name: Install all packages needed to hack on ab.
-    dnf:
-      name:
-      - git
-      - make
-      - python3-pip
-      - python3-setuptools
-      - python3-setuptools_scm
-      - python3-wheel  # for bdist_wheel
-      - containers-common
-      - buildah
-      - podman
-      - ansible
-      - python3-pytest
-      - python3-pytest-cov
-      - python3-flexmock
-      - python3-ipdb
-      - python3-jsonschema
-      - python3-tabulate
-      - '{{ docker_package_name }}'  # one test uses it
-      state: present
-  # - name: Change storage driver to vfs (ovl on ovl doesn't work)
-  #   lineinfile:
-  #     path: /etc/containers/storage.conf
-  #     regexp: '^driver = '
-  #     line: 'driver = "vfs"'
-  - name: Change storage driver graphroot to /tmp/containers
-    lineinfile:
-      path: /etc/containers/storage.conf
-      regexp: '^graphroot = '
-      line: 'graphroot = "/tmp/containers"'
-    when: with_tests == "no"
-  - name: copy libpod.conf
-    copy:
-      src: /usr/share/containers/libpod.conf
-      dest: /etc/containers/libpod.conf
-      remote_src: yes
-    when: with_tests == "no"
-  - name: Change cgroup driver to cgroupfs.
-    lineinfile:
-      path: /etc/containers/libpod.conf
-      regexp: '^cgroup_manager = '
-      line: 'cgroup_manager = "cgroupfs"'
-    when: with_tests == "no"
-
diff --git a/contrib/setup.yml b/contrib/setup.yml
new file mode 100644
index 0000000..6d514d9
--- /dev/null
+++ b/contrib/setup.yml
@@ -0,0 +1,37 @@
+---
+- name: Set up environment for ansible-bender
+  hosts: all
+  tasks:
+  - name: Install all packages needed to hack on ab.
+    dnf:
+      name:
+      - git
+      - make
+      - python3-pip
+      - python3-setuptools
+      - python3-setuptools_scm
+      - python3-wheel  # for bdist_wheel
+      - containers-common
+      - buildah
+      - podman
+      - ansible
+      - python3-pytest
+      - python3-pytest-cov
+      - python3-flexmock
+      - python3-ipdb
+      - python3-jsonschema
+      - python3-tabulate
+      - docker
+  - name: Start dockerd
+    systemd:
+      name: docker
+      state: started
+  - name: Add vagrant user to docker group (required for tests/functional/test_buildah.py:test_push_to_dockerd)
+    user:
+      append: true
+      name: vagrant
+      groups: docker
+  - name: copy entrypoint script
+    copy:
+      src: entry.sh
+      dest: /entry.sh


Test patch:
diff --git a/tests/unit/test_ansibla.py b/tests/unit/test_ansibla.py
index 0489a5e..1916465 100644
--- a/tests/unit/test_ansibla.py
+++ b/tests/unit/test_ansibla.py
@@ -26,35 +26,6 @@ def _f(name, package=None):
     else:
         flexmock(importlib, import_module=lambda name: None)
 
-
-@pytest.mark.parametrize("mock_r_t,mock_i_m,should_raise", (
-    (
-        partial(mock_read_text, "1"),
-        partial(mock_import_module, False),
-        False
-    ),
-    (
-        partial(mock_read_text, "1"),
-        partial(mock_import_module, True),
-        True
-    ),
-    (
-        partial(mock_read_text, "0"),
-        partial(mock_import_module, False),
-        False
-    ),
-    (
-        partial(mock_read_text, "0"),
-        partial(mock_import_module, True),
-        True
-    ),
-    (
-        partial(mock_read_text, None, True),
-        partial(mock_import_module, False),
-        False
-    ),
-))
-
 @pytest.mark.parametrize("di, error_message", (
     (
         {"target_image": {"user": {}}},