1

I have several roles that run actions on a remote database that executes sentences for user and privilege creation.

I have seen molecule used to test playbooks that run against a single host, but I am unsure of how you could setup a second container to run a docker instance in the same network as the molecule container (similar to a docker-compose setup). However I have not been able to find a setup like this in the documentation.

Is there a recommended way to run molecule tests with external dependencies? Or should I just use docker-compose or similar to run my tests?

srb
  • 81
  • 5

2 Answers2

0

There is a 'prepare' stage in Molecule specifically for that. You need to separate questions:

  1. where external resource (database) is run?
  2. why and how it's configured?

Those are very separate, and mixing them together is a bad idea.

For 1 there are different answers:

  1. It is (out of blue, configured by other people). Use non-managed hosts in molecule.yml.
  2. We OK to run it on the same host as the host we run our code. Shovel installation into 'prepare' stage.
  3. We want it to be on separate server. Put additional host in platforms in a different group and configure it in prepare stage.

If you find your driver is not good enough, you always can opt for 'delegated' driver. In this case you need to write playbooks for create/destroy of hosts. It's relatively easy. The main trick is to use 'platforms' variable to get information about content of molecule.yaml's platform section.

George Shuklin
  • 6,952
  • 10
  • 39
  • 80
0

Yes, this is feasible. I do something following this exact use case.

It seems like you are using docker.Keep in mind you have access to almost full docker capabilities when setting up your tests, by that I mean you can mount volumes, isolate networks, use a Dockerfile etc

platforms:
 - name: client
   image: ubuntu:focal
groups:
  - clients
exposed_ports:
  - 3022
published_ports:
  - 0.0.0.0:3022:3022/tcp
networks:
  - name: molecule_test
    links:
      - server
- name: server
  image: ubuntu:focal
  exposed_ports:
    - 3080
    - 443
    - 3025
  published_ports:
    - 0.0.0.0:3025:3025/tcp
    - 0.0.0.0:3080:3080/tcp
    - 0.0.0.0:443:443/tcp
  networks:
    - name: molecule_test
      links:
        - client
  groups:
    - servers

Now in my case, I am only testing the client. However,I want to ensure the client is making calls to the server correctly. I do the following in my test_scenario.py file. This assumes you are using TestInfra

import testinfra
from testinfra.host import Host

testinfra_hosts = ["clients"]


def get_server() -> Host:
  return testinfra.get_host("docker://root@server")


def test_client_server_interaction(host: Host):
  client_name = "p3-10000"
  server = get_server()
  client_joined = server.run("client-manager nodes list")
  assert client_name in client_joined.stdout

In this case, my host under test is "client", however, I want to make sure the "client" is calling "server" correctly.

I'll also mention you can/should use prepare.yml to setup servers/external dependencies. What goes in your prepare.yml can be wholly unrelated to your role under test

Examples of what you might put in your prepare.yml.

  1. Database servers - In this case you might setup a DB and tests client insert correct rows
  2. Mock Http Endpoints - Setup mock http servers and assert client use correct params/verbs etc
  3. Calling Cloud (AWS GCP) to setup resources
bearrito
  • 2,217
  • 1
  • 25
  • 36