Add release optionality to subcloud-deploy upload/show

Add an optional --release parameter to subcloud-deploy upload/show
commands to enable release optionality in subcloud-deploy upload and
show.

Test Plan:
PASS: Verify that the deployment files were successfully uploaded to
the directory dedicated for that specified release.
PASS: Verify that the deployment files were successfully uploaded to
the directory dedicated for the active release when the '--release'
parameter not present.
PASS: Verify the deployment files that were showed belong to
the specified release.
PASS: Verify the deployment files that were showed belong to
the active release when the '--release' not present.
PASS: Verify that 'None' files were shown when an unknown release
was given to the "subcloud-deploy show" command.

Story: 2010611
Task: 47663

Signed-off-by: lzhu1 <li.zhu@windriver.com>
Change-Id: I402d95a0fd6c5b7818539d2756fc0c13cf60010c
This commit is contained in:
Li Zhu 2023-03-16 21:14:08 -04:00
parent a7e773235c
commit e105209400
3 changed files with 113 additions and 7 deletions

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
#
# Copyright (c) 2020-2022 Wind River Systems, Inc.
# Copyright (c) 2020-2023 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@ -27,6 +27,7 @@ import pecan
from pecan import expose
from pecan import request
from dccommon import consts as dccommon_consts
from dcmanager.api.controllers import restcomm
from dcmanager.api.policies import subcloud_deploy as subcloud_deploy_policy
from dcmanager.api import policy
@ -104,6 +105,12 @@ class SubcloudDeployController(object):
error_msg = "error: argument %s is required" % missing_str.rstrip()
pecan.abort(httpclient.BAD_REQUEST, error_msg)
release = tsc.SW_VERSION
if request.POST.get('release_version'):
release = request.POST.get('release_version')
deploy_dicts['release_version'] = release
dir_path = os.path.join(dccommon_consts.DEPLOY_DIR, release)
for f in consts.DEPLOY_COMMON_FILE_OPTIONS:
if f not in request.POST:
continue
@ -113,7 +120,6 @@ class SubcloudDeployController(object):
if not filename:
pecan.abort(httpclient.BAD_REQUEST,
_("No %s file uploaded" % f))
dir_path = tsc.DEPLOY_PATH
binary = False
if f == consts.DEPLOY_CHART:
@ -128,14 +134,20 @@ class SubcloudDeployController(object):
return deploy_dicts
@index.when(method='GET', template='json')
def get(self):
"""Get the subcloud deploy files that has been uploaded and stored"""
def get(self, release=None):
"""Get the subcloud deploy files that has been uploaded and stored.
:param release: release version
"""
policy.authorize(subcloud_deploy_policy.POLICY_ROOT % "get", {},
restcomm.extract_credentials_for_policy())
deploy_dicts = dict()
if not release:
release = tsc.SW_VERSION
deploy_dicts['release_version'] = release
dir_path = os.path.join(dccommon_consts.DEPLOY_DIR, release)
for f in consts.DEPLOY_COMMON_FILE_OPTIONS:
dir_path = tsc.DEPLOY_PATH
filename = None
if os.path.isdir(dir_path):
prefix = f + '_'

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2022 Wind River Systems, Inc.
# Copyright (c) 2022-2023 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@ -30,6 +30,10 @@ subcloud_deploy_rules = [
{
'method': 'GET',
'path': '/v1.0/subcloud-deploy'
},
{
'method': 'GET',
'path': '/v1.0/subcloud-deploy/{release}'
}
]
)

View File

@ -1,4 +1,4 @@
# Copyright (c) 2020-2022 Wind River Systems, Inc.
# Copyright (c) 2020-2023 Wind River Systems, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -13,21 +13,39 @@
# License for the specific language governing permissions and limitations
# under the License.
#
import os
import mock
from six.moves import http_client
import webtest
from dcmanager.api.controllers.v1 import subcloud_deploy
from dcmanager.common import consts
from dcmanager.common import utils as dutils
from dcmanager.tests.unit.api import test_root_controller as testroot
from dcmanager.tests import utils
from tsconfig.tsconfig import SW_VERSION
FAKE_RELEASE = '21.12'
FAKE_TENANT = utils.UUID1
FAKE_ID = '1'
FAKE_URL = '/v1.0/subcloud-deploy'
FAKE_HEADERS = {'X-Tenant-Id': FAKE_TENANT, 'X_ROLE': 'admin,member,reader',
'X-Identity-Status': 'Confirmed', 'X-Project-Name': 'admin'}
FAKE_DEPLOY_PLAYBOOK_PREFIX = consts.DEPLOY_PLAYBOOK + '_'
FAKE_DEPLOY_OVERRIDES_PREFIX = consts.DEPLOY_OVERRIDES + '_'
FAKE_DEPLOY_CHART_PREFIX = consts.DEPLOY_CHART + '_'
FAKE_DEPLOY_PLAYBOOK_FILE = 'deployment-manager.yaml'
FAKE_DEPLOY_OVERRIDES_FILE = 'deployment-manager-overrides-subcloud.yaml'
FAKE_DEPLOY_CHART_FILE = 'deployment-manager.tgz'
FAKE_DEPLOY_FILES = {
FAKE_DEPLOY_PLAYBOOK_PREFIX: FAKE_DEPLOY_PLAYBOOK_FILE,
FAKE_DEPLOY_OVERRIDES_PREFIX: FAKE_DEPLOY_OVERRIDES_FILE,
FAKE_DEPLOY_CHART_PREFIX: FAKE_DEPLOY_CHART_FILE,
}
class TestSubcloudDeploy(testroot.DCManagerApiTest):
def setUp(self):
@ -37,6 +55,23 @@ class TestSubcloudDeploy(testroot.DCManagerApiTest):
@mock.patch.object(subcloud_deploy.SubcloudDeployController,
'_upload_files')
def test_post_subcloud_deploy(self, mock_upload_files):
params = [('release_version', FAKE_RELEASE)]
fields = list()
for opt in consts.DEPLOY_COMMON_FILE_OPTIONS:
fake_name = opt + "_fake"
fake_content = "fake content".encode('utf-8')
fields.append((opt, webtest.Upload(fake_name, fake_content)))
mock_upload_files.return_value = True
params += fields
response = self.app.post(FAKE_URL,
headers=FAKE_HEADERS,
params=params)
self.assertEqual(response.status_code, http_client.OK)
self.assertEqual(FAKE_RELEASE, response.json['release_version'])
@mock.patch.object(subcloud_deploy.SubcloudDeployController,
'_upload_files')
def test_post_subcloud_deploy_without_release(self, mock_upload_files):
fields = list()
for opt in consts.DEPLOY_COMMON_FILE_OPTIONS:
fake_name = opt + "_fake"
@ -47,6 +82,8 @@ class TestSubcloudDeploy(testroot.DCManagerApiTest):
headers=FAKE_HEADERS,
upload_files=fields)
self.assertEqual(response.status_code, http_client.OK)
# Verify the active release will be returned if release doesn't present
self.assertEqual(SW_VERSION, response.json['release_version'])
@mock.patch.object(subcloud_deploy.SubcloudDeployController,
'_upload_files')
@ -155,3 +192,56 @@ class TestSubcloudDeploy(testroot.DCManagerApiTest):
upload_files=fields,
expect_errors=True)
self.assertEqual(response.status_code, http_client.BAD_REQUEST)
@mock.patch.object(dutils, 'get_filename_by_prefix')
def test_get_subcloud_deploy_with_release(self, mock_get_filename_by_prefix):
def get_filename_by_prefix_side_effect(dir_path, prefix):
filename = FAKE_DEPLOY_FILES.get(prefix)
if filename:
return prefix + FAKE_DEPLOY_FILES.get(prefix)
else:
return None
os.path.isdir = mock.Mock(return_value=True)
mock_get_filename_by_prefix.side_effect = \
get_filename_by_prefix_side_effect
url = FAKE_URL + '/' + FAKE_RELEASE
response = self.app.get(url, headers=FAKE_HEADERS)
self.assertEqual(response.status_code, http_client.OK)
self.assertEqual(FAKE_RELEASE,
response.json['subcloud_deploy']['release_version'])
self.assertEqual(FAKE_DEPLOY_PLAYBOOK_FILE,
response.json['subcloud_deploy'][consts.DEPLOY_PLAYBOOK])
self.assertEqual(FAKE_DEPLOY_OVERRIDES_FILE,
response.json['subcloud_deploy'][consts.DEPLOY_OVERRIDES])
self.assertEqual(FAKE_DEPLOY_CHART_FILE,
response.json['subcloud_deploy'][consts.DEPLOY_CHART])
self.assertEqual(None,
response.json['subcloud_deploy'][consts.DEPLOY_PRESTAGE])
@mock.patch.object(dutils, 'get_filename_by_prefix')
def test_get_subcloud_deploy_without_release(self, mock_get_filename_by_prefix):
def get_filename_by_prefix_side_effect(dir_path, prefix):
filename = FAKE_DEPLOY_FILES.get(prefix)
if filename:
return prefix + FAKE_DEPLOY_FILES.get(prefix)
else:
return None
os.path.isdir = mock.Mock(return_value=True)
mock_get_filename_by_prefix.side_effect = \
get_filename_by_prefix_side_effect
response = self.app.get(FAKE_URL, headers=FAKE_HEADERS)
self.assertEqual(response.status_code, http_client.OK)
self.assertEqual(SW_VERSION,
response.json['subcloud_deploy']['release_version'])
self.assertEqual(FAKE_DEPLOY_PLAYBOOK_FILE,
response.json['subcloud_deploy'][consts.DEPLOY_PLAYBOOK])
self.assertEqual(FAKE_DEPLOY_OVERRIDES_FILE,
response.json['subcloud_deploy'][consts.DEPLOY_OVERRIDES])
self.assertEqual(FAKE_DEPLOY_CHART_FILE,
response.json['subcloud_deploy'][consts.DEPLOY_CHART])
self.assertEqual(None,
response.json['subcloud_deploy'][consts.DEPLOY_PRESTAGE])