github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/state/state_store_service_regisration_test.go (about)

     1  package state
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/go-memdb"
     8  	"github.com/hashicorp/nomad/ci"
     9  	"github.com/hashicorp/nomad/nomad/mock"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestStateStore_UpsertServiceRegistrations(t *testing.T) {
    15  	ci.Parallel(t)
    16  	testState := testStateStore(t)
    17  
    18  	// SubTest Marker: This ensures new service registrations are inserted as
    19  	// expected with their correct indexes, along with an update to the index
    20  	// table.
    21  	services := mock.ServiceRegistrations()
    22  	insertIndex := uint64(20)
    23  
    24  	// Perform the initial upsert of service registrations.
    25  	err := testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, insertIndex, services)
    26  	require.NoError(t, err)
    27  
    28  	// Check that the index for the table was modified as expected.
    29  	initialIndex, err := testState.Index(TableServiceRegistrations)
    30  	require.NoError(t, err)
    31  	require.Equal(t, insertIndex, initialIndex)
    32  
    33  	// List all the service registrations in the table, so we can perform a
    34  	// number of tests on the return array.
    35  	ws := memdb.NewWatchSet()
    36  	iter, err := testState.GetServiceRegistrations(ws)
    37  	require.NoError(t, err)
    38  
    39  	// Count how many table entries we have, to ensure it is the expected
    40  	// number.
    41  	var count int
    42  
    43  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
    44  		count++
    45  
    46  		// Ensure the create and modify indexes are populated correctly.
    47  		serviceReg := raw.(*structs.ServiceRegistration)
    48  		require.Equal(t, insertIndex, serviceReg.CreateIndex, "incorrect create index", serviceReg.ID)
    49  		require.Equal(t, insertIndex, serviceReg.ModifyIndex, "incorrect modify index", serviceReg.ID)
    50  	}
    51  	require.Equal(t, 2, count, "incorrect number of service registrations found")
    52  
    53  	// SubTest Marker: This section attempts to upsert the exact same service
    54  	// registrations without any modification. In this case, the index table
    55  	// should not be updated, indicating no write actually happened due to
    56  	// equality checking.
    57  	reInsertIndex := uint64(30)
    58  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, reInsertIndex, services))
    59  	reInsertActualIndex, err := testState.Index(TableServiceRegistrations)
    60  	require.NoError(t, err)
    61  	require.Equal(t, insertIndex, reInsertActualIndex, "index should not have changed")
    62  
    63  	// SubTest Marker: This section modifies a single one of the previously
    64  	// inserted service registrations and performs an upsert. This ensures the
    65  	// index table is modified correctly and that each service registration is
    66  	// updated, or not, as expected.
    67  	service1Update := services[0].Copy()
    68  	service1Update.Tags = []string{"modified"}
    69  	services1Update := []*structs.ServiceRegistration{service1Update}
    70  
    71  	update1Index := uint64(40)
    72  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, update1Index, services1Update))
    73  
    74  	// Check that the index for the table was modified as expected.
    75  	updateActualIndex, err := testState.Index(TableServiceRegistrations)
    76  	require.NoError(t, err)
    77  	require.Equal(t, update1Index, updateActualIndex, "index should have changed")
    78  
    79  	// Get the service registrations from the table.
    80  	iter, err = testState.GetServiceRegistrations(ws)
    81  	require.NoError(t, err)
    82  
    83  	// Iterate all the stored registrations and assert they are as expected.
    84  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
    85  		serviceReg := raw.(*structs.ServiceRegistration)
    86  
    87  		var expectedModifyIndex uint64
    88  
    89  		switch serviceReg.ID {
    90  		case service1Update.ID:
    91  			expectedModifyIndex = update1Index
    92  		case services[1].ID:
    93  			expectedModifyIndex = insertIndex
    94  		default:
    95  			t.Errorf("unknown service registration found: %s", serviceReg.ID)
    96  			continue
    97  		}
    98  		require.Equal(t, insertIndex, serviceReg.CreateIndex, "incorrect create index", serviceReg.ID)
    99  		require.Equal(t, expectedModifyIndex, serviceReg.ModifyIndex, "incorrect modify index", serviceReg.ID)
   100  	}
   101  
   102  	// SubTest Marker: Here we modify the second registration but send an
   103  	// upsert request that includes this and the already modified registration.
   104  	service2Update := services[1].Copy()
   105  	service2Update.Tags = []string{"modified"}
   106  	services2Update := []*structs.ServiceRegistration{service1Update, service2Update}
   107  
   108  	update2Index := uint64(50)
   109  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, update2Index, services2Update))
   110  
   111  	// Check that the index for the table was modified as expected.
   112  	update2ActualIndex, err := testState.Index(TableServiceRegistrations)
   113  	require.NoError(t, err)
   114  	require.Equal(t, update2Index, update2ActualIndex, "index should have changed")
   115  
   116  	// Get the service registrations from the table.
   117  	iter, err = testState.GetServiceRegistrations(ws)
   118  	require.NoError(t, err)
   119  
   120  	// Iterate all the stored registrations and assert they are as expected.
   121  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   122  		serviceReg := raw.(*structs.ServiceRegistration)
   123  
   124  		var (
   125  			expectedModifyIndex uint64
   126  			expectedServiceReg  *structs.ServiceRegistration
   127  		)
   128  
   129  		switch serviceReg.ID {
   130  		case service2Update.ID:
   131  			expectedModifyIndex = update2Index
   132  			expectedServiceReg = service2Update
   133  		case service1Update.ID:
   134  			expectedModifyIndex = update1Index
   135  			expectedServiceReg = service1Update
   136  		default:
   137  			t.Errorf("unknown service registration found: %s", serviceReg.ID)
   138  			continue
   139  		}
   140  		require.Equal(t, insertIndex, serviceReg.CreateIndex, "incorrect create index", serviceReg.ID)
   141  		require.Equal(t, expectedModifyIndex, serviceReg.ModifyIndex, "incorrect modify index", serviceReg.ID)
   142  		require.True(t, expectedServiceReg.Equal(serviceReg))
   143  	}
   144  }
   145  
   146  func TestStateStore_DeleteServiceRegistrationByID(t *testing.T) {
   147  	ci.Parallel(t)
   148  	testState := testStateStore(t)
   149  
   150  	// Generate some test services that we will use and modify throughout.
   151  	services := mock.ServiceRegistrations()
   152  
   153  	// SubTest Marker: This section attempts to delete a service registration
   154  	// by an ID that does not exist. This is easy to perform here as the state
   155  	// is empty.
   156  	initialIndex := uint64(10)
   157  	err := testState.DeleteServiceRegistrationByID(
   158  		structs.MsgTypeTestSetup, initialIndex, services[0].Namespace, services[0].ID)
   159  	require.EqualError(t, err, "service registration not found")
   160  
   161  	actualInitialIndex, err := testState.Index(TableServiceRegistrations)
   162  	require.NoError(t, err)
   163  	require.Equal(t, uint64(0), actualInitialIndex, "index should not have changed")
   164  
   165  	// SubTest Marker: This section upserts two registrations, deletes one,
   166  	// then ensure the remaining is left as expected.
   167  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, initialIndex, services))
   168  
   169  	// Perform the delete.
   170  	delete1Index := uint64(20)
   171  	require.NoError(t, testState.DeleteServiceRegistrationByID(
   172  		structs.MsgTypeTestSetup, delete1Index, services[0].Namespace, services[0].ID))
   173  
   174  	// Check that the index for the table was modified as expected.
   175  	actualDelete1Index, err := testState.Index(TableServiceRegistrations)
   176  	require.NoError(t, err)
   177  	require.Equal(t, delete1Index, actualDelete1Index, "index should have changed")
   178  
   179  	ws := memdb.NewWatchSet()
   180  
   181  	// Get the service registrations from the table.
   182  	iter, err := testState.GetServiceRegistrations(ws)
   183  	require.NoError(t, err)
   184  
   185  	var delete1Count int
   186  
   187  	// Iterate all the stored registrations and assert we have the expected
   188  	// number.
   189  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   190  		delete1Count++
   191  	}
   192  	require.Equal(t, 1, delete1Count, "unexpected number of registrations in table")
   193  
   194  	// SubTest Marker: Delete the remaining registration and ensure all indexes
   195  	// are updated as expected and the table is empty.
   196  	delete2Index := uint64(30)
   197  	require.NoError(t, testState.DeleteServiceRegistrationByID(
   198  		structs.MsgTypeTestSetup, delete2Index, services[1].Namespace, services[1].ID))
   199  
   200  	// Check that the index for the table was modified as expected.
   201  	actualDelete2Index, err := testState.Index(TableServiceRegistrations)
   202  	require.NoError(t, err)
   203  	require.Equal(t, delete2Index, actualDelete2Index, "index should have changed")
   204  
   205  	// Get the service registrations from the table.
   206  	iter, err = testState.GetServiceRegistrations(ws)
   207  	require.NoError(t, err)
   208  
   209  	var delete2Count int
   210  
   211  	// Iterate all the stored registrations and assert we have the expected
   212  	// number.
   213  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   214  		delete2Count++
   215  	}
   216  	require.Equal(t, 0, delete2Count, "unexpected number of registrations in table")
   217  }
   218  
   219  func TestStateStore_DeleteServiceRegistrationByNodeID(t *testing.T) {
   220  	ci.Parallel(t)
   221  	testState := testStateStore(t)
   222  
   223  	// Generate some test services that we will use and modify throughout.
   224  	services := mock.ServiceRegistrations()
   225  
   226  	// SubTest Marker: This section attempts to delete a service registration
   227  	// by a nodeID that does not exist. This is easy to perform here as the
   228  	// state is empty.
   229  	initialIndex := uint64(10)
   230  	require.NoError(t,
   231  		testState.DeleteServiceRegistrationByNodeID(structs.MsgTypeTestSetup, initialIndex, services[0].NodeID))
   232  
   233  	actualInitialIndex, err := testState.Index(TableServiceRegistrations)
   234  	require.NoError(t, err)
   235  	require.Equal(t, uint64(0), actualInitialIndex, "index should not have changed")
   236  
   237  	// SubTest Marker: This section upserts two registrations then deletes one
   238  	// by using the nodeID.
   239  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, initialIndex, services))
   240  
   241  	// Perform the delete.
   242  	delete1Index := uint64(20)
   243  	require.NoError(t, testState.DeleteServiceRegistrationByNodeID(
   244  		structs.MsgTypeTestSetup, delete1Index, services[0].NodeID))
   245  
   246  	// Check that the index for the table was modified as expected.
   247  	actualDelete1Index, err := testState.Index(TableServiceRegistrations)
   248  	require.NoError(t, err)
   249  	require.Equal(t, delete1Index, actualDelete1Index, "index should have changed")
   250  
   251  	ws := memdb.NewWatchSet()
   252  
   253  	// Get the service registrations from the table.
   254  	iter, err := testState.GetServiceRegistrations(ws)
   255  	require.NoError(t, err)
   256  
   257  	var delete1Count int
   258  
   259  	// Iterate all the stored registrations and assert we have the expected
   260  	// number.
   261  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   262  		delete1Count++
   263  	}
   264  	require.Equal(t, 1, delete1Count, "unexpected number of registrations in table")
   265  
   266  	// SubTest Marker: Add multiple service registrations for a single nodeID
   267  	// then delete these via the nodeID.
   268  	delete2NodeID := services[1].NodeID
   269  	var delete2NodeServices []*structs.ServiceRegistration
   270  
   271  	for i := 0; i < 4; i++ {
   272  		iString := strconv.Itoa(i)
   273  		delete2NodeServices = append(delete2NodeServices, &structs.ServiceRegistration{
   274  			ID:          "_nomad-task-ca60e901-675a-0ab2-2e57-2f3b05fdc540-group-api-countdash-api-http-" + iString,
   275  			ServiceName: "countdash-api-" + iString,
   276  			Namespace:   "platform",
   277  			NodeID:      delete2NodeID,
   278  			Datacenter:  "dc2",
   279  			JobID:       "countdash-api-" + iString,
   280  			AllocID:     "ca60e901-675a-0ab2-2e57-2f3b05fdc54" + iString,
   281  			Tags:        []string{"bar"},
   282  			Address:     "192.168.200.200",
   283  			Port:        27500 + i,
   284  		})
   285  	}
   286  
   287  	// Upsert the new service registrations.
   288  	delete2UpsertIndex := uint64(30)
   289  	require.NoError(t,
   290  		testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, delete2UpsertIndex, delete2NodeServices))
   291  
   292  	delete2Index := uint64(40)
   293  	require.NoError(t, testState.DeleteServiceRegistrationByNodeID(
   294  		structs.MsgTypeTestSetup, delete2Index, delete2NodeID))
   295  
   296  	// Check that the index for the table was modified as expected.
   297  	actualDelete2Index, err := testState.Index(TableServiceRegistrations)
   298  	require.NoError(t, err)
   299  	require.Equal(t, delete2Index, actualDelete2Index, "index should have changed")
   300  
   301  	// Get the service registrations from the table.
   302  	iter, err = testState.GetServiceRegistrations(ws)
   303  	require.NoError(t, err)
   304  
   305  	var delete2Count int
   306  
   307  	// Iterate all the stored registrations and assert we have the expected
   308  	// number.
   309  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   310  		delete2Count++
   311  	}
   312  	require.Equal(t, 0, delete2Count, "unexpected number of registrations in table")
   313  }
   314  
   315  func TestStateStore_GetServiceRegistrations(t *testing.T) {
   316  	ci.Parallel(t)
   317  	testState := testStateStore(t)
   318  
   319  	// Generate some test services and upsert them.
   320  	services := mock.ServiceRegistrations()
   321  	initialIndex := uint64(10)
   322  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, initialIndex, services))
   323  
   324  	// Read the service registrations and check the objects.
   325  	ws := memdb.NewWatchSet()
   326  	iter, err := testState.GetServiceRegistrations(ws)
   327  	require.NoError(t, err)
   328  
   329  	var count int
   330  
   331  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   332  		count++
   333  
   334  		serviceReg := raw.(*structs.ServiceRegistration)
   335  		require.Equal(t, initialIndex, serviceReg.CreateIndex, "incorrect create index", serviceReg.ID)
   336  		require.Equal(t, initialIndex, serviceReg.ModifyIndex, "incorrect modify index", serviceReg.ID)
   337  
   338  		switch serviceReg.ID {
   339  		case services[0].ID:
   340  			require.Equal(t, services[0], serviceReg)
   341  		case services[1].ID:
   342  			require.Equal(t, services[1], serviceReg)
   343  		default:
   344  			t.Errorf("unknown service registration found: %s", serviceReg.ID)
   345  		}
   346  	}
   347  	require.Equal(t, 2, count)
   348  }
   349  
   350  func TestStateStore_GetServiceRegistrationsByNamespace(t *testing.T) {
   351  	ci.Parallel(t)
   352  	testState := testStateStore(t)
   353  
   354  	// Generate some test services and upsert them.
   355  	services := mock.ServiceRegistrations()
   356  	initialIndex := uint64(10)
   357  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, initialIndex, services))
   358  
   359  	// Look up services using the namespace of the first service.
   360  	ws := memdb.NewWatchSet()
   361  	iter, err := testState.GetServiceRegistrationsByNamespace(ws, services[0].Namespace)
   362  	require.NoError(t, err)
   363  
   364  	var count1 int
   365  
   366  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   367  		count1++
   368  		serviceReg := raw.(*structs.ServiceRegistration)
   369  		require.Equal(t, initialIndex, serviceReg.CreateIndex, "incorrect create index", serviceReg.ID)
   370  		require.Equal(t, initialIndex, serviceReg.ModifyIndex, "incorrect modify index", serviceReg.ID)
   371  		require.Equal(t, services[0].Namespace, serviceReg.Namespace)
   372  	}
   373  	require.Equal(t, 1, count1)
   374  
   375  	// Look up services using the namespace of the second service.
   376  	iter, err = testState.GetServiceRegistrationsByNamespace(ws, services[1].Namespace)
   377  	require.NoError(t, err)
   378  
   379  	var count2 int
   380  
   381  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   382  		count2++
   383  		serviceReg := raw.(*structs.ServiceRegistration)
   384  		require.Equal(t, initialIndex, serviceReg.CreateIndex, "incorrect create index", serviceReg.ID)
   385  		require.Equal(t, initialIndex, serviceReg.ModifyIndex, "incorrect modify index", serviceReg.ID)
   386  		require.Equal(t, services[1].Namespace, serviceReg.Namespace)
   387  	}
   388  	require.Equal(t, 1, count2)
   389  
   390  	// Look up services using a namespace that shouldn't contain any
   391  	// registrations.
   392  	iter, err = testState.GetServiceRegistrationsByNamespace(ws, "pony-club")
   393  	require.NoError(t, err)
   394  
   395  	var count3 int
   396  
   397  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   398  		count3++
   399  	}
   400  	require.Equal(t, 0, count3)
   401  }
   402  
   403  func TestStateStore_GetServiceRegistrationByName(t *testing.T) {
   404  	ci.Parallel(t)
   405  	testState := testStateStore(t)
   406  
   407  	// Generate some test services and upsert them.
   408  	services := mock.ServiceRegistrations()
   409  	initialIndex := uint64(10)
   410  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, initialIndex, services))
   411  
   412  	// Try reading a service by a name that shouldn't exist.
   413  	ws := memdb.NewWatchSet()
   414  	iter, err := testState.GetServiceRegistrationByName(ws, "default", "pony-glitter-api")
   415  	require.NoError(t, err)
   416  
   417  	var count1 int
   418  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   419  		count1++
   420  	}
   421  	require.Equal(t, 0, count1)
   422  
   423  	// Read one of the known service registrations.
   424  	expectedReg := services[1].Copy()
   425  
   426  	iter, err = testState.GetServiceRegistrationByName(ws, expectedReg.Namespace, expectedReg.ServiceName)
   427  	require.NoError(t, err)
   428  
   429  	var count2 int
   430  
   431  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   432  		count2++
   433  		serviceReg := raw.(*structs.ServiceRegistration)
   434  		require.Equal(t, expectedReg.ServiceName, serviceReg.ServiceName)
   435  		require.Equal(t, expectedReg.Namespace, serviceReg.Namespace)
   436  	}
   437  	require.Equal(t, 1, count2)
   438  
   439  	// Create a bunch of additional services whose name and namespace match
   440  	// that of expectedReg.
   441  	var newServices []*structs.ServiceRegistration
   442  
   443  	for i := 0; i < 4; i++ {
   444  		iString := strconv.Itoa(i)
   445  		newServices = append(newServices, &structs.ServiceRegistration{
   446  			ID:          "_nomad-task-ca60e901-675a-0ab2-2e57-2f3b05fdc540-group-api-countdash-api-http-" + iString,
   447  			ServiceName: expectedReg.ServiceName,
   448  			Namespace:   expectedReg.Namespace,
   449  			NodeID:      "2873cf75-42e5-7c45-ca1c-415f3e18be3d",
   450  			Datacenter:  "dc1",
   451  			JobID:       expectedReg.JobID,
   452  			AllocID:     "ca60e901-675a-0ab2-2e57-2f3b05fdc54" + iString,
   453  			Tags:        []string{"bar"},
   454  			Address:     "192.168.200.200",
   455  			Port:        27500 + i,
   456  		})
   457  	}
   458  
   459  	updateIndex := uint64(20)
   460  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, updateIndex, newServices))
   461  
   462  	iter, err = testState.GetServiceRegistrationByName(ws, expectedReg.Namespace, expectedReg.ServiceName)
   463  	require.NoError(t, err)
   464  
   465  	var count3 int
   466  
   467  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   468  		count3++
   469  		serviceReg := raw.(*structs.ServiceRegistration)
   470  		require.Equal(t, expectedReg.ServiceName, serviceReg.ServiceName)
   471  		require.Equal(t, expectedReg.Namespace, serviceReg.Namespace)
   472  	}
   473  	require.Equal(t, 5, count3)
   474  }
   475  
   476  func TestStateStore_GetServiceRegistrationByID(t *testing.T) {
   477  	ci.Parallel(t)
   478  	testState := testStateStore(t)
   479  
   480  	// Generate some test services and upsert them.
   481  	services := mock.ServiceRegistrations()
   482  	initialIndex := uint64(10)
   483  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, initialIndex, services))
   484  
   485  	ws := memdb.NewWatchSet()
   486  
   487  	// Try reading a service by an ID that shouldn't exist.
   488  	serviceReg, err := testState.GetServiceRegistrationByID(ws, "default", "pony-glitter-sparkles")
   489  	require.NoError(t, err)
   490  	require.Nil(t, serviceReg)
   491  
   492  	// Read the two services that we should find.
   493  	serviceReg, err = testState.GetServiceRegistrationByID(ws, services[0].Namespace, services[0].ID)
   494  	require.NoError(t, err)
   495  	require.Equal(t, services[0], serviceReg)
   496  
   497  	serviceReg, err = testState.GetServiceRegistrationByID(ws, services[1].Namespace, services[1].ID)
   498  	require.NoError(t, err)
   499  	require.Equal(t, services[1], serviceReg)
   500  }
   501  
   502  func TestStateStore_GetServiceRegistrationsByAllocID(t *testing.T) {
   503  	ci.Parallel(t)
   504  	testState := testStateStore(t)
   505  
   506  	// Generate some test services and upsert them.
   507  	services := mock.ServiceRegistrations()
   508  	initialIndex := uint64(10)
   509  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, initialIndex, services))
   510  
   511  	ws := memdb.NewWatchSet()
   512  
   513  	// Try reading services by an allocation that doesn't have any
   514  	// registrations.
   515  	iter, err := testState.GetServiceRegistrationsByAllocID(ws, "4eed3c6d-6bf1-60d6-040a-e347accae6c4")
   516  	require.NoError(t, err)
   517  
   518  	var count1 int
   519  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   520  		count1++
   521  	}
   522  	require.Equal(t, 0, count1)
   523  
   524  	// Read the two allocations that we should find.
   525  	iter, err = testState.GetServiceRegistrationsByAllocID(ws, services[0].AllocID)
   526  	require.NoError(t, err)
   527  
   528  	var count2 int
   529  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   530  		count2++
   531  		serviceReg := raw.(*structs.ServiceRegistration)
   532  		require.Equal(t, services[0].AllocID, serviceReg.AllocID)
   533  	}
   534  	require.Equal(t, 1, count2)
   535  
   536  	iter, err = testState.GetServiceRegistrationsByAllocID(ws, services[1].AllocID)
   537  	require.NoError(t, err)
   538  
   539  	var count3 int
   540  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   541  		count3++
   542  		serviceReg := raw.(*structs.ServiceRegistration)
   543  		require.Equal(t, services[1].AllocID, serviceReg.AllocID)
   544  	}
   545  	require.Equal(t, 1, count3)
   546  }
   547  
   548  func TestStateStore_GetServiceRegistrationsByJobID(t *testing.T) {
   549  	ci.Parallel(t)
   550  	testState := testStateStore(t)
   551  
   552  	// Generate some test services and upsert them.
   553  	services := mock.ServiceRegistrations()
   554  	initialIndex := uint64(10)
   555  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, initialIndex, services))
   556  
   557  	ws := memdb.NewWatchSet()
   558  
   559  	// Perform a query against a job that shouldn't have any registrations.
   560  	iter, err := testState.GetServiceRegistrationsByJobID(ws, "default", "tamagotchi")
   561  	require.NoError(t, err)
   562  
   563  	var count1 int
   564  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   565  		count1++
   566  	}
   567  	require.Equal(t, 0, count1)
   568  
   569  	// Look up services using the namespace and jobID of the first service.
   570  	iter, err = testState.GetServiceRegistrationsByJobID(ws, services[0].Namespace, services[0].JobID)
   571  	require.NoError(t, err)
   572  
   573  	var outputList1 []*structs.ServiceRegistration
   574  
   575  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   576  		serviceReg := raw.(*structs.ServiceRegistration)
   577  		require.Equal(t, initialIndex, serviceReg.CreateIndex, "incorrect create index", serviceReg.ID)
   578  		require.Equal(t, initialIndex, serviceReg.ModifyIndex, "incorrect modify index", serviceReg.ID)
   579  		outputList1 = append(outputList1, serviceReg)
   580  	}
   581  	require.ElementsMatch(t, outputList1, []*structs.ServiceRegistration{services[0]})
   582  
   583  	// Look up services using the namespace and jobID of the second service.
   584  	iter, err = testState.GetServiceRegistrationsByJobID(ws, services[1].Namespace, services[1].JobID)
   585  	require.NoError(t, err)
   586  
   587  	var outputList2 []*structs.ServiceRegistration
   588  
   589  	for raw := iter.Next(); raw != nil; raw = iter.Next() {
   590  		serviceReg := raw.(*structs.ServiceRegistration)
   591  		require.Equal(t, initialIndex, serviceReg.CreateIndex, "incorrect create index", serviceReg.ID)
   592  		require.Equal(t, initialIndex, serviceReg.ModifyIndex, "incorrect modify index", serviceReg.ID)
   593  		outputList2 = append(outputList2, serviceReg)
   594  	}
   595  	require.ElementsMatch(t, outputList2, []*structs.ServiceRegistration{services[1]})
   596  }
   597  
   598  func TestStateStore_GetServiceRegistrationsByNodeID(t *testing.T) {
   599  	ci.Parallel(t)
   600  	testState := testStateStore(t)
   601  
   602  	// Generate some test services and upsert them.
   603  	services := mock.ServiceRegistrations()
   604  	initialIndex := uint64(10)
   605  	require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, initialIndex, services))
   606  
   607  	ws := memdb.NewWatchSet()
   608  
   609  	// Perform a query against a node that shouldn't have any registrations.
   610  	serviceRegs, err := testState.GetServiceRegistrationsByNodeID(ws, "4eed3c6d-6bf1-60d6-040a-e347accae6c4")
   611  	require.NoError(t, err)
   612  	require.Len(t, serviceRegs, 0)
   613  
   614  	// Read the two nodes that we should find entries for.
   615  	serviceRegs, err = testState.GetServiceRegistrationsByNodeID(ws, services[0].NodeID)
   616  	require.NoError(t, err)
   617  	require.Len(t, serviceRegs, 1)
   618  
   619  	serviceRegs, err = testState.GetServiceRegistrationsByNodeID(ws, services[1].NodeID)
   620  	require.NoError(t, err)
   621  	require.Len(t, serviceRegs, 1)
   622  }