Skip to content

Commit

Permalink
win_domain_membership: added better error handling and basic tests (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
jborean93 authored Sep 21, 2017
1 parent 5918735 commit fb628ac
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/ansible/modules/windows/win_domain_membership.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ Function Join-Domain {
}
$argstr = $add_args | Out-String
Write-DebugLog "calling Add-Computer with args: $argstr"
$add_result = Add-Computer @add_args
try {
$add_result = Add-Computer @add_args
} catch {
Fail-Json -obj $result -message "failed to join domain: $($_.Exception.Message)"
}

Write-DebugLog ("Add-Computer result was \n{0}" -f $add_result | Out-String)
}
Expand All @@ -140,8 +144,16 @@ Function Set-Workgroup {
)

Write-DebugLog ("Calling JoinDomainOrWorkgroup with workgroup {0}" -f $workgroup_name)
try {
$swg_result = (Get-WmiObject -ClassName Win32_ComputerSystem).JoinDomainOrWorkgroup($workgroup_name)
} catch {
Fail-Json -obj $result -message "failed to call Win32_ComputerSystem.JoinDomainOrWorkgroup($workgroup_name): $($_.Exception.Message)"
}

return (Get-WmiObject Win32_ComputerSystem).JoinDomainOrWorkgroup($workgroup_name)
if ($swg_result.ReturnValue -ne 0) {
Fail-Json -obj $result -message "failed to set workgroup through WMI, return value: $($swg_result.ReturnValue)"

return $swg_result}
}

Function Join-Workgroup {
Expand All @@ -155,7 +167,11 @@ Function Join-Workgroup {
$domain_cred = Create-Credential $domain_admin_user $domain_admin_password

# 2012+ call the Workgroup arg WorkgroupName, but seem to accept
$rc_result = Remove-Computer -Workgroup $workgroup_name -Credential $domain_cred -Force
try {
$rc_result = Remove-Computer -Workgroup $workgroup_name -Credential $domain_cred -Force
} catch {
Fail-Json -obj $result -message "failed to remove computer from domain: $($_.Exception.Message)"
}
}

# we're already on a workgroup- change it.
Expand Down
1 change: 1 addition & 0 deletions test/integration/targets/win_domain_membership/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
windows/ci/group2
20 changes: 20 additions & 0 deletions test/integration/targets/win_domain_membership/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: get current workgroup
win_shell: (Get-WmiObject Win32_ComputerSystem).Workgroup
register: workgroup

- name: fail if workgroup result is empty (means test host is in a domain)
fail:
msg: Cannot run tests for win_domain_membership when host is a member of a domain
when: workgroup.stdout == ""

- block:
- include_tasks: tests.yml

always:
- name: revert workgroup back to original before tests
win_domain_membership:
workgroup_name: '{{workgroup.stdout_lines[0]}}'
state: workgroup
domain_admin_user: fake user
domain_admin_password: fake password
59 changes: 59 additions & 0 deletions test/integration/targets/win_domain_membership/tasks/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
- name: change workgroup (check mode)
win_domain_membership:
workgroup_name: ANSIBLETEST
state: workgroup
domain_admin_user: fake user
domain_admin_password: fake password
register: change_workgroup_check
check_mode: yes

- name: get result of change workgroup (check mode)
win_shell: (Get-WmiObject Win32_ComputerSystem).Workgroup
register: change_workgroup_result_check

- name: assert result of change workgroup (check mode)
assert:
that:
- change_workgroup_check|changed
- change_workgroup_result_check.stdout == workgroup.stdout

- name: change workgroup
win_domain_membership:
workgroup_name: ANSIBLETEST
state: workgroup
domain_admin_user: fake user
domain_admin_password: fake password
register: change_workgroup

- name: get result of change workgroup
win_shell: (Get-WmiObject Win32_ComputerSystem).Workgroup
register: change_workgroup_result

- name: assert result of change workgroup
assert:
that:
- change_workgroup|changed
- change_workgroup_result.stdout_lines[0] == "ANSIBLETEST"

- name: change workgroup (idempotent)
win_domain_membership:
workgroup_name: ANSIBLETEST
state: workgroup
domain_admin_user: fake user
domain_admin_password: fake password
register: change_workgroup_again

- name: assert result of change workgroup (idempotent)
assert:
that:
- not change_workgroup_again|changed

- name: change workgroup fail invalid name
win_domain_membership:
workgroup_name: ANSIBLELONGNAMEFAILURE
state: workgroup
domain_admin_user: fake user
domain_admin_password: fake password
register: fail_change_workgroup
failed_when: "fail_change_workgroup.msg != 'failed to set workgroup through WMI, return value: 2695'"

0 comments on commit fb628ac

Please sign in to comment.