1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
2
$moduleHome = Split-Path -Parent $here
3
$moduleRoot = Split-Path -Parent $moduleHome
5
$modulePath = ${env:PSModulePath}.Split(";")
6
if(!($moduleRoot -in $modulePath)){
7
$env:PSModulePath += ";$moduleRoot"
9
$savedEnv = [System.Environment]::GetEnvironmentVariables()
11
Import-Module JujuHooks
13
function Clear-Environment {
14
$current = [System.Environment]::GetEnvironmentVariables()
15
foreach($i in $savedEnv.GetEnumerator()) {
16
[System.Environment]::SetEnvironmentVariable($i.Name, $i.Value, "Process")
18
$current = [System.Environment]::GetEnvironmentVariables()
19
foreach ($i in $current.GetEnumerator()){
20
if(!$savedEnv[$i.Name]){
21
[System.Environment]::SetEnvironmentVariable($i.Name, $null, "Process")
26
Describe "Test Confirm-ContextComplete" {
30
It "Should return False" {
35
Confirm-ContextComplete -Context $ctx | Should Be $false
41
Confirm-ContextComplete -Context $ctx | Should Be $false
44
Confirm-ContextComplete -Context $ctx | Should Be $false
47
It "Should return True" {
51
Confirm-ContextComplete -Context $ctx | Should Be $true
54
It "Should Throw an exception" {
55
$ctx = "not a hashtable"
56
{ Confirm-ContextComplete -Context $ctx} | Should Throw
60
Describe "Test hook environment functions" {
64
It "Should return charm_dir" {
65
$env:CHARM_DIR = "bogus"
66
Get-JujuCharmDir | Should Be "bogus"
69
It "Should return relation name" {
70
$env:JUJU_RELATION = "bogus"
71
Get-JujuRelationType | Should be "bogus"
74
It "Confirm-JujuRelation should return True" {
75
$env:JUJU_RELATION = "bogus"
76
Confirm-JujuRelation | Should be $true
79
It "Confirm-JujuRelation should return False" {
80
$env:JUJU_RELATION = ""
81
Confirm-JujuRelation | Should be $false
84
It "Get-JujuRelationId should return relation ID" {
85
$env:JUJU_RELATION_ID = "bogus:1"
86
Get-JujuRelationId | Should Be "bogus:1"
89
It "Get-JujuLocalUnit should return unit name" {
90
$env:JUJU_UNIT_NAME = "unit-1"
91
Get-JujuLocalUnit | Should Be "unit-1"
94
It "Get-JujuRemoteUnit should return remote unit" {
95
$env:JUJU_REMOTE_UNIT = "remote-1"
96
Get-JujuRemoteUnit | Should Be "remote-1"
99
It "Get-JujuServiceName should get service name" {
100
$env:JUJU_UNIT_NAME = "active-directory/0"
101
Get-JujuServiceName | Should Be "jujud-active-directory-0"
105
Describe "Test Get-JujuCharmConfig" {
106
Mock Invoke-JujuCommand -ModuleName JujuHooks {
111
"stringOption"="Hello";
114
if($Command.Length -gt 2) {
117
return (ConvertTo-Yaml $ret[$x])
121
return (ConvertTo-Yaml $ret)
123
It "Should return a Hashtable" {
124
(Get-JujuCharmConfig).GetType() | Should Be "Hashtable"
125
(Get-JujuCharmConfig).stringOption | Should Be "Hello"
126
(Get-JujuCharmConfig).intOption | Should Be 1
129
It "Should return a string" {
130
Get-JujuCharmConfig -Scope "stringOption" | Should Be "Hello"
133
It "Should return an int" {
134
Get-JujuCharmConfig -Scope "intOption" | Should Be 1
137
It "Should return empty" {
138
Get-JujuCharmConfig -Scope "nonexisting" | Should BeNullOrEmpty
142
Describe "Test Get-JujuRelation" {
146
Context "Invoke Get-JujuRelation without params" {
147
Mock Invoke-JujuCommand -ModuleName JujuHooks {
151
$expect = @("relation-get.exe", "--format=yaml", "-")
152
if ((Compare-Object $Command $expect)) {
153
Throw "Invalid parameters"
155
return '{"user": "guest"}'
158
It "Should pass only - as attr" {
159
$env:JUJU_REMOTE_UNIT = "bogus"
160
$env:JUJU_RELATION_ID = "amqp:1"
161
(Get-JujuRelation).GetType() | Should Be "Hashtable"
162
(Get-JujuRelation).user | Should Be "guest"
164
It "Should throw an exception" {
165
{ Get-JujuRelation }| Should Throw
169
Context "Invoke Get-JujuRelation with Unit"{
170
Mock Invoke-JujuCommand -ModuleName JujuHooks {
174
$expect = @("relation-get.exe", "--format=yaml", "-", "bogus")
175
if ((Compare-Object $Command $expect)) {
176
Throw "Invalid parameters"
179
It "Should pass - and unit" {
180
$env:JUJU_RELATION_ID = "amqp:1"
181
Get-JujuRelation -Unit "bogus" | Should BeNullOrEmpty
183
It "Should throw an exception" {
184
{ Get-JujuRelation -Unit "bogus" } | Should Throw
188
Context "Invoke Get-JujuRelation with Unit and relation ID"{
189
Mock Invoke-JujuCommand -ModuleName JujuHooks {
193
$expect = @("relation-get.exe", "--format=yaml", "-r", "amqp:1", "-", "bogus")
194
if ((Compare-Object $Command $expect)) {
195
Throw "Invalid parameters"
197
return '{"test": "test", "hello": "world"}'
200
It "Should pass unit, relation id and attribute" {
201
$r = Get-JujuRelation -Unit "bogus" -RelationID "amqp:1"
202
$r.GetType() | Should Be "hashtable"
203
$r["test"] | Should Be "test"
204
$r["hello"] | Should Be "world"
208
Context "Invoke Get-JujuRelation with Unit, relation ID and attribute"{
209
Mock Invoke-JujuCommand -ModuleName JujuHooks {
213
$expect = @("relation-get.exe", "--format=yaml", "-r", "amqp:1", "name", "bogus")
214
if ((Compare-Object $Command $expect)) {
215
Throw "Invalid parameters"
220
It "Should pass unit, relation id and attribute" {
221
Get-JujuRelation -Unit "bogus" -RelationID "amqp:1" -Attribute "name" | Should Be "test"
226
Describe "Test Set-JujuRelation"{
231
Context "Call Set-JujuRelation without RelationID" {
232
Mock Invoke-JujuCommand -ModuleName JujuHooks {
236
$expect = @("relation-set.exe", "--file")
237
if ((Compare-Object $Command[0..1] $expect) -or !(Test-Path $Command[-1])) {
238
Throw ("Invalid parameters: {0}" -f ($Command -Join " "))
242
It "Should pass name=value" {
243
$env:JUJU_RELATION_ID = "amqp:1"
247
Set-JujuRelation -Settings $params | Should Be $true
249
It "Should throw an exception (Missing relation ID)" {
253
{ Set-JujuRelation -Settings $params } | Should Throw
257
Context "Call Set-JujuRelation with RelationID" {
258
Mock Invoke-JujuCommand -ModuleName JujuHooks {
262
$expect = @("relation-set.exe", "-r", "amqp:1", "--file")
263
if ((Compare-Object $Command[0..3] $expect) -or !(Test-Path $Command[-1])) {
264
Throw ("Invalid parameters: {0}" -f ($Command -Join " "))
267
It "Should pass relationID" {
271
Set-JujuRelation -Settings $params -RelationID "amqp:1" | Should Be $true
274
It "Should Throw an exception (Missing relation ID)" {
275
{ Set-JujuRelation -RelationID "amqp:1" } | Should Throw
281
Describe "Test Get-JujuRelationIds" {
285
Context "Call Get-JujuRelationIds without -Relation"{
286
Mock Invoke-JujuCommand -ModuleName JujuHooks {
290
$expect = @("relation-ids.exe", "--format=yaml", "amqp")
291
if ((Compare-Object $Command $expect)) {
292
Throw "Invalid parameters"
294
return '["amqp:1", "amqp:2"]'
296
It "Should throw an exception (Missing relation type)" {
297
{ Get-JujuRelationIds } | Should Throw
299
It "Should return relation ID" {
300
$env:JUJU_RELATION = "amqp"
301
Get-JujuRelationIds | Should Be @("amqp:1", "amqp:2")
302
(Get-JujuRelationIds).GetType().BaseType.Name | Should Be "Array"
305
Context "Call Get-JujuRelationIds with -Relation"{
306
Mock Invoke-JujuCommand -ModuleName JujuHooks {
310
$expect = @("relation-ids.exe", "--format=yaml", "shared-db")
311
if ((Compare-Object $Command $expect)) {
312
Throw "Invalid parameters"
316
It "Should return relation ID" {
317
Get-JujuRelationIds -Relation "shared-db" | Should Be "mysql:1"
322
Describe "Test Get-JujuRelatedUnits" {
326
Context "Call Get-JujuRelatedUnits without -RelationID"{
327
Mock Invoke-JujuCommand -ModuleName JujuHooks {
331
$expect = @("relation-list.exe", "--format=yaml", "-r", "amqp:1")
332
if ((Compare-Object $Command $expect)) {
333
Throw "Invalid parameters"
335
return '["rabbitmq/0", "rabbitmq/1"]'
337
It "Should throw an exception (Missing relation ID)" {
338
{ Get-JujuRelatedUnits } | Should Throw
340
It "Should return related units" {
341
$env:JUJU_RELATION_ID = "amqp:1"
342
Get-JujuRelatedUnits | Should Be @("rabbitmq/0", "rabbitmq/1")
345
Context "Get-JujuRelatedUnits with -Relation"{
346
Mock Invoke-JujuCommand -ModuleName JujuHooks {
350
$expect = @("relation-list.exe", "--format=yaml", "-r","shared-db")
351
if ((Compare-Object $Command $expect)) {
352
Throw "Invalid parameters"
356
It "Should return related units" {
357
Get-JujuRelatedUnits -RelationID "shared-db" | Should Be "mysql:1"
362
Describe "Test Get-JujuRelationForUnit" {
363
Context "Call Get-JujuRelationForUnit"{
364
Mock Invoke-JujuCommand -ModuleName JujuHooks {
368
$expect = @("relation-get.exe", "--format=yaml", "-r", "amqp:1", "-", "bogus")
369
if ((Compare-Object $Command $expect)) {
370
Throw "Invalid parameters"
372
return '{"test": "test", "hello": "world", "test-list": "hello world"}'
375
It "Should pass unit and relation id. Return hashtable" {
376
$r = Get-JujuRelationForUnit -Unit "bogus" -RelationId "amqp:1"
377
$r.GetType() | Should Be "hashtable"
378
$r["test"] | Should Be "test"
379
$r["hello"] | Should Be "world"
380
$r["test-list"] | Should Be @("hello", "world")
386
Describe "Test Get-JujuRelationForId" {
387
Context "Call Get-JujuRelationForId"{
388
Mock Get-JujuRelatedUnits -ModuleName JujuHooks {
389
return @("rabbitmq/0", "rabbitmq/1")
391
Mock Get-JujuRelationForUnit -ModuleName JujuHooks {
395
[string]$RelationId=$null
397
if ($RelationId -ne "amqp:1"){
398
Throw "Invalid relationID. Expected amqp:1"
401
'rabbitmq/0'= @{"rabbit-0-test"="test-0"; "rabbit-0-hello"="rabbit-0-world";};
402
'rabbitmq/1'= @{"rabbit-1-test"="test-1"; "rabbit-1-hello"="rabbit-1-world";};
406
It "Should get array of relation data" {
407
$r = Get-JujuRelationForId -RelationId "amqp:1"
408
$r.GetType().BaseType.Name | Should Be "Array"
409
$r.Count | Should Be 2
410
$r[0]["rabbit-0-test"] | Should Be "test-0"
411
$r[0]["rabbit-0-hello"] | Should Be "rabbit-0-world"
412
$r[1]["rabbit-1-test"] | Should Be "test-1"
413
$r[1]["rabbit-1-hello"] | Should Be "rabbit-1-world"
416
It "Should throw an exception (Missing relation ID)" {
417
{ Get-JujuRelationForId } | Should Throw
422
Describe "Test Get-JujuRelationsOfType" {
423
Mock Get-JujuRelationIds -ModuleName JujuHooks {
426
[string]$Relation=$null
428
if($Relation -ne "amqp") {
431
return @("amqp:1", "amqp:2")
433
Mock Get-JujuRelationForUnit -ModuleName JujuHooks {
437
[string]$RelationId=$null
442
"rabbit-0-test"="test-0";
445
"rabbit-1-test"="test-1";
446
"rabbit-1-test2"="test-2";
455
if(!$data[$RelationID]){
456
Throw "Invalid relation ID"
458
$x = $data[$RelationId][$Unit]
461
Mock Get-JujuRelatedUnits -ModuleName JujuHooks {
464
[string]$RelationId=$null
467
"amqp:1"=@("rabbitmq/0", "rabbitmq/1");
468
"amqp:2"=@("keystone/0")
470
return $data[$RelationId]
472
It "Should return an array of relation data" {
473
$r = Get-JujuRelationsOfType -Relation "amqp"
474
$r.GetType().BaseType.Name | Should Be "Array"
475
$r.Count | Should Be 3
478
It "Should return empty" {
479
$r = Get-JujuRelationsOfType -Relation "bogus"
480
$r | Should BeNullOrEmpty
484
Describe "Test Confirm-JujuRelationCreated" {
485
Mock Get-JujuRelationIds -ModuleName JujuHooks {
488
[string]$Relation=$null
491
"amqp" = @("amqp:1", "amqp:2");
494
return $relations[$Relation]
496
It "Should return True" {
497
Confirm-JujuRelationCreated -Relation "amqp" | Should Be $true
500
It "Should return False" {
501
Confirm-JujuRelationCreated -Relation "bogus" | Should Be $false
503
It "Should return False on non existing relation" {
504
Confirm-JujuRelationCreated -Relation "bogus" | Should Be $false
506
It "Should return False on uninitialized relation" {
507
Confirm-JujuRelationCreated -Relation "testing" | Should Be $false
511
Describe "Test Get-JujuUnit" {
512
Mock Invoke-JujuCommand -ModuleName JujuHooks {
516
if(!($Command[-1] -in @("private-address", "public-address"))){
517
Throw "only private-address and public-address are supported"
519
$expect = @("unit-get.exe", "--format=yaml")
520
if ((Compare-Object $Command ($expect + $Command[-1]))) {
521
Throw "Invalid parameters"
524
"private-address"='"192.168.1.1"';
525
"public-address"='"192.168.1.2"';
527
return $addr[$Command[-1]]
529
It "Should throw an exception (invalid attribute)" {
530
{ Get-JujuUnit -Attribute "Bogus" } | Should Throw
533
It "Should return private-address" {
534
Get-JujuUnit -Attribute "private-address" | Should Be "192.168.1.1"
537
It "Should return public-address" {
538
Get-JujuUnit -Attribute "public-address" | Should Be "192.168.1.2"
542
Describe "Test Confirm-IP" {
543
It "Should return False for 'bla'" {
544
Confirm-IP -IP "bla" | Should Be $false
546
It "Should return False for '192.168.1'" {
547
Confirm-IP -IP "192.168.1" | Should Be $false
549
It "Should return True for '192.168.1.1'" {
550
Confirm-IP -IP "192.168.1.1" | Should Be $true
552
It "Should return True for '::1'" {
553
Confirm-IP -IP "::1" | Should Be $true
557
Describe "Test Get-JujuUnitPrivateIP" {
561
Mock Resolve-Address -ModuleName JujuHooks -Verifiable {
563
[Parameter(Mandatory=$true)]
567
"example.com"="192.168.1.1";
569
if(!$data[$Address]){
570
Throw ("Could not resolve address {0} to IP" -f $Address)
572
return $data[$Address]
574
Mock Invoke-JujuCommand -ModuleName JujuHooks {
578
if(!($Command[-1] -in @("private-address", "public-address"))){
579
Throw "only private-address and public-address are supported"
581
$expect = @("unit-get.exe", "--format=yaml")
582
if ((Compare-Object $Command ($expect + $Command[-1]))) {
583
Throw "Invalid parameters"
586
$pi = '"192.168.1.1"'
591
"private-address"=$pi;
592
"public-address"='"192.168.1.2"';
594
return $addr[$Command[-1]]
596
It "Should return the private address (supply IP address)" {
597
Get-JujuUnitPrivateIP | Should Be "192.168.1.1"
600
It "Should return the private address (supply hostname)" {
601
$env:privateIP = '"example.com"'
602
Get-JujuUnitPrivateIP | Should Be "192.168.1.1"
603
Assert-VerifiableMocks
606
It "Should throw an exception" {
607
$env:privateIP = '"example-bogus.com"'
608
{ Get-JujuUnitPrivateIP } | Should Throw
609
Assert-VerifiableMocks
613
Describe "Test Get-JujuRelationContext" {
614
Mock Get-JujuRelationIds -ModuleName JujuHooks {
617
[string]$Relation=$null
620
"amqp" = @("amqp:1", "amqp:2");
623
return $relations[$Relation]
625
Mock Get-JujuRelationsOfType -ModuleName JujuHooks {
628
[string]$Relation=$null
635
"private-address"="192.168.1.1";
640
"private-address"="192.168.1.2";
646
"private-address"="192.168.1.3";
650
return $data[$Relation]
652
It "Should return an empty context" {
658
Get-JujuRelationContext -RequiredContext $ctx -Relation "identity" | Should BeNullOrEmpty
660
It "Should return populated context" {
665
$r = Get-JujuRelationContext -RequiredContext $ctx -Relation "amqp"
666
$r | Should Not BeNullOrEmpty
667
$r["username"] | Should Be "guest"
668
$r["password"] | Should Be "secret"
669
$r.Keys | Should Be @("username", "password")
671
It "Should also contain the private-address" {
675
"private-address"=$null;
677
$r = Get-JujuRelationContext -RequiredContext $ctx -Relation "amqp"
678
$r | Should Not BeNullOrEmpty
679
$r["username"] | Should Be "guest"
680
$r["password"] | Should Be "secret"
681
$r["private-address"] | Should Be "192.168.1.2"
682
$r.Keys | Should Be @("private-address", "username", "password")
686
Describe "Test Invoke-JujuReboot" {
687
Context "Reboot at end" {
688
Mock Invoke-JujuCommand -ModuleName JujuHooks {
692
$expect = @("juju-reboot.exe")
693
if((Compare-Object $Command $expect)) {
694
Throw "Invalid command"
697
It "Should not send the --now flag" {
698
Invoke-JujuReboot | Should BeNullOrEmpty
701
Context "Reboot now" {
702
Mock Invoke-JujuCommand -ModuleName JujuHooks {
706
$expect = @("juju-reboot.exe", "--now")
707
if((Compare-Object $Command $expect)) {
708
Throw "Invalid command"
711
It "Should send the --now flag" {
712
Invoke-JujuReboot -Now | Should BeNullOrEmpty
717
Describe "Test Confirm-JujuPortRangeOpen" {
718
Mock Invoke-JujuCommand -ModuleName JujuHooks {
722
$expect = @("opened-ports.exe", "--format=yaml")
723
if((Compare-Object $Command $expect)) {
724
Throw "Invalid command"
726
return '["111/tcp", "222/udp", "2000-3000/tcp"]'
728
It "Should Throw an exception on invalid ports" {
729
{ Confirm-JujuPortRangeOpen -Port "dummy"} | Should Throw
730
{ Confirm-JujuPortRangeOpen -Port 1111111} | Should Throw
731
{ Confirm-JujuPortRangeOpen -Port "123/TTCP" } | Should Throw
732
{ Confirm-JujuPortRangeOpen } | Should Throw
734
It "Should return True" {
735
Confirm-JujuPortRangeOpen -Port "111/tcp" | Should Be $true
736
Confirm-JujuPortRangeOpen -Port "222/udp" | Should Be $true
737
Confirm-JujuPortRangeOpen -Port "2000-3000/tcp" | Should Be $true
739
It "Should return False" {
740
Confirm-JujuPortRangeOpen -Port "111/udp" | Should Be $false
741
Confirm-JujuPortRangeOpen -Port "222/tcp" | Should Be $false
742
Confirm-JujuPortRangeOpen -Port "2000-3001/tcp" | Should Be $false
746
Describe "Test Open-JujuPort" {
750
Mock Confirm-JujuPortRangeOpen -ModuleName JujuHooks {
752
[Parameter(Mandatory=$true)]
753
[ValidatePattern('^(\d{1,5}-)?\d{1,5}/(tcp|udp)$')]
757
$p = $env:OpenedPortsTest.Split()
765
Mock Invoke-JujuCommand -ModuleName JujuHooks {
770
if($p -eq "999/tcp"){
773
$expect = @("open-port.exe")
774
if((Compare-Object $Command ($expect += $Command[-1]))) {
775
Throw "Invalid command"
777
$p = $env:OpenedPortsTest.Split()
779
if ($i -eq $Command[-1]){
780
Throw "Port already open"
784
$env:OpenedPortsTest = $p
786
Mock Write-JujuErr -Verifiable -ModuleName JujuHooks {
788
[Parameter(Mandatory=$true)]
792
It "Should return if port already open" {
793
$env:OpenedPortsTest = @("1024/tcp")
794
Open-JujuPort -Port "1024/tcp" | Should BeNullOrEmpty
796
It "Should open a new port" {
797
$env:OpenedPortsTest = @("1/tcp")
798
Open-JujuPort -Port "1024/tcp" | Should BeNullOrEmpty
799
$env:OpenedPortsTest.Split() | Should Be @("1/tcp", "1024/tcp")
801
It "Should throw an exception" {
802
$env:OpenedPortsTest = @("1/tcp")
803
{ Open-JujuPort -Port "999/tcp" } | Should Throw
804
Assert-VerifiableMocks
808
Describe "Test Close-JujuPort" {
812
Mock Confirm-JujuPortRangeOpen -ModuleName JujuHooks {
814
[Parameter(Mandatory=$true)]
815
[ValidatePattern('^(\d{1,5}-)?\d{1,5}/(tcp|udp)$')]
819
$p = $env:OpenedPortsTest.Split()
827
Mock Invoke-JujuCommand -ModuleName JujuHooks {
832
if($p -eq "999/tcp"){
835
$expect = @("close-port.exe")
836
if((Compare-Object $Command ($expect += $Command[-1]))) {
837
Throw "Invalid command"
841
foreach($i in $env:OpenedPortsTest.Split()) {
842
if ($i -eq $Command[-1]){
851
$env:OpenedPortsTest = $p
853
Mock Write-JujuErr -Verifiable -ModuleName JujuHooks {
855
[Parameter(Mandatory=$true)]
859
It "Should close a port" {
860
$env:OpenedPortsTest = @("1/tcp", "1024/tcp")
861
Close-JujuPort -Port "1024/tcp" | Should BeNullOrEmpty
862
$env:OpenedPortsTest.Split() | Should Be @("1/tcp")
864
It "Should return if port not open" {
865
$env:OpenedPortsTest = @("1/tcp")
866
Close-JujuPort -Port "1024/tcp" | Should BeNullOrEmpty
867
$env:OpenedPortsTest.Split() | Should Be @("1/tcp")
869
It "Should throw an exception" {
870
$env:OpenedPortsTest = @("1/tcp", "999/tcp")
871
{ Close-JujuPort -Port "999/tcp" } | Should Throw
872
Assert-VerifiableMocks
876
Describe "Test Confirm-Leader" {
877
Mock Invoke-JujuCommand -ModuleName JujuHooks {
881
$expect = @("is-leader.exe", "--format=yaml")
882
if((Compare-Object $Command $expect)) {
883
Throw "Invalid command"
887
It "Should return True" {
888
Confirm-Leader | Should Be $true
892
Describe "Test Set-LeaderData" {
893
Mock Invoke-JujuCommand -ModuleName JujuHooks {
897
$expect = @("leader-set.exe", "hello=world", "password=secret")
898
if((Compare-Object $Command $expect)) {
899
Throw "Invalid command"
902
It "Should send proper parameters to leader-set" {
907
Set-LeaderData -Settings $data | Should BeNullOrEmpty
909
It "Should throw an exception on invalid data" {
910
{ Set-LeaderData -Settings "bogus" } | Should Throw
911
{ Set-LeaderData -Settings @(1,2,3) } | Should Throw
915
Describe "Test Get-LeaderData" {
916
Context "Call Get-LeaderData with no attributes" {
917
Mock Invoke-JujuCommand -ModuleName JujuHooks {
921
$expect = @("leader-get.exe", "--format=yaml")
922
if((Compare-Object $Command $expect)) {
923
Throw "Invalid command"
925
return '{"bogus": "data", "hello": "world"}'
927
It "Should return leader data" {
929
$r.GetType() | Should Be "Hashtable"
930
$r["bogus"] | Should Be "data"
931
$r["hello"] | Should Be "world"
932
$r.Keys.Count | Should Be 2
935
Context "Call Get-LeaderData with attribute" {
936
Mock Invoke-JujuCommand -ModuleName JujuHooks {
940
$expect = @("leader-get.exe", "--format=yaml")
941
if((Compare-Object $Command ($expect += $Command[-1]))) {
942
Throw "Invalid command"
947
$r = $data[$Command[-1]]
951
return $data[$Command[-1]]
953
It "Should return world" {
954
Get-LeaderData -Attribute "hello" | Should Be "world"
956
It "Should return empty" {
957
Get-LeaderData -Attribute "empty" | Should BeNullOrEmpty
962
Describe "Test Get-JujuVersion" {
966
Mock Invoke-JujuCommand -ModuleName JujuHooks {
970
$expect = @("jujud.exe", "version")
971
if((Compare-Object $Command $expect)) {
972
Throw "Invalid command"
974
return $env:binVersion
976
Mock Write-JujuWarning -Verifiable -ModuleName JujuHooks {
978
[Parameter(Mandatory=$true)]
982
It "Should return hashtable with 4 fields (development release)" {
983
$env:binVersion = "1.25.1-alpha1-win2012r2-amd64"
985
$r.Keys.Count | Should Be 4
986
$r["version"] | Should Be "1.25.1"
987
$r["subversion"] | Should Be "alpha1"
988
$r["series"] | Should Be "win2012r2"
989
$r["arch"] | Should Be "amd64"
990
Assert-VerifiableMocks
992
It "Should return a hashtable with 3 fields (production)" {
993
$env:binVersion = "1.25.1-win2012r2-amd64"
995
$r.Keys.Count | Should Be 3
996
$r["version"] | Should Be "1.25.1"
997
$r["series"] | Should Be "win2012r2"
998
$r["arch"] | Should Be "amd64"
1000
It "Should Throw an exception" {
1001
$env:binVersion = "1.25.1"
1002
{ Get-JujuVersion } | Should Throw
1006
Describe "Test Get-JujuStatus" {
1007
Mock Invoke-JujuCommand -ModuleName JujuHooks {
1011
$expect = @("status-get.exe", "--include-data","--format=yaml")
1012
if((Compare-Object $Command $expect)) {
1013
Throw "Invalid command"
1015
return '{"message":"Unit is ready","status":"active","status-data":{"sample": 1}}'
1017
It "Should return the status" {
1018
Get-JujuStatus | Should Be "active"
1020
It "Should full status info" {
1021
$r = Get-JujuStatus -Full
1022
$r.GetType() | Should Be "hashtable"
1023
$r["message"] | Should Be "Unit is ready"
1024
$r["status"] | Should Be "active"
1025
$r["status-data"].GetType() | Should Be "hashtable"
1026
$r["status-data"].sample | Should Be 1
1030
Describe "Test Set-JujuStatus" {
1034
Mock Write-JujuWarning -Verifiable -ModuleName JujuHooks {
1036
[Parameter(Mandatory=$true)]
1040
Mock Invoke-JujuCommand -ModuleName JujuHooks -Verifiable -ParameterFilter { $Command.Count -eq 2 } {
1041
$statuses = @("maintenance", "blocked", "waiting", "active")
1042
if(!($Command[1] -in $statuses)){
1043
throw "invalid status"
1045
$expect = @("status-set.exe", $Command[1])
1046
if((Compare-Object $Command $expect)) {
1047
Throw "Invalid command"
1050
"status"=$Command[1];
1054
$env:PesterTestData = (ConvertTo-Yaml $tmpStatus)
1056
Mock Invoke-JujuCommand -ModuleName JujuHooks -Verifiable -ParameterFilter { $Command.Count -eq 3 } {
1057
$statuses = @("maintenance", "blocked", "waiting", "active")
1058
if(!($Command[1] -in $statuses)){
1059
throw "invalid status"
1061
$expect = @("status-set.exe", $Command[1], $Command[2])
1062
if((Compare-Object $Command $expect)) {
1063
Throw "Invalid command"
1066
"status"=$Command[1];
1067
"message"=$Command[2];
1070
$env:PesterTestData = (ConvertTo-Yaml $tmpStatus)
1072
Mock Get-JujuStatus -ModuleName JujuHooks {
1074
[switch]$Full=$false
1076
$js = $env:PesterTestData
1077
$data = ConvertFrom-Yaml $js
1081
return (ConvertTo-Yaml $data.status)
1083
It "Should only set status" {
1084
$env:PesterTestData = '{"message":"","status":"unknown","status-data":{}}'
1085
Set-JujuStatus -Status "active" | Should BeNullOrEmpty
1086
$d = ConvertFrom-Yaml $env:PesterTestData
1087
$d["status"] | Should Be "active"
1088
$d["message"] | Should BeNullOrEmpty
1089
$d["status-data"] | Should BeNullOrEmpty
1092
It "Should set status and message" {
1093
$env:PesterTestData = '{"message":"","status":"unknown","status-data":{}}'
1094
Set-JujuStatus -Status "active" -Message "Unit is ready" | Should BeNullOrEmpty
1095
Assert-MockCalled Invoke-JujuCommand -Times 1 -ModuleName JujuHooks
1096
$d = ConvertFrom-Yaml $env:PesterTestData
1097
$d["status"] | Should Be "active"
1098
$d["message"] | Should Be "Unit is ready"
1099
$d["status-data"] | Should BeNullOrEmpty
1101
It "Should not change message if status is unchanged" {
1102
$env:PesterTestData = '{"message":"","status":"unknown","status-data":{}}'
1103
Set-JujuStatus -Status "active" -Message "Unit is ready" | Should BeNullOrEmpty
1104
$d = ConvertFrom-Yaml $env:PesterTestData
1105
$d["status"] | Should Be "active"
1106
$d["message"] | Should Be "Unit is ready"
1107
$d["status-data"] | Should BeNullOrEmpty
1109
Set-JujuStatus -Status "active" -Message "Unit is almost ready" | Should BeNullOrEmpty
1110
Assert-MockCalled Invoke-JujuCommand -Times 1 -ModuleName JujuHooks
1111
$d["status"] | Should Be "active"
1112
$d["message"] | Should Be "Unit is ready"
1113
$d["status-data"] | Should BeNullOrEmpty
1115
It "Should Throw an exception on invalid status" {
1116
{ Set-JujuStatus -Status "bogus" -Message "Unit is almost ready" } | Should Throw
1117
{ Set-JujuStatus -Status "bogus" } | Should Throw
1121
Describe "Test Get-JujuAction" {
1122
Mock Invoke-JujuCommand -ModuleName JujuHooks {
1129
if ($Command.Count -gt 3 -or $Command.Count -lt 2){
1130
Throw "invalid command"
1132
$expect = @("action-get.exe", "--format=yaml")
1133
if ($Command.Count -eq 3){
1134
$expect += $Command[-1]
1136
if((Compare-Object $Command $expect)) {
1137
Throw "Invalid command"
1139
if($Command.Count -eq 3){
1140
return (ConvertTo-Yaml $data[$Command[2]])
1142
return (ConvertTo-Yaml $data)
1144
It "Should send proper command" {
1145
(Get-JujuAction).GetType() | Should Be "hashtable"
1147
It "Should return value" {
1148
Get-JujuAction -Parameter "bla" | Should be "bla"
1150
It "Should return empty" {
1151
Get-JujuAction -Parameter "NotThere" | Should BeNullOrEmpty
1155
Describe "Test Set-JujuAction" {
1156
Mock Invoke-JujuCommand -ModuleName JujuHooks {
1160
$expect = @("action-set.exe", "hello=world", "password=secret")
1161
if((Compare-Object $Command $expect)) {
1162
Throw "Invalid command"
1165
It "Should send proper parameters to leader-set" {
1168
"password"="secret";
1170
Set-JujuAction -Settings $data | Should BeNullOrEmpty
1172
It "Should throw an exception on invalid data" {
1173
{ Set-LeaderData -Settings "bogus" } | Should Throw
1174
{ Set-LeaderData -Settings @(1,2,3) } | Should Throw
1178
Describe "Test Set-JujuActionFailed" {
1179
Mock Invoke-JujuCommand -ModuleName JujuHooks {
1183
if ($Command.Count -lt 1 -or $Command.Count -gt 2) {
1184
Throw "Invalid parameters"
1186
$expect = @("action-fail.exe")
1187
if($Command.Count -eq 2) {
1188
$expect += $Command[-1]
1190
if((Compare-Object $Command $expect)) {
1191
Throw "Invalid command"
1194
It "Should send action fail" {
1195
Set-JujuActionFailed | Should BeNullOrEmpty
1199
Describe "Test Convert-JujuUnitNameToNetbios" {
1203
Mock Get-JujuLocalUnit -ModuleName JujuHooks {
1204
return $env:PesterTestData
1206
It "Should return a valid netbios name" {
1207
$env:PesterTestData = "active-directory/12"
1208
Convert-JujuUnitNameToNetbios | Should Be "active-direct12"
1209
$env:PesterTestData = "test/11"
1210
Convert-JujuUnitNameToNetbios | Should Be "test11"
1211
$env:PesterTestData = "thisisareallylonghostnamethatwillprobablybreakstuff/1"
1212
Convert-JujuUnitNameToNetbios | Should Be "thisisareallyl1"
1216
Describe "Test Set-CharmState" {
1218
$CharmStateKey = "HKCU:\Software\Juju-Charms"
1219
if($CharmStateKey -and (Test-Path $CharmStateKey)) {
1220
Remove-Item $CharmStateKey -Recurse -Force
1224
$CharmStateKey = "HKCU:\Software\Juju-Charms"
1225
if($CharmStateKey -and (Test-Path $CharmStateKey)) {
1226
Remove-Item $CharmStateKey -Recurse -Force
1229
Mock Get-StateInformationRepository -ModuleName JujuHooks { return "HKCU:\Software\Juju-Charms"}
1230
It "Should set a registry key" {
1231
$p = "HKCU:\Software\Juju-Charms"
1232
(Test-Path -Path $p) | Should Be $false
1233
Set-CharmState -Namespace "active-directory" -Key "username" -Value "guest" | Should BeNullOrEmpty
1234
$keyPath = Join-Path $p "active-directory"
1235
(Test-Path -Path $keyPath) | Should Be $true
1236
$k = (Get-ItemProperty -Path $keyPath -Name "username")
1237
(Select-Object -InputObject $k -ExpandProperty "username") | Should Be (ConvertTo-Yaml "guest")
1241
Describe "Test Get-CharmState" {
1243
$CharmStateKey = "HKCU:\Software\Juju-Charms"
1244
if($CharmStateKey -and (Test-Path $CharmStateKey)) {
1245
Remove-Item $CharmStateKey -Recurse -Force
1249
$CharmStateKey = "HKCU:\Software\Juju-Charms"
1250
if($CharmStateKey -and (Test-Path $CharmStateKey)) {
1251
Remove-Item $CharmStateKey -Recurse -Force
1254
Mock Get-StateInformationRepository -ModuleName JujuHooks { return "HKCU:\Software\Juju-Charms"}
1255
It "Should return charm state" {
1256
Set-CharmState -Namespace "active-directory" -Key "username" -Value "guest" | Should BeNullOrEmpty
1257
Get-CharmState -Namespace "active-directory" -Key "username" | Should be "guest"
1261
Describe "Test Remove-CharmState" {
1263
$CharmStateKey = "HKCU:\Software\Juju-Charms"
1264
if($CharmStateKey -and (Test-Path $CharmStateKey)) {
1265
Remove-Item $CharmStateKey -Recurse -Force
1269
$CharmStateKey = "HKCU:\Software\Juju-Charms"
1270
if($CharmStateKey -and (Test-Path $CharmStateKey)) {
1271
Remove-Item $CharmStateKey -Recurse -Force
1274
Mock Get-StateInformationRepository -ModuleName JujuHooks { return "HKCU:\Software\Juju-Charms"}
1275
It "Should remove charm state" {
1276
Set-CharmState -Namespace "active-directory" -Key "username" -Value "guest" | Should BeNullOrEmpty
1277
Get-CharmState -Namespace "active-directory" -Key "username" | Should be "guest"
1278
Remove-CharmState -Namespace "active-directory" -Key "username" | Should BeNullOrEmpty
b'\\ No newline at end of file'