-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from natan-dias/feature-update-test-for-better…
…-coverage update test
- Loading branch information
Showing
1 changed file
with
39 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,43 @@ | ||
import pytest | ||
import os | ||
import subprocess | ||
from unittest.mock import patch, Mock, call | ||
|
||
from build_and_push import build_image | ||
from build_and_push import build_image, build_and_push_image | ||
|
||
mock_run = Mock() | ||
def test_build_image(): | ||
build_image('images/webserver-example') | ||
build_image('images/webserver-example') | ||
|
||
def test_build_and_push_image(): | ||
# Mock the subprocess.run calls | ||
with patch('subprocess.run') as mock_run: | ||
# Test case 1: Successful build and push | ||
folder_path = 'images/webserver-example' | ||
folder_name = os.path.basename(folder_path) | ||
image_name = f"natandias1/{folder_name}:latest" | ||
|
||
mock_run.side_effect = [None, None] | ||
|
||
build_and_push_image(folder_path) | ||
|
||
# Assert that the correct commands were run | ||
expected_build_command = ['docker', 'build', '-t', image_name, folder_path] | ||
expected_push_command = ['docker', 'push', image_name] | ||
|
||
mock_run.assert_has_calls([ | ||
call(expected_build_command), | ||
call(expected_push_command) | ||
]) | ||
|
||
# Test case 2: Build failure | ||
mock_run.side_effect = subprocess.CalledProcessError(returncode=1, cmd=expected_build_command) | ||
with pytest.raises(subprocess.CalledProcessError): | ||
build_and_push_image(folder_path) | ||
|
||
# Test case 3: Push failure | ||
mock_run.side_effect = None | ||
mock_run.return_value = None | ||
mock_run.side_effect = subprocess.CalledProcessError(returncode=1, cmd=expected_push_command) | ||
with pytest.raises(subprocess.CalledProcessError): | ||
build_and_push_image(folder_path) |