Add more tests
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
parent
cc9ecf5847
commit
0c8c6f5668
5 changed files with 253 additions and 16 deletions
|
|
@ -25,15 +25,22 @@ import (
|
|||
"gorm.io/gorm/clause"
|
||||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
"github.com/cloudbase/garm/database/common"
|
||||
"github.com/cloudbase/garm/params"
|
||||
)
|
||||
|
||||
func (s *sqlDatabase) CreateInstance(_ context.Context, poolID string, param params.CreateInstanceParams) (params.Instance, error) {
|
||||
func (s *sqlDatabase) CreateInstance(_ context.Context, poolID string, param params.CreateInstanceParams) (instance params.Instance, err error) {
|
||||
pool, err := s.getPoolByID(s.conn, poolID)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "fetching pool")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err == nil {
|
||||
s.sendNotify(common.InstanceEntityType, common.CreateOperation, instance)
|
||||
}
|
||||
}()
|
||||
|
||||
var labels datatypes.JSON
|
||||
if len(param.AditionalLabels) > 0 {
|
||||
labels, err = json.Marshal(param.AditionalLabels)
|
||||
|
|
@ -134,11 +141,28 @@ func (s *sqlDatabase) GetInstanceByName(ctx context.Context, instanceName string
|
|||
return s.sqlToParamsInstance(instance)
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) DeleteInstance(_ context.Context, poolID string, instanceName string) error {
|
||||
func (s *sqlDatabase) DeleteInstance(_ context.Context, poolID string, instanceName string) (err error) {
|
||||
instance, err := s.getPoolInstanceByName(poolID, instanceName)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting instance")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err == nil {
|
||||
var providerID string
|
||||
if instance.ProviderID != nil {
|
||||
providerID = *instance.ProviderID
|
||||
}
|
||||
s.sendNotify(common.InstanceEntityType, common.DeleteOperation, params.Instance{
|
||||
ID: instance.ID.String(),
|
||||
Name: instance.Name,
|
||||
ProviderID: providerID,
|
||||
AgentID: instance.AgentID,
|
||||
PoolID: instance.PoolID.String(),
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
if q := s.conn.Unscoped().Delete(&instance); q.Error != nil {
|
||||
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
|
|
@ -230,8 +254,12 @@ func (s *sqlDatabase) UpdateInstance(ctx context.Context, instanceName string, p
|
|||
return params.Instance{}, errors.Wrap(err, "updating addresses")
|
||||
}
|
||||
}
|
||||
|
||||
return s.sqlToParamsInstance(instance)
|
||||
inst, err := s.sqlToParamsInstance(instance)
|
||||
if err != nil {
|
||||
return params.Instance{}, errors.Wrap(err, "converting instance")
|
||||
}
|
||||
s.sendNotify(common.InstanceEntityType, common.UpdateOperation, inst)
|
||||
return inst, nil
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) ListPoolInstances(_ context.Context, poolID string) ([]params.Instance, error) {
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ package sql
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
|
|
@ -74,16 +73,11 @@ func (s *sqlDatabase) DeletePoolByID(_ context.Context, poolID string) (err erro
|
|||
return errors.Wrap(err, "fetching pool by ID")
|
||||
}
|
||||
|
||||
defer func(pool Pool) {
|
||||
defer func() {
|
||||
if err == nil {
|
||||
asParams, innerErr := s.sqlToCommonPool(pool)
|
||||
if innerErr == nil {
|
||||
s.sendNotify(common.PoolEntityType, common.DeleteOperation, asParams)
|
||||
} else {
|
||||
slog.With(slog.Any("error", innerErr)).ErrorContext(s.ctx, "error sending delete notification", "pool", poolID)
|
||||
}
|
||||
s.sendNotify(common.PoolEntityType, common.DeleteOperation, params.Pool{ID: poolID})
|
||||
}
|
||||
}(pool)
|
||||
}()
|
||||
|
||||
if q := s.conn.Unscoped().Delete(&pool); q.Error != nil {
|
||||
return errors.Wrap(q.Error, "removing pool")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue