github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/model/model_test.go (about)

     1  package model
     2  
     3  import (
     4  	"testing"
     5  
     6  	sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestGetLocalAndRegionIDs(t *testing.T) {
    11  	type expected struct {
    12  		localID  int64
    13  		regionID int
    14  	}
    15  
    16  	testcases := map[string]struct {
    17  		input    GlobalID
    18  		expected expected
    19  	}{
    20  		"local id": {
    21  			input:    1,
    22  			expected: expected{localID: 1, regionID: 0},
    23  		},
    24  		"min local id, min region id": {
    25  			input:    localToGlobal(t, sm.MinLocalID, sm.MinRegionID),
    26  			expected: expected{localID: sm.MinLocalID, regionID: sm.MinRegionID},
    27  		},
    28  		"min local id, max region id": {
    29  			input:    localToGlobal(t, sm.MinLocalID, sm.MaxRegionID),
    30  			expected: expected{localID: sm.MinLocalID, regionID: sm.MaxRegionID},
    31  		},
    32  		"max local id, min region id": {
    33  			input:    localToGlobal(t, sm.MaxLocalID, sm.MinRegionID),
    34  			expected: expected{localID: sm.MaxLocalID, regionID: sm.MinRegionID},
    35  		},
    36  		"max local id, max region id": {
    37  			input:    localToGlobal(t, sm.MaxLocalID, sm.MaxRegionID),
    38  			expected: expected{localID: sm.MaxLocalID, regionID: sm.MaxRegionID},
    39  		},
    40  	}
    41  
    42  	for name, tc := range testcases {
    43  		t.Run(name, func(t *testing.T) {
    44  			localID, regionID := GetLocalAndRegionIDs(tc.input)
    45  			require.Equal(t, tc.expected.localID, localID)
    46  			require.Equal(t, tc.expected.regionID, regionID)
    47  		})
    48  	}
    49  }
    50  
    51  func TestCheckFromSM(t *testing.T) {
    52  	var (
    53  		testRid       = sm.MinRegionID
    54  		testCid       = int64(sm.MinLocalID)
    55  		testTid       = int64(sm.MinLocalID + 1)
    56  		testGlobalCid = int64(localToGlobal(t, testCid, testRid))
    57  		testGlobalTid = int64(localToGlobal(t, testTid, testRid))
    58  	)
    59  
    60  	type expected struct {
    61  		check Check
    62  	}
    63  
    64  	testcases := map[string]struct {
    65  		input    sm.Check
    66  		expected expected
    67  	}{
    68  		"empty check": {
    69  			input:    sm.Check{},
    70  			expected: expected{check: Check{}},
    71  		},
    72  		"local ids": {
    73  			input: sm.Check{
    74  				Id:       testCid,
    75  				TenantId: testTid,
    76  				Settings: sm.CheckSettings{
    77  					Ping: &sm.PingSettings{},
    78  				},
    79  			},
    80  			expected: expected{
    81  				check: Check{
    82  					Check: sm.Check{
    83  						Id:       testCid,
    84  						TenantId: testTid,
    85  						Settings: sm.CheckSettings{
    86  							Ping: &sm.PingSettings{},
    87  						},
    88  					},
    89  					RegionId: 0,
    90  				},
    91  			},
    92  		},
    93  		"global ids": {
    94  			input: sm.Check{
    95  				Id:       testGlobalCid,
    96  				TenantId: testGlobalTid,
    97  				Settings: sm.CheckSettings{
    98  					Ping: &sm.PingSettings{},
    99  				},
   100  			},
   101  			expected: expected{
   102  				check: Check{
   103  					Check: sm.Check{
   104  						Id:       testCid,
   105  						TenantId: testTid,
   106  						Settings: sm.CheckSettings{
   107  							Ping: &sm.PingSettings{},
   108  						},
   109  					},
   110  					RegionId: testRid,
   111  				},
   112  			},
   113  		},
   114  	}
   115  
   116  	for name, tc := range testcases {
   117  		t.Run(name, func(t *testing.T) {
   118  			var c Check
   119  			require.NoError(t, c.FromSM(tc.input))
   120  
   121  			require.Equal(t, tc.expected.check, c)
   122  			require.Equal(t, GlobalID(tc.input.Id), c.GlobalID())
   123  			require.Equal(t, GlobalID(tc.input.TenantId), c.GlobalTenantID())
   124  		})
   125  	}
   126  }
   127  
   128  func localToGlobal(t *testing.T, localID int64, regionID int) GlobalID {
   129  	t.Helper()
   130  
   131  	globalID, err := sm.LocalIDToGlobalID(localID, regionID)
   132  	require.NoError(t, err)
   133  
   134  	return GlobalID(globalID)
   135  }