github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/prober/tcp/tcp_test.go (about)

     1  package tcp
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/grafana/synthetic-monitoring-agent/internal/model"
    10  	sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
    11  	"github.com/prometheus/blackbox_exporter/config"
    12  	"github.com/rs/zerolog"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestName(t *testing.T) {
    17  	name := Prober.Name(Prober{})
    18  	require.Equal(t, name, "tcp")
    19  }
    20  
    21  func TestNewProber(t *testing.T) {
    22  	testcases := map[string]struct {
    23  		input       model.Check
    24  		expected    Prober
    25  		ExpectError bool
    26  	}{
    27  		"default": {
    28  			input: model.Check{
    29  				Check: sm.Check{
    30  					Target: "www.grafana.com",
    31  					Settings: sm.CheckSettings{
    32  						Tcp: &sm.TcpSettings{},
    33  					},
    34  				},
    35  			},
    36  			expected: Prober{
    37  				config: config.Module{
    38  					Prober:  "tcp",
    39  					Timeout: 0,
    40  					TCP: config.TCPProbe{
    41  						IPProtocol:         "ip6",
    42  						IPProtocolFallback: true,
    43  						QueryResponse:      []config.QueryResponse{},
    44  					},
    45  				},
    46  			},
    47  			ExpectError: false,
    48  		},
    49  		"no-settings": {
    50  			input: model.Check{
    51  				Check: sm.Check{
    52  					Target: "www.grafana.com",
    53  					Settings: sm.CheckSettings{
    54  						Tcp: nil,
    55  					},
    56  				},
    57  			},
    58  			expected:    Prober{},
    59  			ExpectError: true,
    60  		},
    61  	}
    62  
    63  	ctx := testCtx(context.Background(), t)
    64  
    65  	for name, testcase := range testcases {
    66  		logger := zerolog.New(io.Discard)
    67  
    68  		t.Run(name, func(t *testing.T) {
    69  			actual, err := NewProber(ctx, testcase.input, logger)
    70  			require.Equal(t, &testcase.expected, &actual)
    71  
    72  			if testcase.ExpectError {
    73  				require.Error(t, err, "unsupported check")
    74  			} else {
    75  				require.NoError(t, err)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  func TestSettingsToModule(t *testing.T) {
    82  	testcases := map[string]struct {
    83  		input    sm.TcpSettings
    84  		expected config.Module
    85  	}{
    86  		"default": {
    87  			input: sm.TcpSettings{},
    88  			expected: config.Module{
    89  				Prober:  "tcp",
    90  				Timeout: 0,
    91  				TCP: config.TCPProbe{
    92  					IPProtocol:         "ip6",
    93  					IPProtocolFallback: true,
    94  					QueryResponse:      []config.QueryResponse{},
    95  				},
    96  			},
    97  		},
    98  		"partial-settings": {
    99  			input: sm.TcpSettings{
   100  				SourceIpAddress: "0.0.0.0",
   101  				Tls:             true,
   102  				IpVersion:       1,
   103  			},
   104  			expected: config.Module{
   105  				Prober:  "tcp",
   106  				Timeout: 0,
   107  				TCP: config.TCPProbe{
   108  					IPProtocol:         "ip4",
   109  					IPProtocolFallback: false,
   110  					QueryResponse:      []config.QueryResponse{},
   111  					TLS:                true,
   112  					SourceIPAddress:    "0.0.0.0",
   113  				},
   114  			},
   115  		},
   116  	}
   117  
   118  	ctx := testCtx(context.Background(), t)
   119  
   120  	for name, testcase := range testcases {
   121  		logger := zerolog.New(io.Discard)
   122  
   123  		t.Run(name, func(t *testing.T) {
   124  			actual, err := settingsToModule(ctx, &testcase.input, logger)
   125  			require.NoError(t, err)
   126  			require.Equal(t, &testcase.expected, &actual)
   127  		})
   128  	}
   129  }
   130  
   131  func testCtx(ctx context.Context, t *testing.T) context.Context {
   132  	if deadline, ok := t.Deadline(); ok {
   133  		ctx, cancel := context.WithDeadline(ctx, deadline)
   134  		t.Cleanup(cancel)
   135  
   136  		return ctx
   137  	}
   138  
   139  	ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
   140  	t.Cleanup(cancel)
   141  
   142  	return ctx
   143  }