<commit_before># Copyright 2013 tsuru-circus authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import copy
import os
import unittest

from tsuru.plugins import ApprcWatcher

NOPATH_OUTPUT = {
    "status": "ok",
    "options": {
        "max_retry": 5,
        "env": {"SOMETHING": "adios"},
    },
}


def create_fake_call(status_return, options_return=NOPATH_OUTPUT):
    kw = []

    def fake_call(command, *args, **kwargs):
        if command == "status":
            return status_return
        elif command == "set":
            kw.append(kwargs)
            return {"set": "ok"}
        elif command == "options":
            return options_return
    return fake_call, kw


class ApprcWatcherTest(unittest.TestCase):
    def test_add_envs(self):
        plugin = ApprcWatcher("", "", 1)
        plugin.call, kw = create_fake_call(None)
        plugin.add_envs(name="name", envs={"foo": "bar"})
        env = copy.deepcopy(os.environ)
        env["foo"] = "bar"
        expected = [{"name": "name", "options": {"env": env}}]
        self.assertEqual(expected, kw)

    def test_add_envs_may_override_os_environ(self):
        def remove_env(name):
            del os.environ[name]
        os.environ["SOMETHING_UNKNOWN"] = "123"
        self.addCleanup(remove_env, "SOMETHING_UNKNOWN")
        plugin = ApprcWatcher("", "", 1)
        plugin.call, kw = create_fake_call(None)
        plugin.add_envs(name="name", envs={"foo": "bar", "SOMETHING_UNKNOWN": "456"})
        env = copy.deepcopy(os.environ)
        env["SOMETHING_UNKNOWN"] = "456"
        env["foo"] = "bar"
        expected = [{"name": "name", "options": {"env": env}}]
        self.assertEqual(expected, kw)

    def test_add_envs_dont_call_set_when_variables_dont_change(self):
        self.maxDiff = None
        plugin = ApprcWatcher("", "", 1)
        envs = copy.deepcopy(NOPATH_OUTPUT)
        envs["options"]["env"].update(os.environ)
        plugin.call, kw = create_fake_call(None, envs)
        plugin.add_envs(name="name", envs=NOPATH_OUTPUT["options"]["env"])
        self.assertEqual([], kw)

    def test_add_envs_dont_call_set_when_variables_dont_change_and_ses_os_environ(self):
        self.maxDiff = None
        plugin = ApprcWatcher("", "", 1)
        envs = copy.deepcopy(NOPATH_OUTPUT)
        envs["options"]["env"].update(os.environ)
        plugin.call, kw = create_fake_call(None, envs)
        plugin.add_envs(name="name", envs=envs["options"]["env"])
        self.assertEqual([], kw)

    def test_look_after_add_envs(self):
        sr = {"statuses": {"name": "name", "cmd": "cmd"}}
        fake_call, kw = create_fake_call(sr)
        plugin = ApprcWatcher("", "", 1)
        plugin.call = fake_call
        plugin.apprc = os.path.join(os.path.dirname(__file__),
                                    "testdata/apprc")
        plugin.look_after()
        env = {"VAR1": "value-1", "port": "8888", "VAR2": "value2"}
        env.update(os.environ)
        expected = [
            {"name": "cmd", "options": {"env": env}},
            {"name": "name", "options": {"env": env}},
        ]
        self.assertEqual(expected, sorted(kw, key=lambda x: x["name"]))

    def test_look_after_skips_plugins(self):
        sr = {"statuses": {"name": "name",
                           "plugin:tsuru-circus-ApprcWatcher": "ok"}}
        fake_call, kw = create_fake_call(sr)
        plugin = ApprcWatcher("", "", 1)
        plugin.call = fake_call
        plugin.apprc = os.path.join(os.path.dirname(__file__),
                                    "testdata/apprc")
        plugin.look_after()
        env = {"VAR1": "value-1", "port": "8888", "VAR2": "value2"}<commit_msg>plugin/apprc_watcher: add test to ensure that variables may change<commit_after># Copyright 2013 tsuru-circus authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import copy
import os
import unittest

from tsuru.plugins import ApprcWatcher

NOPATH_OUTPUT = {
    "status": "ok",
    "options": {
        "max_retry": 5,
        "env": {"SOMETHING": "adios"},
    },
}


def create_fake_call(status_return, options_return=NOPATH_OUTPUT):
    kw = []

    def fake_call(command, *args, **kwargs):
        if command == "status":
            return status_return
        elif command == "set":
            kw.append(kwargs)
            return {"set": "ok"}
        elif command == "options":
            return options_return
    return fake_call, kw


class ApprcWatcherTest(unittest.TestCase):
    def test_add_envs(self):
        plugin = ApprcWatcher("", "", 1)
        plugin.call, kw = create_fake_call(None)
        plugin.add_envs(name="name", envs={"foo": "bar"})
        env = copy.deepcopy(os.environ)
        env["foo"] = "bar"
        expected = [{"name": "name", "options": {"env": env}}]
        self.assertEqual(expected, kw)

    def test_add_envs_may_override_os_environ(self):
        def remove_env(name):
            del os.environ[name]
        os.environ["SOMETHING_UNKNOWN"] = "123"
        self.addCleanup(remove_env, "SOMETHING_UNKNOWN")
        plugin = ApprcWatcher("", "", 1)
        plugin.call, kw = create_fake_call(None)
        plugin.add_envs(name="name", envs={"foo": "bar", "SOMETHING_UNKNOWN": "456"})
        env = copy.deepcopy(os.environ)
        env["SOMETHING_UNKNOWN"] = "456"
        env["foo"] = "bar"
        expected = [{"name": "name", "options": {"env": env}}]
        self.assertEqual(expected, kw)

    def test_add_envs_dont_call_set_when_variables_dont_change(self):
        plugin = ApprcWatcher("", "", 1)
        envs = copy.deepcopy(NOPATH_OUTPUT)
        envs["options"]["env"].update(os.environ)
        plugin.call, kw = create_fake_call(None, envs)
        plugin.add_envs(name="name", envs=NOPATH_OUTPUT["options"]["env"])
        self.assertEqual([], kw)

    def test_add_envs_dont_call_set_when_variables_dont_change_and_ses_os_environ(self):
        plugin = ApprcWatcher("", "", 1)
        envs = copy.deepcopy(NOPATH_OUTPUT)
        envs["options"]["env"].update(os.environ)
        plugin.call, kw = create_fake_call(None, envs)
        plugin.add_envs(name="name", envs=envs["options"]["env"])
        self.assertEqual([], kw)

    def test_add_envs_override_environs(self):
        plugin = ApprcWatcher("", "", 1)
        envs = copy.deepcopy(NOPATH_OUTPUT)
        envs["options"]["env"]["foo"] = "foo"
        plugin.call, kw = create_fake_call(None, envs)
        plugin.add_envs(name="name", envs={"foo": "bar"})
        env = copy.deepcopy(os.environ)
        env["foo"] = "bar"
        expected = [{"name": "name", "options": {"env": env}}]
        self.assertEqual(expected, kw)

    def test_look_after_add_envs(self):
        sr = {"statuses": {"name": "name", "cmd": "cmd"}}
        fake_call, kw = create_fake_call(sr)
        plugin = ApprcWatcher("", "", 1)
        plugin.call = fake_call
        plugin.apprc = os.path.join(os.path.dirname(__file__),
                                    "testdata/apprc")
        plugin.look_after()
        env = {"VAR1": "value-1", "port": "8888", "VAR2": "value2"}
        env.update(os.environ)
        expected = [
            {"name": "cmd", "options": {"env": env}},
            {"name": "name", "options": {"env": env}},
        ]
        self.assertEqual(expected, sorted(kw, key=lambda x: x["name"]))

    def test_look_after_skips_plugins(self):
        sr = {"statuses": {"name": "name",
                           "plugin:tsuru-circus-ApprcWatcher": "ok"}}
        fake_call, kw = create_fake_call(sr)
        plugin = ApprcWatcher("", "", 1)
        plugin.call = fake_call
        plugin.apprc = os.path.join(os.path.dirname(__file__),
                                    "testdata/apprc")
        plugin.look_after()
        env = {"VAR1": "value-1", "port": "8888", "VAR2": "value2"}
{'fraction_non_alphanumeric': 0.15629722921914357, 'fraction_numerical': 0.009193954659949622, 'mean_word_length': 2.6510344827586207, 'pattern_counts': {'":': 76, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5205111', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2622, 'n_words': 513}