github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_drop_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 funcs
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/GuanceCloud/cliutils/pipeline/ptinput"
    13  	"github.com/GuanceCloud/cliutils/point"
    14  	tu "github.com/GuanceCloud/cliutils/testutil"
    15  )
    16  
    17  func TestDrop(t *testing.T) {
    18  	cases := []struct {
    19  		name, pl, in string
    20  		outkey       string
    21  		expect       interface{}
    22  		fail         bool
    23  	}{
    24  		{
    25  			name: "cast int",
    26  			in:   `162.62.81.1 - - [29/Nov/2021:07:30:50 +0000] "123 /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`,
    27  			pl: `
    28  	grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA:data} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
    29  	drop()
    30  	cast(data, "int")
    31  	`,
    32  			outkey: "data",
    33  			expect: nil,
    34  			fail:   false,
    35  		},
    36  	}
    37  
    38  	for idx, tc := range cases {
    39  		t.Run(tc.name, func(t *testing.T) {
    40  			runner, err := NewTestingRunner(tc.pl)
    41  			if err != nil {
    42  				if tc.fail {
    43  					t.Logf("[%d]expect error: %s", idx, err)
    44  				} else {
    45  					t.Errorf("[%d] failed: %s", idx, err)
    46  				}
    47  				return
    48  			}
    49  			pt := ptinput.NewPlPoint(
    50  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
    51  			errR := runScript(runner, pt)
    52  			if errR != nil {
    53  				t.Fatal(*errR)
    54  			}
    55  
    56  			if errR != nil {
    57  				t.Fatal(errR)
    58  			}
    59  
    60  			if pt.Dropped() {
    61  				return
    62  			}
    63  			v, _, _ := pt.Get(tc.outkey)
    64  			tu.Equals(t, tc.expect, v)
    65  			t.Logf("[%d] PASS", idx)
    66  		})
    67  	}
    68  }