# Copyright 2016 Rackspace
#
# All Rights Reserved.
#
# 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 os
import subprocess
import tempfile

from tempest.cmd import subunit_describe_calls
from tempest.tests import base


class TestSubunitDescribeCalls(base.TestCase):
    def test_return_code(self):
        subunit_file = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            'sample_streams/calls.subunit')
        p = subprocess.Popen([
            'subunit-describe-calls', '-s', subunit_file,
            '-o', tempfile.mkstemp()[1]], stdin=subprocess.PIPE)
        p.communicate()
        self.assertEqual(0, p.returncode)

    def test_parse(self):
        subunit_file = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            'sample_streams/calls.subunit')
        parser = subunit_describe_calls.parse(
            subunit_file, "pythonlogging", None)
        expected_result = {
            'bar': [{'name': 'AgentsAdminTestJSON:setUp',
                     'service': 'Nova',
                     'status_code': '200',
                     'url': 'v2.1/<id>/os-agents',
                     'verb': 'POST'},
                    {'name': 'AgentsAdminTestJSON:test_create_agent',
                     'service': 'Nova',
                     'status_code': '200',
                     'url': 'v2.1/<id>/os-agents',
                     'verb': 'POST'},
                    {'name': 'AgentsAdminTestJSON:tearDown',
                     'service': 'Nova',
                     'status_code': '200',
                     'url': 'v2.1/<id>/os-agents/1',
                     'verb': 'DELETE'},
                    {'name': 'AgentsAdminTestJSON:_run_cleanups',
                     'service': 'Nova',
                     'status_code': '200',
                     'url': 'v2.1/<id>/os-agents/2',
                     'verb': 'DELETE'}],
            'foo': [{'name': 'AgentsAdminTestJSON:setUp',
                     'service': 'Nova',
                     'status_code': '200',
                     'url': 'v2.1/<id>/os-agents',
                     'verb': 'POST'},
                    {'name': 'AgentsAdminTestJSON:test_delete_agent',
                     'service': 'Nova',
                     'status_code': '200',
                     'url': 'v2.1/<id>/os-agents/3',
                     'verb': 'DELETE'},
                    {'name': 'AgentsAdminTestJSON:test_delete_agent',
                     'service': 'Nova',
                     'status_code': '200',
                     'url': 'v2.1/<id>/os-agents',
                     'verb': 'GET'},
                    {'name': 'AgentsAdminTestJSON:tearDown',
                     'service': 'Nova',
                     'status_code': '404',
                     'url': 'v2.1/<id>/os-agents/3',
                     'verb': 'DELETE'}]}
        self.assertEqual(expected_result, parser.test_logs)
