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

     1  package traceroute
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/grafana/synthetic-monitoring-agent/internal/model"
     9  	sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
    10  	"github.com/rs/zerolog"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestName(t *testing.T) {
    15  	name := Prober.Name(Prober{})
    16  	require.Equal(t, name, "traceroute")
    17  }
    18  
    19  func TestNewProber(t *testing.T) {
    20  	logger := zerolog.New(io.Discard)
    21  	testcases := map[string]struct {
    22  		input       model.Check
    23  		expected    Prober
    24  		ExpectError bool
    25  	}{
    26  		"default": {
    27  			input: model.Check{
    28  				Check: sm.Check{
    29  					Target: "www.grafana.com",
    30  					Settings: sm.CheckSettings{
    31  						Traceroute: &sm.TracerouteSettings{},
    32  					},
    33  				},
    34  			},
    35  			expected: Prober{
    36  				config: Module{
    37  					count:          5,
    38  					timeout:        30 * time.Second,
    39  					hopTimeout:     500 * time.Millisecond,
    40  					interval:       time.Nanosecond,
    41  					hopSleep:       time.Nanosecond,
    42  					maxHops:        64,
    43  					maxUnknownHops: 15,
    44  					ptrLookup:      false,
    45  					ringBufferSize: 50,
    46  					srcAddr:        "",
    47  				},
    48  				logger: logger,
    49  			},
    50  			ExpectError: false,
    51  		},
    52  		"no-settings": {
    53  			input: model.Check{
    54  				Check: sm.Check{
    55  					Target: "www.grafana.com",
    56  					Settings: sm.CheckSettings{
    57  						Tcp: nil,
    58  					},
    59  				},
    60  			},
    61  			expected:    Prober{},
    62  			ExpectError: true,
    63  		},
    64  	}
    65  
    66  	for name, testcase := range testcases {
    67  		logger := zerolog.New(io.Discard)
    68  
    69  		t.Run(name, func(t *testing.T) {
    70  			actual, err := NewProber(testcase.input, logger)
    71  			require.Equal(t, &testcase.expected, &actual)
    72  
    73  			if testcase.ExpectError {
    74  				require.Error(t, err, "unsupported check")
    75  			} else {
    76  				require.NoError(t, err)
    77  			}
    78  		})
    79  	}
    80  }
    81  
    82  func TestSettingsToModule(t *testing.T) {
    83  	testcases := map[string]struct {
    84  		input    sm.TracerouteSettings
    85  		expected Module
    86  	}{
    87  		"default": {
    88  			input: sm.TracerouteSettings{},
    89  			expected: Module{
    90  				count:          5,
    91  				timeout:        30 * time.Second,
    92  				hopTimeout:     500 * time.Millisecond,
    93  				interval:       time.Nanosecond,
    94  				hopSleep:       time.Nanosecond,
    95  				maxHops:        64,
    96  				maxUnknownHops: 15,
    97  				ptrLookup:      false,
    98  				ringBufferSize: 50,
    99  				srcAddr:        "",
   100  			},
   101  		},
   102  		"partial-settings": {
   103  			input: sm.TracerouteSettings{
   104  				HopTimeout:     100,
   105  				MaxUnknownHops: 2,
   106  				PtrLookup:      true,
   107  			},
   108  			expected: Module{
   109  				count:          5,
   110  				timeout:        30 * time.Second,
   111  				hopTimeout:     100,
   112  				interval:       time.Nanosecond,
   113  				hopSleep:       time.Nanosecond,
   114  				maxHops:        64,
   115  				maxUnknownHops: 2,
   116  				ptrLookup:      true,
   117  				ringBufferSize: 50,
   118  				srcAddr:        "",
   119  			},
   120  		},
   121  	}
   122  
   123  	for name, testcase := range testcases {
   124  		t.Run(name, func(t *testing.T) {
   125  			actual := settingsToModule(&testcase.input)
   126  			require.Equal(t, &testcase.expected, &actual)
   127  		})
   128  	}
   129  }