From d3fde0d706e696e8da7d8e0fe5b80513e9e9f56d Mon Sep 17 00:00:00 2001 From: Yoshitaka Nagami Date: Fri, 10 Sep 2021 19:31:11 +0900 Subject: [PATCH 1/3] show progress bar when uploading image --- tests/test_data/dummy.qcow2 | 1 + tests/test_definitions.py | 37 +++++++++++++++++++ virl2_client/models/node_image_definitions.py | 34 ++++++++++------- 3 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 tests/test_data/dummy.qcow2 create mode 100644 tests/test_definitions.py diff --git a/tests/test_data/dummy.qcow2 b/tests/test_data/dummy.qcow2 new file mode 100644 index 0000000..2995a4d --- /dev/null +++ b/tests/test_data/dummy.qcow2 @@ -0,0 +1 @@ +dummy \ No newline at end of file diff --git a/tests/test_definitions.py b/tests/test_definitions.py new file mode 100644 index 0000000..c2a3711 --- /dev/null +++ b/tests/test_definitions.py @@ -0,0 +1,37 @@ +# +# Python bindings for the Cisco VIRL 2 Network Simulation Platform +# +# This file is part of VIRL 2 +# +# Copyright 2020 Cisco Systems Inc. +# +# 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 requests + +import pytest +import requests +from virl2_client.virl2_client import Context + +from virl2_client.models import NodeImageDefinitions + +def test_upload_image_files(requests_mock): + adapter = requests_mock.post('mock://images/upload') + session = requests.Session() + context = Context("mock://", requests_session=session) + definitions = NodeImageDefinitions(context) + image_file_path = "tests/test_data/dummy.qcow2" + definitions.upload_image_file(image_file_path) + + assert adapter.called diff --git a/virl2_client/models/node_image_definitions.py b/virl2_client/models/node_image_definitions.py index fce4efe..150a88e 100644 --- a/virl2_client/models/node_image_definitions.py +++ b/virl2_client/models/node_image_definitions.py @@ -20,6 +20,8 @@ import logging import os +import time +import sys from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor logger = logging.getLogger(__name__) @@ -239,17 +241,23 @@ def create_image_definition(self, image_id, node_definition_id, label, disk_imag def progress_callback(monitor): + if not hasattr(monitor, "start_time"): + monitor.start_time = time.time() + if not hasattr(monitor, "last_bytes"): + monitor.last_bytes = -1 # Track progress in the monitor instance itself. - if not hasattr(monitor, "curr_progress"): - monitor.curr_progress = -1 - - progress = int(100 * (monitor.bytes_read / monitor.len)) - progress = min(progress, 100) - # Report progress every increment of 10% - if progress > monitor.curr_progress and progress % 10 == 0: - print( - "Progress: {0} of {1} bytes ({2}%)".format( - monitor.bytes_read, monitor.len, progress - ) - ) - monitor.curr_progress = progress + if monitor.bytes_read > monitor.last_bytes: + print_progress_bar(monitor.bytes_read, monitor.len, monitor.start_time) + monitor.last_bytes = monitor.bytes_read + + +def print_progress_bar(cur, total, start_time, length=50): + percent = ("{0:.1f}").format(100 * (cur / float(total))) + filled_len = int(round(length * cur / float(total))) + bar = '#' * filled_len + '-' * (length - filled_len) + raw_elapsed = time.time() - start_time + elapsed = time.strftime("[%H:%M:%S]", time.gmtime(raw_elapsed)) + sys.stdout.write(f'\r |{bar}| {cur}/{total} {percent}% {elapsed}') + sys.stdout.flush() + if cur == total: + print() From d0595ab283e1eb193c4ff5daa5ba0103aed10d36 Mon Sep 17 00:00:00 2001 From: Yoshitaka Nagami Date: Mon, 13 Sep 2021 16:15:54 +0900 Subject: [PATCH 2/3] fixup! show progress bar when uploading image --- virl2_client/models/node_image_definitions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virl2_client/models/node_image_definitions.py b/virl2_client/models/node_image_definitions.py index 150a88e..2b03773 100644 --- a/virl2_client/models/node_image_definitions.py +++ b/virl2_client/models/node_image_definitions.py @@ -257,7 +257,7 @@ def print_progress_bar(cur, total, start_time, length=50): bar = '#' * filled_len + '-' * (length - filled_len) raw_elapsed = time.time() - start_time elapsed = time.strftime("[%H:%M:%S]", time.gmtime(raw_elapsed)) - sys.stdout.write(f'\r |{bar}| {cur}/{total} {percent}% {elapsed}') + sys.stdout.write('\r |{}| {}/{} {}% {}'.format(bar, cur, total, percent, elapsed)) sys.stdout.flush() if cur == total: print() From 50088f0a1ae473ee9e25ea071bef597e639e669f Mon Sep 17 00:00:00 2001 From: Yoshitaka Nagami Date: Mon, 13 Sep 2021 16:16:25 +0900 Subject: [PATCH 3/3] fixup! show progress bar when uploading image --- tests/test_definitions.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_definitions.py b/tests/test_definitions.py index c2a3711..3366491 100644 --- a/tests/test_definitions.py +++ b/tests/test_definitions.py @@ -20,14 +20,12 @@ import requests -import pytest -import requests from virl2_client.virl2_client import Context - from virl2_client.models import NodeImageDefinitions + def test_upload_image_files(requests_mock): - adapter = requests_mock.post('mock://images/upload') + adapter = requests_mock.post("mock://images/upload") session = requests.Session() context = Context("mock://", requests_session=session) definitions = NodeImageDefinitions(context)