mirror of
https://github.com/luxfi/vm.git
synced 2026-10-30 15:13:40 +00:00
Six packages would not build, each from a migration that stopped one step short, and a package that does not compile reports nothing at all. The generated mocks had fallen behind their interfaces: Manager kept four of its eight methods, Server was missing SetRootInfoProvider, and there was no Factory mock under the name the tests used. All three are regenerated, and each package now carries the go:generate line that produces them, so a method added to an interface is a regeneration away rather than a puzzle in another package's tests. The interfaces had also taken a context that the expectations had not, so forty-six of them name it now. Aliases and the level test followed their types: the standalone atomic package rather than the compatibility re-export, and log's own Level constants beside the field that holds one. One table field was renamed in every place but the line that read it. Two services lost a behaviour along the way. Registration aliases every VM to its own id, and both GetVMs and LoadVMs handed that back as an alias of itself, which the tests had said all along they should not; and both read their context from a request the tests passed as nil, which segfaulted before a single expectation could run. chains/atomic still fails, deliberately: its fixture asserts a big-endian wire layout "so any on-disk record from the legacy build still parses", and luxfi/util's packer moved to little-endian on purpose. Which side gives is a question about records already on disk, not about this test. Co-authored-by: Hanzo Dev <[email protected]>
149 lines
3.6 KiB
Go
149 lines
3.6 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package registry
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/luxfi/mock/gomock"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/luxfi/ids"
|
|
"github.com/luxfi/vm/manager"
|
|
"github.com/luxfi/vm/manager/managermock"
|
|
"github.com/luxfi/vm/registry/registrymock"
|
|
)
|
|
|
|
var (
|
|
id1 = ids.GenerateTestID()
|
|
id2 = ids.GenerateTestID()
|
|
id3 = ids.GenerateTestID()
|
|
id4 = ids.GenerateTestID()
|
|
)
|
|
|
|
// Tests the happy case where Reload succeeds.
|
|
func TestReload_Success(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
resources := initVMRegistryTest(t)
|
|
|
|
factory1 := managermock.NewFactory(resources.ctrl)
|
|
factory2 := managermock.NewFactory(resources.ctrl)
|
|
factory3 := managermock.NewFactory(resources.ctrl)
|
|
factory4 := managermock.NewFactory(resources.ctrl)
|
|
|
|
registeredVms := map[ids.ID]manager.Factory{
|
|
id1: factory1,
|
|
id2: factory2,
|
|
}
|
|
|
|
unregisteredVms := map[ids.ID]manager.Factory{
|
|
id3: factory3,
|
|
id4: factory4,
|
|
}
|
|
|
|
resources.mockVMGetter.EXPECT().
|
|
Get().
|
|
Times(1).
|
|
Return(registeredVms, unregisteredVms, nil)
|
|
resources.mockVMManager.EXPECT().
|
|
RegisterFactory(gomock.Any(), id3, factory3).
|
|
Times(1).
|
|
Return(nil)
|
|
resources.mockVMManager.EXPECT().
|
|
RegisterFactory(gomock.Any(), id4, factory4).
|
|
Times(1).
|
|
Return(nil)
|
|
|
|
installedVMs, failedVMs, err := resources.vmRegistry.Reload(context.Background())
|
|
require.NoError(err)
|
|
require.ElementsMatch([]ids.ID{id3, id4}, installedVMs)
|
|
require.Empty(failedVMs)
|
|
}
|
|
|
|
// Tests that we fail if we're not able to get the vms on disk
|
|
func TestReload_GetNewVMsFails(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
resources := initVMRegistryTest(t)
|
|
|
|
resources.mockVMGetter.EXPECT().Get().Times(1).Return(nil, nil, errTest)
|
|
|
|
installedVMs, failedVMs, err := resources.vmRegistry.Reload(context.Background())
|
|
require.ErrorIs(err, errTest)
|
|
require.Empty(installedVMs)
|
|
require.Empty(failedVMs)
|
|
}
|
|
|
|
// Tests that if we fail to register a VM, we fail.
|
|
func TestReload_PartialRegisterFailure(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
resources := initVMRegistryTest(t)
|
|
|
|
factory1 := managermock.NewFactory(resources.ctrl)
|
|
factory2 := managermock.NewFactory(resources.ctrl)
|
|
factory3 := managermock.NewFactory(resources.ctrl)
|
|
factory4 := managermock.NewFactory(resources.ctrl)
|
|
|
|
registeredVms := map[ids.ID]manager.Factory{
|
|
id1: factory1,
|
|
id2: factory2,
|
|
}
|
|
|
|
unregisteredVms := map[ids.ID]manager.Factory{
|
|
id3: factory3,
|
|
id4: factory4,
|
|
}
|
|
|
|
resources.mockVMGetter.EXPECT().
|
|
Get().
|
|
Times(1).
|
|
Return(registeredVms, unregisteredVms, nil)
|
|
resources.mockVMManager.EXPECT().
|
|
RegisterFactory(gomock.Any(), id3, factory3).
|
|
Times(1).
|
|
Return(errTest)
|
|
resources.mockVMManager.EXPECT().
|
|
RegisterFactory(gomock.Any(), id4, factory4).
|
|
Times(1).
|
|
Return(nil)
|
|
|
|
installedVMs, failedVMs, err := resources.vmRegistry.Reload(context.Background())
|
|
require.NoError(err)
|
|
require.Len(failedVMs, 1)
|
|
require.ErrorIs(failedVMs[id3], errTest)
|
|
require.Len(installedVMs, 1)
|
|
require.Equal(id4, installedVMs[0])
|
|
}
|
|
|
|
type registryTestResources struct {
|
|
ctrl *gomock.Controller
|
|
mockVMGetter *registrymock.VMGetter
|
|
mockVMManager *managermock.Manager
|
|
vmRegistry VMRegistry
|
|
}
|
|
|
|
func initVMRegistryTest(t *testing.T) *registryTestResources {
|
|
ctrl := gomock.NewController(t)
|
|
|
|
mockVMGetter := registrymock.NewVMGetter(ctrl)
|
|
mockVMManager := managermock.NewManager(ctrl)
|
|
|
|
vmRegistry := NewVMRegistry(
|
|
VMRegistryConfig{
|
|
VMGetter: mockVMGetter,
|
|
VMManager: mockVMManager,
|
|
},
|
|
)
|
|
|
|
return ®istryTestResources{
|
|
ctrl: ctrl,
|
|
mockVMGetter: mockVMGetter,
|
|
mockVMManager: mockVMManager,
|
|
vmRegistry: vmRegistry,
|
|
}
|
|
}
|