forked from microsoft/omi
-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.py
executable file
·228 lines (174 loc) · 8.3 KB
/
test.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
# Copyright: (c) 2020, Jordan Borean (@jborean93) <jborean93@gmail.com>
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import argparse
import os.path
import subprocess
import tempfile
from utils import (
argcomplete,
build_bash_script,
build_package_command,
build_package_repo_command,
complete_distribution,
docker_run,
get_version,
load_distribution_config,
OMI_REPO,
select_distribution,
)
def main():
"""Main program body."""
args = parse_args()
distribution = select_distribution(args)
if not distribution:
return
distro_details = load_distribution_config(distribution)
if args.docker and not distro_details['container_image']:
raise ValueError("Cannot run --docker on %s as no container_image has been specified" % distribution)
# On macOS we aren't running as root in a container so this step needs sudo.
sudo_prefix = 'sudo ' if distribution.startswith('macOS') else ''
script_steps = []
if not args.skip_deps:
repo_script = build_package_repo_command(distro_details['package_manager'], distro_details['microsoft_repo'])
dep_script = build_package_command(distro_details['package_manager'], distro_details['test_deps'])
script_steps.append(('Setting up the Microsoft package manager repo', repo_script))
if distribution == 'debian9':
debian_ms = 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list'
script_steps.append(('Further steps for MS repo on Debian 9', debian_ms))
script_steps.append(('Installing test dependency packages', dep_script))
install_pswsman = '''cat > /tmp/install-pswsman.ps1 << EOL
\$ErrorActionPreference = 'Stop'
\$repoParams = @{
Name = 'PSWSManRepo'
PublishLocation = './build'
SourceLocation = './build'
InstallationPolicy = 'Trusted'
}
if (Get-PSRepository -Name \$repoParams.Name -ErrorAction SilentlyContinue) {
Unregister-PSRepository -Name \$repoParams.Name
}
Register-PSRepository @repoParams
try {
Install-Module -Name PSWSMan -Repository \$repoParams.Name -Scope CurrentUser
} finally {
Unregister-PSRepository -Name \$repoParams.Name
}
EOL
pwsh -NoProfile -NoLogo -File /tmp/install-pswsman.ps1
'''
script_steps.append(('Installing PSWSMan module', install_pswsman))
cert_path = os.path.join('integration_environment', 'cert_setup', 'ca.pem')
if os.path.exists(os.path.join(OMI_REPO, cert_path)):
cert_cmd = "%spwsh -Command 'Register-TrustedCertificate -Path %s -Verbose'" \
% (sudo_prefix, cert_path)
script_steps.append(('Adding CA chain to system trust store', cert_cmd))
pwsh_deps = '''cat > /tmp/pwsh-requirements.ps1 << EOL
\$ErrorActionPreference = 'Stop'
\$ProgressPreference = 'SilentlyContinue'
Install-Module -Name Pester -MinimumVersion 5.0 -Force
Install-Module -Name powershell-yaml -Force
Install-Module -Name MSAL.PS -Force -AcceptLicense
EOL
pwsh -NoProfile -NoLogo -File /tmp/pwsh-requirements.ps1'''
script_steps.append(('Installing Pester 5+ and other PowerShell deps', pwsh_deps))
install_script = '''PWSHDIR="$( dirname "$( readlink "$( which pwsh )" )" )"
%spwsh -Command 'Install-WSMan -Verbose\'''' % sudo_prefix
script_steps.append(('Copying lib artifacts to the PowerShell directory', install_script))
pester_script = '''cat > /tmp/pwsh-test.ps1 << EOL
\$ErrorActionPreference = 'Stop'
\$ProgressPreference = 'SilentlyContinue'
Import-Module -Name Pester -MinimumVersion 5.0
\$configuration = [PesterConfiguration]::Default
\$configuration.Output.Verbosity = 'Detailed'
\$configuration.Run.Path = 'libmi.tests.ps1'
\$configuration.Run.Exit = \$true
Invoke-Pester -Configuration \$configuration
EOL
echo "%s" > /tmp/distro.txt''' % distribution
script_steps.append(('Creating Pester test script', pester_script))
script_steps.append(('Getting PowerShell version', 'pwsh -Command \$PSVersionTable'))
script_steps.append(('Getting libmi version',
"pwsh -Command 'Get-WSManVersion'"))
if distribution.startswith('macOS'):
script_steps.append(('Output libpsrpclient libraries', 'otool -L -arch all "${PWSHDIR}/libpsrpclient.dylib"'))
script_steps.append(('Output libmi libraries', 'otool -L -arch all "${PWSHDIR}/libmi.dylib"'))
else:
script_steps.append(('Output libpsrpclient libraries', 'ldd "${PWSHDIR}/libpsrpclient.so"'))
script_steps.append(('Output libmi libraries', 'ldd "${PWSHDIR}/libmi.so"'))
if args.interactive:
script_steps.append(('Opening interactive shell', '/bin/bash'))
elif args.verify_version:
build_id = os.environ.get('OMI_BUILDVERSION_BUILDNR', '0')
verify_version = "%s.%s.%s.%s" % (get_version() + (build_id,))
script_steps.append(('Verify libraries are loaded and match %s' % verify_version,
'''cat > /tmp/version-test.ps1 << EOL
\$ErrorActionPreference = 'Stop'
\$ProgressPreference = 'SilentlyContinue'
Import-Module -Name PSWSMan
\$expectedVersion = [Version]'%s'
\$actualVersions = Get-WSManVersion
if (\$actualVersions.MI -ne \$expectedVersion) {
throw "libmi version '\$(\$actualVersions.MI)' does not match expected version '\$expectedVersion'"
}
if (\$actualVersions.PSRP -ne \$expectedVersion) {
throw "libpsrpclient version '\$(\$actualVersions.PSRP)' does not match expected version '\$expectedVersion'"
}
"SUCCESS: Versions are good"
EOL
pwsh -NoProfile -NoLogo -File /tmp/version-test.ps1''' % verify_version))
else:
script_steps.append(('Running PowerShell test', 'pwsh -NoProfile -NoLogo -File /tmp/pwsh-test.ps1'))
test_script = build_bash_script(script_steps)
if args.output_script:
print(test_script)
else:
with tempfile.NamedTemporaryFile(dir=OMI_REPO, prefix='test-', suffix='-%s.sh' % distribution) as temp_fd:
temp_fd.write(test_script.encode('utf-8'))
temp_fd.flush()
if args.docker:
docker_run(distro_details['container_image'], '/omi/%s' % os.path.basename(temp_fd.name),
env={'KRB5_CONFIG': '/omi/krb5.conf'}, interactive=args.interactive, shell=distro_details['shell'])
else:
print("Running tests locally")
subprocess.check_call(['bash', temp_fd.name], cwd=OMI_REPO)
def parse_args():
"""Parse and return args."""
parser = argparse.ArgumentParser(description='Test the OMI library in PowerShell.')
parser.add_argument('distribution',
metavar='distribution',
nargs='?',
default=None,
help='The distribution to test.').completer = complete_distribution
parser.add_argument('--interactive',
dest='interactive',
action='store_true',
help='When combined with --docker will start an interactive session in the test container.')
parser.add_argument('--skip-deps',
dest='skip_deps',
action='store_true',
help='Skip installing any dependencies.')
parser.add_argument('--verify-version',
dest='verify_version',
action='store_true',
help='Will only test that the library can be loaded and the version is the value expected.')
run_group = parser.add_mutually_exclusive_group()
run_group.add_argument('--docker',
dest='docker',
action='store_true',
help='Whether to test OMI in a docker container.')
run_group.add_argument('--output-script',
dest='output_script',
action='store_true',
help='Will print out the bash script that can test the library.')
if argcomplete:
argcomplete.autocomplete(parser)
args = parser.parse_args()
if args.interactive and not args.docker:
parser.error('arguement --interactive: must be set with argument --docker')
return args
if __name__ == '__main__':
main()