github.com/GuanceCloud/cliutils@v1.1.21/dialtesting/tcp_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  	"net"
    10  	"strings"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  var tcpCases = []struct {
    16  	t         *TCPTask
    17  	fail      bool
    18  	reasonCnt int
    19  }{
    20  	{
    21  		fail:      false,
    22  		reasonCnt: 0,
    23  		t: &TCPTask{
    24  			SuccessWhen: []*TCPSuccess{
    25  				{
    26  					ResponseTime: []*TCPResponseTime{{
    27  						Target: "10s",
    28  					}},
    29  				},
    30  			},
    31  			ExternalID: "xxxx", Frequency: "10s", Name: "success",
    32  		},
    33  	},
    34  	{
    35  		fail:      false,
    36  		reasonCnt: 1,
    37  		t: &TCPTask{
    38  			SuccessWhen: []*TCPSuccess{
    39  				{
    40  					ResponseTime: []*TCPResponseTime{{
    41  						Target: "1us",
    42  					}},
    43  				},
    44  			},
    45  			ExternalID: "xxxx", Frequency: "10s", Name: "response_time_large",
    46  		},
    47  	},
    48  	{
    49  		fail:      false,
    50  		reasonCnt: 0,
    51  		t: &TCPTask{
    52  			Message: "hello",
    53  			SuccessWhen: []*TCPSuccess{
    54  				{
    55  					ResponseMessage: []*SuccessOption{{
    56  						Contains: "hello",
    57  					}},
    58  				},
    59  			},
    60  			ExternalID: "xxxx", Frequency: "10s", Name: "response_message_valid",
    61  		},
    62  	},
    63  	{
    64  		fail:      false,
    65  		reasonCnt: 1,
    66  		t: &TCPTask{
    67  			Message: "hello",
    68  			SuccessWhen: []*TCPSuccess{
    69  				{
    70  					ResponseMessage: []*SuccessOption{{
    71  						Contains: "invalid",
    72  					}},
    73  				},
    74  			},
    75  			ExternalID: "xxxx", Frequency: "10s", Name: "response_message_invalid",
    76  		},
    77  	},
    78  }
    79  
    80  func TestTcp(t *testing.T) {
    81  	for _, c := range tcpCases {
    82  		server, err := tcpServer()
    83  		if err != nil {
    84  			t.Fail()
    85  		}
    86  		defer server.Close()
    87  
    88  		addr := server.Addr().String()
    89  
    90  		host, port, err := net.SplitHostPort(addr)
    91  		if err != nil {
    92  			t.Errorf(err.Error())
    93  			continue
    94  		}
    95  		c.t.Host = host
    96  		c.t.Port = port
    97  
    98  		if err := c.t.Check(); err != nil {
    99  			if c.fail == false {
   100  				t.Errorf("case: %s, failed: %s", c.t.Name, err)
   101  			} else {
   102  				t.Logf("expected: %s", err.Error())
   103  			}
   104  			continue
   105  		}
   106  
   107  		err = c.t.Run()
   108  
   109  		if err != nil {
   110  			if c.fail == false {
   111  				t.Errorf("case %s failed: %s", c.t.Name, err)
   112  			} else {
   113  				t.Logf("expected: %s", err.Error())
   114  			}
   115  			continue
   116  		}
   117  
   118  		tags, fields := c.t.GetResults()
   119  
   120  		t.Logf("ts: %+#v \n fs: %+#v \n ", tags, fields)
   121  
   122  		reasons, _ := c.t.CheckResult()
   123  		if len(reasons) != c.reasonCnt {
   124  			t.Errorf("case %s expect %d reasons, but got %d reasons:\n\t%s",
   125  				c.t.Name, c.reasonCnt, len(reasons), strings.Join(reasons, "\n\t"))
   126  		} else if len(reasons) > 0 {
   127  			t.Logf("case %s reasons:\n\t%s",
   128  				c.t.Name, strings.Join(reasons, "\n\t"))
   129  		}
   130  	}
   131  }
   132  
   133  func tcpServer() (server net.Listener, err error) {
   134  	server, err = net.Listen("tcp", "")
   135  	if err != nil {
   136  		return
   137  	}
   138  
   139  	go func() {
   140  		time.Sleep(30 * time.Second)
   141  		server.Close()
   142  	}()
   143  
   144  	go func() {
   145  		if conn, err := server.Accept(); err != nil {
   146  			return
   147  		} else {
   148  			defer conn.Close()
   149  			conn.SetDeadline(time.Now().Add(5 * time.Second))
   150  			buf := make([]byte, 1024)
   151  			n, err := conn.Read(buf)
   152  			if err != nil {
   153  				return
   154  			}
   155  
   156  			_, _ = conn.Write(buf[:n])
   157  		}
   158  	}()
   159  
   160  	return
   161  }