<commit_before># Copyright 2016 Google Inc.
#
# 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.

from builtins import str

import copy
import io
import pprint
import os
import yaml

from mobly import keys
from mobly import utils

# An environment variable defining the base location for Mobly logs.
ENV_MOBLY_LOGPATH = 'MOBLY_LOGPATH'
_DEFAULT_LOG_PATH = '/tmp/logs/mobly/'

class MoblyConfigError(Exception):
    """Raised when there is a problem in test configuration file."""

def _validate_test_config(test_config):
    """Validates the raw configuration loaded from the config file.

    Making sure the required key 'TestBeds' is present.
    """
    required_key = keys.Config.key_testbed.value
    if required_key not in test_config:
        raise MoblyConfigError('Required key %s missing in test config.' %
                               required_key)

def _validate_testbed_name(name):
    """Validates the name of a test bed.

    Since test bed names are used as part of the test run id, it needs to meet
    certain requirements.

    Args:
        name: The test bed's name specified in config file.

    Raises:
        MoblyConfigError: The name does not meet any criteria.
    """
    if not name:
        raise MoblyConfigError("Test bed names can't be empty.")
    name = str(name)
    for char in name:
        if char not in utils.valid_filename_chars:
            raise MoblyConfigError(
                'Char "%s" is not allowed in test bed names.' % char)

def _validate_testbed_configs(testbed_configs):
    """Validates the testbed configurations.

    Args:
        testbed_configs: A list of testbed configuration dicts.

    Raises:
        MoblyConfigError: Some parts of the configuration is invalid.
    """
    seen_names = set()
    # Cross checks testbed configs for resource conflicts.
    for config in testbed_configs:
        # Check for conflicts between multiple concurrent testbed configs.
        # No need to call it if there's only one testbed config.
        name = config[keys.Config.key_testbed_name.value]
        _validate_testbed_name(name)
        # Test bed names should be unique.
        if name in seen_names:
            raise MoblyConfigError('Duplicate testbed name %s found.' % name)
        seen_names.add(name)

def load_test_config_file(test_config_path, tb_filters=None):
    """Processes the test configuration file provied by user.

    Loads the configuration file into a dict, unpacks each testbed
    config into its own dict, and validate the configuration in the
    process.

    Args:
        test_config_path: Path to the test configuration file.
        tb_filters: A subset of test bed names to be pulled from the config
            file. If None, then all test beds will be selected.

    Returns:
        A list of test configuration dicts to be passed to
        test_runner.TestRunner.
    """
    configs = _load_config_file(test_config_path)
    if tb_filters:
        tbs = []
        for tb in configs[keys.Config.key_testbed.value]:
            if tb[keys.Config.key_testbed_name.value] in tb_filters:
                tbs.append(tb)
        if len(tbs) != len(tb_filters):
            raise MoblyConfigError(
                'Expect to find %d test bed configs, found %d. Check if'
                ' you have the correct test bed names.' %
                (len(tb_filters), len(tbs)))
        configs[keys.Config.key_testbed.value] = tbs
    mobly_params = configs.get(keys.Config.key_mobly_params.value, {})
    # Decide log path.
    log_path = mobly_params.get(keys.Config.key_log_path.value,
                                _DEFAULT_LOG_PATH)
    if ENV_MOBLY_LOGPATH in os.environ:
        log_path = os.environ[ENV_MOBLY_LOGPATH]
    log_path = utils.abs_path(log_path)
    # Validate configs
    _validate_test_config(configs)
    _validate_testbed_configs(configs[keys.Config.key_testbed.value])
    # Transform config dict from user-facing key mapping to internal config object.
    test_configs = []
    for original_bed_config in configs[keys.Config.key_testbed.value]:
        test_run_config = TestRunConfig()
        test_run_config.testbed_name = original_bed_config[
            keys.Config.key_testbed_name.value]
        # Deprecated, use testbed_name
        test_run_config.test_bed_name = test_run_config.testbed_name
        test_run_config.log_path = log_path
        test_run_config.controller_configs = original_bed_config.get(
            keys.Config.key_testbed_controllers.value, {})
        test_run_config.user_params = original_bed_config.get(
            keys.Config.key_testbed_test_params.value, {})
        test_configs.append(test_run_config)
    return test_configs

def _load_config_file(path):
    """Loads a test config file.

    The test config file has to be in YAML format.

    Args:
        path: A string that is the full path to the config file, including the
            file name.

    Returns:
        A dict that represents info in the config file.
    """
    with io.open(utils.abs_path(path), 'r', encoding='utf-8') as f:
        conf = yaml.safe_load(f)
        return conf

class TestRunConfig(object):
    """The data class that holds all the information needed for a test run.

    Attributes:
        log_path: string, specifies the root directory for all logs written by
            a test run.
        test_bed_name: [Deprecated, use 'testbed_name' instead]
            string, the name of the test bed used by a test run.
        testbed_name: string, the name of the test bed used by a test run.
        controller_configs: dict, configs used for instantiating controller
            objects.
        user_params: dict, all the parameters to be consumed by the test logic.
        summary_writer: records.TestSummaryWriter, used to write elements to
            the test result summary file.
        test_class_name_suffix: string, suffix to append to the class name for
                reporting. This is used for differentiating the same class
                executed with different parameters in a suite.
    """

    def __init__(self):
        self.log_path = None
        # Deprecated, use 'testbed_name'
        self.test_bed_name = None
        self.testbed_name = None
        self.controller_configs = {}
        self.user_params = {}
        self.summary_writer = None
        self.test_class_name_suffix = None

    def copy(self):
        """Returns a deep copy of the current config.
        """
        return copy.deepcopy(self)

    def __str__(self):
        content = dict(self.__dict__)
        content.pop('summary_writer')
        return pprint.pformat(content)
<commit_msg>Use an empty string as the default `TestRunConfig.log_path` (#689)<commit_after># Copyright 2016 Google Inc.
#
# 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.

from builtins import str

import copy
import io
import pprint
import os
import yaml

from mobly import keys
from mobly import utils

# An environment variable defining the base location for Mobly logs.
ENV_MOBLY_LOGPATH = 'MOBLY_LOGPATH'
_DEFAULT_LOG_PATH = '/tmp/logs/mobly/'

class MoblyConfigError(Exception):
    """Raised when there is a problem in test configuration file."""

def _validate_test_config(test_config):
    """Validates the raw configuration loaded from the config file.

    Making sure the required key 'TestBeds' is present.
    """
    required_key = keys.Config.key_testbed.value
    if required_key not in test_config:
        raise MoblyConfigError('Required key %s missing in test config.' %
                               required_key)

def _validate_testbed_name(name):
    """Validates the name of a test bed.

    Since test bed names are used as part of the test run id, it needs to meet
    certain requirements.

    Args:
        name: The test bed's name specified in config file.

    Raises:
        MoblyConfigError: The name does not meet any criteria.
    """
    if not name:
        raise MoblyConfigError("Test bed names can't be empty.")
    name = str(name)
    for char in name:
        if char not in utils.valid_filename_chars:
            raise MoblyConfigError(
                'Char "%s" is not allowed in test bed names.' % char)

def _validate_testbed_configs(testbed_configs):
    """Validates the testbed configurations.

    Args:
        testbed_configs: A list of testbed configuration dicts.

    Raises:
        MoblyConfigError: Some parts of the configuration is invalid.
    """
    seen_names = set()
    # Cross checks testbed configs for resource conflicts.
    for config in testbed_configs:
        # Check for conflicts between multiple concurrent testbed configs.
        # No need to call it if there's only one testbed config.
        name = config[keys.Config.key_testbed_name.value]
        _validate_testbed_name(name)
        # Test bed names should be unique.
        if name in seen_names:
            raise MoblyConfigError('Duplicate testbed name %s found.' % name)
        seen_names.add(name)

def load_test_config_file(test_config_path, tb_filters=None):
    """Processes the test configuration file provied by user.

    Loads the configuration file into a dict, unpacks each testbed
    config into its own dict, and validate the configuration in the
    process.

    Args:
        test_config_path: Path to the test configuration file.
        tb_filters: A subset of test bed names to be pulled from the config
            file. If None, then all test beds will be selected.

    Returns:
        A list of test configuration dicts to be passed to
        test_runner.TestRunner.
    """
    configs = _load_config_file(test_config_path)
    if tb_filters:
        tbs = []
        for tb in configs[keys.Config.key_testbed.value]:
            if tb[keys.Config.key_testbed_name.value] in tb_filters:
                tbs.append(tb)
        if len(tbs) != len(tb_filters):
            raise MoblyConfigError(
                'Expect to find %d test bed configs, found %d. Check if'
                ' you have the correct test bed names.' %
                (len(tb_filters), len(tbs)))
        configs[keys.Config.key_testbed.value] = tbs
    mobly_params = configs.get(keys.Config.key_mobly_params.value, {})
    # Decide log path.
    log_path = mobly_params.get(keys.Config.key_log_path.value,
                                _DEFAULT_LOG_PATH)
    if ENV_MOBLY_LOGPATH in os.environ:
        log_path = os.environ[ENV_MOBLY_LOGPATH]
    log_path = utils.abs_path(log_path)
    # Validate configs
    _validate_test_config(configs)
    _validate_testbed_configs(configs[keys.Config.key_testbed.value])
    # Transform config dict from user-facing key mapping to internal config object.
    test_configs = []
    for original_bed_config in configs[keys.Config.key_testbed.value]:
        test_run_config = TestRunConfig()
        test_run_config.testbed_name = original_bed_config[
            keys.Config.key_testbed_name.value]
        # Deprecated, use testbed_name
        test_run_config.test_bed_name = test_run_config.testbed_name
        test_run_config.log_path = log_path
        test_run_config.controller_configs = original_bed_config.get(
            keys.Config.key_testbed_controllers.value, {})
        test_run_config.user_params = original_bed_config.get(
            keys.Config.key_testbed_test_params.value, {})
        test_configs.append(test_run_config)
    return test_configs

def _load_config_file(path):
    """Loads a test config file.

    The test config file has to be in YAML format.

    Args:
        path: A string that is the full path to the config file, including the
            file name.

    Returns:
        A dict that represents info in the config file.
    """
    with io.open(utils.abs_path(path), 'r', encoding='utf-8') as f:
        conf = yaml.safe_load(f)
        return conf

class TestRunConfig(object):
    """The data class that holds all the information needed for a test run.

    Attributes:
        log_path: string, specifies the root directory for all logs written by
            a test run.
        test_bed_name: [Deprecated, use 'testbed_name' instead]
            string, the name of the test bed used by a test run.
        testbed_name: string, the name of the test bed used by a test run.
        controller_configs: dict, configs used for instantiating controller
            objects.
        user_params: dict, all the parameters to be consumed by the test logic.
        summary_writer: records.TestSummaryWriter, used to write elements to
            the test result summary file.
        test_class_name_suffix: string, suffix to append to the class name for
                reporting. This is used for differentiating the same class
                executed with different parameters in a suite.
    """

    def __init__(self):
        # Init value is an empty string to avoid string joining errors.
        self.log_path = ''
        # Deprecated, use 'testbed_name'
        self.test_bed_name = None
        self.testbed_name = None
        self.controller_configs = {}
        self.user_params = {}
        self.summary_writer = None
        self.test_class_name_suffix = None

    def copy(self):
        """Returns a deep copy of the current config.
        """
        return copy.deepcopy(self)

    def __str__(self):
        content = dict(self.__dict__)
        content.pop('summary_writer')
        return pprint.pformat(content)
<|endoftext|>


<commit_before>import unittest
import mock

from record import record, RECORD_FN_NAME

class TestRecord(unittest.TestCase):

    # Patch path refers to current module because the decorator injects the
    # record fn in here.
    record_state_fn_path = 'test_record.%s' % RECORD_FN_NAME
    dump_state_fn_path = 'record.dump_recorded_state'

    def setUp(self):
        self.dump_patcher = mock.patch(self.dump_state_fn_path)
        self.dump_mock = self.dump_patcher.start()

    def tearDown(self):
        if self.dump_patcher:
            self.dump_patcher.stop()

    def test_simple(self):
        """Simple function, no loop, no return, no conditional."""

        @record
        def foo():
            x = 5
            y = 6

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_conditional(self):
        """Fn with a simple conditional."""

        @record
        def foo():
            x = 3
            y = 2
            if x == 3:
                y = 5

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_conditional_else(self):
        """Fn with conditional having else."""

        @record
        def foo():
            x = 3
            y = 2
            if x != 3:
                y = 5
            else:
                y = 6

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        # Note: `else` does not have a lineno, using `if`'s lineno.
        self._check_record_calls(record_mock, [3, 4, 5, 8])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_while(self):
        """Fn with a while."""

        @record
        def foo():
            x = 3
            y = 6

            while x < y:
                x += 1

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 6, 7, 6, 7, 6, 7, 6])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_for(self):
        """Fn with a for."""

        @record
        def foo():
            x = 3
            s = 0
            for i in range(x):
                s = s + i

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6, 5, 6, 5, 6, 5])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_nested_if_in_for(self):
        """Fn with a for containing an if."""

        @record
        def foo():
            x = 3
            s = 0
            for i in range(x):
                if s > -1:
                    s += i

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_recursive_function(self):
        """Fn with a recursive call."""

        @record
        def foo(x):
            if x == 0:
                return 1
            return 1 + foo(x - 1)

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo(2)

        # Recursive calls are expanded/eval'd first, that's why 3, 3, 3.
        self._check_record_calls(record_mock, [3, 3, 3, 4, 5, 5])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_return_wrapping(self):
        """Fn with return has return value captured."""

        @record
        def foo():
            x = 3
            return x

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def _check_record_calls(self, record_mock, expected_linenos):
        self.assertEqual(record_mock.call_count, len(expected_linenos),
                         "Wrong number of calls to record.")
        for i, lineno in enumerate(expected_linenos):
            self.assertEqual(record_mock.call_args_list[i][0][0], lineno,
                             "Record was called with the wrong lineno.")
<commit_msg>Test for with else.<commit_after>import unittest
import mock

from record import record, RECORD_FN_NAME

class TestRecord(unittest.TestCase):

    # Patch path refers to current module because the decorator injects the
    # record fn in here.
    record_state_fn_path = 'test_record.%s' % RECORD_FN_NAME
    dump_state_fn_path = 'record.dump_recorded_state'

    def setUp(self):
        self.dump_patcher = mock.patch(self.dump_state_fn_path)
        self.dump_mock = self.dump_patcher.start()

    def tearDown(self):
        if self.dump_patcher:
            self.dump_patcher.stop()

    def test_simple(self):
        """Simple function, no loop, no return, no conditional."""

        @record
        def foo():
            x = 5
            y = 6

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_conditional(self):
        """Fn with a simple conditional."""

        @record
        def foo():
            x = 3
            y = 2
            if x == 3:
                y = 5

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_conditional_else(self):
        """Fn with conditional having else."""

        @record
        def foo():
            x = 3
            y = 2
            if x != 3:
                y = 5
            else:
                y = 6

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        # Note: `else` does not have a lineno, using `if`'s lineno.
        self._check_record_calls(record_mock, [3, 4, 5, 8])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_while(self):
        """Fn with a while."""

        @record
        def foo():
            x = 3
            y = 6

            while x < y:
                x += 1

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 6, 7, 6, 7, 6, 7, 6])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_for(self):
        """Fn with a for."""

        @record
        def foo():
            x = 3
            s = 0
            for i in range(x):
                s = s + i

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6, 5, 6, 5, 6, 5])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_for_else(self):
        """Fn with a for+else."""

        @record
        def foo():
            x = 3
            s = 0
            for i in range(x):
                s = s + i
            else:
                ok = 1

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6, 5, 6, 5, 6, 5, 8])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_nested_if_in_for(self):
        """Fn with a for containing an if."""

        @record
        def foo():
            x = 3
            s = 0
            for i in range(x):
                if s > -1:
                    s += i

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_recursive_function(self):
        """Fn with a recursive call."""

        @record
        def foo(x):
            if x == 0:
                return 1
            return 1 + foo(x - 1)

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo(2)

        # Recursive calls are expanded/eval'd first, that's why 3, 3, 3.
        self._check_record_calls(record_mock, [3, 3, 3, 4, 5, 5])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def test_return_wrapping(self):
        """Fn with return has return value captured."""

        @record
        def foo():
            x = 3
            return x

        with mock.patch(self.record_state_fn_path) as record_mock:
            foo()

        self._check_record_calls(record_mock, [3, 4])
        self.assertEqual(self.dump_mock.call_count, 1, "Too many calls to dump fn.")

    def _check_record_calls(self, record_mock, expected_linenos):
        self.assertEqual(record_mock.call_count, len(expected_linenos),
                         "Wrong number of calls to record.")
        for i, lineno in enumerate(expected_linenos):
            self.assertEqual(record_mock.call_args_list[i][0][0], lineno,
                             "Record was called with the wrong lineno.")
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
from unittest import TestCase
from unittest.mock import MagicMock, patch

import pexpect

import logger.auto
import logger.params
from logger import remote, watch

MNT_USB = "/tmp"

class TestRemoteLogger(TestCase):

    @classmethod
    def setUpClass(cls):
        local_dist_dir = os.path.join(os.getcwd(), "dist")
        if not os.path.exists(local_dist_dir):
            os.mkdir(local_dist_dir)

    @classmethod
    def tearDownClass(cls):
        local_dist_dir = os.path.join(os.getcwd(), "dist")
        if os.path.exists(local_dist_dir):
            os.rmdir(local_dist_dir)

    def test_get_log(self):
        p = logger.params.LogParam()
        p.host_name = 'root@172.30.10.2'
        p.shell = 'ssh'
        p.log_cmd = 'log_to_rom'
        p.log_extension = 'tar.gz'
        p.remote_log_dir = '/root'
        p.remote_dist_dir = '/mnt/log'
        p.local_src_dir = '.'
        p.local_dist_dir = os.path.join(os.getcwd(), "dist")

        ret = remote.RemoteLogger(p).get_log()

        self.assertTrue(os.path.exists(ret[0]))
        os.remove(ret[0])

    @patch.object(pexpect, 'spawn', MagicMock(return_value=MagicMock))
    def test_get_log__timeout(self):
        params = logger.params.LogParam()
        params.log_extension = "tar.gz"
        remote_logger = remote.RemoteLogger(params)
        remote.RemoteLogger.TIMEOUT_LOGGING = 0.5

        p = pexpect.spawn()
        p.expect = MagicMock()
        p.sendline = MagicMock()
        p.match = MagicMock()
        p.terminate = MagicMock()
        p.before = None
        p.after = None
        array = [MagicMock()]
        p.match.groups = MagicMock(return_value=array)
        sentinel = "__tmp__.%s" % params.log_extension
        array[0].decode = MagicMock(return_value=sentinel)

        ret = remote_logger.get_log()
        self.assertEqual(ret, None)

    def test_get_log__usb(self):
        p = logger.params.LogParam()
        p.host_name = 'root@172.30.10.2'
        p.shell = 'ssh'
        p.log_cmd = 'log_to_rom'
        p.log_extension = 'tar.gz'
        p.remote_log_dir = '/root'
        p.usb_dir = '/mnt/USB0'

        ret = remote.RemoteLogger(p).get_log(to_usb=True)

        df = os.path.join(MNT_USB, os.path.basename(ret[0]))
        self.assertTrue(os.path.exists(df))

    def test_get_log__usb_not_exists(self):
        p = logger.params.LogParam()
        p.host_name = 'root@172.30.10.2'
        p.shell = 'ssh'
        p.log_cmd = 'log_to_rom'
        p.log_extension = 'tar.gz'
        p.remote_log_dir = '/root'
        p.usb_dir = '/mnt/USB1'

        ret = remote.RemoteLogger(p).get_log(to_usb=True)

        self.assertIsNone(ret)

    @patch.object(watch, 'file', MagicMock(return_value=True))
    def test_move_log(self):
        os.chdir("tests")
        p = logger.params.LogParam()
        p.read_ini()
        os.chdir("..")
        p.remote_log_dir = "/mnt/log"
        p.local_src_dir = os.getcwd()
        p.local_dist_dir = os.path.join(os.getcwd(), "dist")

        # create src file, and dist directory
        filename = "testdata"
        f = open(os.path.join(os.getcwd(), filename), "w")
        f.close()

        # exec
        ret = remote.RemoteLogger(p).move_log(filename)

        for f in ret:
            self.assertTrue(os.path.exists(f))
            os.remove(f)

    @patch.object(watch, 'file', MagicMock(return_value=True))
    def test_move_log__glob(self):
        os.chdir("tests")
        p = logger.params.LogParam()
        p.read_ini()
        os.chdir("..")
        p.remote_log_dir = "/mnt/log"
        p.local_src_dir = os.getcwd()
        p.local_dist_dir = os.path.join(os.getcwd(), "dist")

        # create src file, and dist directory
        filename = "testdata"
        f = open(os.path.join(os.getcwd(), filename + '1'), "w")
        f.close()
        f = open(os.path.join(os.getcwd(), filename + '2'), "w")
        f.close()

        # exec
        ret = remote.RemoteLogger(p).move_log(filename + '*')
        self.assertTrue(len(ret) == 2)
        self.assertTrue(ret[0])
        self.assertTrue(ret[1])
        os.remove(ret[0])
        os.remove(ret[1])

    def test_move_log__usb(self):
        os.chdir("tests")
        p = logger.params.LogParam()
        p.read_ini()
        os.chdir("..")
        p.remote_log_dir = "/mnt/log"

        # exec
        filename = "testfile_test_move_log__usb"
        fd = open(filename, "w")
        fd.close()
        remote.RemoteLogger(p).move_log(filename, to_usb=True)

        df = os.path.join(MNT_USB, filename)
        self.assertTrue(os.path.exists(df))

    def test_move_log__not_found(self):
        os.chdir("tests")
        p = logger.params.LogParam()
        p.read_ini()
        os.chdir("..")
        p.remote_log_dir = "/mnt/log"
        p.local_src_dir = os.getcwd()
        p.local_dist_dir = os.path.join(os.getcwd(), "dist")
        remote.RemoteLogger.TIMEOUT_MOVE = 1

        filename = "testdata"
        r = remote.RemoteLogger(p)
        ret = r.move_log(filename)

        self.assertEqual(ret, None)

    @patch.object(watch, 'file', MagicMock(return_value=False))
    def test_move_log__move_failed(self):
        p = logger.params.LogParam()
        p.read_ini()

        r = remote.RemoteLogger(p)
        r._RemoteLogger__connect = MagicMock()
        r._RemoteLogger__disconnect = MagicMock()
        r._RemoteLogger__get_file_list = MagicMock(return_value=[None, 'test'])
        r._RemoteLogger__send = MagicMock()

        filename = "testdata"
        ret = r.move_log(filename)

        self.assertEqual(ret, None)

    def test_list_log(self):
        files = ["1.tar.gz", "2.tar.gz"]
        for f in files:
            with open(f, "w") as fd:
                fd.write("")

        os.chdir("./tests")
        p = logger.params.LogParam()
        p.read_ini()
        p.remote_log_dir = "/mnt/log"
        remote_logger = remote.RemoteLogger(p)
        ls = remote_logger.list_log()
        self.assertEqual(ls, files)

        os.chdir("..")
        for f in files:
            os.remove(f)

    def test_clear_log(self):
        files = ["3.tar.gz", "4.tar.gz"]
        for f in files:
            with open(f, "w") as fd:
                fd.write("")

        os.chdir("./tests")
        p = logger.params.LogParam()
        p.read_ini()
        p.remote_log_dir = "/mnt/log"

        remote.RemoteLogger(p).clear_log()

        os.chdir("..")
        for f in files:
            self.assertFalse(os.path.exists(f))
<commit_msg>[clean] Move local_dist_dir to class variable.<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
from unittest import TestCase
from unittest.mock import MagicMock, patch

import pexpect

import logger.auto
import logger.params
from logger import remote, watch

MNT_USB = "/tmp"

class TestRemoteLogger(TestCase):

    local_dist_dir = os.path.join(os.getcwd(), "dist")

    @classmethod
    def setUpClass(cls):
        if not os.path.exists(cls.local_dist_dir):
            os.mkdir(cls.local_dist_dir)

    @classmethod
    def tearDownClass(cls):
        if os.path.exists(cls.local_dist_dir):
            os.rmdir(cls.local_dist_dir)

    def test_get_log(self):
        p = logger.params.LogParam()
        p.host_name = 'root@172.30.10.2'
        p.shell = 'ssh'
        p.log_cmd = 'log_to_rom'
        p.log_extension = 'tar.gz'
        p.remote_log_dir = '/root'
        p.remote_dist_dir = '/mnt/log'
        p.local_src_dir = '.'
        p.local_dist_dir = TestRemoteLogger.local_dist_dir

        ret = remote.RemoteLogger(p).get_log()

        self.assertTrue(os.path.exists(ret[0]))
        os.remove(ret[0])

    @patch.object(pexpect, 'spawn', MagicMock(return_value=MagicMock))
    def test_get_log__timeout(self):
        params = logger.params.LogParam()
        params.log_extension = "tar.gz"
        remote_logger = remote.RemoteLogger(params)
        remote.RemoteLogger.TIMEOUT_LOGGING = 0.5

        p = pexpect.spawn()
        p.expect = MagicMock()
        p.sendline = MagicMock()
        p.match = MagicMock()
        p.terminate = MagicMock()
        p.before = None
        p.after = None
        array = [MagicMock()]
        p.match.groups = MagicMock(return_value=array)
        sentinel = "__tmp__.%s" % params.log_extension
        array[0].decode = MagicMock(return_value=sentinel)

        ret = remote_logger.get_log()
        self.assertEqual(ret, None)

    def test_get_log__usb(self):
        p = logger.params.LogParam()
        p.host_name = 'root@172.30.10.2'
        p.shell = 'ssh'
        p.log_cmd = 'log_to_rom'
        p.log_extension = 'tar.gz'
        p.remote_log_dir = '/root'
        p.usb_dir = '/mnt/USB0'

        ret = remote.RemoteLogger(p).get_log(to_usb=True)

        df = os.path.join(MNT_USB, os.path.basename(ret[0]))
        self.assertTrue(os.path.exists(df))

    def test_get_log__usb_not_exists(self):
        p = logger.params.LogParam()
        p.host_name = 'root@172.30.10.2'
        p.shell = 'ssh'
        p.log_cmd = 'log_to_rom'
        p.log_extension = 'tar.gz'
        p.remote_log_dir = '/root'
        p.usb_dir = '/mnt/USB1'

        ret = remote.RemoteLogger(p).get_log(to_usb=True)

        self.assertIsNone(ret)

    @patch.object(watch, 'file', MagicMock(return_value=True))
    def test_move_log(self):
        os.chdir("tests")
        p = logger.params.LogParam()
        p.read_ini()
        os.chdir("..")
        p.remote_log_dir = "/mnt/log"
        p.local_src_dir = os.getcwd()
        p.local_dist_dir = TestRemoteLogger.local_dist_dir

        # create src file, and dist directory
        filename = "testdata"
        f = open(os.path.join(os.getcwd(), filename), "w")
        f.close()

        # exec
        ret = remote.RemoteLogger(p).move_log(filename)

        for f in ret:
            self.assertTrue(os.path.exists(f))
            os.remove(f)

    @patch.object(watch, 'file', MagicMock(return_value=True))
    def test_move_log__glob(self):
        os.chdir("tests")
        p = logger.params.LogParam()
        p.read_ini()
        os.chdir("..")
        p.remote_log_dir = "/mnt/log"
        p.local_src_dir = os.getcwd()
        p.local_dist_dir = TestRemoteLogger.local_dist_dir

        # create src file, and dist directory
        filename = "testdata"
        f = open(os.path.join(os.getcwd(), filename + '1'), "w")
        f.close()
        f = open(os.path.join(os.getcwd(), filename + '2'), "w")
        f.close()

        # exec
        ret = remote.RemoteLogger(p).move_log(filename + '*')
        self.assertTrue(len(ret) == 2)
        self.assertTrue(ret[0])
        self.assertTrue(ret[1])
        os.remove(ret[0])
        os.remove(ret[1])

    def test_move_log__usb(self):
        os.chdir("tests")
        p = logger.params.LogParam()
        p.read_ini()
        os.chdir("..")
        p.remote_log_dir = "/mnt/log"

        # exec
        filename = "testfile_test_move_log__usb"
        fd = open(filename, "w")
        fd.close()
        remote.RemoteLogger(p).move_log(filename, to_usb=True)

        df = os.path.join(MNT_USB, filename)
        self.assertTrue(os.path.exists(df))

    def test_move_log__not_found(self):
        os.chdir("tests")
        p = logger.params.LogParam()
        p.read_ini()
        os.chdir("..")
        p.remote_log_dir = "/mnt/log"
        p.local_src_dir = os.getcwd()
        p.local_dist_dir = TestRemoteLogger.local_dist_dir
        remote.RemoteLogger.TIMEOUT_MOVE = 1

        filename = "testdata"
        r = remote.RemoteLogger(p)
        ret = r.move_log(filename)

        self.assertEqual(ret, None)

    @patch.object(watch, 'file', MagicMock(return_value=False))
    def test_move_log__move_failed(self):
        p = logger.params.LogParam()
        p.read_ini()

        r = remote.RemoteLogger(p)
        r._RemoteLogger__connect = MagicMock()
        r._RemoteLogger__disconnect = MagicMock()
        r._RemoteLogger__get_file_list = MagicMock(return_value=[None, 'test'])
        r._RemoteLogger__send = MagicMock()

        filename = "testdata"
        ret = r.move_log(filename)

        self.assertEqual(ret, None)

    def test_list_log(self):
        files = ["1.tar.gz", "2.tar.gz"]
        for f in files:
            with open(f, "w") as fd:
                fd.write("")

        os.chdir("./tests")
        p = logger.params.LogParam()
        p.read_ini()
        p.remote_log_dir = "/mnt/log"
        remote_logger = remote.RemoteLogger(p)
        ls = remote_logger.list_log()
        self.assertEqual(ls, files)

        os.chdir("..")
        for f in files:
            os.remove(f)

    def test_clear_log(self):
        files = ["3.tar.gz", "4.tar.gz"]
        for f in files:
            with open(f, "w") as fd:
                fd.write("")

        os.chdir("./tests")
        p = logger.params.LogParam()
        p.read_ini()
        p.remote_log_dir = "/mnt/log"

        remote.RemoteLogger(p).clear_log()

        os.chdir("..")
        for f in files:
            self.assertFalse(os.path.exists(f))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from builtins import str

import json
import pytest
import uuid
from freezegun import freeze_time

import rasa_core
from rasa_core import events, constants
from rasa_core.actions.action import ACTION_LISTEN_NAME
from rasa_core.domain import Domain
from rasa_core.events import (
    UserUttered, BotUttered, SlotSet, Event, ActionExecuted)
from rasa_core.remote import RasaCoreClient
from rasa_core.utils import EndpointConfig

# a couple of event instances that we can use for testing
test_events = [
    Event.from_parameters({"event": UserUttered.type_name,
                           "text": "/goodbye",
                           "parse_data": {
                               "intent": {
                                   "confidence": 1.0, "name": "greet"},
                               "entities": []}
                           }),
    BotUttered("Welcome!", {"test": True}),
    SlotSet("cuisine", 34),
    SlotSet("cuisine", "34"),
    SlotSet("location", None),
    SlotSet("location", [34, "34", None]),
]

@pytest.fixture(scope="module")
def app(core_server):
    return core_server.test_client()

@pytest.fixture(scope="module")
def secured_app(core_server_secured):
    return core_server_secured.test_client()

def test_root(app):
    response = app.get("http://dummy/")
    content = response.get_data(as_text=True)
    assert response.status_code == 200 and content.startswith("hello")

def test_root_secured(secured_app):
    response = secured_app.get("http://dummy/")
    content = response.get_data(as_text=True)
    assert response.status_code == 200 and content.startswith("hello")

def test_version(app):
    response = app.get("http://dummy/version")
    content = response.get_json()
    assert response.status_code == 200
    assert content.get("version") == rasa_core.__version__
    assert (content.get(
        "minimum_compatible_version") == constants.MINIMUM_COMPATIBLE_VERSION)

def test_status(app):
    response = app.get("http://dummy/status")
    content = response.get_json()
    assert response.status_code == 200
    assert content.get("is_ready")
    assert content.get("model_fingerprint") is not None

@freeze_time("2018-01-01")
def test_requesting_non_existent_tracker(app):
    response = app.get("http://dummy/conversations/madeupid/tracker")
    content = response.get_json()
    assert response.status_code == 200
    assert content["paused"] is False
    assert content["slots"] == {"location": None, "cuisine": None}
    assert content["sender_id"] == "madeupid"
    assert content["events"] == [{"event": "action",
                                  "policy": None,
                                  "confidence": None,
                                  "name": "action_listen",
                                  "policy": None,
                                  "confidence": None,
                                  "timestamp": 1514764800}]
    assert content["latest_message"] == {"text": None,
                                         "intent": {},
                                         "entities": []}

def test_respond(app):
    data = json.dumps({"query": "/greet"})
    response = app.post("http://dummy/conversations/myid/respond",
                        data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200
    assert content == [{'text': 'hey there!', 'recipient_id': 'myid'}]

@pytest.mark.parametrize("event", test_events)
def test_pushing_event(app, event):
    cid = str(uuid.uuid1())
    conversation = "http://dummy/conversations/{}".format(cid)
    data = json.dumps({"query": "/greet"})
    response = app.post("{}/respond".format(conversation),
                        data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200

    data = json.dumps(event.as_dict())
    response = app.post("{}/tracker/events".format(conversation),
                        data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200

    tracker_response = app.get("http://dummy/conversations/{}/tracker"
                               "".format(cid))
    tracker = tracker_response.get_json()
    assert tracker is not None
    assert len(tracker.get("events")) == 6

    evt = tracker.get("events")[5]
    assert Event.from_parameters(evt) == event

def test_put_tracker(app):
    data = json.dumps([event.as_dict() for event in test_events])
    response = app.put("http://dummy/conversations/pushtracker/tracker/events",
                       data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200
    assert len(content["events"]) == len(test_events)
    assert content["sender_id"] == "pushtracker"

    tracker_response = app.get("http://dummy/conversations/pushtracker/tracker")
    tracker = tracker_response.get_json()
    assert tracker is not None
    evts = tracker.get("events")
    assert events.deserialise_events(evts) == test_events

def test_list_conversations(app):
    data = json.dumps({"query": "/greet"})
    response = app.post("http://dummy/conversations/myid/respond",
                        data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200

    response = app.get("http://dummy/conversations")
    content = response.get_json()
    assert response.status_code == 200

    assert len(content) > 0
    assert "myid" in content

def test_remote_status(http_app):
    client = RasaCoreClient(EndpointConfig(http_app))

    status = client.status()

    assert status.get("version") == rasa_core.__version__

def test_remote_clients(http_app):
    client = RasaCoreClient(EndpointConfig(http_app))

    cid = str(uuid.uuid1())
    client.respond("/greet", cid)

    clients = client.clients()

    assert cid in clients

@pytest.mark.parametrize("event", test_events)
def test_remote_append_events(http_app, event):
    client = RasaCoreClient(EndpointConfig(http_app))

    cid = str(uuid.uuid1())

    client.append_event_to_tracker(cid, event)

    tracker = client.tracker_json(cid)

    evts = tracker.get("events")
    expected = [ActionExecuted(ACTION_LISTEN_NAME), event]
    assert events.deserialise_events(evts) == expected

def test_predict(http_app, app):
    client = RasaCoreClient(EndpointConfig(http_app))
    cid = str(uuid.uuid1())
    for event in test_events[:2]:
        client.append_event_to_tracker(cid, event)
    out = app.get('/domain', headers={'Accept': 'yml'})
    domain = Domain.from_yaml(out.get_data())
    tracker = client.tracker(cid, domain)
    event_dicts = [ev.as_dict() for ev in tracker.applied_events()]
    response = app.post('/predict',
                        json=event_dicts)
    assert response.status_code == 200

def test_list_conversations_with_jwt(secured_app):
    # token generated with secret "core" and algorithm HS256
    # on https://jwt.io/

    # {"user": {"user": "testadmin", "role": "admin"}}
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
                         "eyJ1c2VyIjp7InVzZXIiOiJ0ZXN0YWRtaW4iLCJyb2xlIjoiYWRt"
                         "aW4ifX0.VUOiT2DL3LWoesfKm7wWv5Yp8mSnc5v2OXFSq6Tiis0"
    }
    response = secured_app.get("/conversations",
                               headers=jwt_header)
    assert response.status_code == 200

    # {"user": {"user": "testuser", "role": "user"}}
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
                         "eyJ1c2VyIjp7InVzZXIiOiJ0ZXN0dXNlciIsInJvbGUiOiJ1c2Vy"
                         "In19._Gu7YX6euPvq9pfDFHzgH4qPNMbJH1XGXGCVRnXiP24"
    }
    response = secured_app.get("/conversations",
                               headers=jwt_header)
    assert response.status_code == 403

def test_get_tracker_with_jwt(secured_app):
    # token generated with secret "core" and algorithm HS256
    # on https://jwt.io/

    # {"user": {"user": "testadmin", "role": "admin"}}
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
                         "eyJ1c2VyIjp7InVzZXIiOiJ0ZXN0YWRtaW4iLCJyb2xlIjoiYWRt"
                         "aW4ifX0.VUOiT2DL3LWoesfKm7wWv5Yp8mSnc5v2OXFSq6Tiis0"
    }
    response = secured_app.get("/conversations/testadmin/tracker",
                               headers=jwt_header)
    assert response.status_code == 200

    response = secured_app.get("/conversations/testuser/tracker",
                               headers=jwt_header)
    assert response.status_code == 200

    # {"user": {"user": "testuser", "role": "user"}}
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
                         "eyJ1c2VyIjp7InVzZXIiOiJ0ZXN0dXNlciIsInJvbGUiOiJ1c2Vy"
                         "In19._Gu7YX6euPvq9pfDFHzgH4qPNMbJH1XGXGCVRnXiP24"
    }
    response = secured_app.get("/conversations/testadmin/tracker",
                               headers=jwt_header)
    assert response.status_code == 403

    response = secured_app.get("/conversations/testuser/tracker",
                               headers=jwt_header)
    assert response.status_code == 200

def test_list_conversations_with_token(secured_app):
    response = secured_app.get("/conversations?token=rasa")
    assert response.status_code == 200

def test_list_conversations_with_wrong_token(secured_app):
    response = secured_app.get("/conversations?token=Rasa")
    assert response.status_code == 401

def test_list_conversations_without_auth(secured_app):
    response = secured_app.get("/conversations")
    assert response.status_code == 401

def test_list_conversations_with_wrong_jwt(secured_app):
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ"
                         "zdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIi"
                         "wiaWF0IjoxNTE2MjM5MDIyfQ.qdrr2_a7Sd80gmCWjnDomO"
                         "Gl8eZFVfKXA6jhncgRn-I"
    }
    response = secured_app.get("/conversations",
                               headers=jwt_header)
    assert response.status_code == 422
<commit_msg>add line back<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from builtins import str

import json
import pytest
import uuid
from freezegun import freeze_time

import rasa_core
from rasa_core import events, constants
from rasa_core.actions.action import ACTION_LISTEN_NAME
from rasa_core.domain import Domain
from rasa_core.events import (
    UserUttered, BotUttered, SlotSet, Event, ActionExecuted)
from rasa_core.remote import RasaCoreClient
from rasa_core.utils import EndpointConfig

# a couple of event instances that we can use for testing
test_events = [
    Event.from_parameters({"event": UserUttered.type_name,
                           "text": "/goodbye",
                           "parse_data": {
                               "intent": {
                                   "confidence": 1.0, "name": "greet"},
                               "entities": []}
                           }),
    BotUttered("Welcome!", {"test": True}),
    SlotSet("cuisine", 34),
    SlotSet("cuisine", "34"),
    SlotSet("location", None),
    SlotSet("location", [34, "34", None]),
]

@pytest.fixture(scope="module")
def app(core_server):
    return core_server.test_client()

@pytest.fixture(scope="module")
def secured_app(core_server_secured):
    return core_server_secured.test_client()

def test_root(app):
    response = app.get("http://dummy/")
    content = response.get_data(as_text=True)
    assert response.status_code == 200 and content.startswith("hello")

def test_root_secured(secured_app):
    response = secured_app.get("http://dummy/")
    content = response.get_data(as_text=True)
    assert response.status_code == 200 and content.startswith("hello")

def test_version(app):
    response = app.get("http://dummy/version")
    content = response.get_json()
    assert response.status_code == 200
    assert content.get("version") == rasa_core.__version__
    assert (content.get(
        "minimum_compatible_version") == constants.MINIMUM_COMPATIBLE_VERSION)

def test_status(app):
    response = app.get("http://dummy/status")
    content = response.get_json()
    assert response.status_code == 200
    assert content.get("is_ready")
    assert content.get("model_fingerprint") is not None

@freeze_time("2018-01-01")
def test_requesting_non_existent_tracker(app):
    response = app.get("http://dummy/conversations/madeupid/tracker")
    content = response.get_json()
    assert response.status_code == 200
    assert content["paused"] is False
    assert content["slots"] == {"location": None, "cuisine": None}
    assert content["sender_id"] == "madeupid"
    assert content["events"] == [{"event": "action",
                                  "policy": None,
                                  "confidence": None,
                                  "name": "action_listen",
                                  "policy": None,
                                  "confidence": None,
                                  "timestamp": 1514764800}]
    assert content["latest_message"] == {"text": None,
                                         "intent": {},
                                         "entities": []}

def test_respond(app):
    data = json.dumps({"query": "/greet"})
    response = app.post("http://dummy/conversations/myid/respond",
                        data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200
    assert content == [{'text': 'hey there!', 'recipient_id': 'myid'}]

@pytest.mark.parametrize("event", test_events)
def test_pushing_event(app, event):
    cid = str(uuid.uuid1())
    conversation = "http://dummy/conversations/{}".format(cid)
    data = json.dumps({"query": "/greet"})
    response = app.post("{}/respond".format(conversation),
                        data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200

    data = json.dumps(event.as_dict())
    response = app.post("{}/tracker/events".format(conversation),
                        data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200

    tracker_response = app.get("http://dummy/conversations/{}/tracker"
                               "".format(cid))
    tracker = tracker_response.get_json()
    assert tracker is not None
    assert len(tracker.get("events")) == 6

    evt = tracker.get("events")[5]
    assert Event.from_parameters(evt) == event

def test_put_tracker(app):
    data = json.dumps([event.as_dict() for event in test_events])
    response = app.put("http://dummy/conversations/pushtracker/tracker/events",
                       data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200
    assert len(content["events"]) == len(test_events)
    assert content["sender_id"] == "pushtracker"

    tracker_response = app.get("http://dummy/conversations/pushtracker/tracker")
    tracker = tracker_response.get_json()
    assert tracker is not None
    evts = tracker.get("events")
    assert events.deserialise_events(evts) == test_events

def test_list_conversations(app):
    data = json.dumps({"query": "/greet"})
    response = app.post("http://dummy/conversations/myid/respond",
                        data=data, content_type='application/json')
    content = response.get_json()
    assert response.status_code == 200

    response = app.get("http://dummy/conversations")
    content = response.get_json()
    assert response.status_code == 200

    assert len(content) > 0
    assert "myid" in content

def test_remote_status(http_app):
    client = RasaCoreClient(EndpointConfig(http_app))

    status = client.status()

    assert status.get("version") == rasa_core.__version__

def test_remote_clients(http_app):
    client = RasaCoreClient(EndpointConfig(http_app))

    cid = str(uuid.uuid1())
    client.respond("/greet", cid)

    clients = client.clients()

    assert cid in clients

@pytest.mark.parametrize("event", test_events)
def test_remote_append_events(http_app, event):
    client = RasaCoreClient(EndpointConfig(http_app))

    cid = str(uuid.uuid1())

    client.append_event_to_tracker(cid, event)

    tracker = client.tracker_json(cid)

    evts = tracker.get("events")
    expected = [ActionExecuted(ACTION_LISTEN_NAME), event]
    assert events.deserialise_events(evts) == expected

def test_predict(http_app, app):
    client = RasaCoreClient(EndpointConfig(http_app))
    cid = str(uuid.uuid1())
    for event in test_events[:2]:
        client.append_event_to_tracker(cid, event)
    out = app.get('/domain', headers={'Accept': 'yml'})
    domain = Domain.from_yaml(out.get_data())
    tracker = client.tracker(cid, domain)
    event_dicts = [ev.as_dict() for ev in tracker.applied_events()]
    response = app.post('/predict',
                        json=event_dicts)
    assert response.status_code == 200

def test_list_conversations_with_jwt(secured_app):
    # token generated with secret "core" and algorithm HS256
    # on https://jwt.io/

    # {"user": {"user": "testadmin", "role": "admin"}}
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
                         "eyJ1c2VyIjp7InVzZXIiOiJ0ZXN0YWRtaW4iLCJyb2xlIjoiYWRt"
                         "aW4ifX0.VUOiT2DL3LWoesfKm7wWv5Yp8mSnc5v2OXFSq6Tiis0"
    }
    response = secured_app.get("/conversations",
                               headers=jwt_header)
    assert response.status_code == 200

    # {"user": {"user": "testuser", "role": "user"}}
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
                         "eyJ1c2VyIjp7InVzZXIiOiJ0ZXN0dXNlciIsInJvbGUiOiJ1c2Vy"
                         "In19._Gu7YX6euPvq9pfDFHzgH4qPNMbJH1XGXGCVRnXiP24"
    }
    response = secured_app.get("/conversations",
                               headers=jwt_header)
    assert response.status_code == 403

def test_get_tracker_with_jwt(secured_app):
    # token generated with secret "core" and algorithm HS256
    # on https://jwt.io/

    # {"user": {"user": "testadmin", "role": "admin"}}
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
                         "eyJ1c2VyIjp7InVzZXIiOiJ0ZXN0YWRtaW4iLCJyb2xlIjoiYWRt"
                         "aW4ifX0.VUOiT2DL3LWoesfKm7wWv5Yp8mSnc5v2OXFSq6Tiis0"
    }
    response = secured_app.get("/conversations/testadmin/tracker",
                               headers=jwt_header)
    assert response.status_code == 200

    response = secured_app.get("/conversations/testuser/tracker",
                               headers=jwt_header)
    assert response.status_code == 200

    # {"user": {"user": "testuser", "role": "user"}}
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
                         "eyJ1c2VyIjp7InVzZXIiOiJ0ZXN0dXNlciIsInJvbGUiOiJ1c2Vy"
                         "In19._Gu7YX6euPvq9pfDFHzgH4qPNMbJH1XGXGCVRnXiP24"
    }
    response = secured_app.get("/conversations/testadmin/tracker",
                               headers=jwt_header)
    assert response.status_code == 403

    response = secured_app.get("/conversations/testuser/tracker",
                               headers=jwt_header)
    assert response.status_code == 200

def test_list_conversations_with_token(secured_app):
    response = secured_app.get("/conversations?token=rasa")
    assert response.status_code == 200

def test_list_conversations_with_wrong_token(secured_app):
    response = secured_app.get("/conversations?token=Rasa")
    assert response.status_code == 401

def test_list_conversations_without_auth(secured_app):
    response = secured_app.get("/conversations")
    assert response.status_code == 401

def test_list_conversations_with_wrong_jwt(secured_app):
    jwt_header = {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ"
                         "zdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIi"
                         "wiaWF0IjoxNTE2MjM5MDIyfQ.qdrr2_a7Sd80gmCWjnDomO"
                         "Gl8eZFVfKXA6jhncgRn-I"
    }
    response = secured_app.get("/conversations",
                               headers=jwt_header)
    assert response.status_code == 422
<|endoftext|>


<commit_before># coding=utf-8
# Copyright 2018 The Tensor2Tensor Authors.
#
# 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.
"""Batch of environments inside the TensorFlow graph."""

# The code was based on Danijar Hafner's code from tf.agents:
# https://github.com/tensorflow/agents/blob/master/agents/tools/in_graph_batch_env.py

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Dependency imports

from tensor2tensor.rl.envs.in_graph_batch_env import InGraphBatchEnv
from tensor2tensor.utils import registry
from tensor2tensor.utils import trainer_lib

import tensorflow as tf

flags = tf.flags
FLAGS = flags.FLAGS

class SimulatedBatchEnv(InGraphBatchEnv):
  """Batch of environments inside the TensorFlow graph.

  The batch of environments will be stepped and reset inside of the graph using
  a tf.py_func(). The current batch of observations, actions, rewards, and done
  flags are held in according variables.
  """
  NUMBER_OF_HISTORY_FRAMES = 2

  def __init__(self, environment_lambda, length):
    """Batch of environments inside the TensorFlow graph."""
    self.length = length
    initalization_env = environment_lambda()
    hparams = trainer_lib.create_hparams(
        FLAGS.hparams_set, problem_name=FLAGS.problem, data_dir="UNUSED")
    hparams.force_full_predict = True
    self._model = registry.model(FLAGS.model)(
        hparams, tf.estimator.ModeKeys.PREDICT)

    self.action_space = initalization_env.action_space
    self.action_shape = list(initalization_env.action_space.shape)
    self.action_dtype = tf.int32

    if hasattr(initalization_env.env, "get_starting_data"):
      starting_observations, _, _ = initalization_env.env.get_starting_data()
      obs_1 = starting_observations[0]
      obs_2 = starting_observations[1]
    else:
      # Ancient method for environments not supporting get_starting_data
      initalization_env.reset()
      skip_frames = 20
      for _ in range(skip_frames):
        initalization_env.step(0)
      obs_1 = initalization_env.step(0)[0]
      obs_2 = initalization_env.step(0)[0]

    self.frame_1 = tf.expand_dims(tf.cast(obs_1, tf.float32), 0)
    self.frame_2 = tf.expand_dims(tf.cast(obs_2, tf.float32), 0)

    shape = (self.length,) + initalization_env.observation_space.shape
    # TODO(blazej0) - make more generic - make higher number of
    # and make it compatibile with NUMBER_OF_HISTORY_FRAMES
    #   previous observations possible.
    self._observ = tf.Variable(tf.zeros(shape, tf.float32), trainable=False)
    self._prev_observ = tf.Variable(tf.zeros(shape, tf.float32),
                                    trainable=False)

  def __len__(self):
    """Number of combined environments."""
    return self.length

  def simulate(self, action):
    with tf.name_scope("environment/simulate"):
      input0 = self._prev_observ.read_value()
      input1 = self._observ.read_value()
      # Note: the merging below must be consistent with video_utils format.
      inputs_merged = tf.concat([input0, input1], axis=0)
      action = tf.expand_dims(action, axis=0)  # Action needs time too.
      action = tf.concat([action, action], axis=0)
      inputs = {"inputs": tf.expand_dims(inputs_merged, axis=0),  # Add batch.
                "input_action": tf.expand_dims(action, axis=0)}
      with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE):
        model_output = self._model.infer(inputs)
      observ = model_output["targets"]
      observ = tf.cast(observ[:, 0, :, :, :], tf.float32)
      # TODO(lukaszkaiser): instead of -1 use min_reward in the line below.
      reward = model_output["target_reward"][:, 0, 0, 0] - 1
      reward = tf.cast(reward, tf.float32)
      done = tf.constant(False, tf.bool, shape=(self.length,))

      with tf.control_dependencies([observ]):
        with tf.control_dependencies([self._prev_observ.assign(self._observ)]):
          with tf.control_dependencies([self._observ.assign(observ)]):
            return tf.identity(reward), tf.identity(done)

  def reset(self, indices=None):
    """Reset the batch of environments.

    Args:
      indices: The batch indices of the environments to reset.

    Returns:
      Batch tensor of the new observations.
    """
    return tf.cond(
        tf.cast(tf.shape(indices)[0], tf.bool),
        lambda: self._reset_non_empty(indices), lambda: 0.0)

  def _reset_non_empty(self, indices):
    """Reset the batch of environments.

    Args:
      indices: The batch indices of the environments to reset; defaults to all.

    Returns:
      Batch tensor of the new observations.
    """
    observ = tf.gather(self._observ, indices)
    observ = 0.0 * tf.check_numerics(observ, "observ")
    with tf.control_dependencies([
        tf.scatter_update(self._observ, indices, observ + self.frame_2),
        tf.scatter_update(self._prev_observ, indices, observ + self.frame_1)]):
      return tf.identity(self._observ.read_value())

  @property
  def observ(self):
    """Access the variable holding the current observation."""
    return self._observ
<commit_msg>setting shape (to make MaxSkipWrapper to work)<commit_after># coding=utf-8
# Copyright 2018 The Tensor2Tensor Authors.
#
# 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.
"""Batch of environments inside the TensorFlow graph."""

# The code was based on Danijar Hafner's code from tf.agents:
# https://github.com/tensorflow/agents/blob/master/agents/tools/in_graph_batch_env.py

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Dependency imports

from tensor2tensor.rl.envs.in_graph_batch_env import InGraphBatchEnv
from tensor2tensor.utils import registry
from tensor2tensor.utils import trainer_lib

import tensorflow as tf

flags = tf.flags
FLAGS = flags.FLAGS

class SimulatedBatchEnv(InGraphBatchEnv):
  """Batch of environments inside the TensorFlow graph.

  The batch of environments will be stepped and reset inside of the graph using
  a tf.py_func(). The current batch of observations, actions, rewards, and done
  flags are held in according variables.
  """
  NUMBER_OF_HISTORY_FRAMES = 2

  def __init__(self, environment_lambda, length):
    """Batch of environments inside the TensorFlow graph."""
    self.length = length
    initialization_env = environment_lambda()
    hparams = trainer_lib.create_hparams(
        FLAGS.hparams_set, problem_name=FLAGS.problem, data_dir="UNUSED")
    hparams.force_full_predict = True
    self._model = registry.model(FLAGS.model)(
        hparams, tf.estimator.ModeKeys.PREDICT)

    self.action_space = initialization_env.action_space
    self.action_shape = list(initialization_env.action_space.shape)
    self.action_dtype = tf.int32

    if hasattr(initialization_env.env, "get_starting_data"):
      starting_observations, _, _ = initialization_env.env.get_starting_data()
      obs_1 = starting_observations[0]
      obs_2 = starting_observations[1]
    else:
      # Ancient method for environments not supporting get_starting_data
      initialization_env.reset()
      skip_frames = 20
      for _ in range(skip_frames):
        initialization_env.step(0)
      obs_1 = initialization_env.step(0)[0]
      obs_2 = initialization_env.step(0)[0]

    self.frame_1 = tf.expand_dims(tf.cast(obs_1, tf.float32), 0)
    self.frame_2 = tf.expand_dims(tf.cast(obs_2, tf.float32), 0)

    shape = (self.length,) + initialization_env.observation_space.shape
    # TODO(blazej0) - make more generic - make higher number of
    # and make it compatibile with NUMBER_OF_HISTORY_FRAMES
    #   previous observations possible.
    self._observ = tf.Variable(tf.zeros(shape, tf.float32), trainable=False)
    self._prev_observ = tf.Variable(tf.zeros(shape, tf.float32),
                                    trainable=False)

  def __len__(self):
    """Number of combined environments."""
    return self.length

  def simulate(self, action):
    with tf.name_scope("environment/simulate"):
      input0 = self._prev_observ.read_value()
      input1 = self._observ.read_value()
      # Note: the merging below must be consistent with video_utils format.
      inputs_merged = tf.concat([input0, input1], axis=0)
      action = tf.expand_dims(action, axis=0)  # Action needs time too.
      action = tf.concat([action, action], axis=0)
      inputs = {"inputs": tf.expand_dims(inputs_merged, axis=0),  # Add batch.
                "input_action": tf.expand_dims(action, axis=0)}
      with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE):
        model_output = self._model.infer(inputs)
      observ = model_output["targets"]
      observ = tf.cast(observ[:, 0, :, :, :], tf.float32)
      # TODO(lukaszkaiser): instead of -1 use min_reward in the line below.
      reward = model_output["target_reward"][:, 0, 0, 0] - 1
      reward = tf.cast(reward, tf.float32)
      reward = tf.reshape(reward, shape=(self.length,)) #some wrappers need explicit shape
      done = tf.constant(False, tf.bool, shape=(self.length,))

      with tf.control_dependencies([observ]):
        with tf.control_dependencies([self._prev_observ.assign(self._observ)]):
          with tf.control_dependencies([self._observ.assign(observ)]):
            return tf.identity(reward), tf.identity(done)

  def reset(self, indices=None):
    """Reset the batch of environments.

    Args:
      indices: The batch indices of the environments to reset.

    Returns:
      Batch tensor of the new observations.
    """
    return tf.cond(
        tf.cast(tf.shape(indices)[0], tf.bool),
        lambda: self._reset_non_empty(indices), lambda: 0.0)

  def _reset_non_empty(self, indices):
    """Reset the batch of environments.

    Args:
      indices: The batch indices of the environments to reset; defaults to all.

    Returns:
      Batch tensor of the new observations.
    """
    observ = tf.gather(self._observ, indices)
    observ = 0.0 * tf.check_numerics(observ, "observ")
    with tf.control_dependencies([
        tf.scatter_update(self._observ, indices, observ + self.frame_2),
        tf.scatter_update(self._prev_observ, indices, observ + self.frame_1)]):
      return tf.identity(self._observ.read_value())

  @property
  def observ(self):
    """Access the variable holding the current observation."""
    return self._observ
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import pytest, re, time
from plexapi.exceptions import BadRequest, NotFound
from plexapi.server import PlexServer
from plexapi.utils import download
from PIL import Image, ImageStat
from requests import Session
from . import conftest as utils

def test_server_attr(plex, account):
    assert plex._baseurl == utils.SERVER_BASEURL
    assert len(plex.friendlyName) >= 1
    assert len(plex.machineIdentifier) == 40
    assert plex.myPlex is True
    # if you run the tests very shortly after server creation the state in rare cases may be `unknown`
    assert plex.myPlexMappingState in ('mapped', 'unknown')
    assert plex.myPlexSigninState == 'ok'
    assert utils.is_int(plex.myPlexSubscription, gte=0)
    assert re.match(utils.REGEX_EMAIL, plex.myPlexUsername)
    assert plex.platform in ('Linux', 'Windows')
    assert len(plex.platformVersion) >= 5
    assert plex._token == account.authenticationToken
    assert utils.is_int(plex.transcoderActiveVideoSessions, gte=0)
    assert utils.is_datetime(plex.updatedAt)
    assert len(plex.version) >= 5

def test_server_alert_listener(plex, movies):
    try:
        messages = []
        listener = plex.startAlertListener(messages.append)
        movies.refresh()
        utils.wait_until(lambda: len(messages) >= 3, delay=1, timeout=30)
        assert len(messages) >= 3
    finally:
        listener.stop()

@pytest.mark.req_client
def test_server_session():
    # TODO: Implement test_server_session
    pass

def test_server_library(plex):
    # TODO: Implement test_server_library
    assert plex.library

def test_server_url(plex):
    assert 'ohno' in plex.url('ohno')

def test_server_transcodeImage(tmpdir, plex, show):
    width, height = 500, 500
    imgurl = plex.transcodeImage(show.banner, height, width)
    gray = imgurl = plex.transcodeImage(show.banner, height, width, saturation=0)
    resized_img = download(imgurl, plex._token, savepath=str(tmpdir), filename='resize_image')
    original_img = download(show._server.url(show.banner), plex._token, savepath=str(tmpdir), filename='original_img')
    grayscale_img = download(gray, plex._token, savepath=str(tmpdir), filename='grayscale_img')
    with Image.open(resized_img) as image:
        assert width, height == image.size
    with Image.open(original_img) as image:
        assert width, height != image.size
    assert _detect_color_image(grayscale_img, thumb_size=150) == 'grayscale'

def _detect_color_image(file, thumb_size=150, MSE_cutoff=22, adjust_color_bias=True):
    # http://stackoverflow.com/questions/20068945/detect-if-image-is-color-grayscale-or-black-and-white-with-python-pil
    pilimg = Image.open(file)
    bands = pilimg.getbands()
    if bands == ('R', 'G', 'B') or bands == ('R', 'G', 'B', 'A'):
        thumb = pilimg.resize((thumb_size, thumb_size))
        sse, bias = 0, [0, 0, 0]
        if adjust_color_bias:
            bias = ImageStat.Stat(thumb).mean[:3]
            bias = [b - sum(bias) / 3 for b in bias]
        for pixel in thumb.getdata():
            mu = sum(pixel) / 3
            sse += sum((pixel[i] - mu - bias[i]) * (pixel[i] - mu - bias[i]) for i in [0, 1, 2])
        mse = float(sse) / (thumb_size * thumb_size)
        return 'grayscale' if mse <= MSE_cutoff else 'color'
    elif len(bands) == 1:
        return 'blackandwhite'

def test_server_search(plex, movie):
    title = movie.title
    assert plex.search(title)
    assert plex.search(title, mediatype='movie')

def test_server_playlist(plex, show):
    episodes = show.episodes()
    playlist = plex.createPlaylist('test_playlist', episodes[:3])
    try:
        assert playlist.title == 'test_playlist'
        with pytest.raises(NotFound):
            plex.playlist('<playlist-not-found>')
    finally:
        playlist.delete()

def test_server_playlists(plex, show):
    playlists = plex.playlists()
    count = len(playlists)
    episodes = show.episodes()
    playlist = plex.createPlaylist('test_playlist', episodes[:3])
    try:
        playlists = plex.playlists()
        assert len(playlists) == count + 1
    finally:
        playlist.delete()

def test_server_history(plex, movie):
    movie.markWatched()
    history = plex.history()
    assert len(history)
    movie.markUnwatched()

@pytest.mark.anonymously
def test_server_Server_query(plex):
    assert plex.query('/')
    with pytest.raises(BadRequest):
        assert plex.query('/asdf/1234/asdf', headers={'random_headers': '1234'})

@pytest.mark.authenticated
def test_server_Server_query_authenticated(plex):
    assert plex.query('/')
    with pytest.raises(BadRequest):
        assert plex.query('/asdf/1234/asdf', headers={'random_headers': '1234'})
    with pytest.raises(BadRequest):
        # This is really requests.exceptions.HTTPError
        # 401 Client Error: Unauthorized for url
        PlexServer(utils.SERVER_BASEURL, '1234')

def test_server_Server_session(account):
    # Mock Sesstion
    class MySession(Session):
        def __init__(self):
            super(self.__class__, self).__init__()
            self.plexapi_session_test = True
    # Test Code
    plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken, session=MySession())
    assert hasattr(plex._session, 'plexapi_session_test')

@pytest.mark.authenticated
def test_server_token_in_headers(plex):
    headers = plex._headers()
    assert 'X-Plex-Token' in headers
    assert len(headers['X-Plex-Token']) >= 1

def test_server_createPlayQueue(plex, movie):
    playqueue = plex.createPlayQueue(movie, shuffle=1, repeat=1)
    assert 'shuffle=1' in playqueue._initpath
    assert 'repeat=1' in playqueue._initpath
    assert playqueue.playQueueShuffled is True

def test_server_client_not_found(plex):
    with pytest.raises(NotFound):
        plex.client('<This-client-should-not-be-found>')

def test_server_sessions(plex):
    assert len(plex.sessions()) >= 0

def test_server_isLatest(plex, mocker):
    from os import environ
    is_latest = plex.isLatest()
    if environ.get('PLEX_CONTAINER_TAG') and environ['PLEX_CONTAINER_TAG'] != 'latest':
        assert not is_latest
    else:
        return pytest.skip('Do not forget to run with PLEX_CONTAINER_TAG != latest to ensure that update is available')

def test_server_installUpdate(plex, mocker):
    m = mocker.MagicMock(release='aa')
    mocker.patch('plexapi.server.PlexServer.check_for_update', return_value=m)
    with utils.callable_http_patch():
        plex.installUpdate()

def test_server_check_for_update(plex, mocker):
    class R():
        def __init__(self, **kwargs):
            self.download_key = 'plex.tv/release/1337'
            self.version = '1337'
            self.added = 'gpu transcode'
            self.fixed = 'fixed rare bug'
            self.downloadURL = 'http://path-to-update'
            self.state = 'downloaded'

    with mocker.patch('plexapi.server.PlexServer.check_for_update', return_value=R()):
        rel = plex.check_for_update(force=False, download=True)
        assert rel.download_key == 'plex.tv/release/1337'
        assert rel.version == '1337'
        assert rel.added == 'gpu transcode'
        assert rel.fixed == 'fixed rare bug'
        assert rel.downloadURL == 'http://path-to-update'
        assert rel.state == 'downloaded'

@pytest.mark.client
def test_server_clients(plex):
    assert len(plex.clients())
    client = plex.clients()[0]
    assert client._baseurl == 'http://127.0.0.1:32400'
    assert client.device is None
    assert client.deviceClass == 'pc'
    assert client.machineIdentifier == '89hgkrbqxaxmf45o1q2949ru'
    assert client.model is None
    assert client.platform is None
    assert client.platformVersion is None
    assert client.product == 'Plex Web'
    assert client.protocol == 'plex'
    assert client.protocolCapabilities == ['timeline', 'playback', 'navigation', 'mirror', 'playqueues']
    assert client.protocolVersion == '1'
    assert client._server._baseurl == 'http://138.68.157.5:32400'
    assert client.state is None
    assert client.title == 'Plex Web (Chrome)'
    assert client.token is None
    assert client.vendor is None
    assert client.version == '2.12.5'

@pytest.mark.authenticated
def test_server_account(plex):
    account = plex.account()
    assert account.authToken
    # TODO: Figure out why this is missing from time to time.
    # assert account.mappingError == 'publisherror'
    assert account.mappingErrorMessage is None
    assert account.mappingState == 'mapped'
    if account.mappingError != 'unreachable':
        assert re.match(utils.REGEX_IPADDR, account.privateAddress)
        assert int(account.privatePort) >= 1000
        assert re.match(utils.REGEX_IPADDR, account.publicAddress)
        assert int(account.publicPort) >= 1000
    else:
        assert account.privateAddress == ''
        assert int(account.privatePort) == 0
        assert account.publicAddress == ''
        assert int(account.publicPort) == 0
    assert account.signInState == 'ok'
    assert isinstance(account.subscriptionActive, bool)
    if account.subscriptionActive:
        assert len(account.subscriptionFeatures)
    # Below check keeps failing.. it should go away.
    # else: assert sorted(account.subscriptionFeatures) == ['adaptive_bitrate',
    #     'download_certificates', 'federated-auth', 'news']
    assert account.subscriptionState == 'Active' if account.subscriptionActive else 'Unknown'
    assert re.match(utils.REGEX_EMAIL, account.username)

def test_server_downloadLogs(tmpdir, plex):
    plex.downloadLogs(savepath=str(tmpdir), unpack=True)
    assert len(tmpdir.listdir()) > 1

def test_server_downloadDatabases(tmpdir, plex):
    plex.downloadDatabases(savepath=str(tmpdir), unpack=True)
    assert len(tmpdir.listdir()) > 1

def test_server_allowMediaDeletion(account):
    plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
    plex._allowMediaDeletion(True)
    time.sleep(1)
    plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
    assert plex.allowMediaDeletion == True
    plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
    plex._allowMediaDeletion(False)
    time.sleep(1)
    plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
    assert plex.allowMediaDeletion == None<commit_msg>update test check server current allowMediaDeletion test for redundant toggles<commit_after># -*- coding: utf-8 -*-
import pytest, re, time
from plexapi.exceptions import BadRequest, NotFound
from plexapi.server import PlexServer
from plexapi.utils import download
from PIL import Image, ImageStat
from requests import Session
from . import conftest as utils

def test_server_attr(plex, account):
    assert plex._baseurl == utils.SERVER_BASEURL
    assert len(plex.friendlyName) >= 1
    assert len(plex.machineIdentifier) == 40
    assert plex.myPlex is True
    # if you run the tests very shortly after server creation the state in rare cases may be `unknown`
    assert plex.myPlexMappingState in ('mapped', 'unknown')
    assert plex.myPlexSigninState == 'ok'
    assert utils.is_int(plex.myPlexSubscription, gte=0)
    assert re.match(utils.REGEX_EMAIL, plex.myPlexUsername)
    assert plex.platform in ('Linux', 'Windows')
    assert len(plex.platformVersion) >= 5
    assert plex._token == account.authenticationToken
    assert utils.is_int(plex.transcoderActiveVideoSessions, gte=0)
    assert utils.is_datetime(plex.updatedAt)
    assert len(plex.version) >= 5

def test_server_alert_listener(plex, movies):
    try:
        messages = []
        listener = plex.startAlertListener(messages.append)
        movies.refresh()
        utils.wait_until(lambda: len(messages) >= 3, delay=1, timeout=30)
        assert len(messages) >= 3
    finally:
        listener.stop()

@pytest.mark.req_client
def test_server_session():
    # TODO: Implement test_server_session
    pass

def test_server_library(plex):
    # TODO: Implement test_server_library
    assert plex.library

def test_server_url(plex):
    assert 'ohno' in plex.url('ohno')

def test_server_transcodeImage(tmpdir, plex, show):
    width, height = 500, 500
    imgurl = plex.transcodeImage(show.banner, height, width)
    gray = imgurl = plex.transcodeImage(show.banner, height, width, saturation=0)
    resized_img = download(imgurl, plex._token, savepath=str(tmpdir), filename='resize_image')
    original_img = download(show._server.url(show.banner), plex._token, savepath=str(tmpdir), filename='original_img')
    grayscale_img = download(gray, plex._token, savepath=str(tmpdir), filename='grayscale_img')
    with Image.open(resized_img) as image:
        assert width, height == image.size
    with Image.open(original_img) as image:
        assert width, height != image.size
    assert _detect_color_image(grayscale_img, thumb_size=150) == 'grayscale'

def _detect_color_image(file, thumb_size=150, MSE_cutoff=22, adjust_color_bias=True):
    # http://stackoverflow.com/questions/20068945/detect-if-image-is-color-grayscale-or-black-and-white-with-python-pil
    pilimg = Image.open(file)
    bands = pilimg.getbands()
    if bands == ('R', 'G', 'B') or bands == ('R', 'G', 'B', 'A'):
        thumb = pilimg.resize((thumb_size, thumb_size))
        sse, bias = 0, [0, 0, 0]
        if adjust_color_bias:
            bias = ImageStat.Stat(thumb).mean[:3]
            bias = [b - sum(bias) / 3 for b in bias]
        for pixel in thumb.getdata():
            mu = sum(pixel) / 3
            sse += sum((pixel[i] - mu - bias[i]) * (pixel[i] - mu - bias[i]) for i in [0, 1, 2])
        mse = float(sse) / (thumb_size * thumb_size)
        return 'grayscale' if mse <= MSE_cutoff else 'color'
    elif len(bands) == 1:
        return 'blackandwhite'

def test_server_search(plex, movie):
    title = movie.title
    assert plex.search(title)
    assert plex.search(title, mediatype='movie')

def test_server_playlist(plex, show):
    episodes = show.episodes()
    playlist = plex.createPlaylist('test_playlist', episodes[:3])
    try:
        assert playlist.title == 'test_playlist'
        with pytest.raises(NotFound):
            plex.playlist('<playlist-not-found>')
    finally:
        playlist.delete()

def test_server_playlists(plex, show):
    playlists = plex.playlists()
    count = len(playlists)
    episodes = show.episodes()
    playlist = plex.createPlaylist('test_playlist', episodes[:3])
    try:
        playlists = plex.playlists()
        assert len(playlists) == count + 1
    finally:
        playlist.delete()

def test_server_history(plex, movie):
    movie.markWatched()
    history = plex.history()
    assert len(history)
    movie.markUnwatched()

@pytest.mark.anonymously
def test_server_Server_query(plex):
    assert plex.query('/')
    with pytest.raises(BadRequest):
        assert plex.query('/asdf/1234/asdf', headers={'random_headers': '1234'})

@pytest.mark.authenticated
def test_server_Server_query_authenticated(plex):
    assert plex.query('/')
    with pytest.raises(BadRequest):
        assert plex.query('/asdf/1234/asdf', headers={'random_headers': '1234'})
    with pytest.raises(BadRequest):
        # This is really requests.exceptions.HTTPError
        # 401 Client Error: Unauthorized for url
        PlexServer(utils.SERVER_BASEURL, '1234')

def test_server_Server_session(account):
    # Mock Sesstion
    class MySession(Session):
        def __init__(self):
            super(self.__class__, self).__init__()
            self.plexapi_session_test = True
    # Test Code
    plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken, session=MySession())
    assert hasattr(plex._session, 'plexapi_session_test')

@pytest.mark.authenticated
def test_server_token_in_headers(plex):
    headers = plex._headers()
    assert 'X-Plex-Token' in headers
    assert len(headers['X-Plex-Token']) >= 1

def test_server_createPlayQueue(plex, movie):
    playqueue = plex.createPlayQueue(movie, shuffle=1, repeat=1)
    assert 'shuffle=1' in playqueue._initpath
    assert 'repeat=1' in playqueue._initpath
    assert playqueue.playQueueShuffled is True

def test_server_client_not_found(plex):
    with pytest.raises(NotFound):
        plex.client('<This-client-should-not-be-found>')

def test_server_sessions(plex):
    assert len(plex.sessions()) >= 0

def test_server_isLatest(plex, mocker):
    from os import environ
    is_latest = plex.isLatest()
    if environ.get('PLEX_CONTAINER_TAG') and environ['PLEX_CONTAINER_TAG'] != 'latest':
        assert not is_latest
    else:
        return pytest.skip('Do not forget to run with PLEX_CONTAINER_TAG != latest to ensure that update is available')

def test_server_installUpdate(plex, mocker):
    m = mocker.MagicMock(release='aa')
    mocker.patch('plexapi.server.PlexServer.check_for_update', return_value=m)
    with utils.callable_http_patch():
        plex.installUpdate()

def test_server_check_for_update(plex, mocker):
    class R():
        def __init__(self, **kwargs):
            self.download_key = 'plex.tv/release/1337'
            self.version = '1337'
            self.added = 'gpu transcode'
            self.fixed = 'fixed rare bug'
            self.downloadURL = 'http://path-to-update'
            self.state = 'downloaded'

    with mocker.patch('plexapi.server.PlexServer.check_for_update', return_value=R()):
        rel = plex.check_for_update(force=False, download=True)
        assert rel.download_key == 'plex.tv/release/1337'
        assert rel.version == '1337'
        assert rel.added == 'gpu transcode'
        assert rel.fixed == 'fixed rare bug'
        assert rel.downloadURL == 'http://path-to-update'
        assert rel.state == 'downloaded'

@pytest.mark.client
def test_server_clients(plex):
    assert len(plex.clients())
    client = plex.clients()[0]
    assert client._baseurl == 'http://127.0.0.1:32400'
    assert client.device is None
    assert client.deviceClass == 'pc'
    assert client.machineIdentifier == '89hgkrbqxaxmf45o1q2949ru'
    assert client.model is None
    assert client.platform is None
    assert client.platformVersion is None
    assert client.product == 'Plex Web'
    assert client.protocol == 'plex'
    assert client.protocolCapabilities == ['timeline', 'playback', 'navigation', 'mirror', 'playqueues']
    assert client.protocolVersion == '1'
    assert client._server._baseurl == 'http://138.68.157.5:32400'
    assert client.state is None
    assert client.title == 'Plex Web (Chrome)'
    assert client.token is None
    assert client.vendor is None
    assert client.version == '2.12.5'

@pytest.mark.authenticated
def test_server_account(plex):
    account = plex.account()
    assert account.authToken
    # TODO: Figure out why this is missing from time to time.
    # assert account.mappingError == 'publisherror'
    assert account.mappingErrorMessage is None
    assert account.mappingState == 'mapped'
    if account.mappingError != 'unreachable':
        assert re.match(utils.REGEX_IPADDR, account.privateAddress)
        assert int(account.privatePort) >= 1000
        assert re.match(utils.REGEX_IPADDR, account.publicAddress)
        assert int(account.publicPort) >= 1000
    else:
        assert account.privateAddress == ''
        assert int(account.privatePort) == 0
        assert account.publicAddress == ''
        assert int(account.publicPort) == 0
    assert account.signInState == 'ok'
    assert isinstance(account.subscriptionActive, bool)
    if account.subscriptionActive:
        assert len(account.subscriptionFeatures)
    # Below check keeps failing.. it should go away.
    # else: assert sorted(account.subscriptionFeatures) == ['adaptive_bitrate',
    #     'download_certificates', 'federated-auth', 'news']
    assert account.subscriptionState == 'Active' if account.subscriptionActive else 'Unknown'
    assert re.match(utils.REGEX_EMAIL, account.username)

def test_server_downloadLogs(tmpdir, plex):
    plex.downloadLogs(savepath=str(tmpdir), unpack=True)
    assert len(tmpdir.listdir()) > 1

def test_server_downloadDatabases(tmpdir, plex):
    plex.downloadDatabases(savepath=str(tmpdir), unpack=True)
    assert len(tmpdir.listdir()) > 1

def test_server_allowMediaDeletion(account):
    plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
    # Check server current allowMediaDeletion setting
    if plex.allowMediaDeletion:
        # If allowed then test disallowed
        plex._allowMediaDeletion(False)
        time.sleep(1)
        plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
        assert plex.allowMediaDeletion is None
        # Test redundant toggle
        with pytest.raises(BadRequest):
            plex._allowMediaDeletion(False)

        plex._allowMediaDeletion(True)
        time.sleep(1)
        plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
        assert plex.allowMediaDeletion is True
        # Test redundant toggle
        with pytest.raises(BadRequest):
            plex._allowMediaDeletion(True)
    else:
        # If disallowed then test allowed
        plex._allowMediaDeletion(True)
        time.sleep(1)
        plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
        assert plex.allowMediaDeletion is True
        # Test redundant toggle
        with pytest.raises(BadRequest):
            plex._allowMediaDeletion(True)

        plex._allowMediaDeletion(False)
        time.sleep(1)
        plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
        assert plex.allowMediaDeletion is None
        # Test redundant toggle
        with pytest.raises(BadRequest):
            plex._allowMediaDeletion(False)
<|endoftext|>


<commit_before>import nose
import angr
import subprocess

import logging
l = logging.getLogger('angr.tests.strtol')

import os
test_location = str(os.path.dirname(os.path.realpath(__file__)))

def test_strtol():
    b = angr.Project(os.path.join(test_location, "../../binaries/tests/x86_64/strtol_test"))

    pg = b.factory.path_group(immutable=False)

    # find the end of main
    expected_outputs = {"base 8 worked\n", "base +8 worked\n", "0x worked\n", "+0x worked\n", "base +10 worked\n",
                        "base 10 worked\n", "base -8 worked\n", "-0x worked\n", "base -10 worked\n", "Nope\n"}
    pg.explore(find=0x400804, num_find=len(expected_outputs))

    # check the outputs
    pipe = subprocess.PIPE
    for f in pg.found:
        test_input = f.state.posix.dumps(0)
        test_output = f.state.posix.dumps(1)
        expected_outputs.remove(test_output)

        # check the output works as expected
        p = subprocess.Popen("./test2", stdout=pipe, stderr=pipe, stdin=pipe)
        ret = p.communicate(test_input)[0]
        nose.tools.assert_equal(ret, test_output)

    # check that all of the outputs were seen
    nose.tools.assert_equal(len(expected_outputs), 0)

if __name__ == "__main__":
    test_strtol()
<commit_msg>fix binary path<commit_after>import nose
import angr
import subprocess

import logging
l = logging.getLogger('angr.tests.strtol')

import os
test_location = str(os.path.dirname(os.path.realpath(__file__)))

def test_strtol():
    test_bin = os.path.join(test_location, "../../binaries/tests/x86_64/strtol_test")
    b = angr.Project(test_bin)

    pg = b.factory.path_group(immutable=False)

    # find the end of main
    expected_outputs = {"base 8 worked\n", "base +8 worked\n", "0x worked\n", "+0x worked\n", "base +10 worked\n",
                        "base 10 worked\n", "base -8 worked\n", "-0x worked\n", "base -10 worked\n", "Nope\n"}
    pg.explore(find=0x400804, num_find=len(expected_outputs))

    # check the outputs
    pipe = subprocess.PIPE
    for f in pg.found:
        test_input = f.state.posix.dumps(0)
        test_output = f.state.posix.dumps(1)
        expected_outputs.remove(test_output)

        # check the output works as expected
        p = subprocess.Popen(test_bin, stdout=pipe, stderr=pipe, stdin=pipe)
        ret = p.communicate(test_input)[0]
        nose.tools.assert_equal(ret, test_output)

    # check that all of the outputs were seen
    nose.tools.assert_equal(len(expected_outputs), 0)

if __name__ == "__main__":
    test_strtol()
<|endoftext|>