Skip to content

Commit

Permalink
redo CWS tests using different if cws expected method
Browse files Browse the repository at this point in the history
Allows breaking up into more readable `it` blocks for different segments of
the test.
  • Loading branch information
derekwbrown committed Nov 23, 2023
1 parent 159053a commit c64f523
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ def windows_service_status(service)
raise "windows_service_status is only for windows" unless os == :windows
# Language-independent way of getting the service status
res = `powershell -command \"try { (get-service #{service} -ErrorAction Stop).Status } catch { write-host NOTINSTALLED }\"`
puts res
return (res).upcase.strip
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@


shared_examples_for 'a Windows Agent with CWS driver disabled' do
# We retrieve the value defined in kitchen.yml because there is no simple way
# to set env variables on the target machine or via parameters in Kitchen/Busser
# See https://github.com/test-kitchen/test-kitchen/issues/662 for reference
let(:expect_cws_installed) {
parse_dna().fetch('dd-agent-rspec').fetch('cws_included')
}

it 'has the service disabled' do

if expect_cws_installed
if expect_windows_cws?
it 'has the service disabled' do
expect(is_windows_service_disabled("ddprocmon")).to be_truthy
end
end
Expand All @@ -23,51 +15,50 @@
expect(is_windows_service_installed("datadog-system-probe")).to be_truthy
end

# We retrieve the value defined in kitchen.yml because there is no simple way
# to set env variables on the target machine or via parameters in Kitchen/Busser
# See https://github.com/test-kitchen/test-kitchen/issues/662 for reference

# the `let` has to be inside an it block.
let(:expect_cws_installed) {
parse_dna().fetch('dd-agent-rspec').fetch('cws_included')
}

it 'has properly installed driver files' do
if expect_cws_installed
if expect_windows_cws?
it 'has required services installed' do
expect(is_windows_service_installed("datadog-security-agent")).to be_truthy
expect(is_windows_service_installed("ddprocmon")).to be_truthy

end
it 'has driver files' do
program_files = safe_program_files
expect(File).to exist("#{program_files}\\DataDog\\Datadog Agent\\bin\\agent\\ddprocmon.cat")
expect(File).to exist("#{program_files}\\DataDog\\Datadog Agent\\bin\\agent\\ddprocmon.sys")
expect(File).to exist("#{program_files}\\DataDog\\Datadog Agent\\bin\\agent\\ddprocmon.inf")
else
end

it 'does not have the driver running on install' do
## verify that the driver is not started yet
expect(is_service_running?("ddprocmon")).to be_falsey
end
else
## if windows CWS is not expected, do the reverse check (that services aren't installed, files
## not present, etc.) Once CWS is released, remove this part of the test.
it 'does not have cws services installed' do
expect(is_windows_service_installed("datadog-security-agent")).to be_falsey
expect(is_windows_service_installed("ddprocmon")).to be_falsey

end
it 'does not have driver files' do
program_files = safe_program_files
expect(File).not_to exist("#{program_files}\\DataDog\\Datadog Agent\\bin\\agent\\ddprocmon.cat")
expect(File).not_to exist("#{program_files}\\DataDog\\Datadog Agent\\bin\\agent\\ddprocmon.sys")
expect(File).not_to exist("#{program_files}\\DataDog\\Datadog Agent\\bin\\agent\\ddprocmon.inf")

end
end
it 'does not have the driver running on install' do
## verify that the driver is not started yet
expect(is_service_running?("ddprocmon")).to be_falsey
end



end

shared_examples_for 'a Windows Agent with CWS running' do
# We retrieve the value defined in kitchen.yml because there is no simple way
# to set env variables on the target machine or via parameters in Kitchen/Busser
# See https://github.com/test-kitchen/test-kitchen/issues/662 for reference
let(:expect_cws_installed) {
parse_dna().fetch('dd-agent-rspec').fetch('cws_included')
}
it 'can start security agent' do
if expect_cws_installed
if expect_windows_cws?
it 'has cws services not started by default' do
expect(is_service_running?("datadog-system-probe")).to be_falsey
expect(is_service_running?("datadog-security-agent")).to be_falsey
end

it 'can start security agent' do

sa_conf_path = ""
sp_conf_path = ""
if os != :windows
Expand Down Expand Up @@ -102,24 +93,29 @@
spconfYaml["runtime_security_config"]["enabled"] = true
File.write(sp_conf_path, spconfYaml.to_yaml)

expect(is_service_running?("datadog-system-probe")).to be_falsey
expect(is_service_running?("datadog-security-agent")).to be_falsey
stop "datadog-agent"

start "datadog-agent"
sleep 30
expect(is_service_running?("datadogagent")).to be_truthy
expect(is_service_running?("datadog-system-probe")).to be_truthy
expect(is_service_running?("datadog-security-agent")).to be_truthy


end
it 'can gracefully shut down security agent' do
stop "datadog-agent"

## these tests return false for any state other than running. So "shutting down"
## will erroneously pass here
expect(is_service_running?("datadogagent")).to be_falsey
expect(is_service_running?("datadog-system-probe")).to be_falsey
expect(is_service_running?("datadog-security-agent")).to be_falsey

## so also check that the process is actually gone
expect(security_agent_running?).to be_falsey
expect(system_probe_running?).to be_falsey

end
end
end ## endif expect CWS, no tests at all if not expected.
end


Expand Down

0 comments on commit c64f523

Please sign in to comment.