github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_exit_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 TestExit(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  	exit()
    30  	cast(data, "int")
    31  	`,
    32  			outkey: "data",
    33  			expect: "123",
    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  
    50  			pt := ptinput.NewPlPoint(
    51  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
    52  			errR := runScript(runner, pt)
    53  
    54  			if errR != nil {
    55  				t.Fatal(errR.Error())
    56  			}
    57  
    58  			if tc.fail {
    59  				t.Logf("[%d]expect error: %s", idx, errR.Error())
    60  			}
    61  			v, _, _ := pt.Get(tc.outkey)
    62  			tu.Equals(t, tc.expect, v)
    63  			t.Logf("[%d] PASS", idx)
    64  		})
    65  	}
    66  }