Pass X-Auth-Token into download snapshot request

Since 7.0 the diagnostic snapshow aren't shared by Nginx anymore, and
any request to download goes through Nailgun and therefore requires
authentication token.

Closes-Bug: #1492207

Change-Id: I29e1a5142dd7c7050ba58b68f1161eba339cf53f
Signed-off-by: Igor Kalnitsky <igor@kalnitsky.org>
This commit is contained in:
Igor Kalnitsky
2015-09-04 16:56:09 +03:00
parent 9643fa07f1
commit 1ce8ecd8be
3 changed files with 22 additions and 5 deletions

View File

@@ -19,6 +19,7 @@ import yaml
from fuelclient.cli.actions.base import Action
import fuelclient.cli.arguments as Args
from fuelclient.cli.formatting import download_snapshot_with_progress_bar
from fuelclient.client import APIClient
from fuelclient.objects.task import SnapshotTask
@@ -63,6 +64,7 @@ class SnapshotAction(Action):
snapshot_task.wait()
download_snapshot_with_progress_bar(
snapshot_task.connection.root + snapshot_task.data["message"],
auth_token=APIClient.auth_token,
directory=params.dir
)

View File

@@ -103,7 +103,8 @@ def get_bar_for_progress(full_width, progress):
)
def download_snapshot_with_progress_bar(url, directory=os.path.curdir):
def download_snapshot_with_progress_bar(
url, auth_token, directory=os.path.curdir):
"""downloads file from specific 'url' with progress bar and save it
to some 'directory'.
"""
@@ -113,7 +114,8 @@ def download_snapshot_with_progress_bar(url, directory=os.path.curdir):
os.path.abspath(directory),
url.split('/')[-1]
)
download_handle = urllib2.urlopen(url)
request = urllib2.Request(url, headers={'x-auth-token': auth_token})
download_handle = urllib2.urlopen(request)
with open(file_name, 'wb') as file_handle:
meta = download_handle.info()
file_size = int(meta.getheaders("Content-Length")[0])

View File

@@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from mock import call
from mock import patch
@@ -30,11 +30,13 @@ class TestSnapshot(base.UnitTestCase):
self.execute(['fuel', 'snapshot', '--conf'])
self.assertEqual(mstdout.write.call_args_list, [call('key: value\n')])
@patch('fuelclient.cli.actions.snapshot.APIClient',
mock.Mock(auth_token='token123'))
@patch('fuelclient.cli.actions.snapshot.SnapshotTask.start_snapshot_task')
@patch('fuelclient.cli.actions.snapshot.'
'download_snapshot_with_progress_bar')
@patch('sys.stdin')
def test_snapshot_with_provided_conf(self, mstdin, mbar, mstart):
def test_snapshot_with_provided_conf(self, mstdin, mdownload, mstart):
conf = 'key: value\n'
mstdin.isatty.return_value = False
@@ -45,14 +47,25 @@ class TestSnapshot(base.UnitTestCase):
mstart.assert_called_once_with({'key': 'value'})
self.assertEqual(mstdin.read.call_count, 1)
mdownload.assert_called_once_with(
mock.ANY,
auth_token='token123',
directory='.')
@patch('fuelclient.cli.actions.snapshot.APIClient',
mock.Mock(auth_token='token123'))
@patch('fuelclient.cli.actions.snapshot.SnapshotTask.start_snapshot_task')
@patch('fuelclient.cli.actions.snapshot.'
'download_snapshot_with_progress_bar')
@patch('sys.stdin')
def test_snapshot_without_conf(self, mstdin, mbar, mstart):
def test_snapshot_without_conf(self, mstdin, mdownload, mstart):
mstdin.isatty.return_value = True
self.execute(['fuel', 'snapshot'])
mstart.assert_called_once_with({})
mdownload.assert_called_once_with(
mock.ANY,
auth_token='token123',
directory='.')