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
This commit is contained in:
Alexis Couvreur
2024-07-04 12:06:21 -04:00
parent fbb6e38d53
commit dfb9bacf59
68 changed files with 1116 additions and 106 deletions

View File

@@ -0,0 +1,76 @@
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)
}