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

     1  package scripted
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
     9  	"github.com/grafana/synthetic-monitoring-agent/internal/model"
    10  	"github.com/grafana/synthetic-monitoring-agent/internal/testhelper"
    11  	sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
    12  	"github.com/rs/zerolog"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestNewProber(t *testing.T) {
    17  	ctx, cancel := testContext(t)
    18  	t.Cleanup(cancel)
    19  
    20  	logger := zerolog.New(zerolog.NewTestWriter(t))
    21  
    22  	testcases := map[string]struct {
    23  		check         model.Check
    24  		expectFailure bool
    25  	}{
    26  		"valid": {
    27  			expectFailure: false,
    28  			check: model.Check{
    29  				Check: sm.Check{
    30  					Target:    "http://www.example.org",
    31  					Job:       "test",
    32  					Frequency: 10 * 1000,
    33  					Timeout:   10 * 1000,
    34  					Probes:    []int64{1},
    35  					Settings: sm.CheckSettings{
    36  						Scripted: &sm.ScriptedSettings{
    37  							Script: []byte("// test"),
    38  						},
    39  					},
    40  				},
    41  			},
    42  		},
    43  		"invalid": {
    44  			expectFailure: true,
    45  			check: model.Check{
    46  				Check: sm.Check{
    47  					Target:    "http://www.example.org",
    48  					Job:       "test",
    49  					Frequency: 10 * 1000,
    50  					Timeout:   10 * 1000,
    51  					Probes:    []int64{1},
    52  					Settings:  sm.CheckSettings{},
    53  				},
    54  			},
    55  		},
    56  	}
    57  
    58  	for name, tc := range testcases {
    59  		t.Run(name, func(t *testing.T) {
    60  			var (
    61  				runner noopRunner
    62  				store  testhelper.NoopSecretStore
    63  			)
    64  
    65  			p, err := NewProber(ctx, tc.check, logger, runner, store)
    66  			if tc.expectFailure {
    67  				require.Error(t, err)
    68  				return
    69  			}
    70  
    71  			require.NoError(t, err)
    72  			require.Equal(t, proberName, p.module.Prober)
    73  			require.Equal(t, 10*time.Second, time.Duration(p.module.Script.Settings.Timeout)*time.Millisecond)
    74  			require.Equal(t, tc.check.Settings.Scripted.Script, p.module.Script.Script)
    75  		})
    76  	}
    77  }
    78  
    79  type noopRunner struct{}
    80  
    81  func (noopRunner) WithLogger(logger *zerolog.Logger) k6runner.Runner {
    82  	var r noopRunner
    83  	return r
    84  }
    85  
    86  func (noopRunner) Run(ctx context.Context, script k6runner.Script, secretStore k6runner.SecretStore, _ string) (*k6runner.RunResponse, error) {
    87  	return &k6runner.RunResponse{}, nil
    88  }
    89  
    90  func (noopRunner) Versions(_ context.Context) <-chan []string {
    91  	return nil // Blocks forever if read.
    92  }
    93  
    94  func testContext(t *testing.T) (context.Context, func()) {
    95  	if deadline, ok := t.Deadline(); ok {
    96  		return context.WithDeadline(context.Background(), deadline)
    97  	}
    98  
    99  	return context.WithTimeout(context.Background(), 10*time.Second)
   100  }