chore: combine some e2e tests to reduce the number of ucind clusters hence used system resources

This commit is contained in:
Pasha Sviderski
2026-03-09 19:07:00 +10:00
parent a67f936cc1
commit d5687cd713
3 changed files with 211 additions and 290 deletions
+61 -75
View File
@@ -12,10 +12,10 @@ import (
"github.com/stretchr/testify/require"
)
func TestMachineRename(t *testing.T) {
func TestMachineOperations(t *testing.T) {
t.Parallel()
name := "ucind-test.machine-rename"
name := "ucind-test.machine-ops"
ctx := context.Background()
c, _ := createTestCluster(t, name, ucind.CreateClusterOptions{Machines: 3}, true)
@@ -23,59 +23,61 @@ func TestMachineRename(t *testing.T) {
require.NoError(t, err)
defer cli.Close()
// RenameMachine subtests.
t.Run("rename machine by name", func(t *testing.T) {
// Get initial machine state
// Get initial machine state.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
require.Len(t, machines, 3)
// Select the second machine to rename
// Select the second machine to rename.
originalMachine := machines[1]
originalName := originalMachine.Machine.Name
newName := "renamed-machine-1"
// Rename the machine
// Rename the machine.
updatedMachine, err := cli.RenameMachine(ctx, originalName, newName)
require.NoError(t, err)
assert.Equal(t, newName, updatedMachine.Name)
assert.Equal(t, originalMachine.Machine.Id, updatedMachine.Id)
// Verify the machine list reflects the change
// Verify the machine list reflects the change.
machines, err = cli.ListMachines(ctx, nil)
require.NoError(t, err)
require.Len(t, machines, 3)
// Find the renamed machine
// Find the renamed machine.
var found bool
for _, m := range machines {
if m.Machine.Id == originalMachine.Machine.Id {
assert.Equal(t, newName, m.Machine.Name)
found = true
} else {
// Ensure other machines are unaffected
// Ensure other machines are unaffected.
assert.NotEqual(t, newName, m.Machine.Name)
}
}
assert.True(t, found, "Renamed machine should be in the list")
// Verify we can inspect the machine by its new name
// Verify we can inspect the machine by its new name.
inspectedMachine, err := cli.InspectMachine(ctx, newName)
require.NoError(t, err)
assert.Equal(t, newName, inspectedMachine.Machine.Name)
assert.Equal(t, originalMachine.Machine.Id, inspectedMachine.Machine.Id)
// Verify the old name no longer works
// Verify the old name no longer works.
_, err = cli.InspectMachine(ctx, originalName)
assert.ErrorIs(t, err, api.ErrNotFound)
})
t.Run("rename machine by ID", func(t *testing.T) {
// Get the third machine
// Get the third machine.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
require.Len(t, machines, 3)
// Find a machine that hasn't been renamed yet
// Find a machine that hasn't been renamed yet.
var targetMachine *pb.MachineMember
for _, m := range machines {
if m.Machine.Name != "renamed-machine-1" {
@@ -89,67 +91,67 @@ func TestMachineRename(t *testing.T) {
machineID := targetMachine.Machine.Id
newName := "renamed-machine-2"
// Rename using ID instead of name
// Rename using ID instead of name.
updatedMachine, err := cli.RenameMachine(ctx, machineID, newName)
require.NoError(t, err)
assert.Equal(t, newName, updatedMachine.Name)
assert.Equal(t, machineID, updatedMachine.Id)
// Verify the rename was successful
// Verify the rename was successful.
inspectedMachine, err := cli.InspectMachine(ctx, newName)
require.NoError(t, err)
assert.Equal(t, newName, inspectedMachine.Machine.Name)
assert.Equal(t, machineID, inspectedMachine.Machine.Id)
// Verify the old name no longer works
// Verify the old name no longer works.
_, err = cli.InspectMachine(ctx, originalName)
assert.ErrorIs(t, err, api.ErrNotFound)
})
t.Run("rename non-existent machine", func(t *testing.T) {
// Try to rename a machine that doesn't exist
// Try to rename a machine that doesn't exist.
_, err := cli.RenameMachine(ctx, "non-existent-machine", "new-name")
assert.ErrorIs(t, err, api.ErrNotFound)
// Try with a non-existent ID
// Try with a non-existent ID.
_, err = cli.RenameMachine(ctx, "non-existent-id-12345", "new-name")
assert.ErrorIs(t, err, api.ErrNotFound)
})
t.Run("rename to existing name", func(t *testing.T) {
// Get current machines
// Get current machines.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
require.Len(t, machines, 3)
// Try to rename machine 0 to the name of machine 1
// Try to rename machine 0 to the name of machine 1.
machine0Name := machines[0].Machine.Name
machine1Name := machines[1].Machine.Name
// This should fail because the name is already taken
// This should fail because the name is already taken.
_, err = cli.RenameMachine(ctx, machine0Name, machine1Name)
assert.Error(t, err)
})
t.Run("rename with empty name", func(t *testing.T) {
// Get a machine to rename
// Get a machine to rename.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
require.Len(t, machines, 3)
machineName := machines[0].Machine.Name
// Try to rename with empty string
// Try to rename with empty string.
_, err = cli.RenameMachine(ctx, machineName, "")
assert.Error(t, err)
})
t.Run("service continuity after rename", func(t *testing.T) {
// Deploy a service on a specific machine
// Deploy a service on a specific machine.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
// Find a machine that hasn't been renamed to test with
// Find a machine that hasn't been renamed to test with.
var targetMachine *pb.MachineMember
for _, m := range machines {
if m.Machine.Name != "renamed-machine-1" && m.Machine.Name != "renamed-machine-2" {
@@ -162,7 +164,7 @@ func TestMachineRename(t *testing.T) {
originalMachineName := targetMachine.Machine.Name
serviceName := "test-service-rename-continuity"
// Create a service on the specific machine
// Create a service on the specific machine.
spec := api.ServiceSpec{
Name: serviceName,
Mode: api.ServiceModeGlobal,
@@ -184,51 +186,41 @@ func TestMachineRename(t *testing.T) {
}
})
// Verify service is running on the machine
// Verify service is running on the machine.
svc, err := cli.InspectService(ctx, serviceName)
require.NoError(t, err)
assert.Len(t, svc.Containers, 1)
assert.Equal(t, targetMachine.Machine.Id, svc.Containers[0].MachineID)
// Rename the machine
// Rename the machine.
newMachineName := "renamed-for-service-test"
_, err = cli.RenameMachine(ctx, originalMachineName, newMachineName)
require.NoError(t, err)
// Verify service is still running on the renamed machine
// Verify service is still running on the renamed machine.
svc, err = cli.InspectService(ctx, serviceName)
require.NoError(t, err)
assert.Len(t, svc.Containers, 1)
assert.Equal(t, targetMachine.Machine.Id, svc.Containers[0].MachineID)
// The service spec's placement still references the old name,
// but the service should continue to run on the same machine id
// but the service should continue to run on the same machine id.
})
}
func TestUpdateMachine(t *testing.T) {
t.Parallel()
name := "ucind-test.machine-update"
ctx := context.Background()
c, _ := createTestCluster(t, name, ucind.CreateClusterOptions{Machines: 3}, true)
cli, err := c.Machines[0].Connect(ctx)
require.NoError(t, err)
defer cli.Close()
// UpdateMachine subtests.
t.Run("update machine name", func(t *testing.T) {
// Get initial machine state
// Get initial machine state.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
require.Len(t, machines, 3)
// Select a machine to update
// Select a machine to update.
targetMachine := machines[1]
originalName := targetMachine.Machine.Name
newName := "updated-machine-name"
// Update the machine name using UpdateMachine directly
// Update the machine name using UpdateMachine directly.
req := &pb.UpdateMachineRequest{
MachineId: targetMachine.Machine.Id,
Name: &newName,
@@ -238,22 +230,22 @@ func TestUpdateMachine(t *testing.T) {
assert.Equal(t, newName, updatedMachine.Name)
assert.Equal(t, targetMachine.Machine.Id, updatedMachine.Id)
// Verify the change persisted
// Verify the change persisted.
inspected, err := cli.InspectMachine(ctx, updatedMachine.Id)
require.NoError(t, err)
assert.Equal(t, newName, inspected.Machine.Name)
// Verify old name no longer works
// Verify old name no longer works.
_, err = cli.InspectMachine(ctx, originalName)
assert.ErrorIs(t, err, api.ErrNotFound)
})
t.Run("update machine public IP", func(t *testing.T) {
// Get a machine to update
// Get a machine to update.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
// Find a machine that hasn't been renamed
// Find a machine that hasn't been renamed.
var targetMachine *pb.MachineMember
for _, m := range machines {
if m.Machine.Name != "updated-machine-name" {
@@ -263,12 +255,12 @@ func TestUpdateMachine(t *testing.T) {
}
require.NotNil(t, targetMachine)
// Create a new public IP (must be a valid public IP address)
// Create a new public IP (must be a valid public IP address).
newPublicIP := &pb.IP{
Ip: []byte{8, 8, 8, 8},
}
// Update the public IP
// Update the public IP.
req := &pb.UpdateMachineRequest{
MachineId: targetMachine.Machine.Id,
PublicIp: newPublicIP,
@@ -277,31 +269,31 @@ func TestUpdateMachine(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, newPublicIP.Ip, updatedMachine.PublicIp.Ip)
// Verify the change persisted
// Verify the change persisted.
inspected, err := cli.InspectMachine(ctx, targetMachine.Machine.Id)
require.NoError(t, err)
assert.Equal(t, newPublicIP.Ip, inspected.Machine.PublicIp.Ip)
})
t.Run("remove machine public IP", func(t *testing.T) {
// Get machines
// Get machines.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
require.True(t, len(machines) > 0, "Need at least one machine")
// First, set a public IP on a machine
// First, set a public IP on a machine.
targetMachine := machines[0]
setIPReq := &pb.UpdateMachineRequest{
MachineId: targetMachine.Machine.Id,
PublicIp: &pb.IP{Ip: []byte{192, 0, 2, 1}}, // TEST-NET-1 address
PublicIp: &pb.IP{Ip: []byte{192, 0, 2, 1}}, // TEST-NET-1 address.
}
updatedMachine, err := cli.UpdateMachine(ctx, setIPReq)
require.NoError(t, err)
require.NotNil(t, updatedMachine.PublicIp)
// Now test removing the public IP
// Now test removing the public IP.
// Remove the public IP by setting it to empty
// Remove the public IP by setting it to empty.
emptyIP := &pb.IP{}
req := &pb.UpdateMachineRequest{
MachineId: updatedMachine.Id,
@@ -311,23 +303,17 @@ func TestUpdateMachine(t *testing.T) {
require.NoError(t, err)
assert.Nil(t, removedIPMachine.PublicIp)
// Verify the change persisted
// Verify the change persisted.
inspected, err := cli.InspectMachine(ctx, updatedMachine.Id)
require.NoError(t, err)
assert.Nil(t, inspected.Machine.PublicIp)
})
t.Run("update machine endpoints", func(t *testing.T) {
// Get a machine to update
// Get a machine to update.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
var targetMachine *pb.MachineMember
for _, m := range machines {
targetMachine = m
break
}
require.NotNil(t, targetMachine)
targetMachine := machines[0]
newEndpoints := []*pb.IPPort{
{
@@ -348,13 +334,13 @@ func TestUpdateMachine(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, len(newEndpoints), len(updatedMachine.Network.Endpoints))
// Verify endpoints were updated
// Verify endpoints were updated.
for i, endpoint := range updatedMachine.Network.Endpoints {
assert.Equal(t, newEndpoints[i].Ip.Ip, endpoint.Ip.Ip)
assert.Equal(t, newEndpoints[i].Port, endpoint.Port)
}
// Verify other network fields remain unchanged
// Verify other network fields remain unchanged.
assert.Equal(t, targetMachine.Machine.Network.Subnet.Ip.Ip, updatedMachine.Network.Subnet.Ip.Ip)
assert.Equal(t, targetMachine.Machine.Network.Subnet.Bits, updatedMachine.Network.Subnet.Bits)
assert.Equal(t, targetMachine.Machine.Network.ManagementIp.Ip, updatedMachine.Network.ManagementIp.Ip)
@@ -362,7 +348,7 @@ func TestUpdateMachine(t *testing.T) {
})
t.Run("update multiple fields simultaneously", func(t *testing.T) {
// Get a machine to update
// Get a machine to update.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
@@ -375,7 +361,7 @@ func TestUpdateMachine(t *testing.T) {
}
require.NotNil(t, targetMachine)
// Update both name and public IP
// Update both name and public IP.
newName := "multi-update-machine"
newPublicIP := &pb.IP{
Ip: []byte{1, 1, 1, 1},
@@ -391,7 +377,7 @@ func TestUpdateMachine(t *testing.T) {
assert.Equal(t, newName, updatedMachine.Name)
assert.Equal(t, newPublicIP.Ip, updatedMachine.PublicIp.Ip)
// Verify both changes persisted
// Verify both changes persisted.
inspected, err := cli.InspectMachine(ctx, updatedMachine.Id)
require.NoError(t, err)
assert.Equal(t, newName, inspected.Machine.Name)
@@ -399,7 +385,7 @@ func TestUpdateMachine(t *testing.T) {
})
t.Run("update non-existent machine", func(t *testing.T) {
// Try to update properties on a machine that doesn't exist
// Try to update properties on a machine that doesn't exist.
nonExistentName := "should-be-updated"
req := &pb.UpdateMachineRequest{
MachineId: "non-existent-machine-id",
@@ -410,7 +396,7 @@ func TestUpdateMachine(t *testing.T) {
})
t.Run("update to duplicate name", func(t *testing.T) {
// Get two machines
// Get two machines.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
require.Len(t, machines, 3)
@@ -418,7 +404,7 @@ func TestUpdateMachine(t *testing.T) {
machine1 := machines[0]
machine2 := machines[1]
// Try to update machine2 with machine1's name
// Try to update machine2 with machine1's name.
req := &pb.UpdateMachineRequest{
MachineId: machine2.Machine.Id,
Name: &machine1.Machine.Name,
@@ -428,20 +414,20 @@ func TestUpdateMachine(t *testing.T) {
})
t.Run("update with empty request", func(t *testing.T) {
// Get a machine
// Get a machine.
machines, err := cli.ListMachines(ctx, nil)
require.NoError(t, err)
targetMachine := machines[0]
// Update with no fields set (should be a no-op)
// Update with no fields set (should be a no-op).
req := &pb.UpdateMachineRequest{
MachineId: targetMachine.Machine.Id,
}
updatedMachine, err := cli.UpdateMachine(ctx, req)
require.NoError(t, err)
// Machine should remain unchanged
// Machine should remain unchanged.
assert.Equal(t, targetMachine.Machine.Name, updatedMachine.Name)
if targetMachine.Machine.PublicIp != nil && updatedMachine.PublicIp != nil {
assert.Equal(t, targetMachine.Machine.PublicIp.Ip, updatedMachine.PublicIp.Ip)