github.com/GuanceCloud/cliutils@v1.1.21/dialtesting/icmp_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 dialtesting
     7  
     8  import (
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  var icmpCases = []struct {
    14  	t         *ICMPTask
    15  	fail      bool
    16  	reasonCnt int
    17  }{
    18  	{
    19  		fail:      false,
    20  		reasonCnt: 0,
    21  		t: &ICMPTask{
    22  			Host:        "localhost",
    23  			PacketCount: 5,
    24  			SuccessWhen: []*ICMPSuccess{
    25  				{
    26  					ResponseTime: []*ResponseTimeSucess{
    27  						{
    28  							Func:   "avg",
    29  							Op:     "lt",
    30  							Target: "10ms",
    31  						},
    32  					},
    33  				},
    34  			},
    35  			ExternalID: "xxxx", Frequency: "10s", Name: "success-ipv4",
    36  		},
    37  	},
    38  	{
    39  		fail:      false,
    40  		reasonCnt: 0,
    41  		t: &ICMPTask{
    42  			Host:        "::1",
    43  			PacketCount: 5,
    44  			SuccessWhen: []*ICMPSuccess{
    45  				{
    46  					ResponseTime: []*ResponseTimeSucess{
    47  						{
    48  							Func:   "avg",
    49  							Op:     "lt",
    50  							Target: "10ms",
    51  						},
    52  					},
    53  				},
    54  			},
    55  			ExternalID: "xxxx", Frequency: "10s", Name: "success-ipv6",
    56  		},
    57  	},
    58  }
    59  
    60  func TestIcmp(t *testing.T) {
    61  	for _, c := range icmpCases {
    62  		if err := c.t.Check(); err != nil {
    63  			if c.fail == false {
    64  				t.Errorf("case: %s, failed: %s", c.t.Name, err)
    65  			} else {
    66  				t.Logf("expected: %s", err.Error())
    67  			}
    68  			continue
    69  		}
    70  
    71  		err := c.t.Run()
    72  		if err != nil {
    73  			if c.fail == false {
    74  				t.Errorf("case %s failed: %s", c.t.Name, err)
    75  			} else {
    76  				t.Logf("expected: %s", err.Error())
    77  			}
    78  			continue
    79  		}
    80  
    81  		tags, fields := c.t.GetResults()
    82  
    83  		t.Logf("ts: %+#v \n fs: %+#v \n ", tags, fields)
    84  
    85  		reasons, _ := c.t.CheckResult()
    86  		if len(reasons) != c.reasonCnt {
    87  			t.Errorf("case %s expect %d reasons, but got %d reasons:\n\t%s",
    88  				c.t.Name, c.reasonCnt, len(reasons), strings.Join(reasons, "\n\t"))
    89  		} else if len(reasons) > 0 {
    90  			t.Logf("case %s reasons:\n\t%s",
    91  				c.t.Name, strings.Join(reasons, "\n\t"))
    92  		}
    93  	}
    94  }