|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/StackVista/tcptracer-bpf/pkg/tracer" |
| 6 | + tracerConfig "github.com/StackVista/tcptracer-bpf/pkg/tracer/config" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestConnectionsCheck_retryTracerInit(t *testing.T) { |
| 13 | + retryDuration := 40 * time.Millisecond |
| 14 | + retryAmount := 3 |
| 15 | + testRetry := 0 |
| 16 | + |
| 17 | + for _, tc := range []struct { |
| 18 | + name string |
| 19 | + mockMakeTracerFunc func(config *tracerConfig.Config) (tracer.Tracer, error) |
| 20 | + expectedTracer tracer.Tracer |
| 21 | + expectedError string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "Returns the tracer when the make tracer function returns one", |
| 25 | + mockMakeTracerFunc: func(config *tracerConfig.Config) (tracer.Tracer, error) { |
| 26 | + return &tracer.LinuxTracer{}, nil |
| 27 | + }, |
| 28 | + expectedTracer: &tracer.LinuxTracer{}, |
| 29 | + expectedError: "", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Returns the tracer when the amount of retries are below the configured amount. ie retrying makes it work.", |
| 33 | + mockMakeTracerFunc: func(config *tracerConfig.Config) (tracer.Tracer, error) { |
| 34 | + if testRetry < retryAmount-1 { |
| 35 | + testRetry = testRetry + 1 |
| 36 | + return nil, errors.New("failed to create tracer") |
| 37 | + } |
| 38 | + return &tracer.LinuxTracer{}, nil |
| 39 | + }, |
| 40 | + expectedTracer: &tracer.LinuxTracer{}, |
| 41 | + expectedError: "", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Returns an error when max retries are reached.", |
| 45 | + mockMakeTracerFunc: func(config *tracerConfig.Config) (tracer.Tracer, error) { |
| 46 | + if testRetry <= retryAmount-1 { |
| 47 | + testRetry = testRetry + 1 |
| 48 | + return nil, errors.New("failed to create tracer") |
| 49 | + } |
| 50 | + return &tracer.LinuxTracer{}, nil |
| 51 | + }, |
| 52 | + expectedTracer: nil, |
| 53 | + expectedError: "failed to create tracer", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Return an error when the make tracer function returns an error", |
| 57 | + mockMakeTracerFunc: func(config *tracerConfig.Config) (tracer.Tracer, error) { |
| 58 | + return nil, errors.New("failed to create tracer") |
| 59 | + }, |
| 60 | + expectedError: "failed to create tracer", |
| 61 | + }, |
| 62 | + } { |
| 63 | + t.Run(tc.name, func(t *testing.T) { |
| 64 | + tr, err := retryTracerInit(retryDuration, retryAmount, tracerConfig.DefaultConfig, tc.mockMakeTracerFunc) |
| 65 | + if tc.expectedError != "" { |
| 66 | + assert.EqualError(t, err, tc.expectedError) |
| 67 | + } else { |
| 68 | + assert.NoError(t, err) |
| 69 | + } |
| 70 | + assert.EqualValues(t, tc.expectedTracer, tr) |
| 71 | + |
| 72 | + // reset the test retries |
| 73 | + testRetry = 0 |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments