text: #!/usr/bin/env python
  
  '''
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  '''
  import json
  import os
  from mock.mock import MagicMock, call, patch
  from stacks.utils.RMFTestCase import *
  from resource_management.libraries.functions import version
  from resource_management.libraries.script.script import Script
  from resource_management.libraries import functions
  
  origin_exists = os.path.exists
  @patch("platform.linux_distribution", new = MagicMock(return_value="Linux"))
  @patch.object(os.path, "exists", new=MagicMock(
    side_effect=lambda *args: origin_exists(args[0])
    if args[0][-2:] == "j2" else True))
  class TestYarnClient(RMFTestCase):
    COMMON_SERVICES_PACKAGE_DIR = "YARN/2.1.0.2.0/package"
    STACK_VERSION = "2.0.6"
  
    def test_configure_default(self):
      self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/yarn_client.py",
                         classname = "YarnClient",
                         command = "configure",
                         config_file="default.json",
                         hdp_stack_version = self.STACK_VERSION,
                         target = RMFTestCase.TARGET_COMMON_SERVICES
      )
  
      self.assertResourceCalled('Directory', '/var/run/hadoop-yarn',
        owner = 'yarn',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-yarn/yarn',
        owner = 'yarn',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-yarn/yarn',
        owner = 'yarn',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-mapreduce',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-mapreduce/mapred',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-mapreduce',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-mapreduce/mapred',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-yarn',
        owner = 'yarn',
        recursive = True,
        ignore_failures = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('XmlConfig', 'core-site.xml',
        owner = 'hdfs',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['core-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['core-site']
      )
      self.assertResourceCalled('XmlConfig', 'mapred-site.xml',
        owner = 'yarn',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['mapred-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['mapred-site']
      )
      self.assertResourceCalled('XmlConfig', 'yarn-site.xml',
        owner = 'yarn',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['yarn-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['yarn-site']
      )
      self.assertResourceCalled('XmlConfig', 'capacity-scheduler.xml',
        owner = 'yarn',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['capacity-scheduler'],
        configuration_attributes = self.getConfig()['configuration_attributes']['capacity-scheduler']
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/yarn.exclude',
        owner = 'yarn',
        group = 'hadoop',
      )
      self.assertResourceCalled('File', '/etc/security/limits.d/yarn.conf',
        content = Template('yarn.conf.j2'),
        mode = 0644,
      )
      self.assertResourceCalled('File', '/etc/security/limits.d/mapreduce.conf',
        content = Template('mapreduce.conf.j2'),
        mode = 0644,
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/yarn-env.sh',
        content = InlineTemplate(self.getConfig()['configurations']['yarn-env']['content']),
        owner = 'yarn',
        group = 'hadoop',
        mode = 0755,
      )
      self.assertResourceCalled('File', '/usr/lib/hadoop-yarn/bin/container-executor',
                                group = 'hadoop',
                                mode = 06050,
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/container-executor.cfg',
                                content = Template('container-executor.cfg.j2'),
                                group = 'hadoop',
                                mode = 0644,
                                )
      self.assertResourceCalled('Directory', '/cgroups_test/cpu',
                                group = 'hadoop',
                                recursive = True,
                                mode = 0755,
                                cd_access="a"
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/mapred-env.sh',
                                content = InlineTemplate(self.getConfig()['configurations']['mapred-env']['content']),
                                owner = 'hdfs',
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/taskcontroller.cfg',
                                content = Template('taskcontroller.cfg.j2'),
                                owner = 'hdfs',
                                )
      self.assertResourceCalled('XmlConfig', 'mapred-site.xml',
                                owner = 'mapred',
                                group = 'hadoop',
                                conf_dir = '/etc/hadoop/conf',
                                configurations = self.getConfig()['configurations']['mapred-site'],
                                configuration_attributes = self.getConfig()['configuration_attributes']['mapred-site']
                                )
      self.assertResourceCalled('XmlConfig', 'capacity-scheduler.xml',
                                owner = 'hdfs',
                                group = 'hadoop',
                                conf_dir = '/etc/hadoop/conf',
                                configurations = self.getConfig()['configurations']['capacity-scheduler'],
                                configuration_attributes = self.getConfig()['configuration_attributes']['capacity-scheduler']
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/fair-scheduler.xml',
                                owner = 'mapred',
                                group = 'hadoop',
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/ssl-client.xml.example',
                                owner = 'mapred',
                                group = 'hadoop',
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/ssl-server.xml.example',
                                owner = 'mapred',
                                group = 'hadoop',
                                )
      self.assertNoMoreResources()
  
    def test_configure_secured(self):
  
      self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/yarn_client.py",
                         classname = "YarnClient",
                         command = "configure",
                         config_file="secured.json",
                         hdp_stack_version = self.STACK_VERSION,
                         target = RMFTestCase.TARGET_COMMON_SERVICES
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-yarn',
        owner = 'yarn',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-yarn/yarn',
        owner = 'yarn',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-yarn/yarn',
        owner = 'yarn',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-mapreduce',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-mapreduce/mapred',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-mapreduce',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-mapreduce/mapred',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-yarn',
        owner = 'yarn',
        recursive = True,
        ignore_failures = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('XmlConfig', 'core-site.xml',
        owner = 'hdfs',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['core-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['core-site']
      )
      self.assertResourceCalled('XmlConfig', 'mapred-site.xml',
        owner = 'yarn',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['mapred-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['mapred-site']
      )
      self.assertResourceCalled('XmlConfig', 'yarn-site.xml',
        owner = 'yarn',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['yarn-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['yarn-site']
      )
      self.assertResourceCalled('XmlConfig', 'capacity-scheduler.xml',
        owner = 'yarn',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['capacity-scheduler'],
        configuration_attributes = self.getConfig()['configuration_attributes']['capacity-scheduler']
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/yarn.exclude',
        owner = 'yarn',
        group = 'hadoop',
      )
      self.assertResourceCalled('File', '/etc/security/limits.d/yarn.conf',
        content = Template('yarn.conf.j2'),
        mode = 0644,
      )
      self.assertResourceCalled('File', '/etc/security/limits.d/mapreduce.conf',
        content = Template('mapreduce.conf.j2'),
        mode = 0644,
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/yarn-env.sh',
        content = InlineTemplate(self.getConfig()['configurations']['yarn-env']['content']),
        owner = 'yarn',
        group = 'hadoop',
        mode = 0755,
      )
      self.assertResourceCalled('File', '/usr/lib/hadoop-yarn/bin/container-executor',
        group = 'hadoop',
        mode = 06050,
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/container-executor.cfg',
        content = Template('container-executor.cfg.j2'),
        group = 'hadoop',
        mode = 0644,
      )
      self.assertResourceCalled('Directory', '/cgroups_test/cpu',
                                group = 'hadoop',
                                recursive = True,
                                mode = 0755,
                                cd_access="a"
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/mapred-env.sh',
                                content = InlineTemplate(self.getConfig()['configurations']['mapred-env']['content']),
                                owner = 'root',
                                )
      self.assertResourceCalled('File', '/usr/lib/hadoop/sbin/task-controller',
                                owner = 'root',
                                group = 'hadoop',
                                mode = 06050,
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/taskcontroller.cfg',
                                content = Template('taskcontroller.cfg.j2'),
                                owner = 'root',
                                group = 'hadoop',
                                mode = 0644,
                                )
      self.assertResourceCalled('XmlConfig', 'mapred-site.xml',
                                owner = 'mapred',
                                group = 'hadoop',
                                conf_dir = '/etc/hadoop/conf',
                                configurations = self.getConfig()['configurations']['mapred-site'],
                                configuration_attributes = self.getConfig()['configuration_attributes']['mapred-site']
                                )
      self.assertResourceCalled('XmlConfig', 'capacity-scheduler.xml',
                                owner = 'hdfs',
                                group = 'hadoop',
                                conf_dir = '/etc/hadoop/conf',
                                configurations = self.getConfig()['configurations']['capacity-scheduler'],
                                configuration_attributes = self.getConfig()['configuration_attributes']['capacity-scheduler']
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/fair-scheduler.xml',
                                owner = 'mapred',
                                group = 'hadoop',
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/ssl-client.xml.example',
                                owner = 'mapred',
                                group = 'hadoop',
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/ssl-server.xml.example',
                                owner = 'mapred',
                                group = 'hadoop',
                                )
      self.assertNoMoreResources()
  
    def test_restart_client(self):
      self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/yarn_client.py",
                         classname = "YarnClient",
                         command = "restart",
                         config_file="default.json",
                         config_overrides = { 'roleParams' : { "component_category": "CLIENT" } },
                         hdp_stack_version = self.STACK_VERSION,
                         target = RMFTestCase.TARGET_COMMON_SERVICES
      )
  
      self.assertResourceCalled('Directory', '/var/run/hadoop-yarn',
        owner = 'yarn',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-yarn/yarn',
        owner = 'yarn',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-yarn/yarn',
        owner = 'yarn',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-mapreduce',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/run/hadoop-mapreduce/mapred',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-mapreduce',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-mapreduce/mapred',
        owner = 'mapred',
        group = 'hadoop',
        recursive = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('Directory', '/var/log/hadoop-yarn',
        owner = 'yarn',
        recursive = True,
        ignore_failures = True,
        cd_access = 'a',
      )
      self.assertResourceCalled('XmlConfig', 'core-site.xml',
        owner = 'hdfs',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['core-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['core-site']
      )
      self.assertResourceCalled('XmlConfig', 'mapred-site.xml',
        owner = 'yarn',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['mapred-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['mapred-site']
      )
      self.assertResourceCalled('XmlConfig', 'yarn-site.xml',
        owner = 'yarn',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['yarn-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['yarn-site']
      )
      self.assertResourceCalled('XmlConfig', 'capacity-scheduler.xml',
        owner = 'yarn',
        group = 'hadoop',
        mode = 0644,
        conf_dir = '/etc/hadoop/conf',
        configurations = self.getConfig()['configurations']['capacity-scheduler'],
        configuration_attributes = self.getConfig()['configuration_attributes']['capacity-scheduler']
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/yarn.exclude',
        owner = 'yarn',
        group = 'hadoop',
      )
      self.assertResourceCalled('File', '/etc/security/limits.d/yarn.conf',
        content = Template('yarn.conf.j2'),
        mode = 0644,
      )
      self.assertResourceCalled('File', '/etc/security/limits.d/mapreduce.conf',
        content = Template('mapreduce.conf.j2'),
        mode = 0644,
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/yarn-env.sh',
        content = InlineTemplate(self.getConfig()['configurations']['yarn-env']['content']),
        owner = 'yarn',
        group = 'hadoop',
        mode = 0755,
      )
      self.assertResourceCalled('File', '/usr/lib/hadoop-yarn/bin/container-executor',
                                group = 'hadoop',
                                mode = 06050,
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/container-executor.cfg',
                                content = Template('container-executor.cfg.j2'),
                                group = 'hadoop',
                                mode = 0644,
                                )
      self.assertResourceCalled('Directory', '/cgroups_test/cpu',
                                group = 'hadoop',
                                recursive = True,
                                mode = 0755,
                                cd_access="a"
      )
      self.assertResourceCalled('File', '/etc/hadoop/conf/mapred-env.sh',
                                content = InlineTemplate(self.getConfig()['configurations']['mapred-env']['content']),
                                owner = 'hdfs',
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/taskcontroller.cfg',
                                content = Template('taskcontroller.cfg.j2'),
                                owner = 'hdfs',
                                )
      self.assertResourceCalled('XmlConfig', 'mapred-site.xml',
                                owner = 'mapred',
                                group = 'hadoop',
                                conf_dir = '/etc/hadoop/conf',
                                configurations = self.getConfig()['configurations']['mapred-site'],
                                configuration_attributes = self.getConfig()['configuration_attributes']['mapred-site']
                                )
      self.assertResourceCalled('XmlConfig', 'capacity-scheduler.xml',
                                owner = 'hdfs',
                                group = 'hadoop',
                                conf_dir = '/etc/hadoop/conf',
                                configurations = self.getConfig()['configurations']['capacity-scheduler'],
                                configuration_attributes = self.getConfig()['configuration_attributes']['capacity-scheduler']
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/fair-scheduler.xml',
                                owner = 'mapred',
                                group = 'hadoop',
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/ssl-client.xml.example',
                                owner = 'mapred',
                                group = 'hadoop',
                                )
      self.assertResourceCalled('File', '/etc/hadoop/conf/ssl-server.xml.example',
                                owner = 'mapred',
                                group = 'hadoop',
                                )
      self.assertNoMoreResources()
  
  
    @patch.object(functions, "get_hdp_version", new=MagicMock(return_value="2.2.0.0-2041"))
    def test_upgrade(self):
      self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/yarn_client.py",
                     classname = "YarnClient",
                     command = "restart",
                     config_file="client-upgrade.json",
                     hdp_stack_version = self.STACK_VERSION,
                     target = RMFTestCase.TARGET_COMMON_SERVICES
      )
  
      self.assertResourceCalled("Execute", "hdp-select set hadoop-client 2.2.1.0-2067")
  
      # for now, it's enough that hdp-select is confirmed
  
    @patch.object(functions, "get_hdp_version", new = MagicMock(return_value='2.3.0.0-1234'))
    def test_pre_rolling_restart_23(self):
      config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/default.json"
      with open(config_file, "r") as f:
        json_content = json.load(f)
      version = '2.3.0.0-1234'
      json_content['commandParams']['version'] = version
  
      mocks_dict = {}
      self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/yarn_client.py",
                         classname = "YarnClient",
                         command = "pre_rolling_restart",
                         config_dict = json_content,
                         hdp_stack_version = self.STACK_VERSION,
                         target = RMFTestCase.TARGET_COMMON_SERVICES,
                         call_mocks = [(0, None), (0, None)],
                         mocks_dict = mocks_dict)
  
      self.assertResourceCalled('Execute', 'hdp-select set hadoop-client {0}'.format(version))
      self.assertNoMoreResources()
  
      self.assertEquals(2, mocks_dict['call'].call_count)
      self.assertEquals(
        "conf-select create-conf-dir --package hadoop --stack-version 2.3.0.0-1234 --conf-version 0",
         mocks_dict['call'].call_args_list[0][0][0])
      self.assertEquals(
        "conf-select set-conf-dir --package hadoop --stack-version 2.3.0.0-1234 --conf-version 0",
         mocks_dict['call'].call_args_list[1][0][0])
  