This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / spindle / models / logger_test.go
2.6 kB 102 lines
1package models 2 3import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8) 9 10func testWorkflowId(name string) WorkflowId { 11 return WorkflowId{PipelineId: PipelineId{Knot: "knot1", Rkey: "rkey1"}, Name: name} 12} 13 14func TestDataWriterMasksSecretSplitAcrossWrites(t *testing.T) { 15 dir := t.TempDir() 16 secret := "hunter2-super-secret-token" 17 wid := testWorkflowId("mask") 18 logger, err := NewFileWorkflowLogger(dir, wid, []string{secret}) 19 if err != nil { 20 t.Fatal(err) 21 } 22 w := logger.DataWriter(0, "stdout") 23 24 for _, ch := range strings.Split("prefix "+secret+" suffix", "") { 25 if _, err := w.Write([]byte(ch)); err != nil { 26 t.Fatal(err) 27 } 28 } 29 if err := logger.Close(); err != nil { 30 t.Fatal(err) 31 } 32 33 raw, err := os.ReadFile(filepath.Join(dir, wid.String()+".log")) 34 if err != nil { 35 t.Fatal(err) 36 } 37 if strings.Contains(string(raw), secret) { 38 t.Errorf("log contains raw secret: %s", raw) 39 } 40 if !strings.Contains(string(raw), "***") { 41 t.Errorf("log does not contain masked marker: %s", raw) 42 } 43 // trailing bytes land in the final flush entry contiguously 44 if !strings.Contains(string(raw), "suffix") { 45 t.Errorf("log lost trailing output: %s", raw) 46 } 47} 48 49func TestDataWriterMasksSingleFrame(t *testing.T) { 50 dir := t.TempDir() 51 secret := "hunter2-super-secret-token" 52 wid := testWorkflowId("frame") 53 logger, err := NewFileWorkflowLogger(dir, wid, []string{secret}) 54 if err != nil { 55 t.Fatal(err) 56 } 57 w := logger.DataWriter(0, "stdout") 58 if _, err := w.Write([]byte("token is " + secret + " ok")); err != nil { 59 t.Fatal(err) 60 } 61 if err := logger.Close(); err != nil { 62 t.Fatal(err) 63 } 64 65 raw, err := os.ReadFile(filepath.Join(dir, wid.String()+".log")) 66 if err != nil { 67 t.Fatal(err) 68 } 69 if strings.Contains(string(raw), secret) { 70 t.Errorf("log contains raw secret: %s", raw) 71 } 72 if !strings.Contains(string(raw), "en is *** ok") { 73 t.Errorf("masked entry mangled: %s", raw) 74 } 75} 76 77func TestDataWriterNoMaskPassthrough(t *testing.T) { 78 dir := t.TempDir() 79 wid := testWorkflowId("plain") 80 logger, err := NewFileWorkflowLogger(dir, wid, nil) 81 if err != nil { 82 t.Fatal(err) 83 } 84 w := logger.DataWriter(0, "stdout") 85 if _, err := w.Write([]byte("hello")); err != nil { 86 t.Fatal(err) 87 } 88 if _, err := w.Write([]byte(" world")); err != nil { 89 t.Fatal(err) 90 } 91 if err := logger.Close(); err != nil { 92 t.Fatal(err) 93 } 94 95 raw, err := os.ReadFile(filepath.Join(dir, wid.String()+".log")) 96 if err != nil { 97 t.Fatal(err) 98 } 99 if !strings.Contains(string(raw), "hello") || !strings.Contains(string(raw), " world") { 100 t.Errorf("log missing output: %s", raw) 101 } 102}