#    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 json
import os
import requests
import subprocess
import sys
import tempfile
import time
import yaml
from oslo_utils import timeutils
from heat_integrationtests.common import exceptions
from heat_integrationtests.functional import functional_base
[docs]class ParallelDeploymentsTest(functional_base.FunctionalTestsBase):
    server_template = '''
heat_template_version: "2013-05-23"
parameters:
  flavor:
    type: string
  image:
    type: string
  network:
    type: string
resources:
  server:
    type: OS::Nova::Server
    properties:
      image: {get_param: image}
      flavor: {get_param: flavor}
      user_data_format: SOFTWARE_CONFIG
      networks: [{network: {get_param: network}}]
outputs:
  server:
    value: {get_resource: server}
'''
    config_template = '''
heat_template_version: "2013-05-23"
parameters:
  server:
    type: string
resources:
  config:
    type: OS::Heat::SoftwareConfig
    properties:
'''
    deployment_snippet = '''
type: OS::Heat::SoftwareDeployments
properties:
  config: {get_resource: config}
  servers: {'0': {get_param: server}}
'''
    enable_cleanup = True
[docs]    def deploy_many_configs(self, stack, server, config_stacks,
                            stack_count, deploys_per_stack,
                            deploy_count_start):
        for a in range(stack_count):
            config_stacks.append(
                self.deploy_config(server, deploys_per_stack))
        new_count = deploy_count_start + stack_count * deploys_per_stack
        self.wait_for_deploy_metadata_set(stack, new_count)
        return new_count 
[docs]    def deploy_config(self, server, deploy_count):
        parms = {'server': server}
        template = yaml.safe_load(self.config_template)
        resources = template['resources']
        resources['config']['properties'] = {'config': 'x' * 10000}
        for a in range(deploy_count):
            resources['dep_%s' % a] = yaml.safe_load(self.deployment_snippet)
        return self.stack_create(
            parameters=parms,
            template=template,
            enable_cleanup=self.enable_cleanup,
            expected_status=None) 
[docs]    def signal_deployments(self, stack_identifier):
        server_metadata = self.client.resources.metadata(
            stack_identifier, 'server')
        for dep in server_metadata['deployments']:
            iv = dict((i['name'], i['value']) for i in dep['inputs'])
            sigurl = iv.get('deploy_signal_id')
            requests.post(sigurl, data='{}',
                          headers={'content-type': 'application/json'},
                          verify=self.verify_cert)  
[docs]class ZaqarSignalTransportTest(functional_base.FunctionalTestsBase):
    server_template = '''
heat_template_version: "2013-05-23"
parameters:
  flavor:
    type: string
  image:
    type: string
  network:
    type: string
resources:
  server:
    type: OS::Nova::Server
    properties:
      image: {get_param: image}
      flavor: {get_param: flavor}
      user_data_format: SOFTWARE_CONFIG
      software_config_transport: ZAQAR_MESSAGE
      networks: [{network: {get_param: network}}]
  config:
    type: OS::Heat::SoftwareConfig
    properties:
      config: echo 'foo'
  deployment:
    type: OS::Heat::SoftwareDeployment
    properties:
      config: {get_resource: config}
      server: {get_resource: server}
      signal_transport: ZAQAR_SIGNAL
outputs:
  data:
    value: {get_attr: [deployment, deploy_stdout]}
'''
    conf_template = '''
[zaqar]
user_id = %(user_id)s
password = %(password)s
project_id = %(project_id)s
auth_url = %(auth_url)s
queue_id = %(queue_id)s
    '''
[docs]    def test_signal_queues(self):
        parms = {'flavor': self.conf.minimal_instance_type,
                 'network': self.conf.fixed_network_name,
                 'image': self.conf.minimal_image_ref}
        stack_identifier = self.stack_create(
            parameters=parms,
            template=self.server_template,
            expected_status=None)
        metadata = self.wait_for_deploy_metadata_set(stack_identifier)
        config = metadata['os-collect-config']['zaqar']
        conf_content = self.conf_template % config
        fd, temp_path = tempfile.mkstemp()
        os.write(fd, conf_content.encode('utf-8'))
        os.close(fd)
        cmd = ['os-collect-config', '--one-time',
               '--config-file=%s' % temp_path, 'zaqar']
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        stdout_value = proc.communicate()[0]
        data = json.loads(stdout_value.decode('utf-8'))
        self.assertEqual(config, data['zaqar']['os-collect-config']['zaqar'])
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        stdout_value = proc.communicate()[0]
        data = json.loads(stdout_value.decode('utf-8'))
        fd, temp_path = tempfile.mkstemp()
        os.write(fd,
                 json.dumps(data['zaqar']['deployments'][0]).encode('utf-8'))
        os.close(fd)
        cmd = [sys.executable, self.conf.heat_config_notify_script, temp_path]
        proc = subprocess.Popen(cmd,
                                stderr=subprocess.PIPE,
                                stdin=subprocess.PIPE)
        proc.communicate(
            json.dumps({'deploy_stdout': 'here!'}).encode('utf-8'))
        self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE')
        stack = self.client.stacks.get(stack_identifier)
        self.assertEqual('here!', stack.outputs[0]['output_value'])