Better encapsulation for deploy class and test cases

This commit is contained in:
Omar Zevallos 2019-01-05 13:24:51 -05:00
Родитель 99c1b10536
Коммит 7d823ce439
2 изменённых файлов: 52 добавлений и 57 удалений

Просмотреть файл

@ -12,7 +12,6 @@ Objects require the following environment variables at instantiation:
* AZURE_SUBSCRIPTION_ID
"""
import json
import logging
import os
from datetime import datetime
@ -23,22 +22,19 @@ from string import ascii_lowercase
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import DeploymentMode, TemplateLink
from azure.mgmt.resource.resources.models import DeploymentMode
class AvereTemplateDeploy:
def __init__(self, deploy_params={}, resource_group=None,
location='eastus2'):
"""Initialize, authenticate to Azure, generate deploy params."""
self.template_link = TemplateLink(
uri='https://raw.githubusercontent.com/' +
'Azure/Avere/master/src/vfxt/azuredeploy-auto.json')
location='eastus2', deploy_name='azurePySDK', template={}):
"""Initialize, authenticate to Azure."""
self.deploy_params = deploy_params
self.resource_group = self.deploy_params.pop('resourceGroup',
resource_group)
self.location = self.deploy_params.pop('location', location)
self.ssh_pub_key_path = os.path.expanduser('~/.ssh/id_rsa.pub')
self.deploy_name = self.deploy_params.pop('deployName', deploy_name)
self.template = self.deploy_params.pop('template', template)
logging.debug('> Loading Azure credentials')
sp_creds = ServicePrincipalCredentials(
@ -55,25 +51,10 @@ class AvereTemplateDeploy:
subscription_id=os.environ['AZURE_SUBSCRIPTION_ID']
)
if not self.deploy_params:
gen_id = 'av' + \
datetime.utcnow().strftime('%m%d%H%M%S') + \
choice(ascii_lowercase)
self.resource_group = gen_id + '-rg'
self.deploy_params = {
'virtualNetworkResourceGroup': self.resource_group,
'virtualNetworkName': gen_id + '-vnet',
'virtualNetworkSubnetName': gen_id + '-subnet',
'avereBackedStorageAccountName': gen_id + 'sa',
'controllerName': gen_id + '-con',
'controllerAdminUsername': 'azureuser',
'controllerAuthenticationType': 'sshPublicKey'
}
logging.debug('> Generated deploy parameters: \n{}'.format(
json.dumps(self.deploy_params, indent=4)))
self.controller_name = self.deploy_params['controllerName']
self.controller_user = self.deploy_params['controllerAdminUsername']
self.deploy_id = 'av' + \
datetime.utcnow().strftime('%m%d%H%M%S') + choice(ascii_lowercase)
if not self.resource_group:
self.resource_group = self.deploy_id + '-rg'
def create_resource_group(self):
"""Creates the Azure resource group for this deployment."""
@ -88,32 +69,17 @@ class AvereTemplateDeploy:
logging.debug('> Deleting resource group: ' + self.resource_group)
return self.rm_client.resource_groups.delete(self.resource_group)
def deploy(self, add_secrets_params=True):
def deploy(self):
"""Deploys the Avere vFXT template."""
logging.debug('> Deploying template')
params = {**self.deploy_params}
if add_secrets_params:
with open(self.ssh_pub_key_path, 'r') as ssh_pub_key_file:
ssh_pub_key = ssh_pub_key_file.read()
deploy_secrets = {
'adminPassword': os.environ['AVERE_ADMIN_PW'],
'controllerPassword': os.environ['AVERE_CONTROLLER_PW'],
'servicePrincipalAppId': os.environ['AZURE_CLIENT_ID'],
'servicePrincipalPassword': os.environ['AZURE_CLIENT_SECRET'],
'servicePrincipalTenant': os.environ['AZURE_TENANT_ID'],
'controllerSSHKeyData': ssh_pub_key
}
params = {**deploy_secrets, **params}
return self.rm_client.deployments.create_or_update(
resource_group_name=self.resource_group,
deployment_name='avere_template_deploy',
deployment_name=self.deploy_name,
properties={
'mode': DeploymentMode.incremental,
'parameters': {k: {'value': v} for k, v in params.items()},
'template_link': self.template_link
'parameters':
{k: {'value': v} for k, v in self.deploy_params.items()},
'template': self.template
}
)

Просмотреть файл

@ -4,9 +4,12 @@
Driver for testing template-based deployment of the Avere vFXT product.
"""
import json
import logging
import os
from time import time
import requests
import paramiko
import pytest
from scp import SCPClient
@ -18,18 +21,44 @@ from avere_template_deploy import AvereTemplateDeploy
class TestDeployment:
def test_deploy_template(self, group_vars):
atd = group_vars['atd']
atd.template = requests.get(
url='https://raw.githubusercontent.com/' +
'Azure/Avere/master/src/vfxt/azuredeploy-auto.json').json()
with open(os.path.expanduser(r'~/.ssh/id_rsa.pub'), 'r') as ssh_pub_f:
ssh_pub_key = ssh_pub_f.read()
atd.deploy_params = {
'virtualNetworkResourceGroup': atd.resource_group,
'virtualNetworkName': atd.deploy_id + '-vnet',
'virtualNetworkSubnetName': atd.deploy_id + '-subnet',
'avereBackedStorageAccountName': atd.deploy_id + 'sa',
'controllerName': atd.deploy_id + '-con',
'controllerAdminUsername': 'azureuser',
'controllerAuthenticationType': 'sshPublicKey',
'controllerSSHKeyData': ssh_pub_key,
'adminPassword': os.environ['AVERE_ADMIN_PW'],
'controllerPassword': os.environ['AVERE_CONTROLLER_PW'],
'servicePrincipalAppId': os.environ['AZURE_CLIENT_ID'],
'servicePrincipalPassword': os.environ['AZURE_CLIENT_SECRET'],
'servicePrincipalTenant': os.environ['AZURE_TENANT_ID']
}
group_vars['controller_name'] = atd.deploy_params['controllerName']
group_vars['controller_user'] = atd.deploy_params['controllerAdminUsername']
logging.debug('> Generated deploy parameters: \n{}'.format(
json.dumps(atd.deploy_params, indent=4)))
group_vars['deploy_result'] = wait_for_op(atd.deploy())
def test_get_vfxt_log(self, group_vars):
atd = group_vars['atd']
logging.info('> Getting vfxt.log from controller: {}'.format(
atd.controller_name))
group_vars['controller_name']))
controller_ip = atd.nm_client.public_ip_addresses.get(
atd.resource_group,
'publicip-' + atd.controller_name).ip_address
ssh = create_ssh_client(atd.controller_user, controller_ip)
'publicip-' + group_vars['controller_name']).ip_address
ssh = create_ssh_client(group_vars['controller_user'], controller_ip)
scp = SCPClient(ssh.get_transport())
scp.get(r'~/vfxt.log', r'./vfxt.' + atd.controller_name + '.log')
scp.get(r'~/vfxt.log',
r'./vfxt.' + group_vars['controller_name'] + '.log')
# FIXTURES ####################################################################
@ -68,11 +97,11 @@ def wait_for_op(op, timeout_sec=60):
def create_ssh_client(user, host, port=22):
"""Creates (and returns) an SSHClient. Auth'n is via publickey."""
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, port, user)
return client
ssh_client = paramiko.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(host, port, user)
return ssh_client
if __name__ == '__main__':