Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
semyon-dev committed Feb 20, 2025
1 parent 67921df commit fe19dfb
Showing 1 changed file with 61 additions and 60 deletions.
121 changes: 61 additions & 60 deletions training/tests/functional/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/ecdsa"
"math/big"
"slices"
"strings"
"testing"
"time"

Expand All @@ -26,7 +27,8 @@ import (

type DaemonServiceSuite struct {
suite.Suite
blockchain *blockchain.Processor
blockchain blockchain.Processor
currentBlock *big.Int
modelStorage *training.ModelStorage
userModelStorage *training.ModelUserStorage
pendingModelStorage *training.PendingModelStorage
Expand Down Expand Up @@ -54,9 +56,18 @@ var (
func (suite *DaemonServiceSuite) SetupSuite() {
// setup test config once
suite.setupTestConfig()
err := config.Validate()
assert.Nil(suite.T(), err)

// init service metadata and organization metadata
serviceMetadata, err := blockchain.InitServiceMetaDataFromJson([]byte(testJsonServiceData))
suite.blockchain, err = blockchain.NewProcessor(serviceMetadata)
if err != nil {
zap.L().Fatal("can't connect to blockchain")
return
}
suite.currentBlock, _ = suite.blockchain.CurrentBlock()

orgMetadata, err := blockchain.InitOrganizationMetaDataFromJson([]byte(testJsonOrgGroupData))
if err != nil {
zap.L().Fatal("Error in initinalize organization metadata from json", zap.Error(err))
Expand All @@ -81,7 +92,7 @@ func (suite *DaemonServiceSuite) SetupTest() {
suite.pendingModelStorage = pendingModelStorage

suite.daemonService = training.NewTrainingService(
suite.blockchain,
&suite.blockchain,
suite.serviceMetadata,
suite.organizationMetadata,
modelStorage,
Expand All @@ -95,13 +106,13 @@ func (suite *DaemonServiceSuite) TearDownSuite() {
suite.grpcServer.Stop()
}

func getTestSignature(text string, blockNumber int, privateKey *ecdsa.PrivateKey) (signature []byte) {
func getTestSignature(text string, blockNumber uint64, privateKey *ecdsa.PrivateKey) (signature []byte) {
HashPrefix32Bytes := []byte("\x19Ethereum Signed Message:\n32")

message := bytes.Join([][]byte{
[]byte(text),
crypto.PubkeyToAddress(privateKey.PublicKey).Bytes(),
math.U256Bytes(big.NewInt(int64(blockNumber))),
math.U256Bytes(big.NewInt(0).SetUint64(blockNumber)),
}, nil)

hash := crypto.Keccak256(
Expand All @@ -117,28 +128,28 @@ func getTestSignature(text string, blockNumber int, privateKey *ecdsa.PrivateKey
return signature
}

func createTestAuthDetails() *training.AuthorizationDetails {
func createTestAuthDetails(block *big.Int, method string) *training.AuthorizationDetails {
privateKey, err := crypto.HexToECDSA("c0e4803a3a5b3c26cfc96d19a6dc4bbb4ba653ce5fa68f0b7dbf3903cda17ee6")
if err != nil {
zap.L().Fatal("error in creating private key", zap.Error(err))
}
return &training.AuthorizationDetails{
CurrentBlock: 0,
Message: "__CreateModel",
Signature: getTestSignature("__CreateModel", 0, privateKey),
CurrentBlock: block.Uint64(),
Message: method,
Signature: getTestSignature(method, block.Uint64(), privateKey),
SignerAddress: "0x3432cBa6BF635Df5fBFD1f1a794fA66D412b8774",
}
}

func creatBadTestAuthDetails() *training.AuthorizationDetails {
func creatBadTestAuthDetails(block *big.Int) *training.AuthorizationDetails {
privateKey, err := crypto.HexToECDSA("c0e4803a3a5b3c26cfc96d19a6dc4bbb4ba653ce5fa68f0b7dbf3903cda17ee6")
if err != nil {
zap.L().Fatal("error in creating private key", zap.Error(err))
}
return &training.AuthorizationDetails{
CurrentBlock: 0,
CurrentBlock: block.Uint64(),
Message: "badMessage",
Signature: getTestSignature("badMessage", 0, privateKey),
Signature: getTestSignature("badMessage", block.Uint64(), privateKey),
SignerAddress: "0x4444cBa6BF635Df5fBFD1f1a794fA66D412b8774",
}
}
Expand All @@ -150,16 +161,17 @@ func (suite *DaemonServiceSuite) setupTestConfig() {
"blockchain_network_selected": "sepolia",
"daemon_end_point": "127.0.0.1:8080",
"daemon_group_name":"default_group",
"payment_channel_storage_type": "etcd",
"payment_channel_storage_type": "_etcd",
"ipfs_end_point": "http://ipfs.singularitynet.io:80",
"ethereum_json_rpc_http_endpoint": "https://sepolia.infura.io/v3/09027f4a13e841d48dbfefc67e7685d5",
"ipfs_timeout" : 30,
"passthrough_enabled": true,
"passthrough_endpoint":"http://127.0.0.1:5002",
"passthrough_endpoint":"http://0.0.0.0:5001",
"model_maintenance_endpoint": "http://0.0.0.0:5001",
"model_training_endpoint": "http://0.0.0.0:5001",
"service_id": "service_id",
"organization_id": "test_org_id",
"metering_enabled": false,
"ssl_cert": "",
"ssl_key": "",
"max_message_size_in_mb" : 4,
"daemon_type": "grpc",
"enable_dynamic_pricing":false,
Expand All @@ -175,39 +187,21 @@ func (suite *DaemonServiceSuite) setupTestConfig() {
"timestamp_format": "2006-01-02T15:04:05.999Z07:00"
},
"output": {
"type": ["file", "stdout"],
"file_pattern": "./snet-daemon.%Y%m%d.log",
"current_link": "./snet-daemon.log",
"max_size_in_mb": 10,
"max_age_in_days": 7,
"rotation_count": 0
"type": ["stdout"]
},
"hooks": []
},
"model_maintenance_endpoint": "http://localhost:5001",
"payment_channel_storage_client": {
"connection_timeout": "0s",
"request_timeout": "0s",
"hot_reload": true
},
"payment_channel_storage_server": {
"id": "storage-1",
"scheme": "http",
"host" : "127.0.0.1",
"client_port": 2379,
"peer_port": 2380,
"token": "unique-token",
"cluster": "storage-1=http://127.0.0.1:2380",
"startup_timeout": "1m",
"data_dir": "storage-data-dir-1.etcd",
"log_level": "info",
"log_outputs": ["./etcd-server.log"],
"enabled": false
},
"alerts_email": "",
"service_heartbeat_type": "http",
"token_expiry_in_minutes": 1440,
"model_training_enabled": false
"model_training_enabled": true
}`

var testConfig = viper.New()
Expand Down Expand Up @@ -374,7 +368,7 @@ func (suite *DaemonServiceSuite) createAdditionalTestModel(modelName string, aut
Authorization: authDetails,
Model: newModel,
}
response, err := suite.daemonService.CreateModel(context.Background(), request)
response, err := suite.daemonService.CreateModel(context.WithValue(context.Background(), "method", "create_model"), request)
if err != nil {
zap.L().Fatal("error in creating additional test model", zap.Error(err))
}
Expand All @@ -383,11 +377,11 @@ func (suite *DaemonServiceSuite) createAdditionalTestModel(modelName string, aut
}

func (suite *DaemonServiceSuite) TestDaemonService_GetModel() {
testAuthCreads := createTestAuthDetails()
badTestAuthCreads := creatBadTestAuthDetails()
testAuthCreads := createTestAuthDetails(suite.currentBlock, "get_model")
badTestAuthCreads := creatBadTestAuthDetails(suite.currentBlock)

// check without request
response1, err := suite.daemonService.GetModel(context.Background(), nil)
response1, err := suite.daemonService.GetModel(context.WithValue(context.Background(), "method", "get_model"), nil)
assert.ErrorContains(suite.T(), err, training.ErrNoAuthorization.Error())
assert.Equal(suite.T(), training.Status_ERRORED, response1.Status)

Expand All @@ -396,7 +390,7 @@ func (suite *DaemonServiceSuite) TestDaemonService_GetModel() {
Authorization: nil,
ModelId: "test_2",
}
response2, err := suite.daemonService.GetModel(context.Background(), request2)
response2, err := suite.daemonService.GetModel(context.WithValue(context.Background(), "method", "get_model"), request2)
assert.ErrorContains(suite.T(), err, training.ErrNoAuthorization.Error())
assert.Equal(suite.T(), training.Status_ERRORED, response2.Status)

Expand All @@ -405,7 +399,7 @@ func (suite *DaemonServiceSuite) TestDaemonService_GetModel() {
Authorization: badTestAuthCreads,
ModelId: "test_2",
}
response3, err := suite.daemonService.GetModel(context.Background(), request3)
response3, err := suite.daemonService.GetModel(context.WithValue(context.Background(), "method", "get_model"), request3)
assert.ErrorContains(suite.T(), err, training.ErrBadAuthorization.Error())
assert.Equal(suite.T(), training.Status_ERRORED, response3.Status)

Expand All @@ -414,7 +408,7 @@ func (suite *DaemonServiceSuite) TestDaemonService_GetModel() {
Authorization: testAuthCreads,
ModelId: "",
}
response4, err := suite.daemonService.GetModel(context.Background(), request4)
response4, err := suite.daemonService.GetModel(context.WithValue(context.Background(), "method", "get_model"), request4)
assert.NotNil(suite.T(), err)
assert.ErrorContains(suite.T(), err, training.ErrEmptyModelID.Error())
assert.Equal(suite.T(), training.Status_ERRORED, response4.Status)
Expand All @@ -424,7 +418,7 @@ func (suite *DaemonServiceSuite) TestDaemonService_GetModel() {
Authorization: testAuthCreads,
ModelId: "test_2",
}
response5, err := suite.daemonService.GetModel(context.Background(), request5)
response5, err := suite.daemonService.GetModel(context.WithValue(context.Background(), "method", "get_model"), request5)
assert.ErrorContains(suite.T(), err, training.ErrAccessToModel.Error())
assert.Equal(suite.T(), &training.ModelResponse{}, response5)

Expand All @@ -433,7 +427,7 @@ func (suite *DaemonServiceSuite) TestDaemonService_GetModel() {
Authorization: testAuthCreads,
ModelId: "test_1",
}
response6, err := suite.daemonService.GetModel(context.Background(), request6)
response6, err := suite.daemonService.GetModel(context.WithValue(context.Background(), "method", "get_model"), request6)
assert.Nil(suite.T(), err)
assert.NotEmpty(suite.T(), response6)
assert.Equal(suite.T(), true, response6.IsPublic)
Expand All @@ -443,15 +437,18 @@ func (suite *DaemonServiceSuite) TestDaemonService_GetModel() {
Authorization: testAuthCreads,
ModelId: "test_3",
}
response7, err := suite.daemonService.GetModel(context.Background(), request7)
response7, err := suite.daemonService.GetModel(context.WithValue(context.Background(), "method", "get_model"), request7)
assert.Nil(suite.T(), err)
assert.NotEmpty(suite.T(), response7)
assert.Equal(suite.T(), false, response7.IsPublic)
}

func (suite *DaemonServiceSuite) TestDaemonService_CreateModel() {
testAuthCreads := createTestAuthDetails()
badTestAuthCreads := creatBadTestAuthDetails()
var err error
suite.currentBlock, err = suite.blockchain.CurrentBlock()
assert.Nil(suite.T(), err)
testAuthCreads := createTestAuthDetails(suite.currentBlock, "create_model")
badTestAuthCreads := creatBadTestAuthDetails(suite.currentBlock)

newModel := &training.NewModel{
Name: "new_test_model",
Expand All @@ -468,7 +465,7 @@ func (suite *DaemonServiceSuite) TestDaemonService_CreateModel() {
newEmptyModel := &training.NewModel{}

// check without request
response1, err := suite.daemonService.CreateModel(context.Background(), nil)
response1, err := suite.daemonService.CreateModel(context.WithValue(context.Background(), "method", "create_model"), nil)
assert.ErrorContains(suite.T(), err, training.ErrNoAuthorization.Error())
assert.Equal(suite.T(), training.Status_ERRORED, response1.Status)

Expand All @@ -486,16 +483,20 @@ func (suite *DaemonServiceSuite) TestDaemonService_CreateModel() {
Authorization: badTestAuthCreads,
Model: newModel,
}
response3, err := suite.daemonService.CreateModel(context.Background(), request3)
response3, err := suite.daemonService.CreateModel(context.WithValue(context.Background(), "method", "create_model"), request3)
assert.ErrorContains(suite.T(), err, training.ErrBadAuthorization.Error())
assert.Equal(suite.T(), training.Status_ERRORED, response3.Status)

suite.currentBlock, err = suite.blockchain.CurrentBlock()
assert.Nil(suite.T(), err)
testAuthCreads = createTestAuthDetails(suite.currentBlock, "create_model")

// check with emptyModel
request4 := &training.NewModelRequest{
Authorization: testAuthCreads,
Model: newEmptyModel,
}
response4, err := suite.daemonService.CreateModel(context.Background(), request4)
response4, err := suite.daemonService.CreateModel(context.WithValue(context.Background(), "method", "create_model"), request4)
assert.ErrorContains(suite.T(), err, training.ErrNoGRPCServiceOrMethod.Error())
assert.Equal(suite.T(), training.Status_ERRORED, response4.Status)

Expand All @@ -504,7 +505,7 @@ func (suite *DaemonServiceSuite) TestDaemonService_CreateModel() {
Authorization: testAuthCreads,
Model: newModel,
}
response5, err := suite.daemonService.CreateModel(context.Background(), request5)
response5, err := suite.daemonService.CreateModel(context.WithValue(context.Background(), "method", "create_model"), request5)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), training.Status_CREATED, response5.Status)

Expand All @@ -527,7 +528,7 @@ func (suite *DaemonServiceSuite) TestDaemonService_CreateModel() {
OrganizationId: "test_org_id",
ServiceId: "service_id",
GroupId: "99ybRIg2wAx55mqVsA6sB4S7WxPQHNKqa4BPu/bhj+U=",
UserAddress: testUserAddress,
UserAddress: strings.ToLower(testUserAddress),
}

userModelData, ok, err := suite.userModelStorage.Get(userModelKey)
Expand All @@ -537,39 +538,39 @@ func (suite *DaemonServiceSuite) TestDaemonService_CreateModel() {
}

func (suite *DaemonServiceSuite) TestDaemonService_GetAllModels() {
testAuthCreads := createTestAuthDetails()
badTestAuthCreads := creatBadTestAuthDetails()
testAuthCreads := createTestAuthDetails(suite.currentBlock, "unified")
badTestAuthCreads := creatBadTestAuthDetails(suite.currentBlock)

newAdditionalTestModelId := suite.createAdditionalTestModel("new_additional_test_model", testAuthCreads)

expectedModelIds := []string{"test_3", newAdditionalTestModelId, "test_1"}

// check without request
response1, err := suite.daemonService.GetAllModels(context.Background(), nil)
response1, err := suite.daemonService.GetAllModels(context.WithValue(context.Background(), "method", "get_all_models"), nil)
assert.ErrorContains(suite.T(), err, training.ErrNoAuthorization.Error())
assert.Nil(suite.T(), response1.ListOfModels)

// check without auth
request2 := &training.AllModelsRequest{
Authorization: nil,
}
response2, err := suite.daemonService.GetAllModels(context.Background(), request2)
response2, err := suite.daemonService.GetAllModels(context.WithValue(context.Background(), "method", "get_all_models"), request2)
assert.ErrorContains(suite.T(), err, training.ErrNoAuthorization.Error())
assert.Nil(suite.T(), response2.ListOfModels)

// check with bad auth
request3 := &training.AllModelsRequest{
Authorization: badTestAuthCreads,
}
response3, err := suite.daemonService.GetAllModels(context.Background(), request3)
response3, err := suite.daemonService.GetAllModels(context.WithValue(context.Background(), "method", "get_all_models"), request3)
assert.ErrorContains(suite.T(), err, training.ErrBadAuthorization.Error())
assert.Nil(suite.T(), response3.ListOfModels)

// check with auth and without filters
request4 := &training.AllModelsRequest{
Authorization: testAuthCreads,
}
response4, err := suite.daemonService.GetAllModels(context.Background(), request4)
response4, err := suite.daemonService.GetAllModels(context.WithValue(context.Background(), "method", "get_all_models"), request4)
assert.Nil(suite.T(), err)
modelIds := []string{}
for _, model := range response4.ListOfModels {
Expand All @@ -579,8 +580,8 @@ func (suite *DaemonServiceSuite) TestDaemonService_GetAllModels() {
assert.True(suite.T(), slices.Equal(expectedModelIds, modelIds))
}

func (suite *DaemonServiceSuite) TestDaemonSerice_ManageUpdateStatusWorkers() {
duration := time.Second * 10
func (suite *DaemonServiceSuite) TestDaemonService_ManageUpdateStatusWorkers() {
duration := time.Second * 12
deadline := time.Now().Add(duration)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
Expand Down

0 comments on commit fe19dfb

Please sign in to comment.