Skip to content

Commit e9bffbc

Browse files
Update Canary.Utilities.psm1
1 parent 7a7a326 commit e9bffbc

1 file changed

Lines changed: 76 additions & 19 deletions

File tree

CanaryValidator/Canary.Utilities.psm1

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
$Global:JSONLogFile = "Run-Canary.JSON"
22
$Global:TxtLogFile = "AzureStackCanaryLog.Log"
3-
4-
[double]$Global:UsecaseID = 1.0
5-
$Global:Canary = New-Object -TypeName PSCustomObject
6-
$Global:canaryName = ""
7-
$Global:canaryType = ""
8-
$Global:CurrUsecase = ""
9-
3+
$UseCase = @{}
4+
[System.Collections.Stack] $AllUseCases = New-Object System.Collections.Stack
105
filter timestamp {"$(Get-Date -Format G): $_"}
116

127
function Log-Info
@@ -18,6 +13,7 @@ function Log-Info
1813
$Message = "[INFO] " + $Message | timestamp
1914
}
2015
$Message | Tee-Object -FilePath $Global:TxtLogFile -Append
16+
Log-JSONReport $Message
2117
}
2218

2319
function Log-Error
@@ -26,6 +22,7 @@ function Log-Error
2622

2723
$Message = "[ERR] " + $Message | timestamp
2824
$Message | Tee-Object -FilePath $Global:TxtLogFile -Append
25+
Log-JSONReport $Message
2926
}
3027

3128
function Log-Exception
@@ -34,6 +31,68 @@ function Log-Exception
3431

3532
$Message = "[EXCEPTION] " + $Message | timestamp
3633
$Message | Tee-Object -FilePath $Global:TxtLogFile -Append
34+
Log-JSONReport $Message
35+
}
36+
37+
function Log-JSONReport
38+
{
39+
param (
40+
[string] $Message
41+
)
42+
if ($Message)
43+
{
44+
if ($Message.Contains(": ["))
45+
{
46+
$time = $Message.Substring(0, $Message.IndexOf(": ["))
47+
}
48+
if ($Message.Contains("[START]"))
49+
{
50+
$name = $Message.Substring($Message.LastIndexOf(":") + 1).Trim().Replace("######", "").Trim()
51+
if ($AllUseCases.Count)
52+
{
53+
$nestedUseCase = @{
54+
"Name" = $name
55+
"StartTime" = $time
56+
}
57+
if (-not $AllUseCases.Peek().UseCase)
58+
{
59+
$AllUseCases.Peek().Add("UseCase", @())
60+
}
61+
$AllUseCases.Peek().UseCase += , $nestedUseCase
62+
$AllUseCases.Push($nestedUseCase)
63+
}
64+
else
65+
{
66+
$UseCase.Add("Name", $name)
67+
$UseCase.Add("StartTime", $time)
68+
$AllUseCases.Push($UseCase)
69+
}
70+
}
71+
elseif ($Message.Contains("[END]"))
72+
{
73+
$result = $Message.Substring($Message.LastIndexOf("=") + 1).Trim().Replace("] ######", "").Trim()
74+
$AllUseCases.Peek().Add("EndTime", $time)
75+
$AllUseCases.Peek().Add("Result", $result)
76+
$AllUseCases.Pop() | Out-Null
77+
if (-not $AllUseCases.Count)
78+
{
79+
$jsonReport = ConvertFrom-Json (Get-Content -Path $Global:JSONLogFile -Raw)
80+
$jsonReport.UseCases += , $UseCase
81+
$jsonReport | ConvertTo-Json -Depth 10 | Out-File -FilePath $Global:JSONLogFile
82+
$UseCase.Clear()
83+
}
84+
}
85+
elseif ($Message.Contains("[DESCRIPTION]"))
86+
{
87+
$description = $Message.Substring($Message.IndexOf("[DESCRIPTION]") + "[DESCRIPTION]".Length).Trim()
88+
$AllUseCases.Peek().Add("Description", $description)
89+
}
90+
elseif ($Message.Contains("[EXCEPTION]"))
91+
{
92+
$exception = $Message.Substring($Message.IndexOf("[EXCEPTION]") + "[EXCEPTION]".Length).Trim()
93+
$AllUseCases.Peek().Add("Exception", $exception)
94+
}
95+
}
3796
}
3897

3998
function Start-Scenario
@@ -66,17 +125,15 @@ function Start-Scenario
66125
}
67126
New-Item -Path $Global:JSONLogFile -Type File -Force
68127
New-Item -Path $Global:TxtLogFile -Type File -Force
69-
70-
$Global:canaryName = $Name
71-
$Global:canaryType = $Type
72-
$Global:Canary | Add-Member -Type NoteProperty -TypeName System.Management.Automation.PSCustomObject -Name "$Name-$Type" -Value (New-Object -TypeName PSCustomObject)
73-
$Global:Canary."$Name-$Type" | Add-Member -Type NoteProperty -TypeName System.Management.Automation.PSCustomObject -Name "Usecases" -Value (New-Object -TypeName PSCustomObject)
128+
$jsonReport = @{
129+
"Scenario" = ($Name + "-" + $Type)
130+
"UseCases" = @()
131+
}
132+
$jsonReport | ConvertTo-Json -Depth 10 | Out-File -FilePath $Global:JSONLogFile
74133
}
75134

76135
function End-Scenario
77-
{
78-
$Global:Canary | ConvertTo-Json -Depth 10 | Out-File -FilePath $Global:JSONLogFile
79-
}
136+
{}
80137

81138
function Invoke-Usecase
82139
{
@@ -92,17 +149,17 @@ function Invoke-Usecase
92149
[ValidateNotNullOrEmpty()]
93150
[ScriptBlock]$UsecaseBlock
94151
)
95-
Log-Info ("###### [START] Usecase: $Name ######")
152+
Log-Info ("###### [START] Usecase: $Name ######`n")
96153

97154
if ($Description)
98155
{
99-
Log-Info ($Description)
156+
Log-Info ("[DESCRIPTION] $Description`n")
100157
}
101158

102159
try
103160
{
104161
$result = Invoke-Command -ScriptBlock $UsecaseBlock
105-
if ($result)
162+
if ($result -and (-not $UsecaseBlock.ToString().Contains("Invoke-Usecase")))
106163
{
107164
Log-Info ($result)
108165
}
@@ -580,4 +637,4 @@ function GetAzureStackBlobUri
580637
{
581638
throw $_.Exception.Message
582639
}
583-
}
640+
}

0 commit comments

Comments
 (0)