github.com/GuanceCloud/cliutils@v1.1.21/pipeline/manager/script_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package manager 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/cliutils/point" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestScript(t *testing.T) { 18 ret, retErr := NewScripts(map[string]string{ 19 "abc.p": "if true {}", 20 }, nil, NSGitRepo, point.Logging) 21 22 if len(retErr) > 0 { 23 t.Fatal(retErr) 24 } 25 26 s := ret["abc.p"] 27 28 if ng := s.Engine(); ng == nil { 29 t.Fatalf("no engine") 30 } 31 plpt := ptinput.NewPlPoint(point.Logging, "ng", nil, nil, time.Now()) 32 err := s.Run(plpt, nil, nil) 33 if err != nil { 34 t.Fatal(err) 35 } 36 assert.Equal(t, plpt.Fields(), map[string]interface{}{"status": DefaultStatus}) 37 assert.Equal(t, 0, len(plpt.Tags())) 38 assert.Equal(t, "abc.p", s.Name()) 39 assert.Equal(t, point.Logging, s.Category()) 40 assert.Equal(t, s.NS(), NSGitRepo) 41 42 //nolint:dogsled 43 plpt = ptinput.NewPlPoint(point.Logging, "ng", nil, nil, time.Now()) 44 err = s.Run(plpt, nil, &Option{DisableAddStatusField: true}) 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 if len(plpt.Fields()) != 0 { 50 t.Fatal(plpt.Fields()) 51 } 52 53 //nolint:dogsled 54 plpt = ptinput.NewPlPoint(point.Logging, "ng", nil, nil, time.Now()) 55 err = s.Run(plpt, nil, &Option{ 56 DisableAddStatusField: false, 57 IgnoreStatus: []string{DefaultStatus}, 58 }) 59 if err != nil { 60 t.Fatal(err) 61 } 62 if plpt.Dropped() != true { 63 t.Fatal("!drop") 64 } 65 } 66 67 func TestDrop(t *testing.T) { 68 ret, retErr := NewScripts(map[string]string{"abc.p": "add_key(a, \"a\"); add_key(status, \"debug\"); drop(); add_key(b, \"b\")"}, 69 nil, NSGitRepo, point.Logging) 70 if len(retErr) > 0 { 71 t.Fatal(retErr) 72 } 73 74 s := ret["abc.p"] 75 76 plpt := ptinput.NewPlPoint(point.Logging, "ng", nil, nil, time.Now()) 77 if err := s.Run(plpt, nil, nil); err != nil { 78 t.Fatal(err) 79 } 80 81 if plpt.Dropped() != true { 82 t.Error("drop != true") 83 } 84 }