summaryrefslogtreecommitdiff
path: root/test/lib/ansible_test/_data/playbooks/windows_coverage_teardown.yml
blob: ab34dc277064704cbce64f27e108ac8d34351665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
- name: collect the coverage files from the Windows host
  hosts: windows
  gather_facts: no
  tasks:
  - name: make sure all vars have been set
    assert:
      that:
      - local_temp_path is defined
      - remote_temp_path is defined

  - name: zip up all coverage files in the
    ansible.windows.win_shell: |
      $coverage_dir = '{{ remote_temp_path }}'
      $zip_file = Join-Path -Path $coverage_dir -ChildPath 'coverage.zip'
      if (Test-Path -LiteralPath $zip_file) {
          Remove-Item -LiteralPath $zip_file -Force
      }

      $coverage_files = Get-ChildItem -LiteralPath $coverage_dir -Include '*=coverage*' -File

      $legacy = $false
      try {
          # Requires .NET 4.5+ which isn't present on older WIndows versions. Remove once 2008/R2 is EOL.
          # We also can't use the Shell.Application as it will fail on GUI-less servers (Server Core).
          Add-Type -AssemblyName System.IO.Compression -ErrorAction Stop > $null
      } catch {
          $legacy = $true
      }

      if ($legacy) {
          New-Item -Path $zip_file -ItemType File > $null
          $shell = New-Object -ComObject Shell.Application
          $zip = $shell.Namespace($zip_file)
          foreach ($file in $coverage_files) {
              $zip.CopyHere($file.FullName)
          }
      } else {
          $fs = New-Object -TypeName System.IO.FileStream -ArgumentList $zip_file, 'CreateNew'
          try {
              $archive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList @(
                  $fs,
                  [System.IO.Compression.ZipArchiveMode]::Create
              )
              try  {
                  foreach ($file in $coverage_files) {
                      $archive_entry = $archive.CreateEntry($file.Name, 'Optimal')
                      $entry_fs = $archive_entry.Open()
                      try {
                          $file_fs = [System.IO.File]::OpenRead($file.FullName)
                          try {
                              $file_fs.CopyTo($entry_fs)
                          } finally {
                              $file_fs.Dispose()
                          }
                      } finally {
                          $entry_fs.Dispose()
                      }
                  }
              } finally {
                  $archive.Dispose()
              }
          } finally {
              $fs.Dispose()
          }
      }

  - name: fetch coverage zip file to localhost
    fetch:
      src: '{{ remote_temp_path }}\coverage.zip'
      dest: '{{ local_temp_path }}/coverage-{{ inventory_hostname }}.zip'
      flat: yes

  - name: remove the temporary coverage directory
    ansible.windows.win_file:
      path: '{{ remote_temp_path }}'
      state: absent