Files
sablier/app/discovery/autostop_test.go
Alexis Couvreur dfb9bacf59 feat(providers): add provider.auto-stop-on-startup argument (#346)
This feature adds the capability to stop unregistered running instances upon startup.

Previously, you had to stop running instances manually or issue an initial request that will shut down instances afterwards.

With this change, all discovered instances will be shutdown. They need to be registered using labels. E.g.: sablier.enable=true

Fixes #153
2024-10-01 17:30:14 -07:00

77 lines
2.1 KiB
Go

package discovery_test
import (
"context"
"errors"
"github.com/acouvreur/sablier/app/discovery"
"github.com/acouvreur/sablier/app/instance"
"github.com/acouvreur/sablier/app/providers"
"github.com/acouvreur/sablier/app/providers/mock"
"github.com/acouvreur/sablier/app/types"
"testing"
)
func TestStopAllUnregisteredInstances(t *testing.T) {
mockProvider := new(mock.ProviderMock)
ctx := context.TODO()
// Define instances and registered instances
instances := []types.Instance{
{Name: "instance1"},
{Name: "instance2"},
{Name: "instance3"},
}
registered := []string{"instance1"}
// Set up expectations for InstanceList
mockProvider.On("InstanceList", ctx, providers.InstanceListOptions{
All: false,
Labels: []string{discovery.LabelEnable},
}).Return(instances, nil)
// Set up expectations for Stop
mockProvider.On("Stop", ctx, "instance2").Return(instance.State{}, nil)
mockProvider.On("Stop", ctx, "instance3").Return(instance.State{}, nil)
// Call the function under test
err := discovery.StopAllUnregisteredInstances(ctx, mockProvider, registered)
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
// Check expectations
mockProvider.AssertExpectations(t)
}
func TestStopAllUnregisteredInstances_WithError(t *testing.T) {
mockProvider := new(mock.ProviderMock)
ctx := context.TODO()
// Define instances and registered instances
instances := []types.Instance{
{Name: "instance1"},
{Name: "instance2"},
{Name: "instance3"},
}
registered := []string{"instance1"}
// Set up expectations for InstanceList
mockProvider.On("InstanceList", ctx, providers.InstanceListOptions{
All: false,
Labels: []string{discovery.LabelEnable},
}).Return(instances, nil)
// Set up expectations for Stop with error
mockProvider.On("Stop", ctx, "instance2").Return(instance.State{}, errors.New("stop error"))
mockProvider.On("Stop", ctx, "instance3").Return(instance.State{}, nil)
// Call the function under test
err := discovery.StopAllUnregisteredInstances(ctx, mockProvider, registered)
if err == nil {
t.Fatalf("Expected error, but got nil")
}
// Check expectations
mockProvider.AssertExpectations(t)
}