github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_getkey_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 TestGetkey(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  			key = "shanghai"
    28  			add_key(key)
    29  			key = "tokyo" 
    30  			add_key(add_new_key, key)
    31  `,
    32  			expect: "tokyo",
    33  			fail:   false,
    34  		},
    35  		{
    36  			name: "value type: string",
    37  			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"`,
    38  			pl: `
    39  			key = "shanghai"
    40  			add_key(key)
    41  			key = "tokyo" 
    42  			add_key(add_new_key, get_key(key))
    43  `,
    44  			expect: "shanghai",
    45  			fail:   false,
    46  		},
    47  	}
    48  
    49  	for idx, tc := range cases {
    50  		t.Run(tc.name, func(t *testing.T) {
    51  			runner, err := NewTestingRunner(tc.pl)
    52  			if err != nil {
    53  				if tc.fail {
    54  					t.Logf("[%d]expect error: %s", idx, err)
    55  				} else {
    56  					t.Errorf("[%d] failed: %s", idx, err)
    57  				}
    58  				return
    59  			}
    60  			pt := ptinput.NewPlPoint(
    61  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
    62  			errR := runScript(runner, pt)
    63  
    64  			if errR == nil {
    65  				v, _, _ := pt.Get("add_new_key")
    66  				assert.Equal(t, tc.expect, v)
    67  				t.Logf("[%d] PASS", idx)
    68  			} else {
    69  				t.Error(errR)
    70  			}
    71  		})
    72  	}
    73  }