github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_addkey_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  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestAddkey(t *testing.T) {
    18  	cases := []struct {
    19  		name, pl, in string
    20  		expect       interface{}
    21  		fail         bool
    22  	}{
    23  		{
    24  			name: "value type: string",
    25  			in:   `1.2.3.4 - - [29/Nov/2021:07:30:50 +0000] "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`,
    26  			pl: `
    27  add_key(add_new_key, "shanghai")
    28  `,
    29  			expect: "shanghai",
    30  			fail:   false,
    31  		},
    32  		{
    33  			name: "value type: number(int64)",
    34  			in:   `1.2.3.4 - - [29/Nov/2021:07:30:50 +0000] "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`,
    35  			pl: `
    36  		grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
    37  		add_key(add_new_key, -1)
    38  		`,
    39  			expect: int64(-1),
    40  			fail:   false,
    41  		},
    42  		{
    43  			name: "value type: number(float64)",
    44  			in:   `1.2.3.4 - - [29/Nov/2021:07:30:50 +0000] "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`,
    45  			pl: `
    46  		grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
    47  		add_key(add_new_key, 1.)
    48  		`,
    49  			expect: float64(1),
    50  			fail:   false,
    51  		},
    52  		{
    53  			name: "value type: number(float64)",
    54  			in:   `1.2.3.4 - - [29/Nov/2021:07:30:50 +0000] "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`,
    55  			pl: `
    56  		grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
    57  		add_key(add_new_key, .1)
    58  		`,
    59  			expect: float64(.1),
    60  			fail:   true, // .1 not supported
    61  		},
    62  		{
    63  			name: "value type: bool",
    64  			in:   `1.2.3.4 - - [29/Nov/2021:07:30:50 +0000] "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`,
    65  			pl: `
    66  		grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
    67  		add_key(add_new_key, true)
    68  		`,
    69  			expect: true,
    70  			fail:   false,
    71  		},
    72  		{
    73  			name: "value type: bool",
    74  			in:   `1.2.3.4 - - [29/Nov/2021:07:30:50 +0000] "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`,
    75  			pl: `
    76  		grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
    77  		add_key(add_new_key, tRue)
    78  		`,
    79  			expect: true,
    80  			fail:   false,
    81  		},
    82  		{
    83  			name: "value type: bool",
    84  			in:   `1.2.3.4 - - [29/Nov/2021:07:30:50 +0000] "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`,
    85  			pl: `
    86  		grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
    87  		add_key(add_new_key, false)
    88  		`,
    89  			expect: false,
    90  			fail:   false,
    91  		},
    92  		{
    93  			name: "value type: nil",
    94  			in:   `1.2.3.4 - - [29/Nov/2021:07:30:50 +0000] "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`,
    95  			pl: `
    96  		grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
    97  		add_key(add_new_key, nil)
    98  		`,
    99  			expect: nil,
   100  			fail:   false,
   101  		},
   102  	}
   103  
   104  	for idx, tc := range cases {
   105  		t.Run(tc.name, func(t *testing.T) {
   106  			runner, err := NewTestingRunner(tc.pl)
   107  			if err != nil {
   108  				if tc.fail {
   109  					t.Logf("[%d]expect error: %s", idx, err)
   110  				} else {
   111  					t.Errorf("[%d] failed: %s", idx, err)
   112  				}
   113  				return
   114  			}
   115  			pt := ptinput.NewPlPoint(point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
   116  
   117  			errR := runScript(runner, pt)
   118  
   119  			if errR == nil {
   120  				v, _, e := pt.Get("add_new_key")
   121  				assert.NoError(t, e)
   122  				assert.Equal(t, tc.expect, v)
   123  				t.Logf("[%d] PASS", idx)
   124  			} else {
   125  				t.Error(errR)
   126  			}
   127  		})
   128  	}
   129  }