# -*- coding: utf-8 -*-
from mock import patch
from tests import TestCase

from yarn_api_client.node_manager import NodeManager
from yarn_api_client.errors import IllegalArgumentError


@patch('yarn_api_client.node_manager.NodeManager.request')
class NodeManagerTestCase(TestCase):
    def setUp(self):
        self.nm = NodeManager('localhost')

    def test_node_information(self, request_mock):
        self.nm.node_information()
        request_mock.assert_called_with('/ws/v1/node/info')

    def test_node_applications(self, request_mock):
        self.nm.node_applications('RUNNING', 'root')
        request_mock.assert_called_with('/ws/v1/node/apps',
                                        params={"state": 'RUNNING', "user": 'root'})

        self.nm.node_applications()
        request_mock.assert_called_with('/ws/v1/node/apps', params={})

        with self.assertRaises(IllegalArgumentError):
            self.nm.node_applications('ololo', 'root')

    def test_node_application(self, request_mock):
        self.nm.node_application('app_1')
        request_mock.assert_called_with('/ws/v1/node/apps/app_1')

    def test_node_containers(self, request_mock):
        self.nm.node_containers()
        request_mock.assert_called_with('/ws/v1/node/containers')

    def test_node_container(self, request_mock):
        self.nm.node_container('container_1')
        request_mock.assert_called_with('/ws/v1/node/containers/container_1')

    def test_auxiliary_services(self, request_mock):
        self.nm.auxiliary_services()
        request_mock.assert_called_with('/ws/v1/node/auxiliaryservices')

    def test_auxiliary_services_update(self, request_mock):
        self.nm.auxiliary_services_update({
          "services": [
            {
              "name": "mapreduce_shuffle",
              "version": "2",
              "configuration": {
                "properties": {
                  "class.name": "org.apache.hadoop.mapred.ShuffleHandler",
                  "mapreduce.shuffle.transfer.buffer.size": "102400",
                  "mapreduce.shuffle.port": "13563"
                }
              }
            }
          ]
        })
        request_mock.assert_called_with('/ws/v1/node/auxiliaryservices', 'PUT', json={
          "services": [
            {
              "name": "mapreduce_shuffle",
              "version": "2",
              "configuration": {
                "properties": {
                  "class.name": "org.apache.hadoop.mapred.ShuffleHandler",
                  "mapreduce.shuffle.transfer.buffer.size": "102400",
                  "mapreduce.shuffle.port": "13563"
                }
              }
            }
          ]
        })
