#!/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 os, json, copy, crypt
from mock.mock import patch
from stacks.utils.RMFTestCase import RMFTestCase


class TestHawqSegment(RMFTestCase):

  COMMON_SERVICES_PACKAGE_DIR = 'HAWQ/2.0.0/package'
  STACK_VERSION = '2.3'
  GPADMIN = 'gpadmin'
  POSTGRES = 'postgres'
  CONFIG_FILE = os.path.join(os.path.dirname(__file__), '../configs/hawq_default.json')
  with open(CONFIG_FILE, "r") as f:
    hawq_config = json.load(f)

  def setUp(self):
    self.config_dict = copy.deepcopy(self.hawq_config)

  def __asserts_for_configure(self):

    self.assertResourceCalled('Group', self.GPADMIN,
        ignore_failures = True
        )

    self.assertResourceCalled('User', self.GPADMIN,
        gid = self.GPADMIN,
        groups = [self.GPADMIN, u'hadoop'],
        ignore_failures = True,
        password = crypt.crypt(self.getConfig()['configurations']['hawq-env']['hawq_password'], "$1$salt$")
        )

    self.assertResourceCalled('Group', self.POSTGRES,
        ignore_failures = True
        )

    self.assertResourceCalled('User', self.POSTGRES,
        gid = self.POSTGRES,
        groups = [self.POSTGRES, u'hadoop'],
        ignore_failures = True
        )

    self.assertResourceCalled('Execute', 'chown -R gpadmin:gpadmin /usr/local/hawq/',
        timeout = 600
        )

    self.assertResourceCalled('XmlConfig', 'hdfs-client.xml',
        conf_dir = '/usr/local/hawq/etc/',
        configurations = self.getConfig()['configurations']['hdfs-client'],
        configuration_attributes = self.getConfig()['configuration_attributes']['hdfs-client'],
        group = self.GPADMIN,
        owner = self.GPADMIN,
        mode = 0644
        )

    self.assertResourceCalled('XmlConfig', 'yarn-client.xml',
        conf_dir = '/usr/local/hawq/etc/',
        configurations = self.getConfig()['configurations']['yarn-client'],
        configuration_attributes = self.getConfig()['configuration_attributes']['yarn-client'],
        group = self.GPADMIN,
        owner = self.GPADMIN,
        mode = 0644
        )

    self.assertResourceCalled('XmlConfig', 'hawq-site.xml',
        conf_dir = '/usr/local/hawq/etc/',
        configurations = self.getConfig()['configurations']['hawq-site'],
        configuration_attributes = self.getConfig()['configuration_attributes']['hawq-site'],
        group = self.GPADMIN,
        owner = self.GPADMIN,
        mode = 0644
        )

    self.assertResourceCalled('Directory', '/data/hawq/segment',
        owner = self.GPADMIN,
        group = self.GPADMIN,
        create_parents = True
        )

    self.assertResourceCalled('Execute', 'chmod 700 /data/hawq/segment',
        user = 'root',
        timeout = 600
        )

    self.assertResourceCalled('Directory', '/data/hawq/tmp/segment',
        owner = self.GPADMIN,
        group = self.GPADMIN,
        create_parents = True
        )


  @patch ('hawqsegment.common.__set_osparams')
  def test_configure_default(self, set_osparams_mock):

    self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + '/scripts/hawqsegment.py',
        classname = 'HawqSegment',
        command = 'configure',
        config_dict = self.config_dict,
        stack_version = self.STACK_VERSION,
        target = RMFTestCase.TARGET_COMMON_SERVICES
        )

    self.__asserts_for_configure()
    self.assertNoMoreResources()


  @patch ('hawqsegment.common.__set_osparams')
  def test_install_default(self, set_osparams_mock):

    self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + '/scripts/hawqsegment.py',
        classname = 'HawqSegment',
        command = 'install',
        config_dict = self.config_dict,
        stack_version = self.STACK_VERSION,
        target = RMFTestCase.TARGET_COMMON_SERVICES
        )

    self.__asserts_for_configure()
    self.assertNoMoreResources()


  @patch ('hawqsegment.common.__set_osparams')
  def test_start_default(self, set_osparams_mock):

    self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + '/scripts/hawqsegment.py',
        classname = 'HawqSegment',
        command = 'start',
        config_dict = self.config_dict,
        stack_version = self.STACK_VERSION,
        target = RMFTestCase.TARGET_COMMON_SERVICES
        )

    self.__asserts_for_configure()

    self.assertResourceCalled('Execute', 'source /usr/local/hawq/greenplum_path.sh && hawq init segment -a -v',
        logoutput = True,
        not_if = None,
        only_if = None,
        user = self.GPADMIN,
        timeout = 900
        )

    self.assertNoMoreResources()


  @patch ('common.get_local_hawq_site_property_value')
  def test_stop_default(self, get_local_hawq_site_property_value_mock):
    get_local_hawq_site_property_value_mock.return_value = 40000

    self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + '/scripts/hawqsegment.py',
        classname = 'HawqSegment',
        command = 'stop',
        config_dict = self.config_dict,
        stack_version = self.STACK_VERSION,
        target = RMFTestCase.TARGET_COMMON_SERVICES
        )

    self.assertResourceCalled('Execute', 'source /usr/local/hawq/greenplum_path.sh && hawq stop segment -M fast -a -v',
        logoutput = True,
        not_if = None,
        only_if = "netstat -tupln | egrep ':40000\\s' | egrep postgres",
        user = self.GPADMIN,
        timeout = 900
        )

    self.assertNoMoreResources()
