github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/ifelse_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 TestIfelse(t *testing.T) {
    18  	cases := []struct {
    19  		name   string
    20  		pl, in string
    21  		expect interface{}
    22  		fail   bool
    23  	}{
    24  		{
    25  			name: "valind-key-is-nil",
    26  			in:   `1.2.3.4 "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413`,
    27  			pl: `
    28  grok(_, "%{IPORHOST:client_ip} \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
    29  
    30  if invalid_status_code == nil {
    31    add_key(add_new_key, "OK")
    32  }
    33  `,
    34  			expect: "OK",
    35  		},
    36  		{
    37  			name: "if 1",
    38  			in:   ``,
    39  			pl: `
    40  if 1 {
    41  	add_key(add_new_key, "OK")
    42  }
    43  `,
    44  			expect: "OK",
    45  		},
    46  		{
    47  			name: "if 1.1",
    48  			in:   ``,
    49  			pl: `
    50  if 1.1 {
    51  	add_key(add_new_key, "OK")
    52  }
    53  `,
    54  			expect: "OK",
    55  		},
    56  
    57  		{
    58  			name: "if 0",
    59  			in:   ``,
    60  			pl: `
    61  if 0 {
    62  	add_key(add_new_key, "OK")
    63  }
    64  `,
    65  			expect: nil,
    66  		},
    67  		{
    68  			name: "if true",
    69  			in:   ``,
    70  			pl: `
    71  if true {
    72  	add_key(add_new_key, "OK")
    73  }
    74  `,
    75  			expect: "OK",
    76  		},
    77  		{
    78  			name: "if string",
    79  			in:   ``,
    80  			pl: `
    81  if "str" {
    82  	add_key(add_new_key, "OK")
    83  }
    84  `,
    85  			expect: "OK",
    86  		},
    87  		{
    88  			name: "if nil",
    89  			in:   ``,
    90  			pl: `
    91  if nil {
    92  	add_key(add_new_key, "OK")
    93  }
    94  `,
    95  			expect: nil,
    96  		},
    97  		{
    98  			name: "if [123]",
    99  			in:   ``,
   100  			pl: `
   101  if [123] {
   102  	add_key(add_new_key, "OK")
   103  }
   104  `,
   105  			expect: "OK",
   106  		},
   107  		{
   108  			name: "if LEN([123])",
   109  			in:   ``,
   110  			pl: `
   111  if len([123]) {
   112  	add_key(add_new_key, "OK")
   113  }
   114  `,
   115  			expect: "OK",
   116  		},
   117  		{
   118  			name: "invalid-key-is-not-nil",
   119  			in:   `1.2.3.4 "POST /?signature=b8d8ea&timestamp=1638171049 HTTP/1.1" 200 413`,
   120  			pl: `
   121  grok(_, "%{IPORHOST:client_ip} \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}")
   122  
   123  if invalid_status_code != nil {
   124    add_key(add_new_key, "OK")
   125  }
   126  `,
   127  			fail: true,
   128  		},
   129  		// 		{
   130  		// 			name: "if expr func",
   131  		// 			in:   `pd`,
   132  		// 			pl: `
   133  		// if match(_, "p([a-z]+)ch"){
   134  		//    add_key(add_new_key, "OK")
   135  		// }
   136  		// `,
   137  		// 			fail: true,
   138  		// 		},
   139  		// 		{
   140  		// 			name: "if expr func",
   141  		// 			in:   `pddeech`,
   142  		// 			pl: `
   143  		// if match(_, "p([a-z]+)ch")  {
   144  		//    add_key(add_new_key, "OK")
   145  		// }
   146  		// `,
   147  		// 			expect: "OK",
   148  		// 		},
   149  	}
   150  	for idx, tc := range cases {
   151  		t.Run(tc.name, func(t *testing.T) {
   152  			runner, err := NewTestingRunner(tc.pl)
   153  			if err != nil {
   154  				if tc.fail {
   155  					t.Logf("[%d]expect error: %s", idx, err)
   156  				} else {
   157  					t.Log(tc.pl)
   158  					t.Errorf("[%d] failed: %s", idx, err)
   159  				}
   160  				return
   161  			}
   162  
   163  			pt := ptinput.NewPlPoint(
   164  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
   165  			errR := runScript(runner, pt)
   166  
   167  			if errR != nil {
   168  				t.Fatal(errR)
   169  			}
   170  
   171  			v, _, _ := pt.Get("add_new_key")
   172  			tu.Equals(t, tc.expect, v)
   173  			t.Logf("[%d] PASS", idx)
   174  		})
   175  	}
   176  }