Skip to content

Commit

Permalink
Bug fixes and creating innitial testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gvicentin committed Apr 5, 2024
1 parent 68ff646 commit a13d113
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/pkg/rpaas/fake/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ func (m *RpaasManager) Scale(ctx context.Context, instanceName string, replicas
}

func (m *RpaasManager) Start(ctx context.Context, instanceName string) error {
if m.FakeScale != nil {
if m.FakeStart != nil {
return m.FakeStart(instanceName)
}
return nil
}

func (m *RpaasManager) Stop(ctx context.Context, instanceName string) error {
if m.FakeScale != nil {
if m.FakeStop != nil {
return m.FakeStop(instanceName)
}
return nil
Expand Down
47 changes: 46 additions & 1 deletion internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3645,7 +3645,13 @@ func Test_k8sRpaasManager_Scale(t *testing.T) {
instance2.Name = "another-instance"
instance2.Spec.Autoscale = nil

resources := []runtime.Object{instance1, instance2}
instance3 := newEmptyRpaasInstance()
instance3.Name = "instance-scale-from-zero"
instance3.Spec.Shutdown = true
instance3.Spec.Autoscale = nil
instance3.Spec.Replicas = pointerToInt32(0)

resources := []runtime.Object{instance1, instance2, instance3}

testCases := []struct {
instance string
Expand Down Expand Up @@ -3684,6 +3690,19 @@ func Test_k8sRpaasManager_Scale(t *testing.T) {
assert.Equal(t, int32(30), *instance.Spec.Replicas)
},
},
{
instance: "instance-scale-from-zero",
assertion: func(t *testing.T, err error, m *k8sRpaasManager) {
assert.NoError(t, err)

instance := v1alpha1.RpaasInstance{}
err = m.cli.Get(context.Background(), types.NamespacedName{Name: "instance-scale-from-zero", Namespace: getServiceName()}, &instance)
require.NoError(t, err)

assert.Equal(t, int32(30), *instance.Spec.Replicas)
assert.False(t, instance.Spec.Shutdown)
},
},
}

for _, tt := range testCases {
Expand All @@ -3695,6 +3714,32 @@ func Test_k8sRpaasManager_Scale(t *testing.T) {
}
}

func Test_k8sRpaasManager_Start(t *testing.T) {
scheme := newScheme()
instance := newEmptyRpaasInstance()
instance.Name = "ronaldo-instance"
instance.Spec.Shutdown = true

manager := &k8sRpaasManager{cli: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(instance).Build()}
err := manager.Start(context.Background(), "ronaldo-instance")

require.NoError(t, err)
// assert.False(t, instance.Spec.Shutdown)
}

func Test_k8sRpaasManager_Stop(t *testing.T) {
scheme := newScheme()
instance := newEmptyRpaasInstance()
instance.Name = "my-instance"
instance.Spec.Shutdown = false

manager := &k8sRpaasManager{cli: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(instance).Build()}
err := manager.Stop(context.Background(), "my-instance")

require.NoError(t, err)
// assert.True(t, instance.Spec.Shutdown)
}

func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) {
cfg := config.Get()
defer func() { config.Set(cfg) }()
Expand Down
2 changes: 1 addition & 1 deletion pkg/web/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func stop(c echo.Context) error {
if err != nil {
return err
}
if err = manager.Start(ctx, c.Param("instance")); err != nil {
if err = manager.Stop(ctx, c.Param("instance")); err != nil {
return err
}
return c.NoContent(http.StatusOK)
Expand Down

0 comments on commit a13d113

Please sign in to comment.