2024-03-13 04:11:45 +03:00
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
Author : Adolfo Gómez , dkmaster at dkmon dot com
"""
from unittest import mock
from uds import models
from uds . core import types
from . import fixtures
from . . . utils . test import UDSTransactionTestCase
2024-05-28 20:37:29 +03:00
from . . . utils . helpers import limited_iterator
2024-03-13 04:11:45 +03:00
2024-03-25 07:01:09 +03:00
from uds . services . OpenStack . openstack import types as openstack_types
2024-03-13 04:11:45 +03:00
# We use transactions on some related methods (storage access, etc...)
2024-03-31 16:21:40 +03:00
class TestOpenstackFixedUserService ( UDSTransactionTestCase ) :
2024-03-13 04:11:45 +03:00
def test_userservice_fixed_user ( self ) - > None :
"""
Test the user service
"""
2024-04-10 00:49:55 +03:00
fixtures . set_all_vms_status ( openstack_types . ServerStatus . ACTIVE )
2024-03-26 05:36:48 +03:00
for patcher in ( fixtures . patched_provider , fixtures . patched_provider_legacy ) :
with patcher ( ) as prov :
2024-03-15 01:20:44 +03:00
service = fixtures . create_fixed_service ( prov ) # Will use provider patched api
userservice = fixtures . create_fixed_userservice ( service )
# patch userservice db_obj() method to return a mock
userservice_db = mock . MagicMock ( )
userservice . db_obj = mock . MagicMock ( return_value = userservice_db )
# Test Deploy for cache, should raise Exception due
# to the fact fixed services cannot have cached items
2024-03-22 03:17:34 +03:00
state = userservice . deploy_for_cache ( level = types . services . CacheLevel . L1 )
self . assertEqual ( state , types . states . TaskState . ERROR )
2024-03-15 01:20:44 +03:00
state = userservice . deploy_for_user ( models . User ( ) )
self . assertEqual ( state , types . states . TaskState . RUNNING )
for _ in limited_iterator ( lambda : state == types . states . TaskState . RUNNING , limit = 32 ) :
state = userservice . check_state ( )
self . assertEqual ( state , types . states . TaskState . FINISHED )
2024-06-26 02:59:16 +03:00
if service . get_token ( ) is None :
userservice_db . set_in_use . assert_called_once_with ( True )
2024-03-15 01:20:44 +03:00
# vmid should have been assigned, so it must be in the assigned machines
2024-03-31 16:21:40 +03:00
with service . _assigned_access ( ) as assigned_machines :
2024-03-15 01:20:44 +03:00
self . assertEqual ( { userservice . _vmid } , assigned_machines )
# Now, let's release the service
state = userservice . destroy ( )
self . assertEqual ( state , types . states . TaskState . RUNNING )
while state == types . states . TaskState . RUNNING :
state = userservice . check_state ( )
self . assertEqual ( state , types . states . TaskState . FINISHED )
# must be empty now
2024-03-31 16:21:40 +03:00
with service . _assigned_access ( ) as assigned_machines :
2024-03-15 01:20:44 +03:00
self . assertEqual ( set ( ) , assigned_machines )
# set_ready, machine is "stopped" in this test, so must return RUNNING
2024-03-25 07:01:09 +03:00
# ensure cache is empty, may affect from other tests
userservice . cache . clear ( )
# Also that machine is stopped
2024-06-16 21:55:09 +03:00
fixtures . search_id ( fixtures . SERVERS_LIST , userservice . _vmid ) . power_state = (
2024-03-26 05:36:48 +03:00
openstack_types . PowerState . SHUTDOWN
)
2024-03-15 01:20:44 +03:00
state = userservice . set_ready ( )
self . assertEqual ( state , types . states . TaskState . RUNNING )
for _ in limited_iterator ( lambda : state == types . states . TaskState . RUNNING , limit = 32 ) :
state = userservice . check_state ( )
# Should be finished now
self . assertEqual ( state , types . states . TaskState . FINISHED )