mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-27 07:31:42 +01:00
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
77 lines
2.1 KiB
Go
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)
|
|
}
|