Skip to content

Test & better document collect_cx_queues#13117

Merged
hithwen merged 20 commits into
masterfrom
js/refactor_check_linux
Oct 17, 2022
Merged

Test & better document collect_cx_queues#13117
hithwen merged 20 commits into
masterfrom
js/refactor_check_linux

Conversation

@hithwen

@hithwen hithwen commented Oct 13, 2022

Copy link
Copy Markdown
Contributor

Followup of #13109

collect_cx_queues option is only used in linux, moving it there and document that it is only used in linux.
Also it had no coverage so adding tests for it.
image

Moved all tests that are linux specific to test_linux.
Explicitly test the linux subclass to avoid so much mocking

def test_cx_state(aggregator):
instance = copy.deepcopy(common.INSTANCE)
instance['collect_connection_state'] = True
check_instance = LinuxNetwork('network', {}, [instance])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other tests now directly instantiate LinuxNetwork

@hithwen
hithwen marked this pull request as ready for review October 13, 2022 14:05
@hithwen
hithwen requested review from a team as code owners October 13, 2022 14:05
clarkb7
clarkb7 previously approved these changes Oct 13, 2022
Comment on lines +191 to +221
@mock.patch('datadog_checks.network.network.Platform.is_linux', return_value=True)
def test_returns_the_right_instance(is_linux, check):
check_instance = check({})
assert isinstance(check_instance, LinuxNetwork)


@pytest.mark.skipif(not Platform.is_linux(), reason="Only works on Linux systems")
def test_collect_cx_queues(check, aggregator):
instance = copy.deepcopy(common.INSTANCE)
instance['collect_connection_queues'] = True
instance['collect_connection_state'] = True
check_instance = LinuxNetwork('network', {}, [instance])

check_instance.check({})

for metric in CONNECTION_QUEUES_METRICS + common.EXPECTED_METRICS:
aggregator.assert_metric(metric)


@pytest.mark.skipif(not Platform.is_linux(), reason="Only works on Linux systems")
def test_collect_cx_queues_when_ss_fails(check, aggregator):
instance = copy.deepcopy(common.INSTANCE)
instance['collect_connection_queues'] = True
instance['collect_connection_state'] = True
check_instance = LinuxNetwork('network', {}, [instance])
with mock.patch('datadog_checks.network.check_linux.get_subprocess_output') as out:
out.side_effect = ss_subprocess_mock_fails
check_instance.check({})

for metric in CONNECTION_QUEUES_METRICS + common.EXPECTED_METRICS:
aggregator.assert_metric(metric)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the new tests

@DataDog DataDog deleted a comment from codecov Bot Oct 13, 2022
@DataDog DataDog deleted a comment from codecov Bot Oct 13, 2022
@codecov

codecov Bot commented Oct 13, 2022

Copy link
Copy Markdown

Codecov Report

Merging #13117 (56b46e0) into master (d550215) will increase coverage by 0.08%.
The diff coverage is 97.17%.

Flag Coverage Δ
network 87.80% <97.17%> (+2.71%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.


@pytest.mark.skipif(not Platform.is_linux(), reason="Only works on Linux systems")
def test_collect_cx_queues(check, aggregator):
instance = copy.deepcopy(common.INSTANCE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been wondering what the difference is between this and providing INSTANCE as a per-test fixture. I'm guessing performance isn't an issue here, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No difference, but since it's a constant I don't really see the need of using a fixture

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we use an instance fixture it's typically the same thing, we simply abstract the copy.deepcopy out inside the fixture.


@pytest.mark.skipif(Platform.is_windows(), reason="Only runs on Unix systems")
@mock.patch('os.listdir', side_effect=os_list_dir_mock)
@mock.patch('datadog_checks.network.check_linux.LinuxNetwork._read_int_file', side_effect=read_int_file_mock)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the team's general stance on using mock.patch? I personally try to only use it for legacy stuff, for new code I feel like I've designed something wrong if I have to mock deep inside a function instead of parametrizing (eg with dependency injection).

@hithwen hithwen Oct 14, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not use dependency injection (except for test fixtures). In general I agree it is usually a smell if you need to use it although one might argue that some times extracting something to a separate module for the sake of correctness can be too much if that something is small and not shared with other things. I don't want to get too philosophical here but here all I'm doing is moving the tests from one file to another and change the SUT from a Network object to a LinuxNetwork object so I do not need to mock the Platform. This is very much legacy code still, I'm just giving it a little bit more structure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for metric, value in iteritems(LINUX_SYS_NET_STATS):
aggregator.assert_metric(metric, value=value[0], tags=['iface:lo'])
aggregator.assert_metric(metric, value=value[1], tags=['iface:ens5'])
aggregator.reset()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this resetting necessary a the end of a test? (I know, this was there already)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hithwen
hithwen merged commit da1f5e0 into master Oct 17, 2022
@hithwen
hithwen deleted the js/refactor_check_linux branch October 17, 2022 08:25
github-actions Bot pushed a commit that referenced this pull request Oct 17, 2022
* move all linux tests to test_linux
* Explicitly test NetworkLinux to avoid so much mocking
* Add more unit tests da1f5e0
steveny91 pushed a commit that referenced this pull request Oct 27, 2022
* move all linux tests to test_linux
* Explicitly test NetworkLinux to avoid so much mocking
* Add more unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants