This commit is contained in:
2025-11-18 15:27:53 +03:00
parent e3a65c0f33
commit 06992b8949
10 changed files with 291 additions and 10 deletions

42
samples/client/client.go Normal file
View File

@@ -0,0 +1,42 @@
package client
import (
"context"
"fmt"
"errors"
decortsdk "repository.basistech.ru/BASIS/dynamix-golang-sdk/v12"
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v12/pkg/cloudbroker/compute"
)
type Migrator interface {
Migrate(ctxOrigin context.Context, vmUUID, to uint64) (bool, error)
}
type migrator struct {
cfg *Config
client decortsdk.ClientInterface
}
func NewMigrator(cfg *Config, c decortsdk.ClientInterface) Migrator {
return &migrator{
cfg: cfg,
client: c,
}
}
func (m *migrator) Migrate(ctxOrigin context.Context, dxVMID, stackID uint64) (bool, error) {
req := compute.MigrateRequest{
ComputeID: dxVMID,
TargetStackID: stackID,
Force: false,
}
ctx, cancel := context.WithTimeout(ctxOrigin, m.cfg.QueryTimeout)
ok, err := m.client.CloudBroker().Compute().Migrate(ctx, req)
cancel()
if err != nil {
return false, errors.Join(err, fmt.Errorf("Migrate VM %d to Node %d", dxVMID, stackID))
}
return ok, nil
}

View File

@@ -0,0 +1,76 @@
package client_test
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
decortsdk "repository.basistech.ru/BASIS/dynamix-golang-sdk/v12"
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v12/pkg/cloudbroker/compute"
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v12/samples/client"
)
// Пример юнит тестирования на моках
func TestClient(t *testing.T) {
ctrl := gomock.NewController(t)
// Создаем мок инстанс интерфейса Caller
mockCaller := decortsdk.NewMockCaller(ctrl)
// Создаем мок инстанс интерфейса DecortClient
mockClient := decortsdk.NewMockDecortClient(mockCaller)
dxVMID := uint64(100500)
VMID := "vm-100500"
listComputes := &compute.ListComputes{
Data: []compute.ItemCompute{
{
InfoCompute: compute.InfoCompute{
ID: dxVMID,
ReferenceID: VMID,
},
},
}}
b, err := json.Marshal(listComputes)
assert.NoError(t, err)
// Подготавливаем мок для вызова метода CloudBroker().Compute().List()
mockCaller.EXPECT().DecortApiCall(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Eq(compute.ListRequest{})).Return(b, nil).AnyTimes()
listComputesRet, err := mockClient.CloudBroker().Compute().List(context.Background(), compute.ListRequest{})
assert.NoError(t, err)
assert.Equal(t, listComputes, listComputesRet)
}
// Пример юнит тестирования на моках
func TestMigrator(t *testing.T) {
ctrl := gomock.NewController(t)
// Создаем мок инстанс интерфейса Caller
mockCaller := decortsdk.NewMockCaller(ctrl)
// Создаем мок инстанс интерфейса interfaces.ClientInterface
mockClient := decortsdk.NewMockDecortClient(mockCaller)
// Передаем мок инстанс интерфейса interfaces.ClientInterface в конструктор Migrator
migrator := client.NewMigrator(&client.Config{QueryTimeout: time.Second}, mockClient)
b, err := json.Marshal(true)
assert.NoError(t, err)
dxVMID := uint64(100500)
stackID := uint64(100501)
// Записываем поведение клиента
mockCaller.EXPECT().DecortApiCall(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Eq(compute.AsyncWrapperMigrateRequest{
MigrateRequest: compute.MigrateRequest{
ComputeID: dxVMID,
TargetStackID: stackID,
},
SyncMode: true})).Return(b, nil).AnyTimes()
ok, err := migrator.Migrate(context.Background(), dxVMID, stackID)
assert.NoError(t, err)
assert.True(t, ok)
}

7
samples/client/config.go Normal file
View File

@@ -0,0 +1,7 @@
package client
import "time"
type Config struct {
QueryTimeout time.Duration
}