Skip to content

Commit

Permalink
test permissions fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lfkdev committed Jun 25, 2024
1 parent c6f1b15 commit 3b40d50
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
16 changes: 5 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
name: Ansible-Link CI
name: Python application

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
on: [push]

jobs:
test:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
Expand All @@ -20,8 +17,5 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: python -m unittest discover tests
- name: Run linter
run: |
pip install flake8
flake8 .
python -m unittest discover tests
32 changes: 23 additions & 9 deletions tests/test_ansible_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,45 @@
import json
import yaml
from pathlib import Path
import tempfile
import os

# Import the main application
from ansible_link.ansible_link import app, load_config, validate_playbook, run_playbook

class TestAnsibleLink(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp_dir = tempfile.mkdtemp()

@classmethod
def tearDownClass(cls):
os.rmdir(cls.temp_dir)

def setUp(self):
self.app = app.test_client()
self.app.testing = True

@patch('ansible_link.load_config')
@patch('ansible_link.ansible_link.load_config')
def test_load_config(self, mock_load_config):
mock_config = {
'playbook_dir': '/tmp/playbooks',
'inventory_dir': '/tmp/inventory',
'job_storage_dir': '/tmp/job-storage',
'inventory_file': '/tmp/inventory',
'job_storage_dir': self.temp_dir,
'playbook_whitelist': ['test_playbook.yml']
}
mock_load_config.return_value = mock_config
config = load_config()
self.assertEqual(config, mock_config)

@patch('ansible_link.config')
@patch('ansible_link.ansible_link.config')
@patch('pathlib.Path.is_file')
def test_validate_playbook_valid(self, mock_is_file, mock_config):
mock_config.__getitem__.return_value = '/tmp/playbooks'
mock_is_file.return_value = True
result = validate_playbook('test_playbook.yml')
self.assertEqual(result, '/tmp/playbooks/test_playbook.yml')

@patch('ansible_link.config')
@patch('ansible_link.ansible_link.config')
@patch('pathlib.Path.is_file')
def test_validate_playbook_invalid(self, mock_is_file, mock_config):
mock_config.__getitem__.return_value = '/tmp/playbooks'
Expand All @@ -43,8 +52,10 @@ def test_validate_playbook_invalid(self, mock_is_file, mock_config):
validate_playbook('invalid_playbook.yml')

@patch('ansible_runner.run')
@patch('ansible_link.validate_playbook')
def test_run_playbook(self, mock_validate_playbook, mock_ansible_runner):
@patch('ansible_link.ansible_link.validate_playbook')
@patch('ansible_link.ansible_link.config')
def test_run_playbook(self, mock_config, mock_validate_playbook, mock_ansible_runner):
mock_config.get.return_value = self.temp_dir
mock_validate_playbook.return_value = '/tmp/playbooks/test_playbook.yml'
mock_runner = MagicMock()
mock_runner.status = 'successful'
Expand All @@ -63,12 +74,15 @@ def test_run_playbook(self, mock_validate_playbook, mock_ansible_runner):
self.assertEqual(mock_ansible_runner.call_count, 1)
self.assertEqual(mock_validate_playbook.call_count, 1)

def test_playbook_endpoint(self):
@patch('ansible_link.ansible_link.threading.Thread')
@patch('ansible_link.ansible_link.validate_playbook')
def test_playbook_endpoint(self, mock_validate_playbook, mock_thread):
data = {
'playbook': 'test_playbook.yml',
'inventory': 'test_inventory',
'vars': {'test_var': 'value'}
}
mock_validate_playbook.return_value = '/tmp/playbooks/test_playbook.yml'
response = self.app.post('/ansible/playbook', json=data)
self.assertEqual(response.status_code, 202)
result = json.loads(response.data)
Expand Down

0 comments on commit 3b40d50

Please sign in to comment.