~cloudbaseit/charms/win2012r2/drupal-iis/trunk

« back to all changes in this revision

Viewing changes to lib/Modules/JujuHooks/Tests/JujuHooks.Tests.ps1

  • Committer: Ionut Balutoiu
  • Date: 2016-12-23 12:55:39 UTC
  • Revision ID: ibalutoiu@cloudbasesolutions.com-20161223125539-j795kbynab3uflha
Added charm code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
 
2
$moduleHome = Split-Path -Parent $here
 
3
$moduleRoot = Split-Path -Parent $moduleHome
 
4
 
 
5
$modulePath = ${env:PSModulePath}.Split(";")
 
6
if(!($moduleRoot -in $modulePath)){
 
7
    $env:PSModulePath += ";$moduleRoot"
 
8
}
 
9
$savedEnv = [System.Environment]::GetEnvironmentVariables()
 
10
 
 
11
Import-Module JujuHooks
 
12
 
 
13
function Clear-Environment {
 
14
    $current = [System.Environment]::GetEnvironmentVariables()
 
15
    foreach($i in $savedEnv.GetEnumerator()) {
 
16
        [System.Environment]::SetEnvironmentVariable($i.Name, $i.Value, "Process")
 
17
    }
 
18
    $current = [System.Environment]::GetEnvironmentVariables()
 
19
    foreach ($i in $current.GetEnumerator()){
 
20
        if(!$savedEnv[$i.Name]){
 
21
            [System.Environment]::SetEnvironmentVariable($i.Name, $null, "Process")
 
22
        }
 
23
    }
 
24
}
 
25
 
 
26
Describe "Test Confirm-ContextComplete" {
 
27
    AfterEach {
 
28
        Clear-Environment
 
29
    }
 
30
    It "Should return False" {
 
31
        $ctx = @{
 
32
            "test" = $null;
 
33
            "test2" = "test";
 
34
        }
 
35
        Confirm-ContextComplete -Context $ctx | Should Be $false
 
36
 
 
37
        $ctx = @{
 
38
            "test" = $null;
 
39
            "test2" = "test";
 
40
        }
 
41
        Confirm-ContextComplete -Context $ctx | Should Be $false
 
42
 
 
43
        $ctx = @{}
 
44
        Confirm-ContextComplete -Context $ctx | Should Be $false
 
45
    }
 
46
 
 
47
    It "Should return True" {
 
48
        $ctx = @{
 
49
            "hello" = "world";
 
50
        }
 
51
        Confirm-ContextComplete -Context $ctx | Should Be $true
 
52
    }
 
53
 
 
54
    It "Should Throw an exception" {
 
55
        $ctx = "not a hashtable"
 
56
        { Confirm-ContextComplete -Context $ctx} | Should Throw
 
57
    }
 
58
}
 
59
 
 
60
Describe "Test hook environment functions" {
 
61
    AfterEach {
 
62
        Clear-Environment
 
63
    }
 
64
    It "Should return charm_dir" {
 
65
        $env:CHARM_DIR = "bogus"
 
66
        Get-JujuCharmDir | Should Be "bogus"
 
67
    }
 
68
 
 
69
    It "Should return relation name" {
 
70
        $env:JUJU_RELATION = "bogus"
 
71
        Get-JujuRelationType | Should be "bogus"
 
72
    }
 
73
 
 
74
    It "Confirm-JujuRelation should return True" {
 
75
        $env:JUJU_RELATION = "bogus"
 
76
        Confirm-JujuRelation | Should be $true
 
77
    }
 
78
 
 
79
    It "Confirm-JujuRelation should return False" {
 
80
        $env:JUJU_RELATION = ""
 
81
        Confirm-JujuRelation | Should be $false
 
82
    }
 
83
 
 
84
    It "Get-JujuRelationId should return relation ID" {
 
85
        $env:JUJU_RELATION_ID = "bogus:1"
 
86
        Get-JujuRelationId | Should Be "bogus:1"
 
87
    }
 
88
 
 
89
    It "Get-JujuLocalUnit should return unit name" {
 
90
        $env:JUJU_UNIT_NAME = "unit-1"
 
91
        Get-JujuLocalUnit | Should Be "unit-1"
 
92
    }
 
93
 
 
94
    It "Get-JujuRemoteUnit should return remote unit" {
 
95
        $env:JUJU_REMOTE_UNIT = "remote-1"
 
96
        Get-JujuRemoteUnit | Should Be "remote-1"
 
97
    }
 
98
 
 
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"
 
102
    }
 
103
}
 
104
 
 
105
Describe "Test Get-JujuCharmConfig" {
 
106
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
107
        Param (
 
108
            [array]$Command
 
109
        )
 
110
        $ret = @{
 
111
            "stringOption"="Hello";
 
112
            "intOption"=1;
 
113
        }
 
114
        if($Command.Length -gt 2) {
 
115
            $x = $Command[2]
 
116
            if($ret[$x]){
 
117
                return (ConvertTo-Yaml $ret[$x])
 
118
            }
 
119
            return ""
 
120
        }
 
121
        return (ConvertTo-Yaml $ret)
 
122
    }
 
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
 
127
    }
 
128
 
 
129
    It "Should return a string" {
 
130
        Get-JujuCharmConfig -Scope "stringOption" | Should Be "Hello"
 
131
    }
 
132
 
 
133
    It "Should return an int" {
 
134
        Get-JujuCharmConfig -Scope "intOption" | Should Be 1
 
135
    }
 
136
 
 
137
    It "Should return empty" {
 
138
        Get-JujuCharmConfig -Scope "nonexisting" | Should BeNullOrEmpty
 
139
    }
 
140
}
 
141
 
 
142
Describe "Test Get-JujuRelation" {
 
143
    AfterEach {
 
144
        Clear-Environment
 
145
    }
 
146
    Context "Invoke Get-JujuRelation without params" {
 
147
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
148
            Param (
 
149
                [array]$Command
 
150
            )
 
151
            $expect = @("relation-get.exe", "--format=yaml", "-")
 
152
            if ((Compare-Object $Command $expect)) {
 
153
                Throw "Invalid parameters"
 
154
            }
 
155
            return '{"user": "guest"}'
 
156
        }
 
157
 
 
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"
 
163
        }
 
164
        It "Should throw an exception" {
 
165
            { Get-JujuRelation }| Should Throw
 
166
        }
 
167
    }
 
168
 
 
169
    Context "Invoke Get-JujuRelation with Unit"{
 
170
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
171
            Param (
 
172
                [array]$Command
 
173
            )
 
174
            $expect = @("relation-get.exe", "--format=yaml", "-", "bogus")
 
175
            if ((Compare-Object $Command $expect)) {
 
176
                Throw "Invalid parameters"
 
177
            }
 
178
        }
 
179
        It "Should pass - and unit" {
 
180
            $env:JUJU_RELATION_ID = "amqp:1"
 
181
            Get-JujuRelation -Unit "bogus" | Should BeNullOrEmpty
 
182
        }
 
183
        It "Should throw an exception" {
 
184
            { Get-JujuRelation -Unit "bogus" } | Should Throw
 
185
        }
 
186
    }
 
187
 
 
188
    Context "Invoke Get-JujuRelation with Unit and relation ID"{
 
189
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
190
            Param (
 
191
                [array]$Command
 
192
            )
 
193
            $expect = @("relation-get.exe", "--format=yaml", "-r", "amqp:1", "-", "bogus")
 
194
            if ((Compare-Object $Command $expect)) {
 
195
                Throw "Invalid parameters"
 
196
            }
 
197
            return '{"test": "test", "hello": "world"}'
 
198
        }
 
199
 
 
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"
 
205
        }
 
206
    }
 
207
 
 
208
    Context "Invoke Get-JujuRelation with Unit, relation ID and attribute"{
 
209
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
210
            Param (
 
211
                [array]$Command
 
212
            )
 
213
            $expect = @("relation-get.exe", "--format=yaml", "-r", "amqp:1", "name", "bogus")
 
214
            if ((Compare-Object $Command $expect)) {
 
215
                Throw "Invalid parameters"
 
216
            }
 
217
            return '"test"'
 
218
        }
 
219
 
 
220
        It "Should pass unit, relation id and attribute" {
 
221
            Get-JujuRelation -Unit "bogus" -RelationID "amqp:1" -Attribute "name" | Should Be "test"
 
222
        }
 
223
    }
 
224
}
 
225
 
 
226
Describe "Test Set-JujuRelation"{
 
227
    AfterEach {
 
228
        Clear-Environment
 
229
    }
 
230
 
 
231
    Context "Call Set-JujuRelation without RelationID" {
 
232
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
233
            Param (
 
234
                [array]$Command
 
235
            )
 
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 " "))
 
239
            }
 
240
        }
 
241
 
 
242
        It "Should pass name=value" {
 
243
            $env:JUJU_RELATION_ID = "amqp:1"
 
244
            $params = @{
 
245
                "name"="value";
 
246
            }
 
247
            Set-JujuRelation -Settings $params | Should Be $true
 
248
        }
 
249
        It "Should throw an exception (Missing relation ID)" {
 
250
            $params = @{
 
251
                "name"="value";
 
252
            }
 
253
            { Set-JujuRelation -Settings $params } | Should Throw
 
254
        }
 
255
    }
 
256
 
 
257
    Context "Call Set-JujuRelation with RelationID" {
 
258
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
259
            Param (
 
260
                [array]$Command
 
261
            )
 
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 " "))
 
265
            }
 
266
        }
 
267
        It "Should pass relationID" {
 
268
            $params = @{
 
269
                "name"="value";
 
270
            }
 
271
            Set-JujuRelation -Settings $params -RelationID "amqp:1" | Should Be $true
 
272
        }
 
273
 
 
274
        It "Should Throw an exception (Missing relation ID)" {
 
275
            { Set-JujuRelation -RelationID "amqp:1" } | Should Throw
 
276
        }
 
277
    }
 
278
 
 
279
}
 
280
 
 
281
Describe "Test Get-JujuRelationIds" {
 
282
    AfterEach {
 
283
        Clear-Environment
 
284
    }
 
285
    Context "Call Get-JujuRelationIds without -Relation"{
 
286
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
287
            Param (
 
288
                [array]$Command
 
289
            )
 
290
            $expect = @("relation-ids.exe", "--format=yaml", "amqp")
 
291
            if ((Compare-Object $Command $expect)) {
 
292
                Throw "Invalid parameters"
 
293
            }
 
294
            return '["amqp:1", "amqp:2"]'
 
295
        }
 
296
        It "Should throw an exception (Missing relation type)" {
 
297
            { Get-JujuRelationIds } | Should Throw
 
298
        }
 
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"
 
303
        }
 
304
    }
 
305
    Context "Call Get-JujuRelationIds with -Relation"{
 
306
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
307
            Param (
 
308
                [array]$Command
 
309
            )
 
310
            $expect = @("relation-ids.exe", "--format=yaml", "shared-db")
 
311
            if ((Compare-Object $Command $expect)) {
 
312
                Throw "Invalid parameters"
 
313
            }
 
314
            return '"mysql:1"'
 
315
        }
 
316
        It "Should return relation ID" {
 
317
            Get-JujuRelationIds -Relation "shared-db" | Should Be "mysql:1"
 
318
        }
 
319
    }
 
320
}
 
321
 
 
322
Describe "Test Get-JujuRelatedUnits" {
 
323
    AfterEach {
 
324
        Clear-Environment
 
325
    }
 
326
    Context "Call Get-JujuRelatedUnits without -RelationID"{
 
327
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
328
            Param (
 
329
                [array]$Command
 
330
            )
 
331
            $expect = @("relation-list.exe", "--format=yaml", "-r", "amqp:1")
 
332
            if ((Compare-Object $Command $expect)) {
 
333
                Throw "Invalid parameters"
 
334
            }
 
335
            return '["rabbitmq/0", "rabbitmq/1"]'
 
336
        }
 
337
        It "Should throw an exception (Missing relation ID)" {
 
338
            { Get-JujuRelatedUnits } | Should Throw
 
339
        }
 
340
        It "Should return related units" {
 
341
            $env:JUJU_RELATION_ID = "amqp:1"
 
342
            Get-JujuRelatedUnits | Should Be @("rabbitmq/0", "rabbitmq/1")
 
343
        }
 
344
    }
 
345
    Context "Get-JujuRelatedUnits with -Relation"{
 
346
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
347
            Param (
 
348
                [array]$Command
 
349
            )
 
350
            $expect = @("relation-list.exe", "--format=yaml", "-r","shared-db")
 
351
            if ((Compare-Object $Command $expect)) {
 
352
                Throw "Invalid parameters"
 
353
            }
 
354
            return '"mysql:1"'
 
355
        }
 
356
        It "Should return related units" {
 
357
            Get-JujuRelatedUnits -RelationID "shared-db" | Should Be "mysql:1"
 
358
        }
 
359
    }
 
360
}
 
361
 
 
362
Describe "Test Get-JujuRelationForUnit" {
 
363
    Context "Call Get-JujuRelationForUnit"{
 
364
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
365
            Param (
 
366
                [array]$Command
 
367
            )
 
368
            $expect = @("relation-get.exe", "--format=yaml", "-r", "amqp:1", "-", "bogus")
 
369
            if ((Compare-Object $Command $expect)) {
 
370
                Throw "Invalid parameters"
 
371
            }
 
372
            return '{"test": "test", "hello": "world", "test-list": "hello world"}'
 
373
        }
 
374
 
 
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")
 
381
        }
 
382
    }
 
383
    
 
384
}
 
385
 
 
386
Describe "Test Get-JujuRelationForId" {
 
387
    Context "Call Get-JujuRelationForId"{
 
388
        Mock Get-JujuRelatedUnits -ModuleName JujuHooks {
 
389
            return @("rabbitmq/0", "rabbitmq/1")
 
390
        }
 
391
        Mock Get-JujuRelationForUnit -ModuleName JujuHooks {
 
392
            Param(
 
393
                [string]$Unit=$null,
 
394
                [Alias("Rid")]
 
395
                [string]$RelationId=$null
 
396
            )
 
397
            if ($RelationId -ne "amqp:1"){
 
398
                Throw "Invalid relationID. Expected amqp:1"
 
399
            }
 
400
            $ret = @{
 
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";};
 
403
            }
 
404
            return $ret[$Unit]
 
405
        }
 
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"
 
414
        }
 
415
 
 
416
        It "Should throw an exception (Missing relation ID)" {
 
417
            { Get-JujuRelationForId } | Should Throw
 
418
        }
 
419
    }
 
420
}
 
421
 
 
422
Describe "Test Get-JujuRelationsOfType" {
 
423
    Mock Get-JujuRelationIds -ModuleName JujuHooks {
 
424
        Param(
 
425
            [Alias("RelType")]
 
426
            [string]$Relation=$null
 
427
        )
 
428
        if($Relation -ne "amqp") {
 
429
            return $null
 
430
        }
 
431
        return @("amqp:1", "amqp:2")
 
432
    }
 
433
    Mock Get-JujuRelationForUnit -ModuleName JujuHooks {
 
434
        Param(
 
435
            [string]$Unit=$null,
 
436
            [Alias("Rid")]
 
437
            [string]$RelationId=$null
 
438
        )
 
439
        $data = @{
 
440
            "amqp:1"= @{
 
441
                'rabbitmq/0'= @{
 
442
                    "rabbit-0-test"="test-0";
 
443
                };
 
444
                'rabbitmq/1'= @{
 
445
                    "rabbit-1-test"="test-1";
 
446
                    "rabbit-1-test2"="test-2";
 
447
                };
 
448
            };
 
449
            "amqp:2" = @{
 
450
                'keystone/0'=@{
 
451
                    "id"="root";
 
452
                };
 
453
            };
 
454
        }
 
455
        if(!$data[$RelationID]){
 
456
            Throw "Invalid relation ID"
 
457
        }
 
458
        $x = $data[$RelationId][$Unit]
 
459
        return $x
 
460
    }
 
461
    Mock Get-JujuRelatedUnits -ModuleName JujuHooks {
 
462
        Param(
 
463
            [Alias("RelId")]
 
464
            [string]$RelationId=$null
 
465
        )
 
466
        $data = @{
 
467
            "amqp:1"=@("rabbitmq/0", "rabbitmq/1");
 
468
            "amqp:2"=@("keystone/0")
 
469
        }
 
470
        return $data[$RelationId]
 
471
    }
 
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
 
476
    }
 
477
 
 
478
    It "Should return empty" {
 
479
        $r = Get-JujuRelationsOfType -Relation "bogus"
 
480
        $r | Should BeNullOrEmpty
 
481
    }
 
482
}
 
483
 
 
484
Describe "Test Confirm-JujuRelationCreated" {
 
485
    Mock Get-JujuRelationIds -ModuleName JujuHooks {
 
486
        Param(
 
487
            [Alias("RelType")]
 
488
            [string]$Relation=$null
 
489
        )
 
490
        $relations = @{
 
491
            "amqp" = @("amqp:1", "amqp:2");
 
492
            "testing" = @();
 
493
        }
 
494
        return $relations[$Relation]
 
495
    }
 
496
    It "Should return True" {
 
497
        Confirm-JujuRelationCreated -Relation "amqp" | Should Be $true
 
498
    }
 
499
 
 
500
    It "Should return False" {
 
501
        Confirm-JujuRelationCreated -Relation "bogus" | Should Be $false
 
502
    }
 
503
    It "Should return False on non existing relation" {
 
504
        Confirm-JujuRelationCreated -Relation "bogus" | Should Be $false
 
505
    }
 
506
    It "Should return False on uninitialized relation" {
 
507
        Confirm-JujuRelationCreated -Relation "testing" | Should Be $false
 
508
    }
 
509
}
 
510
 
 
511
Describe "Test Get-JujuUnit" {
 
512
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
513
        Param (
 
514
            [array]$Command
 
515
        )
 
516
        if(!($Command[-1] -in @("private-address", "public-address"))){
 
517
            Throw "only private-address and public-address are supported"
 
518
        }
 
519
        $expect = @("unit-get.exe", "--format=yaml")
 
520
        if ((Compare-Object $Command ($expect + $Command[-1]))) {
 
521
            Throw "Invalid parameters"
 
522
        }
 
523
        $addr = @{
 
524
            "private-address"='"192.168.1.1"';
 
525
            "public-address"='"192.168.1.2"';
 
526
        }
 
527
        return $addr[$Command[-1]]
 
528
    }
 
529
    It "Should throw an exception (invalid attribute)" {
 
530
        { Get-JujuUnit -Attribute "Bogus" } | Should Throw
 
531
    }
 
532
 
 
533
    It "Should return private-address" {
 
534
        Get-JujuUnit -Attribute "private-address" | Should Be "192.168.1.1"
 
535
    }
 
536
 
 
537
    It "Should return public-address" {
 
538
        Get-JujuUnit -Attribute "public-address" | Should Be "192.168.1.2"
 
539
    }
 
540
}
 
541
 
 
542
Describe "Test Confirm-IP" {
 
543
    It "Should return False for 'bla'" {
 
544
        Confirm-IP -IP "bla" | Should Be $false
 
545
    }
 
546
    It "Should return False for '192.168.1'" {
 
547
        Confirm-IP -IP "192.168.1" | Should Be $false
 
548
    }
 
549
    It "Should return True for '192.168.1.1'" {
 
550
        Confirm-IP -IP "192.168.1.1" | Should Be $true
 
551
    }
 
552
    It "Should return True for '::1'" {
 
553
        Confirm-IP -IP "::1" | Should Be $true
 
554
    }
 
555
}
 
556
 
 
557
Describe "Test Get-JujuUnitPrivateIP" {
 
558
    AfterEach {
 
559
        Clear-Environment
 
560
    }
 
561
    Mock Resolve-Address -ModuleName JujuHooks -Verifiable {
 
562
        Param(
 
563
            [Parameter(Mandatory=$true)]
 
564
            [string]$Address
 
565
        )
 
566
        $data = @{
 
567
            "example.com"="192.168.1.1";
 
568
        }
 
569
        if(!$data[$Address]){
 
570
            Throw ("Could not resolve address {0} to IP" -f $Address)
 
571
        }
 
572
        return $data[$Address]
 
573
    }
 
574
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
575
        Param (
 
576
            [array]$Command
 
577
        )
 
578
        if(!($Command[-1] -in @("private-address", "public-address"))){
 
579
            Throw "only private-address and public-address are supported"
 
580
        }
 
581
        $expect = @("unit-get.exe", "--format=yaml")
 
582
        if ((Compare-Object $Command ($expect + $Command[-1]))) {
 
583
            Throw "Invalid parameters"
 
584
        }
 
585
        if(!$env:privateIP){
 
586
            $pi = '"192.168.1.1"'
 
587
        }else {
 
588
            $pi = $env:privateIP
 
589
        }
 
590
        $addr = @{
 
591
            "private-address"=$pi;
 
592
            "public-address"='"192.168.1.2"';
 
593
        }
 
594
        return $addr[$Command[-1]]
 
595
    }
 
596
    It "Should return the private address (supply IP address)" {
 
597
        Get-JujuUnitPrivateIP | Should Be "192.168.1.1"
 
598
    }
 
599
 
 
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
 
604
    }
 
605
 
 
606
    It "Should throw an exception" {
 
607
        $env:privateIP = '"example-bogus.com"'
 
608
        { Get-JujuUnitPrivateIP } | Should Throw
 
609
        Assert-VerifiableMocks
 
610
    }
 
611
}
 
612
 
 
613
Describe "Test Get-JujuRelationContext" {
 
614
    Mock Get-JujuRelationIds -ModuleName JujuHooks {
 
615
        Param(
 
616
            [Alias("RelType")]
 
617
            [string]$Relation=$null
 
618
        )
 
619
        $relations = @{
 
620
            "amqp" = @("amqp:1", "amqp:2");
 
621
            "identity" = @();
 
622
        }
 
623
        return $relations[$Relation]
 
624
    }
 
625
    Mock Get-JujuRelationsOfType -ModuleName JujuHooks {
 
626
        Param(
 
627
            [Alias("RelType")]
 
628
            [string]$Relation=$null
 
629
        )
 
630
 
 
631
        $data = @{
 
632
            "amqp"= @(
 
633
                @{
 
634
                    "username"="guest";
 
635
                    "private-address"="192.168.1.1";
 
636
                },
 
637
                @{
 
638
                    "username"="guest";
 
639
                    "password"="secret";
 
640
                    "private-address"="192.168.1.2";
 
641
                }
 
642
            );
 
643
            "identity"=@(
 
644
                @{
 
645
                    "url"="example.com";
 
646
                    "private-address"="192.168.1.3";
 
647
                }
 
648
            );
 
649
        }
 
650
        return $data[$Relation]
 
651
    }
 
652
    It "Should return an empty context" {
 
653
        $ctx = @{
 
654
            "username"=$null;
 
655
            "password"=$null;
 
656
            "url"=$null;
 
657
        }
 
658
        Get-JujuRelationContext -RequiredContext $ctx -Relation "identity" | Should BeNullOrEmpty
 
659
    }
 
660
    It "Should return populated context" {
 
661
        $ctx = @{
 
662
            "username"=$null;
 
663
            "password"=$null;
 
664
        }
 
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")
 
670
    }
 
671
    It "Should also contain the private-address" {
 
672
        $ctx = @{
 
673
            "username"=$null;
 
674
            "password"=$null;
 
675
            "private-address"=$null;
 
676
        }
 
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")
 
683
    }
 
684
}
 
685
 
 
686
Describe "Test Invoke-JujuReboot" {
 
687
    Context "Reboot at end" {
 
688
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
689
            Param (
 
690
                [array]$Command
 
691
            )
 
692
            $expect = @("juju-reboot.exe")
 
693
            if((Compare-Object $Command $expect)) {
 
694
                Throw "Invalid command"
 
695
            }
 
696
        }
 
697
        It "Should not send the --now flag" {
 
698
            Invoke-JujuReboot | Should BeNullOrEmpty
 
699
        }
 
700
    }
 
701
    Context "Reboot now" {
 
702
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
703
            Param (
 
704
                [array]$Command
 
705
            )
 
706
            $expect = @("juju-reboot.exe", "--now")
 
707
            if((Compare-Object $Command $expect)) {
 
708
                Throw "Invalid command"
 
709
            }
 
710
        }
 
711
        It "Should send the --now flag" {
 
712
            Invoke-JujuReboot -Now | Should BeNullOrEmpty
 
713
        }
 
714
    }
 
715
}
 
716
 
 
717
Describe "Test Confirm-JujuPortRangeOpen" {
 
718
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
719
        Param (
 
720
            [array]$Command
 
721
        )
 
722
        $expect = @("opened-ports.exe", "--format=yaml")
 
723
        if((Compare-Object $Command $expect)) {
 
724
            Throw "Invalid command"
 
725
        }
 
726
        return '["111/tcp", "222/udp", "2000-3000/tcp"]'
 
727
    }
 
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
 
733
    }
 
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
 
738
    }
 
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
 
743
    }
 
744
}
 
745
 
 
746
Describe "Test Open-JujuPort" {
 
747
    AfterEach {
 
748
        Clear-Environment
 
749
    }
 
750
    Mock Confirm-JujuPortRangeOpen -ModuleName JujuHooks {
 
751
        Param(
 
752
            [Parameter(Mandatory=$true)]
 
753
            [ValidatePattern('^(\d{1,5}-)?\d{1,5}/(tcp|udp)$')]
 
754
            [string]$Port
 
755
 
 
756
        )
 
757
        $p = $env:OpenedPortsTest.Split()
 
758
        foreach ($i in $p){
 
759
            if ($Port -eq $i){
 
760
                return $true
 
761
            }
 
762
        }
 
763
        return $false
 
764
    }
 
765
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
766
        Param (
 
767
            [array]$Command
 
768
        )
 
769
        $p = $Command[-1]
 
770
        if($p -eq "999/tcp"){
 
771
            Throw "bogus error"
 
772
        }
 
773
        $expect = @("open-port.exe")
 
774
        if((Compare-Object $Command ($expect += $Command[-1]))) {
 
775
            Throw "Invalid command"
 
776
        }
 
777
        $p = $env:OpenedPortsTest.Split()
 
778
        foreach($i in $p) {
 
779
            if ($i -eq $Command[-1]){
 
780
                Throw "Port already open"
 
781
            }
 
782
        }
 
783
        $p += $Command[-1]
 
784
        $env:OpenedPortsTest = $p
 
785
    }
 
786
    Mock Write-JujuErr -Verifiable -ModuleName JujuHooks {
 
787
        Param(
 
788
            [Parameter(Mandatory=$true)]
 
789
            [string]$Message
 
790
        )
 
791
    }
 
792
    It "Should return if port already open" {
 
793
        $env:OpenedPortsTest = @("1024/tcp")
 
794
        Open-JujuPort -Port "1024/tcp" | Should BeNullOrEmpty
 
795
    }
 
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")
 
800
    }
 
801
    It "Should throw an exception" {
 
802
        $env:OpenedPortsTest = @("1/tcp")
 
803
        { Open-JujuPort -Port "999/tcp" } | Should Throw
 
804
        Assert-VerifiableMocks
 
805
    }
 
806
}
 
807
 
 
808
Describe "Test Close-JujuPort" {
 
809
    AfterEach {
 
810
        Clear-Environment
 
811
    }
 
812
    Mock Confirm-JujuPortRangeOpen -ModuleName JujuHooks {
 
813
        Param(
 
814
            [Parameter(Mandatory=$true)]
 
815
            [ValidatePattern('^(\d{1,5}-)?\d{1,5}/(tcp|udp)$')]
 
816
            [string]$Port
 
817
 
 
818
        )
 
819
        $p = $env:OpenedPortsTest.Split()
 
820
        foreach ($i in $p){
 
821
            if ($Port -eq $i){
 
822
                return $true
 
823
            }
 
824
        }
 
825
        return $false
 
826
    }
 
827
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
828
        Param (
 
829
            [array]$Command
 
830
        )
 
831
        $p = $Command[-1]
 
832
        if($p -eq "999/tcp"){
 
833
            Throw "bogus error"
 
834
        }
 
835
        $expect = @("close-port.exe")
 
836
        if((Compare-Object $Command ($expect += $Command[-1]))) {
 
837
            Throw "Invalid command"
 
838
        }
 
839
        $p = @()
 
840
        $found = $false
 
841
        foreach($i in $env:OpenedPortsTest.Split()) {
 
842
            if ($i -eq $Command[-1]){
 
843
                $found = $true
 
844
                continue
 
845
            }
 
846
            $p += $i
 
847
        }
 
848
        if(!$found){
 
849
            Throw "No such port"
 
850
        }
 
851
        $env:OpenedPortsTest = $p
 
852
    }
 
853
    Mock Write-JujuErr -Verifiable -ModuleName JujuHooks {
 
854
        Param(
 
855
            [Parameter(Mandatory=$true)]
 
856
            [string]$Message
 
857
        )
 
858
    }
 
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")
 
863
    }
 
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")
 
868
    }
 
869
    It "Should throw an exception" {
 
870
        $env:OpenedPortsTest = @("1/tcp", "999/tcp")
 
871
        { Close-JujuPort -Port "999/tcp" } | Should Throw
 
872
        Assert-VerifiableMocks
 
873
    }
 
874
}
 
875
 
 
876
Describe "Test Confirm-Leader" {
 
877
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
878
        Param (
 
879
            [array]$Command
 
880
        )
 
881
        $expect = @("is-leader.exe", "--format=yaml")
 
882
        if((Compare-Object $Command $expect)) {
 
883
            Throw "Invalid command"
 
884
        }
 
885
        return '"True"'
 
886
    }
 
887
    It "Should return True" {
 
888
        Confirm-Leader | Should Be $true
 
889
    }
 
890
}
 
891
 
 
892
Describe "Test Set-LeaderData" {
 
893
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
894
        Param (
 
895
            [array]$Command
 
896
        )
 
897
        $expect = @("leader-set.exe", "hello=world", "password=secret")
 
898
        if((Compare-Object $Command $expect)) {
 
899
            Throw "Invalid command"
 
900
        }
 
901
    }
 
902
    It "Should send proper parameters to leader-set" {
 
903
        $data = @{
 
904
            "hello"="world";
 
905
            "password"="secret";
 
906
        }
 
907
        Set-LeaderData -Settings $data | Should BeNullOrEmpty
 
908
    }
 
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
 
912
    }
 
913
}
 
914
 
 
915
Describe "Test Get-LeaderData" {
 
916
    Context "Call Get-LeaderData with no attributes" {
 
917
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
918
            Param (
 
919
                [array]$Command
 
920
            )
 
921
            $expect = @("leader-get.exe", "--format=yaml")
 
922
            if((Compare-Object $Command $expect)) {
 
923
                Throw "Invalid command"
 
924
            }
 
925
            return '{"bogus": "data", "hello": "world"}'
 
926
        }
 
927
        It "Should return leader data" {
 
928
            $r = Get-LeaderData
 
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
 
933
        }
 
934
    }
 
935
    Context "Call Get-LeaderData with attribute" {
 
936
        Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
937
            Param (
 
938
                [array]$Command
 
939
            )
 
940
            $expect = @("leader-get.exe", "--format=yaml")
 
941
            if((Compare-Object $Command ($expect += $Command[-1]))) {
 
942
                Throw "Invalid command"
 
943
            }
 
944
            $data = @{
 
945
                "hello"='"world"';
 
946
            }
 
947
            $r = $data[$Command[-1]]
 
948
            if(!$r){
 
949
                return ''
 
950
            }
 
951
            return $data[$Command[-1]]
 
952
        }
 
953
        It "Should return world" {
 
954
            Get-LeaderData -Attribute "hello" | Should Be "world"
 
955
        }
 
956
        It "Should return empty" {
 
957
            Get-LeaderData -Attribute "empty" | Should BeNullOrEmpty
 
958
        }
 
959
    }
 
960
}
 
961
 
 
962
Describe "Test Get-JujuVersion" {
 
963
    AfterEach {
 
964
        Clear-Environment
 
965
    }
 
966
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
967
        Param (
 
968
            [array]$Command
 
969
        )
 
970
        $expect = @("jujud.exe", "version")
 
971
        if((Compare-Object $Command $expect)) {
 
972
            Throw "Invalid command"
 
973
        }
 
974
        return $env:binVersion
 
975
    }
 
976
    Mock Write-JujuWarning -Verifiable -ModuleName JujuHooks {
 
977
        Param(
 
978
            [Parameter(Mandatory=$true)]
 
979
            [string]$Message
 
980
        )
 
981
    }
 
982
    It "Should return hashtable with 4 fields (development release)" {
 
983
        $env:binVersion = "1.25.1-alpha1-win2012r2-amd64"
 
984
        $r = Get-JujuVersion
 
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
 
991
    }
 
992
    It "Should return a hashtable with 3 fields (production)" {
 
993
        $env:binVersion = "1.25.1-win2012r2-amd64"
 
994
        $r = Get-JujuVersion
 
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"
 
999
    }
 
1000
    It "Should Throw an exception" {
 
1001
        $env:binVersion = "1.25.1"
 
1002
        { Get-JujuVersion } | Should Throw
 
1003
    }
 
1004
}
 
1005
 
 
1006
Describe "Test Get-JujuStatus" {
 
1007
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
1008
        Param (
 
1009
            [array]$Command
 
1010
        )
 
1011
        $expect = @("status-get.exe", "--include-data","--format=yaml")
 
1012
        if((Compare-Object $Command $expect)) {
 
1013
            Throw "Invalid command"
 
1014
        }
 
1015
        return '{"message":"Unit is ready","status":"active","status-data":{"sample": 1}}'
 
1016
    }
 
1017
    It "Should return the status" {
 
1018
        Get-JujuStatus | Should Be "active"
 
1019
    }
 
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
 
1027
    }
 
1028
}
 
1029
 
 
1030
Describe "Test Set-JujuStatus" {
 
1031
    AfterEach {
 
1032
        Clear-Environment
 
1033
    }
 
1034
    Mock Write-JujuWarning -Verifiable -ModuleName JujuHooks {
 
1035
        Param(
 
1036
            [Parameter(Mandatory=$true)]
 
1037
            [string]$Message
 
1038
        )
 
1039
    }
 
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"
 
1044
        }
 
1045
        $expect = @("status-set.exe", $Command[1])
 
1046
        if((Compare-Object $Command $expect)) {
 
1047
            Throw "Invalid command"
 
1048
        }
 
1049
        $tmpStatus = @{
 
1050
            "status"=$Command[1];
 
1051
            "message"="";
 
1052
            "status-data"=@{};
 
1053
        }
 
1054
        $env:PesterTestData = (ConvertTo-Yaml $tmpStatus)
 
1055
    }
 
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"
 
1060
        }
 
1061
        $expect = @("status-set.exe", $Command[1], $Command[2])
 
1062
        if((Compare-Object $Command $expect)) {
 
1063
            Throw "Invalid command"
 
1064
        }
 
1065
        $tmpStatus = @{
 
1066
            "status"=$Command[1];
 
1067
            "message"=$Command[2];
 
1068
            "status-data"=@{};
 
1069
        }
 
1070
        $env:PesterTestData = (ConvertTo-Yaml $tmpStatus)
 
1071
    }
 
1072
    Mock Get-JujuStatus -ModuleName JujuHooks {
 
1073
        Param(
 
1074
            [switch]$Full=$false
 
1075
        )
 
1076
        $js = $env:PesterTestData
 
1077
        $data = ConvertFrom-Yaml $js
 
1078
        if($Full) {
 
1079
            return $js
 
1080
        }
 
1081
        return (ConvertTo-Yaml $data.status)
 
1082
    }
 
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 
 
1090
    }
 
1091
 
 
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 
 
1100
    }
 
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
 
1108
 
 
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
 
1114
    }
 
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
 
1118
    }
 
1119
}
 
1120
 
 
1121
Describe "Test Get-JujuAction" {
 
1122
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
1123
        Param (
 
1124
            [array]$Command
 
1125
        )
 
1126
        $data = @{
 
1127
            "bla"="bla";
 
1128
        }
 
1129
        if ($Command.Count -gt 3 -or $Command.Count -lt 2){
 
1130
            Throw "invalid command"
 
1131
        }
 
1132
        $expect = @("action-get.exe", "--format=yaml")
 
1133
        if ($Command.Count -eq 3){
 
1134
            $expect += $Command[-1]
 
1135
        }
 
1136
        if((Compare-Object $Command $expect)) {
 
1137
            Throw "Invalid command"
 
1138
        }
 
1139
        if($Command.Count -eq 3){
 
1140
            return (ConvertTo-Yaml $data[$Command[2]])
 
1141
        }
 
1142
        return (ConvertTo-Yaml $data)
 
1143
    }
 
1144
    It "Should send proper command" {
 
1145
        (Get-JujuAction).GetType() | Should Be "hashtable"
 
1146
    }
 
1147
    It "Should return value" {
 
1148
        Get-JujuAction -Parameter "bla" | Should be "bla"
 
1149
    }
 
1150
    It "Should return empty" {
 
1151
        Get-JujuAction -Parameter "NotThere" | Should BeNullOrEmpty
 
1152
    }
 
1153
}
 
1154
 
 
1155
Describe "Test Set-JujuAction" {
 
1156
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
1157
        Param (
 
1158
            [array]$Command
 
1159
        )
 
1160
        $expect = @("action-set.exe", "hello=world", "password=secret")
 
1161
        if((Compare-Object $Command $expect)) {
 
1162
            Throw "Invalid command"
 
1163
        }
 
1164
    }
 
1165
    It "Should send proper parameters to leader-set" {
 
1166
        $data = @{
 
1167
            "hello"="world";
 
1168
            "password"="secret";
 
1169
        }
 
1170
        Set-JujuAction -Settings $data | Should BeNullOrEmpty
 
1171
    }
 
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
 
1175
    }
 
1176
}
 
1177
 
 
1178
Describe "Test Set-JujuActionFailed" {
 
1179
    Mock Invoke-JujuCommand -ModuleName JujuHooks {
 
1180
        Param (
 
1181
            [array]$Command
 
1182
        )
 
1183
        if ($Command.Count -lt 1 -or $Command.Count -gt 2) {
 
1184
            Throw "Invalid parameters"
 
1185
        }
 
1186
        $expect = @("action-fail.exe")
 
1187
        if($Command.Count -eq 2) {
 
1188
            $expect += $Command[-1]
 
1189
        }
 
1190
        if((Compare-Object $Command $expect)) {
 
1191
            Throw "Invalid command"
 
1192
        }
 
1193
    }
 
1194
    It "Should send action fail" {
 
1195
        Set-JujuActionFailed | Should BeNullOrEmpty
 
1196
    }
 
1197
}
 
1198
 
 
1199
Describe "Test Convert-JujuUnitNameToNetbios" {
 
1200
    AfterEach {
 
1201
        Clear-Environment
 
1202
    }
 
1203
    Mock Get-JujuLocalUnit -ModuleName JujuHooks {
 
1204
        return $env:PesterTestData
 
1205
    }
 
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"
 
1213
    }
 
1214
}
 
1215
 
 
1216
Describe "Test Set-CharmState" {
 
1217
    AfterEach {
 
1218
        $CharmStateKey = "HKCU:\Software\Juju-Charms"
 
1219
        if($CharmStateKey -and (Test-Path $CharmStateKey)) {
 
1220
            Remove-Item $CharmStateKey -Recurse -Force
 
1221
        }
 
1222
    }
 
1223
    BeforeEach {
 
1224
        $CharmStateKey = "HKCU:\Software\Juju-Charms"
 
1225
        if($CharmStateKey -and (Test-Path $CharmStateKey)) {
 
1226
            Remove-Item $CharmStateKey -Recurse -Force
 
1227
        }
 
1228
    }
 
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")
 
1238
    }
 
1239
}
 
1240
 
 
1241
Describe "Test Get-CharmState" {
 
1242
    AfterEach {
 
1243
        $CharmStateKey = "HKCU:\Software\Juju-Charms"
 
1244
        if($CharmStateKey -and (Test-Path $CharmStateKey)) {
 
1245
            Remove-Item $CharmStateKey -Recurse -Force
 
1246
        }
 
1247
    }
 
1248
    BeforeEach {
 
1249
        $CharmStateKey = "HKCU:\Software\Juju-Charms"
 
1250
        if($CharmStateKey -and (Test-Path $CharmStateKey)) {
 
1251
            Remove-Item $CharmStateKey -Recurse -Force
 
1252
        }
 
1253
    }
 
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"
 
1258
    }
 
1259
}
 
1260
 
 
1261
Describe "Test Remove-CharmState" {
 
1262
    AfterEach {
 
1263
        $CharmStateKey = "HKCU:\Software\Juju-Charms"
 
1264
        if($CharmStateKey -and (Test-Path $CharmStateKey)) {
 
1265
            Remove-Item $CharmStateKey -Recurse -Force
 
1266
        }
 
1267
    }
 
1268
    BeforeEach {
 
1269
        $CharmStateKey = "HKCU:\Software\Juju-Charms"
 
1270
        if($CharmStateKey -and (Test-Path $CharmStateKey)) {
 
1271
            Remove-Item $CharmStateKey -Recurse -Force
 
1272
        }
 
1273
    }
 
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
 
1279
    }
 
1280
}
 
 
b'\\ No newline at end of file'