You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.3 KiB

  1. import os
  2. import yaml
  3. import glob
  4. import testinfra.utils.ansible_runner
  5. from ansible.playbook import Playbook
  6. from ansible.cli.playbook import PlaybookCLI
  7. testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
  8. os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
  9. def read_playbook(playbook):
  10. cli_args = [os.path.realpath(playbook), testinfra_hosts]
  11. cli = PlaybookCLI(cli_args)
  12. cli.parse()
  13. loader, inventory, variable_manager = cli._play_prereqs()
  14. pb = Playbook.load(cli.args[0], variable_manager, loader)
  15. for play in pb.get_plays():
  16. yield variable_manager.get_vars(play)
  17. def get_playbook():
  18. with open(os.path.realpath(' '.join(map(str,glob.glob('molecule.*')))), 'r') as yamlfile:
  19. data = yaml.load(yamlfile, Loader=yaml.FullLoader)
  20. if 'playbooks' in data['provisioner'].keys():
  21. if 'converge' in data['provisioner']['playbooks'].keys():
  22. return data['provisioner']['playbooks']['converge']
  23. else:
  24. return ' '.join(map(str,glob.glob('converge.*')))
  25. def test_user(host):
  26. for vars in read_playbook(get_playbook()):
  27. assert host.user(vars['user']['name']).exists
  28. if 'group' in vars['user'].keys():
  29. assert host.group(vars['user']['group']).exists
  30. else:
  31. assert host.group(vars['user']['name']).exists