Move test for update file to module tests/util.py

Refactor tests for updating files on the node for later
reuse in testing of update of /etc/network/interfaces
file for persisting the interface created by the
control plane cut-over.

Change-Id: Ic65a69a0c2ef442b8d0e3d20b05db639a63fca7c
Related-bug: 1576150
(cherry picked from commit 6504fb25b6)
This commit is contained in:
Oleg Gelbukh 2016-06-22 10:08:16 +00:00
parent 10b2ee3388
commit 92200593f0
2 changed files with 52 additions and 34 deletions

View File

@ -10,11 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import io
import mock
import pytest
from octane.tests import util as test_util
from octane.util import node as node_util
from octane.util import ssh
@ -104,25 +104,6 @@ def test_untar_files(node, mock_ssh_popen, mock_open):
assert buf.getvalue() == content
@contextlib.contextmanager
def _check_upgrade_levels(mocker, node, content, expected_content):
mock_sftp = mocker.patch('octane.util.ssh.sftp')
old = io.BytesIO(content)
mock_new = mocker.Mock()
buf = io.BytesIO()
mock_new.write = buf.write
mock_update_file = mocker.patch('octane.util.ssh.update_file')
mock_update_file.return_value.__enter__.return_value = (old, mock_new)
yield
mock_sftp.assert_called_once_with(node)
mock_update_file.assert_called_once_with(mock_sftp.return_value,
'/etc/nova/nova.conf')
assert buf.getvalue() == expected_content
NOVA_DEFAULT = b"#\u0444\n[DEFAULT]\ndebug = True\n"
NOVA_WITH_EMPTY_LEVELS = NOVA_DEFAULT + b"[upgrade_levels]\n"
NOVA_WITH_JUNO_LEVELS = NOVA_WITH_EMPTY_LEVELS + b"compute=juno\n"
@ -131,26 +112,30 @@ NOVA_BROKEN_LEVELS = NOVA_DEFAULT + b"compute=essex\n[upgrade_levels]\n"
NOVA_BROKEN_LEVELS_WITH_KILO = NOVA_BROKEN_LEVELS + b"compute=kilo\n"
@pytest.mark.parametrize("content,expected_content", [
(NOVA_DEFAULT, NOVA_WITH_KILO_LEVELS),
(NOVA_WITH_EMPTY_LEVELS, NOVA_WITH_KILO_LEVELS),
(NOVA_WITH_JUNO_LEVELS, NOVA_WITH_KILO_LEVELS),
(NOVA_BROKEN_LEVELS, NOVA_BROKEN_LEVELS_WITH_KILO),
@pytest.mark.parametrize("content,expected_content,filename", [
(NOVA_DEFAULT, NOVA_WITH_KILO_LEVELS, '/etc/nova/nova.conf'),
(NOVA_WITH_EMPTY_LEVELS, NOVA_WITH_KILO_LEVELS, '/etc/nova/nova.conf'),
(NOVA_WITH_JUNO_LEVELS, NOVA_WITH_KILO_LEVELS, '/etc/nova/nova.conf'),
(NOVA_BROKEN_LEVELS, NOVA_BROKEN_LEVELS_WITH_KILO, '/etc/nova/nova.conf'),
])
def test_add_compute_upgrade_levels(mocker, node, content, expected_content):
with _check_upgrade_levels(mocker, node, content, expected_content):
def test_add_compute_upgrade_levels(mocker, node, content, expected_content,
filename):
with test_util.mock_update_file(mocker, node, content, expected_content,
filename):
node_util.add_compute_upgrade_levels(node, 'kilo')
@pytest.mark.parametrize("content,expected_content", [
(NOVA_DEFAULT, NOVA_DEFAULT),
(NOVA_WITH_EMPTY_LEVELS, NOVA_WITH_EMPTY_LEVELS),
(NOVA_WITH_KILO_LEVELS, NOVA_WITH_EMPTY_LEVELS),
(NOVA_BROKEN_LEVELS_WITH_KILO, NOVA_WITH_EMPTY_LEVELS),
@pytest.mark.parametrize("content,expected_content,filename", [
(NOVA_DEFAULT, NOVA_DEFAULT, '/etc/nova/nova.conf'),
(NOVA_WITH_EMPTY_LEVELS, NOVA_WITH_EMPTY_LEVELS, '/etc/nova/nova.conf'),
(NOVA_WITH_KILO_LEVELS, NOVA_WITH_EMPTY_LEVELS, '/etc/nova/nova.conf'),
(NOVA_BROKEN_LEVELS_WITH_KILO, NOVA_WITH_EMPTY_LEVELS,
'/etc/nova/nova.conf'),
])
def test_remove_compute_upgrade_levels(mocker, node, content,
expected_content):
with _check_upgrade_levels(mocker, node, content, expected_content):
expected_content, filename):
with test_util.mock_update_file(mocker, node, content, expected_content,
filename):
node_util.remove_compute_upgrade_levels(node)

33
octane/tests/util.py Normal file
View File

@ -0,0 +1,33 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import io
@contextlib.contextmanager
def mock_update_file(mocker, node, content, expected_content, filename):
mock_sftp = mocker.patch('octane.util.ssh.sftp')
old = io.BytesIO(content)
mock_new = mocker.Mock()
buf = io.BytesIO()
mock_new.write = buf.write
mock_update_file = mocker.patch('octane.util.ssh.update_file')
mock_update_file.return_value.__enter__.return_value = (old, mock_new)
yield
mock_sftp.assert_called_once_with(node)
mock_update_file.assert_called_once_with(mock_sftp.return_value,
filename)
assert buf.getvalue() == expected_content