Skip to content

Commit 87f9ed7

Browse files
committed
Merge remote-tracking branch 'bosh-psmodules/windows-2019' into windows-2019
2 parents 43f765f + 52d9db2 commit 87f9ed7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+6558
-0
lines changed

README-psmodules.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# bosh-psmodules
2+
3+
The recommended approach for creating local BOSH Windows stemcells which can be deployed on [Cloud Foundry BOSH](https://bosh.io), is [`stembuild`](https://github.com/cloudfoundry-incubator/stembuild).
4+
5+
[Documentation on how to use `stembuild` can be found here.](https://bosh.io/docs/windows-stemcell-create/)
6+
7+
---
8+
9+
Powershell scripts to set up a Windows VM in a manner appropriate for a BOSH Stemcell.
10+
11+
Used by [stembuild](https://github.com/cloudfoundry-incubator/stembuild) and [bosh-windows-stemcell-builder](https://github.com/cloudfoundry-incubator/bosh-windows-stemcell-builder)
12+
13+
## Testing
14+
15+
Tests are written using the Pester testing framework and must be run in Powershell on a Windows environment.
16+
17+
The test suite for each module currently assumes that the tests are being run with the module as the current working directory.
18+
19+
This requires iterating through the module directories to run all the tests:
20+
21+
```
22+
cd bosh-psmodules
23+
foreach ($module in (Get-ChildItem "./modules").Name) {
24+
Push-Location "modules/$module"
25+
$results=Invoke-Pester -PassThru
26+
if ($results.FailedCount -gt 0) {
27+
$result += $results.FailedCount
28+
}
29+
Pop-Location
30+
}
31+
echo "Failed Tests: $result"
32+
```
33+
34+
If you just need to test a single module, you could do this:
35+
36+
```
37+
cd "bosh-psmodules\module\BOSH.<module>"
38+
Invoke-Pester
39+
```
40+
41+
## Running a subset of tests on MacOS
42+
43+
You can use Powershell and MacOS to run the tests that do not require Windows system calls:
44+
45+
```
46+
cd ~/workspace
47+
brew install powershell
48+
git clone --depth 1 --branch 4.4.0 [email protected]:pester/Pester.git
49+
pwsh
50+
Import-Module ./Pester/Pester.psm1
51+
cd stembuild/module/BOSH.<module>
52+
Invoke-Pester
53+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Remove-Module -Name BOSH.Account -ErrorAction Ignore
2+
Import-Module ./BOSH.Account.psm1
3+
4+
Remove-Module -Name BOSH.Utils -ErrorAction Ignore
5+
Import-Module ../BOSH.Utils/BOSH.Utils.psm1
6+
7+
Describe "Account" {
8+
9+
Context "when username is not provided" {
10+
It "throws" {
11+
{ Add-Account } | Should Throw "Provide a user name"
12+
}
13+
}
14+
15+
Context "when password is not provided" {
16+
It "throws" {
17+
{ Add-Account -User hello } | Should Throw "Provide a password"
18+
}
19+
}
20+
21+
Context "when the username and password are valid" {
22+
$timestamp=(get-date -UFormat "%s" -Millisecond 0)
23+
$user = "TestUser_$timestamp"
24+
$password = "Password123!"
25+
26+
BeforeEach {
27+
$userExists = !!(Get-LocalUser | Where {$_.Name -eq $user})
28+
if($userExists) {
29+
Remove-LocalUser -Name $user
30+
}
31+
}
32+
33+
It "Adds and removes a new user account" {
34+
Add-Account -User $user -Password $password
35+
mkdir "C:\Users\$user" -ErrorAction Ignore
36+
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
37+
$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
38+
$existing | Should Not Be $null
39+
Remove-Account -User $user
40+
$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
41+
$existing | Should Be $null
42+
}
43+
}
44+
}
45+
46+
Remove-Module -Name BOSH.Account -ErrorAction Ignore
47+
Remove-Module -Name BOSH.Utils -ErrorAction Ignore
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@{
2+
RootModule = 'BOSH.Account'
3+
ModuleVersion = '0.1'
4+
GUID = 'ebdf7a79-39df-46ce-a046-42dd10de82ce'
5+
Author = 'BOSH'
6+
Copyright = '(c) 2017 BOSH'
7+
Description = 'Commands for creating a new Windows user'
8+
PowerShellVersion = '4.0'
9+
RequiredModules = @('BOSH.Utils')
10+
FunctionsToExport = @('Add-Account','Remove-Account')
11+
CmdletsToExport = @()
12+
VariablesToExport = '*'
13+
AliasesToExport = @()
14+
PrivateData = @{
15+
PSData = @{
16+
Tags = @('BOSH')
17+
LicenseUri = 'https://github.com/cloudfoundry-incubator/bosh-windows-stemcell-builder/blob/master/LICENSE'
18+
ProjectUri = 'https://github.com/cloudfoundry-incubator/bosh-windows-stemcell-builder'
19+
}
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<#
2+
.Synopsis
3+
Add Windows user
4+
.Description
5+
This cmdlet adds a Windows user
6+
#>
7+
function Add-Account {
8+
Param(
9+
[string]$User = $(Throw "Provide a user name"),
10+
[string]$Password = $(Throw "Provide a password")
11+
)
12+
Write-Log "Add-Account"
13+
14+
Write-Log "Creating new local user $User."
15+
& NET USER $User $Password /add /y /expires:never
16+
17+
$Group = "Administrators"
18+
19+
Write-Log "Adding local user $User to $Group."
20+
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
21+
Write-Log $adsi
22+
$AdminGroup = $adsi.Children | where {$_.SchemaClassName -eq 'group' -and $_.Name -eq $Group }
23+
Write-Log $AdminGroup
24+
$UserObject = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $User }
25+
Write-Log $UserObject
26+
$AdminGroup.Add($UserObject.Path)
27+
Write-Log "Completed adding $User to $Group"
28+
}
29+
30+
<#
31+
.Synopsis
32+
Remove Windows user
33+
.Description
34+
This cmdlet removes a Windows user
35+
#>
36+
function Remove-Account {
37+
Param(
38+
[string]$User = $(Throw "Provide a user name")
39+
)
40+
Write-Log "Remove-Account"
41+
Write-Log "Removing local user $User."
42+
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
43+
$adsi.Delete('User', $User)
44+
Move-Item -Path "C:\Users\$User" -Destination "$env:windir\Temp\$User" -Force -ErrorAction Ignore
45+
}

0 commit comments

Comments
 (0)