Skip to content

Commit

Permalink
use obtained pod to check for env var; remove use of oc exec
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Jul 7, 2017
1 parent c005125 commit efd978b
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 50 deletions.
34 changes: 17 additions & 17 deletions roles/openshift_health_checker/openshift_checks/fluentd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def check_logging_config(self, task_vars):
Returns an error string if the above condition is not met, or None otherwise."""
use_journald = get_var(task_vars, "openshift_logging_fluentd_use_journal", default=True)

# if check is running on a master, use `ocutil` to read the "USE_JOURNAL"
# environment variable inside one of the fluentd pods
# if check is running on a master, retrieve all running pods
# and check any pod's container for the env var "USE_JOURNAL"
group_names = get_var(task_vars, "group_names")
if "masters" in group_names:
use_journald = self.check_fluentd_env_var(task_vars)
Expand Down Expand Up @@ -95,24 +95,24 @@ def check_logging_config(self, task_vars):
def check_fluentd_env_var(self, task_vars):
"""Read and return the value of the 'USE_JOURNAL' environment variable on a fluentd pod."""
running_pods = self.running_fluentd_pods(task_vars)
pod_name = running_pods[0]["metadata"]["name"]
cmd_string = "exec {} /bin/printenv USE_JOURNAL".format(pod_name)
pod_containers = running_pods[0]["spec"]["containers"]

try:
use_journald = self.exec_oc(
self.execute_module,
self.logging_namespace,
cmd_string,
[], task_vars
)
except OpenShiftCheckException as error:
if "false" not in str(error):
msg = "Invalid value received from command {}: {}".format(cmd_string, str(error))
raise OpenShiftCheckException(msg)
if not pod_containers:
msg = ('There are no running containers on selected Fluentd pod "{}".\n'
'Unable to calculate expected logging driver.').format(running_pods[0]["metadata"]["name"])
raise OpenShiftCheckException(msg)

pod_env = pod_containers[0]["env"]
if not pod_env:
msg = ('There are no environment variables set on the Fluentd container "{}".\n'
'Unable to calculate expected logging driver.').format(pod_containers[0]["name"])
raise OpenShiftCheckException(msg)

use_journald = False
for env in pod_env:
if env["name"] == "USE_JOURNAL":
return env["value"] != "false"

return use_journald
return False

def running_fluentd_pods(self, task_vars):
"""Return a list of running fluentd pods."""
Expand Down
156 changes: 123 additions & 33 deletions roles/openshift_health_checker/test/fluentd_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,44 @@ def canned_fluentd_config(exec_oc=None):
return check


def canned_fluentd_pod(containers=list()):
return {
"metadata": {
"labels": {"component": "fluentd", "deploymentconfig": "logging-fluentd"},
"name": "logging-fluentd-1",
},
"spec": {
"host": "node1",
"nodeName": "node1",
"containers": containers,
},
"status": {
"phase": "Running",
"containerStatuses": [{"ready": True}],
"conditions": [{"status": "True", "type": "Ready"}],
}
}

fluentd_pod = {
"metadata": {
"labels": {"component": "fluentd", "deploymentconfig": "logging-fluentd"},
"name": "logging-fluentd-1",
},
"spec": {"host": "node1", "nodeName": "node1"},
"spec": {
"host": "node1",
"nodeName": "node1",
"containers": [
{
"name": "container1",
"env": [
{
"name": "USE_JOURNAL",
"value": "true",
}
],
}
],
},
"status": {
"phase": "Running",
"containerStatuses": [{"ready": True}],
Expand Down Expand Up @@ -101,51 +133,97 @@ def execute_module(module_name, args, task_vars):
assert error is None


@pytest.mark.parametrize('name, pods, response, logging_driver, extra_words', [
@pytest.mark.parametrize('name, pods, logging_driver, extra_words', [
(
'test failure with use_journald=false, but docker config set to use "journald"',
[fluentd_pod],
{
"failed": True,
"result": "false",
},
[canned_fluentd_pod(
[
{
"name": "container1",
"env": [{
"name": "USE_JOURNAL",
"value": "false",
}],
},
]
)],
"journald",
['json log files', 'has been set to use "journald"'],
),
(
'test failure with use_journald=true, but docker config set to use "json-file"',
[fluentd_pod],
{
"failed": False,
"result": "true",
},
"json-file",
['logs from "journald"', 'has been set to use "json-file"'],
),
(
'test failure with use_journald=false, but docker set to use an "unsupported" driver',
[fluentd_pod],
{
"failed": True,
"result": "false",
},
[canned_fluentd_pod(
[
{
"name": "container1",
"env": [{
"name": "USE_JOURNAL",
"value": "false",
}],
},
]
)],
"unsupported",
["json log files", 'has been set to use "unsupported"'],
),
# use_journald returns false (not using journald), but check succeeds
# since docker is set to use json-file
# # use_journald returns false (not using journald), but check succeeds
# # since docker is set to use json-file
(
'test success with use_journald=false, and docker config set to use default driver "json-file"',
[fluentd_pod],
{
"failed": True,
"result": "false",
},
[canned_fluentd_pod(
[
{
"name": "container1",
"env": [{
"name": "USE_JOURNAL",
"value": "false",
}],
},
]
)],
"json-file",
[],
),
(
'test success with USE_JOURNAL env var missing and docker config set to use default driver "json-file"',
[canned_fluentd_pod(
[
{
"name": "container1",
"env": [{
"name": "RANDOM",
"value": "value",
}],
},
]
)],
"json-file",
[],
),
(
'test failure with USE_JOURNAL env var missing and docker config set to use "journald"',
[canned_fluentd_pod(
[
{
"name": "container1",
"env": [{
"name": "RANDOM",
"value": "value",
}],
},
]
)],
"journald",
["configuration is set to", "json log files"],
),
], ids=lambda argvals: argvals[0])
def test_check_logging_config_master(name, pods, response, logging_driver, extra_words):
def test_check_logging_config_master(name, pods, logging_driver, extra_words):
def execute_module(module_name, args, task_vars):
if module_name == "docker_info":
return {
Expand All @@ -154,9 +232,6 @@ def execute_module(module_name, args, task_vars):
}
}

if module_name == "ocutil":
return response

return {}

task_vars = dict(
Expand Down Expand Up @@ -185,14 +260,32 @@ def get_pods(execute_module, namespace, logging_component, task_vars):

@pytest.mark.parametrize('name, pods, response, logging_driver, extra_words', [
(
'test OpenShiftCheckException with use_journald=false, but docker config set to use default "json-file" driver',
[fluentd_pod],
'test OpenShiftCheckException with no running containers',
[canned_fluentd_pod([])],
{
"failed": True,
"result": "unexpected",
},
"json-file",
['Unexpected error', 'using `oc` to validate'],
['no running containers'],
),
(
'test OpenShiftCheckException one container and no env vars set',
[canned_fluentd_pod(
[
{
"name": "container1",
"env": [],
},
]
)]
,
{
"failed": True,
"result": "unexpected",
},
"json-file",
['no environment variables'],
),
], ids=lambda argvals: argvals[0])
def test_failed_check_logging_config_master(name, pods, response, logging_driver, extra_words):
Expand All @@ -204,9 +297,6 @@ def execute_module(module_name, args, task_vars):
}
}

if module_name == "ocutil":
return response

return {}

task_vars = dict(
Expand Down

0 comments on commit efd978b

Please sign in to comment.