Skip to content

Commit

Permalink
Handle freeze (#137)
Browse files Browse the repository at this point in the history
* updating history

Signed-off-by: vvb <[email protected]>

* support for native serialization and deserialization

Signed-off-by: vvb <[email protected]>

* fixing the test for the version changes

Signed-off-by: vvb <[email protected]>

* restoring driver when proxy is configured. adding more tests

Signed-off-by: vvb <[email protected]>
  • Loading branch information
vvb authored Jan 24, 2017
1 parent a67de23 commit 1421e64
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 15 deletions.
22 changes: 22 additions & 0 deletions tests/common/test_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2017 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.

from ..connection.info import custom_setup, custom_teardown
from ucsmsdk.ucshandle import UcsHandle


def test_serialize_handle():
handle1 = custom_setup()
frozen_handle = handle1.freeze()
handle2 = UcsHandle.unfreeze(frozen_handle)
custom_teardown(handle2)
67 changes: 67 additions & 0 deletions tests/common/test_threadedmode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2017 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.

from nose.tools import with_setup, assert_equal
import threading
from ..connection.info import custom_setup, custom_teardown

handle = None


def setup_module():
global handle
handle = custom_setup()


def teardown_module():
custom_teardown(handle)


def t1_func():
from ucsmsdk.mometa.ls.LsServer import LsServer
obj = LsServer("org-root", "temp_sp1")
handle.add_mo(obj)


def t2_func():
from ucsmsdk.mometa.ls.LsServer import LsServer
obj1 = LsServer("org-root", "temp_sp2")
obj2 = LsServer("org-root", "temp_sp3")
handle.add_mo(obj1)
handle.add_mo(obj2)


@with_setup(setup_module, teardown_module)
def test_test_threading_mode():
handle.set_mode_threading()

t1 = threading.Thread(name="t1", target=t1_func)
t2 = threading.Thread(name="t2", target=t2_func)

t1.start()
t2.start()

t1.join()
t2.join()

# commit buffers should be in different contexts
buf1 = handle._get_commit_buf(tag="t1")
buf2 = handle._get_commit_buf(tag="t2")

assert_equal(len(buf1), 1)
assert_equal(len(buf2), 2)

handle.commit_buffer_discard(tag="t1")
handle.commit_buffer_discard(tag="t2")

handle.unset_mode_threading()
57 changes: 57 additions & 0 deletions tests/utils/test_backupucs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2017 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.

from nose.tools import with_setup
from ..connection.info import custom_setup, custom_teardown
from ucsmsdk.utils.ucsbackup import backup_ucs
from ucsmsdk.ucshandle import UcsHandle

handle = None


def setup_module():
global handle
handle = custom_setup()


def teardown_module():
custom_teardown(handle)


def _test_ucs_backup(file_dir, file_name, backup_type):
backup_ucs(handle,
backup_type=backup_type,
file_dir=file_dir,
file_name=file_name)


@with_setup(setup_module, teardown_module)
def test_ucs_backup():
_test_ucs_backup(file_dir="/tmp/backup",
file_name="config1.xml",
backup_type="config-logical")


def test_ucs_backup_after_freeze_unfreeze():
# for this test to be more meaningful there needs to be proxy server
# configured
h1 = custom_setup()
frozen_handle = h1.freeze()
h2 = UcsHandle.unfreeze(frozen_handle)

# Try a download operation using new handle
_test_ucs_backup(file_dir="/tmp/backup",
file_name="config2.xml",
backup_type="config-logical")

custom_teardown(h2)
4 changes: 2 additions & 2 deletions tests/utils/test_comparesync.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def static_setup():
ref_handle = UcsHandle("192.168.1.1", "admin", "password")
diff_handle = UcsHandle("192.168.1.2", "admin", "password")

ref_handle.__dict__['_UcsSession__version'] = UcsVersion("2.2(5a)")
diff_handle.__dict__['_UcsSession__version'] = UcsVersion("2.2(2c)")
ref_handle.__dict__['_UcsSession__version'] = "2.2(5a)"
diff_handle.__dict__['_UcsSession__version'] = "2.2(2c)"


def test_compare_same_obj_with_diff_props():
Expand Down
13 changes: 7 additions & 6 deletions ucsmsdk/ucshandle.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def __init__(self, ip, username, password, port=None, secure=None,
UcsSession.__init__(self, ip, username, password, port, secure, proxy)
self.__commit_buf = {}
self.__commit_buf_tagged = {}
self.__threaded = False

def set_dump_xml(self):
"""
Expand All @@ -79,21 +78,21 @@ def set_mode_threading(self):
This makes every thread a separate transaction that does not
interfere with other threads.
"""
self.__threaded = True
self._set_mode_threading(enable=True)

def unset_mode_threading(self):
"""
Unsets the threaded mode of operation.
Applications use the common common_buffer in this mode.
Only one simultaneous transaction is possible here on
"""
self.__threaded = False
self._set_mode_threading(enable=False)

def is_threading_enabled(self):
"""
returns if threading mode is set
"""
return self.__threaded
return self.threaded

def login(self, auto_refresh=False, force=False):
"""
Expand Down Expand Up @@ -985,8 +984,10 @@ def cb(mce):
wait(self, mo, prop, value, cb, timeout_sec=timeout, poll_sec=poll_sec)

def freeze(self):
return jsonpickle.encode(self)
return self._freeze()

@staticmethod
def unfreeze(frozen_handle):
return jsonpickle.decode(frozen_handle)
handle = UcsHandle("", "", "")
handle._unfreeze(frozen_handle)
return handle
59 changes: 53 additions & 6 deletions ucsmsdk/ucssession.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


import time
import json
import logging
import threading
from threading import Timer
Expand Down Expand Up @@ -53,9 +54,11 @@ def __init__(self, ip, username, password, port=None, secure=None,

self.__refresh_timer = None
self.__force = False
self.__auto_refresh = False

self.__dump_xml = False
self.__redirect = False
self.__threaded = False
self.__driver = UcsDriver(proxy=self.__proxy)

@property
Expand Down Expand Up @@ -92,7 +95,8 @@ def session_id(self):

@property
def version(self):
return self.__version
from .ucscoremeta import UcsVersion
return UcsVersion(self.__version)

@property
def refresh_period(self):
Expand All @@ -118,6 +122,48 @@ def evt_channel(self):
def last_update_time(self):
return self.__last_update_time

@property
def threaded(self):
return self.__threaded

def _freeze(self):
save = {
"ip": self.__ip,
"username": self.__username,
"password": self.__password,
"proxy": self.__proxy,
"uri": self.__uri,
"ucs": self.__ucs,
"name": self.__name,
"cookie": self.__cookie,
"session_id": self.__session_id,
"version": self.__version,
"refresh_period": self.__refresh_period,
"priv": self.__priv,
"domains": self.__domains,
"channel": self.__channel,
"evt_channel": self.__evt_channel,
"last_update_time": self.__last_update_time,
"force": self.__force,
"auto_refresh": self.__auto_refresh,
"dump_xml": self.__dump_xml,
"redirect": self.__redirect,
"threaded": self.__threaded
}
return json.dumps(save)

def _unfreeze(self, params_json):
params_dict = json.loads(params_json)
for param in params_dict:
setattr(self, '_UcsSession__' + param, params_dict[param])

# update the drvier if a proxy configuration exists
self.__driver = UcsDriver(proxy=self.__proxy)

# cookie might be stale, if so relogin
if self.__auto_refresh:
self._refresh(auto_relogin=True)

def __create_uri(self, port, secure):
"""
Generates UCSM URI used for connection
Expand Down Expand Up @@ -159,13 +205,10 @@ def __update(self, response):
"""
Internal method to update the session variables
"""

from .ucscoremeta import UcsVersion

self.__name = response.out_name
self.__cookie = response.out_cookie
self.__session_id = response.out_session_id
self.__version = UcsVersion(response.out_version)
self.__version = response.out_version
self.__refresh_period = int(response.out_refresh_period)
self.__priv = response.out_priv
self.__domains = response.out_domains
Expand Down Expand Up @@ -502,7 +545,7 @@ def _update_version(self, response=None):
raise UcsException(response.error_code,
response.error_descr)
firmware = response.out_config.child[0]
self.__version = UcsVersion(firmware.version)
self.__version = firmware.version

def _update_domain_name_and_ip(self):
from .ucsmethodfactory import config_resolve_dn
Expand Down Expand Up @@ -532,6 +575,7 @@ def _login(self, auto_refresh=False, force=False):
"""
from .ucsmethodfactory import aaa_login

self.__auto_refresh = auto_refresh
self.__force = force

if self.__validate_connection():
Expand Down Expand Up @@ -603,6 +647,9 @@ def _unset_dump_xml(self):
"""
self.__dump_xml = False

def _set_mode_threading(self, enable=False):
self.__threaded = enable


def _get_port(port, secure):
if port is not None:
Expand Down
2 changes: 1 addition & 1 deletion ucsmsdk/utils/ucsbackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def backup_ucs(handle, backup_type, file_dir, file_name, timeout_in_sec=600,
file_dir = "/home/user/backup"\n
file_name = "config_backup.xml"\n
backup_ucs(handle, backup_type="config-logical",
file_dir=test_support, file_name=file_name)\n
file_dir=file_dir, file_name=file_name)\n
"""

from ..mometa.mgmt.MgmtBackup import MgmtBackup, MgmtBackupConsts
Expand Down

0 comments on commit 1421e64

Please sign in to comment.