...

Source file src/github.com/prometheus/alertmanager/cluster/tls_connection_test.go

Documentation: github.com/prometheus/alertmanager/cluster

     1  // Copyright 2020 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cluster
    15  
    16  import (
    17  	"errors"
    18  	"net"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestWriteStream(t *testing.T) {
    26  	w, r := net.Pipe()
    27  	conn := &tlsConn{
    28  		connection: w,
    29  	}
    30  	defer r.Close()
    31  	go func() {
    32  		conn.writeStream()
    33  		w.Close()
    34  	}()
    35  	packet, err := rcvTLSConn(r).read()
    36  	require.Nil(t, err)
    37  	require.Nil(t, packet)
    38  }
    39  
    40  func TestWritePacket(t *testing.T) {
    41  	testCases := []struct {
    42  		fromAddr string
    43  		msg      string
    44  	}{
    45  		{fromAddr: "127.0.0.1:8001", msg: ""},
    46  		{fromAddr: "10.0.0.4:9094", msg: "hello"},
    47  		{fromAddr: "127.0.0.1:8001", msg: "0"},
    48  	}
    49  	for _, tc := range testCases {
    50  		w, r := net.Pipe()
    51  		defer r.Close()
    52  		go func() {
    53  			conn := &tlsConn{connection: w}
    54  			conn.writePacket(tc.fromAddr, []byte(tc.msg))
    55  			w.Close()
    56  		}()
    57  		packet, err := rcvTLSConn(r).read()
    58  		require.Nil(t, err)
    59  		require.Equal(t, tc.msg, string(packet.Buf))
    60  		require.Equal(t, tc.fromAddr, packet.From.String())
    61  
    62  	}
    63  }
    64  
    65  func TestRead_Nil(t *testing.T) {
    66  	packet, err := (&tlsConn{}).read()
    67  	require.Nil(t, packet)
    68  	require.NotNil(t, err)
    69  }
    70  
    71  func TestTLSConn_Close(t *testing.T) {
    72  	testCases := []string{
    73  		"foo",
    74  		"bar",
    75  	}
    76  	for _, tc := range testCases {
    77  		c := &tlsConn{
    78  			connection: &mockConn{
    79  				errMsg: tc,
    80  			},
    81  			live: true,
    82  		}
    83  		err := c.Close()
    84  		require.Equal(t, errors.New(tc), err, tc)
    85  		require.False(t, c.alive())
    86  		require.True(t, c.connection.(*mockConn).closed)
    87  	}
    88  }
    89  
    90  type mockConn struct {
    91  	closed bool
    92  	errMsg string
    93  }
    94  
    95  func (m *mockConn) Read(b []byte) (n int, err error) {
    96  	panic("implement me")
    97  }
    98  
    99  func (m *mockConn) Write(b []byte) (n int, err error) {
   100  	panic("implement me")
   101  }
   102  
   103  func (m *mockConn) Close() error {
   104  	m.closed = true
   105  	return errors.New(m.errMsg)
   106  }
   107  
   108  func (m *mockConn) LocalAddr() net.Addr {
   109  	panic("implement me")
   110  }
   111  
   112  func (m *mockConn) RemoteAddr() net.Addr {
   113  	panic("implement me")
   114  }
   115  
   116  func (m *mockConn) SetDeadline(t time.Time) error {
   117  	panic("implement me")
   118  }
   119  
   120  func (m *mockConn) SetReadDeadline(t time.Time) error {
   121  	panic("implement me")
   122  }
   123  
   124  func (m *mockConn) SetWriteDeadline(t time.Time) error {
   125  	panic("implement me")
   126  }
   127  

View as plain text