Skip to content

Commit

Permalink
[IBM mq] Do not allow for empty values in configuration (#11138)
Browse files Browse the repository at this point in the history
* Do not allow for empty values in configuration

Co-authored-by: Kari Halsted <12926135+kayayarai@users.noreply.github.com>
  • Loading branch information
hithwen and kayayarai committed Jan 25, 2022
1 parent b89996e commit b12871b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 39 deletions.
17 changes: 16 additions & 1 deletion ibm_mq/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ files:
value:
example: 'DEV.ADMIN.SVRCONN'
type: string
minLength: 1
description: The IBMMQ Channel to connect to.
- name: queue_manager
required: true
description: The name of the queue manager.
value:
example: 'datadog'
type: string
minLength: 1
- name: host
value:
example: 'localhost'
type: string
minLength: 1
description: The host IBM MQ is running on.
- name: port
value:
Expand All @@ -42,14 +45,17 @@ files:
Either `host/port` or `connection_name` configuration must be provided.
value:
type: string
minLength: 1
- name: username
value:
type: string
minLength: 1
description: <USERNAME> of the IBMMQ Channel to connect to.
- name: password
secret: true
value:
type: string
minLength: 1
description: <PASSWORD> of the IBMMQ Channel to connect to.
- name: queues
description: |
Expand All @@ -59,6 +65,7 @@ files:
value:
example: ['<QUEUE_NAME>']
type: array
uniqueItems: true
items:
type: string
- name: queue_patterns
Expand All @@ -71,6 +78,7 @@ files:
- 'DEV.*' # match queues starting with "DEV."
- 'SYSTEM.*' # match queues starting with "SYSTEM."
type: array
uniqueItems: true
items:
type: string
- name: queue_regex
Expand All @@ -83,13 +91,15 @@ files:
- '^DEV\..*$' # match queues starting with "DEV."
- '^SYSTEM\..*$' # match queues starting with "SYSTEM."
type: array
uniqueItems: true
items:
type: string
- name: channels
description: Check the status of the following channels
value:
example: ['<CHANNEL_NAME>']
type: array
uniqueItems: true
items:
type: string
- name: channel_status_mapping
Expand All @@ -113,6 +123,7 @@ files:
paused: warning
initializing: warning
type: object
minProperties: 1
properties: []
- name: auto_discover_queues
description: |
Expand All @@ -139,6 +150,7 @@ files:
value:
example: 6
type: number
minimum: 1
- name: convert_endianness
description: |
If you are monitoring IBM MQ remotely and if the Agent and the server are using different endianness,
Expand All @@ -156,12 +168,13 @@ files:
value:
example: Etc/UTC
type: string
minLength: 1
- name: timeout
description: |
The number of seconds to wait for IBM MQ to respond.
value:
example: 5
type: number
type: integer
- name: ssl_auth
description: Whether or not to use SSL auth while connecting to the channel.
value:
Expand All @@ -185,6 +198,7 @@ files:
value:
example: '/var/mqm/ssl-db/client/KeyringClient'
type: string
minLength: 1
- name: ssl_certificate_label
description: Certificate label used for SSL connection.
value:
Expand All @@ -201,6 +215,7 @@ files:
'SYSTEM.*': queue_type:system
'DEV.*': role:dev,queue_type:default
type: object
minProperties: 1
- template: instances/default
- template: logs
example:
Expand Down
22 changes: 11 additions & 11 deletions ibm_mq/datadog_checks/ibm_mq/config_models/instance.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) Datadog, Inc. 2021-present
# (C) Datadog, Inc. 2022-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

Expand All @@ -11,7 +11,7 @@

from typing import Any, Mapping, Optional, Sequence

from pydantic import BaseModel, root_validator, validator
from pydantic import BaseModel, Field, root_validator, validator

from datadog_checks.base.utils.functions import identity
from datadog_checks.base.utils.models import validation
Expand All @@ -24,21 +24,21 @@ class Config:
allow_mutation = False

auto_discover_queues: Optional[bool]
channel: str
channel: str = Field(..., min_length=1)
channel_status_mapping: Optional[Mapping[str, Any]]
channels: Optional[Sequence[str]]
collect_statistics_metrics: Optional[bool]
connection_name: Optional[str]
connection_name: Optional[str] = Field(None, min_length=1)
convert_endianness: Optional[bool]
disable_generic_tags: Optional[bool]
empty_default_hostname: Optional[bool]
host: Optional[str]
host: Optional[str] = Field(None, min_length=1)
min_collection_interval: Optional[float]
mqcd_version: Optional[float]
password: Optional[str]
mqcd_version: Optional[float] = Field(None, ge=1.0)
password: Optional[str] = Field(None, min_length=1)
port: Optional[int]
queue_manager: str
queue_manager_timezone: Optional[str]
queue_manager: str = Field(..., min_length=1)
queue_manager_timezone: Optional[str] = Field(None, min_length=1)
queue_patterns: Optional[Sequence[str]]
queue_regex: Optional[Sequence[str]]
queue_tag_re: Optional[Mapping[str, Any]]
Expand All @@ -49,8 +49,8 @@ class Config:
ssl_cipher_spec: Optional[str]
ssl_key_repository_location: Optional[str]
tags: Optional[Sequence[str]]
timeout: Optional[float]
username: Optional[str]
timeout: Optional[int]
username: Optional[str] = Field(None, min_length=1)

@root_validator(pre=True)
def _initial_validation(cls, values):
Expand Down
2 changes: 1 addition & 1 deletion ibm_mq/datadog_checks/ibm_mq/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ instances:
#
# queue_manager_timezone: Etc/UTC

## @param timeout - number - optional - default: 5
## @param timeout - integer - optional - default: 5
## The number of seconds to wait for IBM MQ to respond.
#
# timeout: 5
Expand Down
48 changes: 24 additions & 24 deletions ibm_mq/tests/test_ibm_mq_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
pytestmark = [skip_windows_ci, pytest.mark.usefixtures("dd_environment"), pytest.mark.integration]


def test_no_msg_errors_are_caught(get_check, instance, caplog):
def test_no_msg_errors_are_caught(get_check, instance, caplog, dd_run_check):
# Late import to ignore missing library for e2e
from pymqi import MQMIError, PCFExecute
from pymqi.CMQC import MQCC_FAILED, MQRC_NO_MSG_AVAILABLE
Expand All @@ -33,12 +33,12 @@ def test_no_msg_errors_are_caught(get_check, instance, caplog):
m.side_effect = error
m.unpack = PCFExecute.unpack
check = get_check(instance)
check.check(instance)
dd_run_check(check)

assert not caplog.records


def test_unknown_service_check(aggregator, get_check, instance, caplog):
def test_unknown_service_check(aggregator, get_check, instance, caplog, dd_run_check):
# Late import to ignore missing library for e2e
from pymqi import MQMIError, PCFExecute
from pymqi.CMQC import MQCC_FAILED, MQRC_NO_MSG_AVAILABLE
Expand All @@ -51,7 +51,7 @@ def test_unknown_service_check(aggregator, get_check, instance, caplog):
m.side_effect = error
m.unpack = PCFExecute.unpack
check = get_check(instance)
check.check(instance)
dd_run_check(check)

tags = [
'queue_manager:{}'.format(common.QUEUE_MANAGER),
Expand All @@ -68,7 +68,7 @@ def test_unknown_service_check(aggregator, get_check, instance, caplog):
aggregator.assert_service_check('ibm_mq.channel', check.UNKNOWN, tags=channel_tags, count=1)


def test_errors_are_logged(get_check, instance, caplog):
def test_errors_are_logged(get_check, instance, caplog, dd_run_check):
# Late import to ignore missing library for e2e
from pymqi import MQMIError, PCFExecute
from pymqi.CMQC import MQCC_FAILED, MQRC_BUFFER_ERROR
Expand All @@ -82,16 +82,16 @@ def test_errors_are_logged(get_check, instance, caplog):
m.side_effect = error
m.unpack = PCFExecute.unpack
check = get_check(instance)
check.check(instance)
dd_run_check(check)

assert caplog.records


def test_check_metrics_and_service_checks(aggregator, get_check, instance, seed_data):
def test_check_metrics_and_service_checks(aggregator, get_check, instance, seed_data, dd_run_check):
instance['mqcd_version'] = os.getenv('IBM_MQ_VERSION')
check = get_check(instance)

check.check(instance)
dd_run_check(check)

assert_all_metrics(aggregator)

Expand All @@ -113,11 +113,11 @@ def test_check_metrics_and_service_checks(aggregator, get_check, instance, seed_
aggregator.assert_service_check('ibm_mq.channel', check.OK, tags=discoverable_tags, count=1)


def test_check_connection_name_one(aggregator, get_check, instance_with_connection_name):
def test_check_connection_name_one(aggregator, get_check, instance_with_connection_name, dd_run_check):
instance_with_connection_name['mqcd_version'] = os.getenv('IBM_MQ_VERSION')

check = get_check(instance_with_connection_name)
check.check(instance_with_connection_name)
dd_run_check(check)

assert_all_metrics(aggregator)

Expand All @@ -130,20 +130,20 @@ def test_check_connection_name_one(aggregator, get_check, instance_with_connecti
aggregator.assert_service_check('ibm_mq.channel', check.OK, tags=channel_tags, count=1)


def test_check_connection_names_multi(aggregator, get_check, instance_with_connection_name):
def test_check_connection_names_multi(aggregator, get_check, instance_with_connection_name, dd_run_check):
instance = instance_with_connection_name
instance['mqcd_version'] = os.getenv('IBM_MQ_VERSION')
instance['connection_name'] = "localhost(9999),{}".format(instance['connection_name'])

check = get_check(instance)
check.check(instance)
dd_run_check(check)

assert_all_metrics(aggregator)


def test_check_all(aggregator, get_check, instance_collect_all, seed_data):
def test_check_all(aggregator, get_check, instance_collect_all, seed_data, dd_run_check):
check = get_check(instance_collect_all)
check.check(instance_collect_all)
dd_run_check(check)

assert_all_metrics(aggregator)

Expand All @@ -153,26 +153,26 @@ def test_check_all(aggregator, get_check, instance_collect_all, seed_data):
[({'running': 'warning'}, AgentCheck.WARNING), ({'running': 'critical'}, AgentCheck.CRITICAL)],
)
def test_integration_custom_mapping(
aggregator, get_check, instance, channel_status_mapping, expected_service_check_status
aggregator, get_check, instance, channel_status_mapping, expected_service_check_status, dd_run_check
):
instance['channel_status_mapping'] = channel_status_mapping
check = get_check(instance)

check.check(instance)
dd_run_check(check)

aggregator.assert_service_check('ibm_mq.channel.status', expected_service_check_status)


def test_check_queue_pattern(aggregator, get_check, instance_queue_pattern, seed_data):
def test_check_queue_pattern(aggregator, get_check, instance_queue_pattern, seed_data, dd_run_check):
check = get_check(instance_queue_pattern)
check.check(instance_queue_pattern)
dd_run_check(check)

assert_all_metrics(aggregator)


def test_check_queue_regex(aggregator, get_check, instance_queue_regex, seed_data):
def test_check_queue_regex(aggregator, get_check, instance_queue_regex, seed_data, dd_run_check):
check = get_check(instance_queue_regex)
check.check(instance_queue_regex)
dd_run_check(check)

assert_all_metrics(aggregator)

Expand Down Expand Up @@ -231,9 +231,9 @@ def test_check_channel_count_status_unknown(aggregator, get_check, instance_queu
)


def test_check_regex_tag(aggregator, get_check, instance_queue_regex_tag, seed_data):
def test_check_regex_tag(aggregator, get_check, instance_queue_regex_tag, seed_data, dd_run_check):
check = get_check(instance_queue_regex_tag)
check.check(instance_queue_regex_tag)
dd_run_check(check)

tags = [
'queue_manager:{}'.format(common.QUEUE_MANAGER),
Expand All @@ -249,7 +249,7 @@ def test_check_regex_tag(aggregator, get_check, instance_queue_regex_tag, seed_d
aggregator.assert_metric(metric, tags=tags)


def test_stats_metrics(aggregator, get_check, instance):
def test_stats_metrics(aggregator, get_check, instance, dd_run_check):
# Late import to ignore missing library for e2e
from pymqi import MQMIError
from pymqi.CMQC import MQCC_FAILED, MQRC_NO_MSG_AVAILABLE
Expand All @@ -272,7 +272,7 @@ def test_stats_metrics(aggregator, get_check, instance):
queue_data,
MQMIError(MQCC_FAILED, MQRC_NO_MSG_AVAILABLE),
]
check.check(instance)
dd_run_check(check)

common_tags = [
'queue_manager:{}'.format(common.QUEUE_MANAGER),
Expand Down
4 changes: 2 additions & 2 deletions ibm_mq/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

@skip_windows_ci
@pytest.mark.integration
def test_metadata(get_check, instance, datadog_agent):
def test_metadata(get_check, instance, datadog_agent, dd_run_check):
check = get_check(instance)
check.check_id = 'test:123'
check.check(instance)
dd_run_check(check)

raw_version = MQ_VERSION_RAW
major, minor, mod, fix = raw_version.split('.')
Expand Down

0 comments on commit b12871b

Please sign in to comment.