
EnvironmentTest.pudu
Pudu55 lines3.3 KB
1/** @Test.Enrichers.Environment.Suite — machine, user, process, and failure properties */2module PuduLangLog.Enrichers.EnvironmentTest34import Std.Io as Io5import Std.Test as Test6import PuduLangLog.Configuration as Configuration7import PuduLangLog.Enrichers.Environment as Environment8import PuduLangLog.Event as Event9import PuduLangLog.Failure as Failure10import PuduLangLog as Log11import PuduLangLog.Logger as Logger12import PuduLangLog.Sinks.Memory as Memory13import PuduLangLog.Value as Value141516fn main() -> Int {17 let memory = Memory.create()18 let logger = Configuration.create().writeTo(Memory.sink(&memory)).enrichWith(Environment.machineName()).enrichWith(Environment.environmentUserName()).enrichWith(Environment.environmentName("PUDU_LANG_LOG_UNSET_VARIABLE")).enrichWith(Environment.environmentName("HOME")).enrichWith(Environment.processId()).enrichWith(Environment.processName()).enrichWith(Environment.environmentVariable("HOME")).enrichWith(Environment.environmentVariable("PUDU_LANG_LOG_UNSET_VARIABLE")).enrichWith(Environment.failureDetail()).createLogger()19 logger.writeFailure(Log.Error, Failure.causedBy(&Failure.withTrace(&Failure.of("IoError", "full"), ["Disk.write"]), Failure.of("Quota", "exceeded")), "failed", [])20 logger.information("fine", [])21 let failed = Memory.events(&memory)[0]22 let fine = Memory.events(&memory)[1]23 let checks = Test.suite("Enrichers.Environment", &[24 Test.that("the machine has a name", match Event.property(&failed, "MachineName") {25 case Some(Log.Scalar(Log.Text(name))) => !name.isEmpty()26 case _ => false27 }),28 Test.that("the user name is text", match Event.property(&failed, "EnvironmentUserName") {29 case Some(Log.Scalar(Log.Text(_))) => true30 case _ => false31 }),32 Test.equals("an unset environment variable means production, and the first enricher wins", &Event.property(&failed, "EnvironmentName"), &Some(Value.text("Production"))),33 Test.that("the process has a positive identifier", match Event.property(&failed, "ProcessId") {34 case Some(Log.Scalar(Log.Integer(number))) => number > 035 case _ => false36 }),37 Test.that("the process has a name", match Event.property(&failed, "ProcessName") {38 case Some(Log.Scalar(Log.Text(name))) => !name.isEmpty() && !name.contains("/")39 case _ => false40 }),41 Test.that("a set variable becomes a property of its name", match Event.property(&failed, "HOME") {42 case Some(Log.Scalar(Log.Text(home))) => !home.isEmpty()43 case _ => false44 }),45 Test.equals("an unset variable adds nothing", &Event.property(&failed, "PUDU_LANG_LOG_UNSET_VARIABLE"), &None),46 Test.equals("failure details as a structure", &Event.property(&failed, "FailureDetail"), &Some(Value.structure("IoError", [("Kind", Value.text("IoError")), ("Message", Value.text("full")), ("Trace", Value.list([Value.text("Disk.write")])), ("Causes", Value.list([Value.structure("Quota", [("Kind", Value.text("Quota")), ("Message", Value.text("exceeded")), ("Trace", Value.list([])), ("Causes", Value.list([]))])]))]))),47 Test.equals("no failure means null detail", &Event.property(&fine, "FailureDetail"), &Some(Value.nothing()))48 ])49 let ran = Test.run(&checks)50 for failure in Test.failuresOf(&ran) {51 let _reported = Io.writeErrorLine(failure)52 }53 Test.report(&ran)54}55