-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues for sonic_installer upgrade-docker and sonic_installer rol…
…lback-docker (#2278) #### What I did Fix issues: 1. upgrade-docker shall not check STATE DB for those container which do not support warm mode 2. rollback-docker sometimes cannot really rollback the container because it cannot get the correct docker image tag #### How I did it 1. for containers do not support warm mode, ignore STATE_DB check 2. use another command to get docker image tag #### How to verify it Manual test. UT is not created for this change because there are many docker command running for these two CLIs, thus: 1. Mock so many docker command causes the test case very tricky. The case would be even more complicated than the CLIs. 2. UT cannot really test the command in case of so many docker command Instead, a new sonic-mgmt test case is on the way to cover these two CLI. Will create PR for the sonic-mgmt new test case later.
- Loading branch information
1 parent
ca14133
commit bcf36eb
Showing
2 changed files
with
142 additions
and
25 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
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import pytest | ||
import sonic_installer.main as sonic_installer | ||
|
||
from click.testing import CliRunner | ||
from unittest.mock import patch, MagicMock | ||
|
||
SUCCESS = 0 | ||
|
||
|
||
@patch('sonic_installer.main.get_container_image_name', MagicMock(return_value='docker-fpm-frr')) | ||
@patch('sonic_installer.main.get_container_image_id_all', MagicMock(return_value=['1', '2'])) | ||
@patch('sonic_installer.main.get_container_image_id', MagicMock(return_value=['1'])) | ||
@patch('sonic_installer.main.get_docker_tag_name', MagicMock(return_value='some_tag')) | ||
@patch('sonic_installer.main.echo_and_log', MagicMock()) | ||
@patch('sonic_installer.main.run_command') | ||
def test_rollback_docker_basic(mock_run_cmd): | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
sonic_installer.sonic_installer.commands['rollback-docker'], ['-y', 'bgp'] | ||
) | ||
|
||
assert result.exit_code == SUCCESS | ||
expect_docker_tag_command = 'docker tag docker-fpm-frr:some_tag docker-fpm-frr:latest' | ||
mock_run_cmd.assert_called_with(expect_docker_tag_command) | ||
|
||
mock_run_cmd.reset() | ||
result = runner.invoke( | ||
sonic_installer.sonic_installer.commands['rollback-docker'], ['-y', 'snmp'] | ||
) | ||
|
||
assert result.exit_code == SUCCESS | ||
mock_run_cmd.assert_any_call('systemctl restart snmp') | ||
|
||
|
||
@patch('sonic_installer.main.get_container_image_name', MagicMock(return_value='docker-fpm-frr')) | ||
@patch('sonic_installer.main.get_container_image_id_all', MagicMock(return_value=['1'])) | ||
def test_rollback_docker_no_extra_image(): | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
sonic_installer.sonic_installer.commands['rollback-docker'], ['-y', 'bgp'] | ||
) | ||
assert result.exit_code != SUCCESS | ||
|
||
|
||
@pytest.mark.parametrize("container", ['bgp', 'swss', 'teamd', 'pmon']) | ||
@patch('sonic_installer.main.get_container_image_name', MagicMock(return_value='docker-fpm-frr')) | ||
@patch('sonic_installer.main.get_container_image_id', MagicMock(return_value='1')) | ||
@patch('sonic_installer.main.get_container_image_id_all', MagicMock(return_value=['1', '2'])) | ||
@patch('sonic_installer.main.validate_url_or_abort', MagicMock()) | ||
@patch('sonic_installer.main.urlretrieve', MagicMock()) | ||
@patch('os.path.isfile', MagicMock(return_value=True)) | ||
@patch('sonic_installer.main.get_docker_tag_name', MagicMock(return_value='some_tag')) | ||
@patch('sonic_installer.main.run_command', MagicMock()) | ||
@patch("sonic_installer.main.subprocess.Popen") | ||
@patch('sonic_installer.main.hget_warm_restart_table') | ||
def test_upgrade_docker_basic(mock_hget, mock_popen, container): | ||
def mock_hget_impl(db_name, table_name, warm_app_name, key): | ||
if table_name == 'WARM_RESTART_ENABLE_TABLE': | ||
return "false" | ||
elif table_name == 'WARM_RESTART_TABLE': | ||
return 'reconciled' | ||
|
||
mock_hget.side_effect = mock_hget_impl | ||
mock_proc = MagicMock() | ||
mock_proc.communicate = MagicMock(return_value=(None, None)) | ||
mock_proc.returncode = 0 | ||
mock_popen.return_value = mock_proc | ||
|
||
runner = CliRunner() | ||
result = runner.invoke( | ||
sonic_installer.sonic_installer.commands['upgrade-docker'], | ||
['-y', '--cleanup_image', '--warm', container, 'http://'] | ||
) | ||
|
||
print(result.output) | ||
assert result.exit_code == SUCCESS | ||
|
||
|
||
@patch('sonic_installer.main.get_container_image_name', MagicMock(return_value='docker-fpm-frr')) | ||
@patch('sonic_installer.main.get_container_image_id', MagicMock(return_value=['1'])) | ||
@patch('sonic_installer.main.validate_url_or_abort', MagicMock()) | ||
@patch('sonic_installer.main.urlretrieve', MagicMock(side_effect=Exception('download failed'))) | ||
def test_upgrade_docker_download_fail(): | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
sonic_installer.sonic_installer.commands['upgrade-docker'], | ||
['-y', '--cleanup_image', '--warm', 'bgp', 'http://'] | ||
) | ||
assert 'download failed' in result.output | ||
assert result.exit_code != SUCCESS | ||
|
||
|
||
@patch('sonic_installer.main.get_container_image_name', MagicMock(return_value='docker-fpm-frr')) | ||
@patch('sonic_installer.main.get_container_image_id', MagicMock(return_value=['1'])) | ||
@patch('sonic_installer.main.validate_url_or_abort', MagicMock()) | ||
@patch('sonic_installer.main.urlretrieve', MagicMock(side_effect=Exception('download failed'))) | ||
def test_upgrade_docker_image_not_exist(): | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
sonic_installer.sonic_installer.commands['upgrade-docker'], | ||
['-y', '--cleanup_image', '--warm', 'bgp', 'invalid_url'] | ||
) | ||
assert 'does not exist' in result.output | ||
assert result.exit_code != SUCCESS | ||
|
||
|
||
@patch('sonic_installer.main.get_container_image_name', MagicMock(return_value='docker-fpm-frr')) | ||
@patch('sonic_installer.main.get_container_image_id', MagicMock(return_value=['1'])) | ||
@patch('sonic_installer.main.validate_url_or_abort', MagicMock()) | ||
@patch('sonic_installer.main.urlretrieve', MagicMock()) | ||
@patch('os.path.isfile', MagicMock(return_value=True)) | ||
@patch('sonic_installer.main.get_docker_tag_name', MagicMock(return_value='some_tag')) | ||
@patch('sonic_installer.main.run_command', MagicMock()) | ||
@patch('sonic_installer.main.hget_warm_restart_table', MagicMock(return_value='false')) | ||
@patch("sonic_installer.main.subprocess.Popen") | ||
def test_upgrade_docker_image_swss_check_failed(mock_popen): | ||
mock_proc = MagicMock() | ||
mock_proc.communicate = MagicMock(return_value=(None, None)) | ||
mock_proc.returncode = 1 | ||
mock_popen.return_value = mock_proc | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
sonic_installer.sonic_installer.commands['upgrade-docker'], | ||
['-y', '--cleanup_image', '--warm', 'swss', 'http://'] | ||
) | ||
assert 'RESTARTCHECK failed' in result.output | ||
assert result.exit_code != SUCCESS |