<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2015: Alignak team, see AUTHORS.txt file for contributors
#
# This file is part of Alignak.
#
# Alignak is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Alignak is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Alignak.  If not, see <http://www.gnu.org/licenses/>.
#
#
# This file incorporates work covered by the following copyright and
# permission notice:
#
#  Copyright (C) 2009-2014:
#     xkilian, <EMAIL>
#     <NAME>, <EMAIL>
#     <NAME>, <EMAIL>
#     <NAME>, <EMAIL>
#     <NAME>, <EMAIL>
#     <NAME>, <EMAIL>

#  This file is part of Shinken.
#
#  Shinken is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Shinken is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Shinken.  If not, see <http://www.gnu.org/licenses/>.

#
# This file is used to test reading and processing of config files
#

from __future__ import print_function

import os
import tempfile
import shutil

from alignak_test import *

import alignak.log as alignak_log

from alignak.daemon import InvalidPidFile, InvalidWorkDir
from alignak.http_daemon import PortNotFree

from alignak.daemons.pollerdaemon import Poller
from alignak.daemons.brokerdaemon import Broker
from alignak.daemons.schedulerdaemon import Alignak
from alignak.daemons.reactionnerdaemon import Reactionner
from alignak.daemons.arbiterdaemon import Arbiter
try:
    import pwd, grp
    from pwd import getpwnam
    from grp import getgrnam


    def get_cur_user():
        return pwd.getpwuid(os.getuid()).pw_name

    def get_cur_group():
        return grp.getgrgid(os.getgid()).gr_name

except ImportError, exp:  # Like in nt system or Android
    # temporary workaround:
    def get_cur_user():
        return os.getlogin()

    def get_cur_group():
        return os.getlogin()

THIS_DIR = os.getcwd()

daemons_config = {
    Broker:       "etc/core/daemons/brokerd.ini",
    Poller:       "etc/core/daemons/pollerd.ini",
    Reactionner:  "etc/core/daemons/reactionnerd.ini",
    Alignak:      "etc/core/daemons/schedulerd.ini",
    Arbiter:    ["etc/core/alignak.cfg"]
}

#############################################################################

class template_Daemon_Bad_Start():

    @classmethod
    def setupClass(cls):
        time_hacker.set_real_time()  # just to be sure..

    def get_login_and_group(self, p):
        try:
            p.user = get_cur_user()
            p.group = get_cur_group()
        except OSError:  # on some rare case, we can have a problem here
            # so bypass it and keep default value
            return

    def create_daemon(self):
        cls = self.daemon_cls
        return cls(daemons_config[cls], False, True, False, None, '')

    def get_daemon(self):

        alignak_log.local_log = None  # otherwise get some "trashs" logs..
        d = self.create_daemon()

        # configuration is "relative" : some config file reference others with
        # a relative path (from THIS_DIR).
        # so any time we load it we have to make sure we are back at THIS_DIR:
        os.chdir(THIS_DIR)

        d.load_config_file()
        d.http_backend = 'wsgiref'
        d.port = get_free_port()<commit_msg>Fix: test_bad_start: added missing tearDown<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2015: Alignak team, see AUTHORS.txt file for contributors
#
# This file is part of Alignak.
#
# Alignak is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Alignak is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Alignak.  If not, see <http://www.gnu.org/licenses/>.
#
#
# This file incorporates work covered by the following copyright and
# permission notice:
#
#  Copyright (C) 2009-2014:
#     xkilian, <EMAIL>
#     <NAME>, <EMAIL>
#     <NAME>, <EMAIL>
#     <NAME>, <EMAIL>
#     <NAME>, <EMAIL>
#     <NAME>, <EMAIL>

#  This file is part of Shinken.
#
#  Shinken is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Shinken is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Shinken.  If not, see <http://www.gnu.org/licenses/>.

#
# This file is used to test reading and processing of config files
#

from __future__ import print_function

import os
import tempfile
import shutil

from alignak_test import *

import alignak.log as alignak_log

from alignak.daemon import InvalidPidFile, InvalidWorkDir
from alignak.http_daemon import PortNotFree

from alignak.daemons.pollerdaemon import Poller
from alignak.daemons.brokerdaemon import Broker
from alignak.daemons.schedulerdaemon import Alignak
from alignak.daemons.reactionnerdaemon import Reactionner
from alignak.daemons.arbiterdaemon import Arbiter
try:
    import pwd, grp
    from pwd import getpwnam
    from grp import getgrnam


    def get_cur_user():
        return pwd.getpwuid(os.getuid()).pw_name

    def get_cur_group():
        return grp.getgrgid(os.getgid()).gr_name

except ImportError, exp:  # Like in nt system or Android
    # temporary workaround:
    def get_cur_user():
        return os.getlogin()

    def get_cur_group():
        return os.getlogin()


daemons_config = {
    Broker:       "etc/core/daemons/brokerd.ini",
    Poller:       "etc/core/daemons/pollerd.ini",
    Reactionner:  "etc/core/daemons/reactionnerd.ini",
    Alignak:      "etc/core/daemons/schedulerd.ini",
    Arbiter:    ["etc/core/alignak.cfg"]
}

#############################################################################

class template_Daemon_Bad_Start():

    @classmethod
    def setupClass(cls):
        time_hacker.set_real_time()  # just to be sure..
        # the daemons startup code does actually a `chrdir`,
        # in Daemon.change_to_workdir,
        # so in order to be always safe, let's save the cwd when we are setup,
        # we'll chdir() to it in tearDown..
        cls._launch_dir = os.getcwd()

    @classmethod
    def tearDown(cls):
        os.chdir(cls._launch_dir)

    def get_login_and_group(self, p):
        try:
            p.user = get_cur_user()
            p.group = get_cur_group()
        except OSError:  # on some rare case, we can have a problem here
            # so bypass it and keep default value
            return

    def create_daemon(self):
        cls = self.daemon_cls
        return cls(daemons_config[cls], False, True, False, None, '')

    def get_daemon(self):

        alignak_log.local_log = None  # otherwise get some "trashs" logs..
        d = self.create_daemon()

        # configuration is actually "relative" :
        # some config file reference others with a relative path (from THIS_DIR).
        # so any time we load it we have to make sure we are back at THIS_DIR:
        # THIS_DIR should also be equal to self._launch_dir, so use that:
        os.chdir(self._launch_dir)

        d.load_config_file()
        d.http_backend = 'wsgiref'
        d.port = get_free_port()
{'fraction_non_alphanumeric': 0.09823475469967904, 'fraction_numerical': 0.004355800091701055, 'mean_word_length': 3.200770341839191, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3791245', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2636, 'n_words': 961}