2022-10-13 17:56:16 +03:00
// Copyright 2022 Cloudbase Solutions SRL
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package sql
import (
"context"
2022-10-17 17:41:00 +03:00
"flag"
2022-10-13 17:56:16 +03:00
"fmt"
2022-10-17 17:41:00 +03:00
"regexp"
2022-10-13 17:56:16 +03:00
"sort"
"testing"
"github.com/stretchr/testify/suite"
2022-10-17 17:41:00 +03:00
"gopkg.in/DATA-DOG/go-sqlmock.v1"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
2024-02-22 16:54:38 +01:00
dbCommon "github.com/cloudbase/garm/database/common"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
2022-10-13 17:56:16 +03:00
)
type RepoTestFixtures struct {
2022-10-14 17:41:31 +03:00
Repos [ ] params . Repository
CreateRepoParams params . CreateRepoParams
CreatePoolParams params . CreatePoolParams
CreateInstanceParams params . CreateInstanceParams
2023-07-04 23:47:55 +00:00
UpdateRepoParams params . UpdateEntityParams
2022-10-14 17:41:31 +03:00
UpdatePoolParams params . UpdatePoolParams
2022-10-17 17:41:00 +03:00
SQLMock sqlmock . Sqlmock
2022-10-13 17:56:16 +03:00
}
type RepoTestSuite struct {
suite . Suite
2022-10-17 17:41:00 +03:00
Store dbCommon . Store
StoreSQLMocked * sqlDatabase
Fixtures * RepoTestFixtures
2022-10-13 17:56:16 +03:00
}
func ( s * RepoTestSuite ) equalReposByName ( expected , actual [ ] params . Repository ) {
s . Require ( ) . Equal ( len ( expected ) , len ( actual ) )
sort . Slice ( expected , func ( i , j int ) bool { return expected [ i ] . Name > expected [ j ] . Name } )
sort . Slice ( actual , func ( i , j int ) bool { return actual [ i ] . Name > actual [ j ] . Name } )
for i := 0 ; i < len ( expected ) ; i ++ {
s . Require ( ) . Equal ( expected [ i ] . Name , actual [ i ] . Name )
}
}
2022-10-14 17:41:31 +03:00
func ( s * RepoTestSuite ) equalInstancesByID ( expected , actual [ ] params . Instance ) {
s . Require ( ) . Equal ( len ( expected ) , len ( actual ) )
sort . Slice ( expected , func ( i , j int ) bool { return expected [ i ] . ID > expected [ j ] . ID } )
sort . Slice ( actual , func ( i , j int ) bool { return actual [ i ] . ID > actual [ j ] . ID } )
for i := 0 ; i < len ( expected ) ; i ++ {
s . Require ( ) . Equal ( expected [ i ] . ID , actual [ i ] . ID )
}
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) assertSQLMockExpectations ( ) {
err := s . Fixtures . SQLMock . ExpectationsWereMet ( )
if err != nil {
s . FailNow ( fmt . Sprintf ( "failed to meet sqlmock expectations, got error: %v" , err ) )
}
}
2022-10-13 17:56:16 +03:00
func ( s * RepoTestSuite ) SetupTest ( ) {
// create testing sqlite database
db , err := NewSQLDatabase ( context . Background ( ) , garmTesting . GetTestSqliteDBConfig ( s . T ( ) ) )
if err != nil {
s . FailNow ( fmt . Sprintf ( "failed to create db connection: %s" , err ) )
}
s . Store = db
// create some repository objects in the database, for testing purposes
repos := [ ] params . Repository { }
for i := 1 ; i <= 3 ; i ++ {
repo , err := db . CreateRepository (
context . Background ( ) ,
fmt . Sprintf ( "test-owner-%d" , i ) ,
fmt . Sprintf ( "test-repo-%d" , i ) ,
fmt . Sprintf ( "test-creds-%d" , i ) ,
fmt . Sprintf ( "test-webhook-secret-%d" , i ) ,
Add pool balancing strategy
This change adds the ability to specify the pool balancing strategy to
use when processing queued jobs. Before this change, GARM would round-robin
through all pools that matched the set of tags requested by queued jobs.
When round-robin (default) is used for an entity (repo, org or enterprise)
and you have 2 pools defined for that entity with a common set of tags that
match 10 jobs (for example), then those jobs would trigger the creation of
a new runner in each of the two pools in turn. Job 1 would go to pool 1,
job 2 would go to pool 2, job 3 to pool 1, job 4 to pool 2 and so on.
When "stack" is used, those same 10 jobs would trigger the creation of a
new runner in the pool with the highest priority, every time.
In both cases, if a pool is full, the next one would be tried automatically.
For the stack case, this would mean that if pool 2 had a priority of 10 and
pool 1 would have a priority of 5, pool 2 would be saturated first, then
pool 1.
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2024-03-14 20:04:34 +00:00
params . PoolBalancerTypeRoundRobin ,
2022-10-13 17:56:16 +03:00
)
if err != nil {
2023-01-23 17:43:32 +01:00
s . FailNow ( fmt . Sprintf ( "failed to create database object (test-repo-%d): %v" , i , err ) )
2022-10-13 17:56:16 +03:00
}
repos = append ( repos , repo )
}
2022-10-17 17:41:00 +03:00
// create store with mocked sql connection
sqlDB , sqlMock , err := sqlmock . New ( )
if err != nil {
s . FailNow ( fmt . Sprintf ( "failed to run 'sqlmock.New()', got error: %v" , err ) )
}
s . T ( ) . Cleanup ( func ( ) { sqlDB . Close ( ) } )
mysqlConfig := mysql . Config {
Conn : sqlDB ,
SkipInitializeWithVersion : true ,
}
gormConfig := & gorm . Config { }
2024-02-22 17:20:05 +01:00
if flag . Lookup ( "test.v" ) . Value . String ( ) == falseString {
2022-10-17 17:41:00 +03:00
gormConfig . Logger = logger . Default . LogMode ( logger . Silent )
}
gormConn , err := gorm . Open ( mysql . New ( mysqlConfig ) , gormConfig )
if err != nil {
s . FailNow ( fmt . Sprintf ( "fail to open gorm connection: %v" , err ) )
}
s . StoreSQLMocked = & sqlDatabase {
conn : gormConn ,
cfg : garmTesting . GetTestSqliteDBConfig ( s . T ( ) ) ,
}
2022-10-13 17:56:16 +03:00
// setup test fixtures
2022-10-14 17:41:31 +03:00
var maxRunners uint = 40
var minIdleRunners uint = 20
2022-10-13 17:56:16 +03:00
fixtures := & RepoTestFixtures {
Repos : repos ,
CreateRepoParams : params . CreateRepoParams {
Owner : "test-owner-repo" ,
Name : "test-repo" ,
CredentialsName : "test-creds-repo" ,
WebhookSecret : "test-webhook-secret" ,
} ,
2022-10-14 17:41:31 +03:00
CreatePoolParams : params . CreatePoolParams {
ProviderName : "test-provider" ,
MaxRunners : 4 ,
MinIdleRunners : 2 ,
Image : "test-image" ,
2023-03-31 15:03:08 +00:00
Enabled : true ,
2022-10-14 17:41:31 +03:00
Flavor : "test-flavor" ,
OSType : "windows" ,
OSArch : "amd64" ,
Tags : [ ] string { "self-hosted" , "arm64" , "windows" } ,
} ,
CreateInstanceParams : params . CreateInstanceParams {
Name : "test-instance" ,
OSType : "linux" ,
} ,
2023-07-04 23:47:55 +00:00
UpdateRepoParams : params . UpdateEntityParams {
2022-10-13 17:56:16 +03:00
CredentialsName : "test-update-creds" ,
WebhookSecret : "test-update-webhook-secret" ,
} ,
2022-10-14 17:41:31 +03:00
UpdatePoolParams : params . UpdatePoolParams {
MaxRunners : & maxRunners ,
MinIdleRunners : & minIdleRunners ,
Image : "test-update-image" ,
Flavor : "test-update-flavor" ,
} ,
2022-10-17 17:41:00 +03:00
SQLMock : sqlMock ,
2022-10-13 17:56:16 +03:00
}
s . Fixtures = fixtures
}
func ( s * RepoTestSuite ) TestCreateRepository ( ) {
// call tested function
repo , err := s . Store . CreateRepository (
context . Background ( ) ,
s . Fixtures . CreateRepoParams . Owner ,
s . Fixtures . CreateRepoParams . Name ,
s . Fixtures . CreateRepoParams . CredentialsName ,
s . Fixtures . CreateRepoParams . WebhookSecret ,
Add pool balancing strategy
This change adds the ability to specify the pool balancing strategy to
use when processing queued jobs. Before this change, GARM would round-robin
through all pools that matched the set of tags requested by queued jobs.
When round-robin (default) is used for an entity (repo, org or enterprise)
and you have 2 pools defined for that entity with a common set of tags that
match 10 jobs (for example), then those jobs would trigger the creation of
a new runner in each of the two pools in turn. Job 1 would go to pool 1,
job 2 would go to pool 2, job 3 to pool 1, job 4 to pool 2 and so on.
When "stack" is used, those same 10 jobs would trigger the creation of a
new runner in the pool with the highest priority, every time.
In both cases, if a pool is full, the next one would be tried automatically.
For the stack case, this would mean that if pool 2 had a priority of 10 and
pool 1 would have a priority of 5, pool 2 would be saturated first, then
pool 1.
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2024-03-14 20:04:34 +00:00
params . PoolBalancerTypeRoundRobin ,
2022-10-13 17:56:16 +03:00
)
// assertions
s . Require ( ) . Nil ( err )
storeRepo , err := s . Store . GetRepositoryByID ( context . Background ( ) , repo . ID )
if err != nil {
s . FailNow ( fmt . Sprintf ( "failed to get repository by id: %v" , err ) )
}
s . Require ( ) . Equal ( storeRepo . Owner , repo . Owner )
s . Require ( ) . Equal ( storeRepo . Name , repo . Name )
s . Require ( ) . Equal ( storeRepo . CredentialsName , repo . CredentialsName )
s . Require ( ) . Equal ( storeRepo . WebhookSecret , repo . WebhookSecret )
}
func ( s * RepoTestSuite ) TestCreateRepositoryInvalidDBPassphrase ( ) {
cfg := garmTesting . GetTestSqliteDBConfig ( s . T ( ) )
conn , err := newDBConn ( cfg )
if err != nil {
s . FailNow ( fmt . Sprintf ( "failed to create db connection: %s" , err ) )
}
// make sure we use a 'sqlDatabase' struct with a wrong 'cfg.Passphrase'
2024-02-22 17:20:05 +01:00
cfg . Passphrase = wrongPassphrase // it must have a size different than 32
2022-10-13 17:56:16 +03:00
sqlDB := & sqlDatabase {
conn : conn ,
cfg : cfg ,
}
_ , err = sqlDB . CreateRepository (
context . Background ( ) ,
s . Fixtures . CreateRepoParams . Owner ,
s . Fixtures . CreateRepoParams . Name ,
s . Fixtures . CreateRepoParams . CredentialsName ,
s . Fixtures . CreateRepoParams . WebhookSecret ,
Add pool balancing strategy
This change adds the ability to specify the pool balancing strategy to
use when processing queued jobs. Before this change, GARM would round-robin
through all pools that matched the set of tags requested by queued jobs.
When round-robin (default) is used for an entity (repo, org or enterprise)
and you have 2 pools defined for that entity with a common set of tags that
match 10 jobs (for example), then those jobs would trigger the creation of
a new runner in each of the two pools in turn. Job 1 would go to pool 1,
job 2 would go to pool 2, job 3 to pool 1, job 4 to pool 2 and so on.
When "stack" is used, those same 10 jobs would trigger the creation of a
new runner in the pool with the highest priority, every time.
In both cases, if a pool is full, the next one would be tried automatically.
For the stack case, this would mean that if pool 2 had a priority of 10 and
pool 1 would have a priority of 5, pool 2 would be saturated first, then
pool 1.
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2024-03-14 20:04:34 +00:00
params . PoolBalancerTypeRoundRobin ,
2022-10-13 17:56:16 +03:00
)
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "failed to encrypt string" , err . Error ( ) )
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) TestCreateRepositoryInvalidDBCreateErr ( ) {
s . Fixtures . SQLMock . ExpectBegin ( )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `repositories`" ) ) .
WillReturnError ( fmt . Errorf ( "creating repo mock error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
_ , err := s . StoreSQLMocked . CreateRepository (
context . Background ( ) ,
s . Fixtures . CreateRepoParams . Owner ,
s . Fixtures . CreateRepoParams . Name ,
s . Fixtures . CreateRepoParams . CredentialsName ,
s . Fixtures . CreateRepoParams . WebhookSecret ,
Add pool balancing strategy
This change adds the ability to specify the pool balancing strategy to
use when processing queued jobs. Before this change, GARM would round-robin
through all pools that matched the set of tags requested by queued jobs.
When round-robin (default) is used for an entity (repo, org or enterprise)
and you have 2 pools defined for that entity with a common set of tags that
match 10 jobs (for example), then those jobs would trigger the creation of
a new runner in each of the two pools in turn. Job 1 would go to pool 1,
job 2 would go to pool 2, job 3 to pool 1, job 4 to pool 2 and so on.
When "stack" is used, those same 10 jobs would trigger the creation of a
new runner in the pool with the highest priority, every time.
In both cases, if a pool is full, the next one would be tried automatically.
For the stack case, this would mean that if pool 2 had a priority of 10 and
pool 1 would have a priority of 5, pool 2 would be saturated first, then
pool 1.
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2024-03-14 20:04:34 +00:00
params . PoolBalancerTypeRoundRobin ,
2022-10-17 17:41:00 +03:00
)
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "creating repository: creating repo mock error" , err . Error ( ) )
}
2022-10-13 17:56:16 +03:00
func ( s * RepoTestSuite ) TestGetRepository ( ) {
repo , err := s . Store . GetRepository ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . Owner , s . Fixtures . Repos [ 0 ] . Name )
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( s . Fixtures . Repos [ 0 ] . Owner , repo . Owner )
s . Require ( ) . Equal ( s . Fixtures . Repos [ 0 ] . Name , repo . Name )
s . Require ( ) . Equal ( s . Fixtures . Repos [ 0 ] . ID , repo . ID )
}
func ( s * RepoTestSuite ) TestGetRepositoryCaseInsensitive ( ) {
repo , err := s . Store . GetRepository ( context . Background ( ) , "TeSt-oWnEr-1" , "TeSt-rEpO-1" )
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( "test-owner-1" , repo . Owner )
s . Require ( ) . Equal ( "test-repo-1" , repo . Name )
}
func ( s * RepoTestSuite ) TestGetRepositoryNotFound ( ) {
_ , err := s . Store . GetRepository ( context . Background ( ) , "dummy-owner" , "dummy-name" )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching repo: not found" , err . Error ( ) )
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) TestGetRepositoryDBDecryptingErr ( ) {
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE (name = ? COLLATE NOCASE and owner = ? COLLATE NOCASE) AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . Name , s . Fixtures . Repos [ 0 ] . Owner , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "name" , "owner" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . Name , s . Fixtures . Repos [ 0 ] . Owner ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE (name = ? COLLATE NOCASE and owner = ? COLLATE NOCASE) AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id`,`repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . Name , s . Fixtures . Repos [ 0 ] . Owner , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "name" , "owner" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . Name , s . Fixtures . Repos [ 0 ] . Owner ) )
_ , err := s . StoreSQLMocked . GetRepository ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . Owner , s . Fixtures . Repos [ 0 ] . Name )
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
2023-01-23 17:43:32 +01:00
s . Require ( ) . Equal ( "fetching repo: missing secret" , err . Error ( ) )
2022-10-17 17:41:00 +03:00
}
2022-10-13 17:56:16 +03:00
func ( s * RepoTestSuite ) TestListRepositories ( ) {
repos , err := s . Store . ListRepositories ( ( context . Background ( ) ) )
s . Require ( ) . Nil ( err )
s . equalReposByName ( s . Fixtures . Repos , repos )
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) TestListRepositoriesDBFetchErr ( ) {
s . Fixtures . SQLMock .
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE `repositories`.`deleted_at` IS NULL" ) ) .
WillReturnError ( fmt . Errorf ( "fetching user from database mock error" ) )
_ , err := s . StoreSQLMocked . ListRepositories ( context . Background ( ) )
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching user from database: fetching user from database mock error" , err . Error ( ) )
}
func ( s * RepoTestSuite ) TestListRepositoriesDBDecryptingErr ( ) {
2024-02-22 17:20:05 +01:00
s . StoreSQLMocked . cfg . Passphrase = wrongPassphrase
2022-10-17 17:41:00 +03:00
s . Fixtures . SQLMock .
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE `repositories`.`deleted_at` IS NULL" ) ) .
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" , "webhook_secret" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID , s . Fixtures . Repos [ 0 ] . WebhookSecret ) )
_ , err := s . StoreSQLMocked . ListRepositories ( context . Background ( ) )
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
2023-01-23 17:43:32 +01:00
s . Require ( ) . Equal ( "fetching repositories: decrypting secret: invalid passphrase length (expected length 32 characters)" , err . Error ( ) )
2022-10-17 17:41:00 +03:00
}
2022-10-13 17:56:16 +03:00
func ( s * RepoTestSuite ) TestDeleteRepository ( ) {
err := s . Store . DeleteRepository ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID )
s . Require ( ) . Nil ( err )
_ , err = s . Store . GetRepositoryByID ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching repo: not found" , err . Error ( ) )
}
func ( s * RepoTestSuite ) TestDeleteRepositoryInvalidRepoID ( ) {
err := s . Store . DeleteRepository ( context . Background ( ) , "dummy-repo-id" )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching repo: parsing id: invalid request" , err . Error ( ) )
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) TestDeleteRepositoryDBRemoveErr ( ) {
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
s . Fixtures . SQLMock . ExpectBegin ( )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "DELETE FROM `repositories`" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID ) .
WillReturnError ( fmt . Errorf ( "mocked deleting repo error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
err := s . StoreSQLMocked . DeleteRepository ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID )
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "deleting repo: mocked deleting repo error" , err . Error ( ) )
}
2022-10-13 17:56:16 +03:00
func ( s * RepoTestSuite ) TestUpdateRepository ( ) {
repo , err := s . Store . UpdateRepository ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID , s . Fixtures . UpdateRepoParams )
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( s . Fixtures . UpdateRepoParams . CredentialsName , repo . CredentialsName )
s . Require ( ) . Equal ( s . Fixtures . UpdateRepoParams . WebhookSecret , repo . WebhookSecret )
}
func ( s * RepoTestSuite ) TestUpdateRepositoryInvalidRepoID ( ) {
_ , err := s . Store . UpdateRepository ( context . Background ( ) , "dummy-repo-id" , s . Fixtures . UpdateRepoParams )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching repo: parsing id: invalid request" , err . Error ( ) )
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) TestUpdateRepositoryDBEncryptErr ( ) {
2024-02-22 17:20:05 +01:00
s . StoreSQLMocked . cfg . Passphrase = wrongPassphrase
2022-10-17 17:41:00 +03:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
_ , err := s . StoreSQLMocked . UpdateRepository ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID , s . Fixtures . UpdateRepoParams )
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
2023-01-23 17:43:32 +01:00
s . Require ( ) . Equal ( "saving repo: failed to encrypt string: invalid passphrase length (expected length 32 characters)" , err . Error ( ) )
2022-10-17 17:41:00 +03:00
}
func ( s * RepoTestSuite ) TestUpdateRepositoryDBSaveErr ( ) {
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
s . Fixtures . SQLMock . ExpectBegin ( )
s . Fixtures . SQLMock .
ExpectExec ( ( "UPDATE `repositories` SET" ) ) .
WillReturnError ( fmt . Errorf ( "saving repo mock error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
_ , err := s . StoreSQLMocked . UpdateRepository ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID , s . Fixtures . UpdateRepoParams )
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "saving repo: saving repo mock error" , err . Error ( ) )
}
func ( s * RepoTestSuite ) TestUpdateRepositoryDBDecryptingErr ( ) {
2024-02-22 17:20:05 +01:00
s . StoreSQLMocked . cfg . Passphrase = wrongPassphrase
s . Fixtures . UpdateRepoParams . WebhookSecret = webhookSecret
2022-10-17 17:41:00 +03:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
_ , err := s . StoreSQLMocked . UpdateRepository ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID , s . Fixtures . UpdateRepoParams )
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
2023-01-23 17:43:32 +01:00
s . Require ( ) . Equal ( "saving repo: failed to encrypt string: invalid passphrase length (expected length 32 characters)" , err . Error ( ) )
2022-10-17 17:41:00 +03:00
}
2022-10-14 17:41:31 +03:00
func ( s * RepoTestSuite ) TestGetRepositoryByID ( ) {
repo , err := s . Store . GetRepositoryByID ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID )
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( s . Fixtures . Repos [ 0 ] . ID , repo . ID )
}
func ( s * RepoTestSuite ) TestGetRepositoryByIDInvalidRepoID ( ) {
_ , err := s . Store . GetRepositoryByID ( context . Background ( ) , "dummy-repo-id" )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching repo: parsing id: invalid request" , err . Error ( ) )
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) TestGetRepositoryByIDDBDecryptingErr ( ) {
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
s . Fixtures . SQLMock .
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `pools` WHERE `pools`.`repo_id` = ? AND `pools`.`deleted_at` IS NULL" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID ) .
WillReturnRows ( sqlmock . NewRows ( [ ] string { "repo_id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
_ , err := s . StoreSQLMocked . GetRepositoryByID ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID )
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
2023-01-23 17:43:32 +01:00
s . Require ( ) . Equal ( "fetching repo: missing secret" , err . Error ( ) )
2022-10-17 17:41:00 +03:00
}
2022-10-14 17:41:31 +03:00
func ( s * RepoTestSuite ) TestCreateRepositoryPool ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-03-28 18:23:49 +00:00
pool , err := s . Store . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-14 17:41:31 +03:00
s . Require ( ) . Nil ( err )
repo , err := s . Store . GetRepositoryByID ( context . Background ( ) , s . Fixtures . Repos [ 0 ] . ID )
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot get repo by ID: %v" , err ) )
}
s . Require ( ) . Equal ( 1 , len ( repo . Pools ) )
s . Require ( ) . Equal ( pool . ID , repo . Pools [ 0 ] . ID )
s . Require ( ) . Equal ( s . Fixtures . CreatePoolParams . ProviderName , repo . Pools [ 0 ] . ProviderName )
s . Require ( ) . Equal ( s . Fixtures . CreatePoolParams . MaxRunners , repo . Pools [ 0 ] . MaxRunners )
s . Require ( ) . Equal ( s . Fixtures . CreatePoolParams . MinIdleRunners , repo . Pools [ 0 ] . MinIdleRunners )
}
func ( s * RepoTestSuite ) TestCreateRepositoryPoolMissingTags ( ) {
s . Fixtures . CreatePoolParams . Tags = [ ] string { }
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
_ , err = s . Store . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-14 17:41:31 +03:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "no tags specified" , err . Error ( ) )
}
func ( s * RepoTestSuite ) TestCreateRepositoryPoolInvalidRepoID ( ) {
2024-03-28 18:23:49 +00:00
entity := params . GithubEntity {
ID : "dummy-repo-id" ,
EntityType : params . GithubEntityTypeRepository ,
}
_ , err := s . Store . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-14 17:41:31 +03:00
s . Require ( ) . NotNil ( err )
2024-03-28 18:23:49 +00:00
s . Require ( ) . Equal ( "parsing id: invalid request" , err . Error ( ) )
2022-10-14 17:41:31 +03:00
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) TestCreateRepositoryPoolDBCreateErr ( ) {
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-10-17 17:41:00 +03:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `pools` WHERE (provider_name = ? and image = ? and flavor = ? and repo_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WillReturnError ( fmt . Errorf ( "mocked creating pool error" ) )
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
_ , err = s . StoreSQLMocked . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-17 17:41:00 +03:00
s . Require ( ) . NotNil ( err )
2024-03-29 18:18:29 +00:00
s . Require ( ) . Equal ( "checking pool existence: mocked creating pool error" , err . Error ( ) )
s . assertSQLMockExpectations ( )
2022-10-17 17:41:00 +03:00
}
func ( s * RepoTestSuite ) TestCreateRepositoryPoolDBPoolAlreadyExistErr ( ) {
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-10-17 17:41:00 +03:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `pools` WHERE (provider_name = ? and image = ? and flavor = ? and repo_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WithArgs (
s . Fixtures . CreatePoolParams . ProviderName ,
s . Fixtures . CreatePoolParams . Image ,
2024-03-29 18:18:29 +00:00
s . Fixtures . CreatePoolParams . Flavor ,
2024-04-22 13:38:51 +00:00
s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "repo_id" , "provider_name" , "image" , "flavor" } ) .
AddRow (
s . Fixtures . Repos [ 0 ] . ID ,
s . Fixtures . CreatePoolParams . ProviderName ,
s . Fixtures . CreatePoolParams . Image ,
s . Fixtures . CreatePoolParams . Flavor ) )
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
_ , err = s . StoreSQLMocked . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-17 17:41:00 +03:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "pool with the same image and flavor already exists on this provider" , err . Error ( ) )
2024-03-29 18:18:29 +00:00
s . assertSQLMockExpectations ( )
2022-10-17 17:41:00 +03:00
}
func ( s * RepoTestSuite ) TestCreateRepositoryPoolDBFetchTagErr ( ) {
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-10-17 17:41:00 +03:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `pools` WHERE (provider_name = ? and image = ? and flavor = ? and repo_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WithArgs (
s . Fixtures . CreatePoolParams . ProviderName ,
s . Fixtures . CreatePoolParams . Image ,
2024-03-29 18:18:29 +00:00
s . Fixtures . CreatePoolParams . Flavor ,
2024-04-22 13:38:51 +00:00
s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "repo_id" } ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WillReturnError ( fmt . Errorf ( "mocked fetching tag error" ) )
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
_ , err = s . StoreSQLMocked . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-17 17:41:00 +03:00
s . Require ( ) . NotNil ( err )
2024-03-29 18:18:29 +00:00
s . Require ( ) . Equal ( "creating tag: fetching tag from database: mocked fetching tag error" , err . Error ( ) )
s . assertSQLMockExpectations ( )
2022-10-17 17:41:00 +03:00
}
func ( s * RepoTestSuite ) TestCreateRepositoryPoolDBAddingPoolErr ( ) {
s . Fixtures . CreatePoolParams . Tags = [ ] string { "linux" }
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-10-17 17:41:00 +03:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `pools` WHERE (provider_name = ? and image = ? and flavor = ? and repo_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WithArgs (
s . Fixtures . CreatePoolParams . ProviderName ,
s . Fixtures . CreatePoolParams . Image ,
2024-03-29 18:18:29 +00:00
s . Fixtures . CreatePoolParams . Flavor ,
2024-04-22 13:38:51 +00:00
s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "repo_id" } ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "linux" } ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `tags`" ) ) .
WillReturnResult ( sqlmock . NewResult ( 1 , 1 ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `pools`" ) ) .
WillReturnError ( fmt . Errorf ( "mocked adding pool error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
_ , err = s . StoreSQLMocked . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-17 17:41:00 +03:00
s . Require ( ) . NotNil ( err )
2024-03-29 18:18:29 +00:00
s . Require ( ) . Equal ( "creating pool: mocked adding pool error" , err . Error ( ) )
s . assertSQLMockExpectations ( )
2022-10-17 17:41:00 +03:00
}
func ( s * RepoTestSuite ) TestCreateRepositoryPoolDBSaveTagErr ( ) {
s . Fixtures . CreatePoolParams . Tags = [ ] string { "linux" }
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-10-17 17:41:00 +03:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `pools` WHERE (provider_name = ? and image = ? and flavor = ? and repo_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WithArgs (
s . Fixtures . CreatePoolParams . ProviderName ,
s . Fixtures . CreatePoolParams . Image ,
2024-03-29 18:18:29 +00:00
s . Fixtures . CreatePoolParams . Flavor ,
2024-04-22 13:38:51 +00:00
s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "repo_id" } ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "linux" } ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `tags`" ) ) .
WillReturnResult ( sqlmock . NewResult ( 1 , 1 ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `pools`" ) ) .
WillReturnResult ( sqlmock . NewResult ( 1 , 1 ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "UPDATE `pools` SET" ) ) .
WillReturnError ( fmt . Errorf ( "mocked saving tag error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2022-10-17 17:41:00 +03:00
2024-03-29 18:18:29 +00:00
_ , err = s . StoreSQLMocked . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-17 17:41:00 +03:00
s . Require ( ) . NotNil ( err )
2024-03-29 18:18:29 +00:00
s . Require ( ) . Equal ( "associating tags: mocked saving tag error" , err . Error ( ) )
s . assertSQLMockExpectations ( )
2022-10-17 17:41:00 +03:00
}
func ( s * RepoTestSuite ) TestCreateRepositoryPoolDBFetchPoolErr ( ) {
s . Fixtures . CreatePoolParams . Tags = [ ] string { "linux" }
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-10-17 17:41:00 +03:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `repositories` WHERE id = ? AND `repositories`.`deleted_at` IS NULL ORDER BY `repositories`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Repos [ 0 ] . ID ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `pools` WHERE (provider_name = ? and image = ? and flavor = ? and repo_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WithArgs (
s . Fixtures . CreatePoolParams . ProviderName ,
s . Fixtures . CreatePoolParams . Image ,
2024-03-29 18:18:29 +00:00
s . Fixtures . CreatePoolParams . Flavor ,
2024-04-22 13:38:51 +00:00
s . Fixtures . Repos [ 0 ] . ID , 1 ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "repo_id" } ) )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "linux" } ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `tags`" ) ) .
WillReturnResult ( sqlmock . NewResult ( 1 , 1 ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `pools`" ) ) .
WillReturnResult ( sqlmock . NewResult ( 1 , 1 ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "UPDATE `pools` SET" ) ) .
WillReturnResult ( sqlmock . NewResult ( 1 , 1 ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `tags`" ) ) .
WillReturnResult ( sqlmock . NewResult ( 1 , 1 ) )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `pool_tags`" ) ) .
WillReturnResult ( sqlmock . NewResult ( 1 , 1 ) )
s . Fixtures . SQLMock . ExpectCommit ( )
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `pools` WHERE id = ? AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-10-17 17:41:00 +03:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) )
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
_ , err = s . StoreSQLMocked . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-17 17:41:00 +03:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching pool: not found" , err . Error ( ) )
2024-03-29 18:18:29 +00:00
s . assertSQLMockExpectations ( )
2022-10-17 17:41:00 +03:00
}
2022-10-14 17:41:31 +03:00
func ( s * RepoTestSuite ) TestListRepoPools ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2022-10-14 17:41:31 +03:00
repoPools := [ ] params . Pool { }
for i := 1 ; i <= 2 ; i ++ {
s . Fixtures . CreatePoolParams . Flavor = fmt . Sprintf ( "test-flavor-%d" , i )
2024-03-28 18:23:49 +00:00
pool , err := s . Store . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-14 17:41:31 +03:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create repo pool: %v" , err ) )
}
repoPools = append ( repoPools , pool )
}
2024-03-29 18:18:29 +00:00
pools , err := s . Store . ListEntityPools ( context . Background ( ) , entity )
2022-10-14 17:41:31 +03:00
s . Require ( ) . Nil ( err )
2022-12-04 17:30:27 +00:00
garmTesting . EqualDBEntityID ( s . T ( ) , repoPools , pools )
2022-10-14 17:41:31 +03:00
}
func ( s * RepoTestSuite ) TestListRepoPoolsInvalidRepoID ( ) {
2024-03-29 18:18:29 +00:00
entity := params . GithubEntity {
ID : "dummy-repo-id" ,
EntityType : params . GithubEntityTypeRepository ,
}
_ , err := s . Store . ListEntityPools ( context . Background ( ) , entity )
2022-10-14 17:41:31 +03:00
s . Require ( ) . NotNil ( err )
2023-04-10 00:03:49 +00:00
s . Require ( ) . Equal ( "fetching pools: parsing id: invalid request" , err . Error ( ) )
2022-10-14 17:41:31 +03:00
}
func ( s * RepoTestSuite ) TestGetRepositoryPool ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-03-28 18:23:49 +00:00
pool , err := s . Store . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-14 17:41:31 +03:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create repo pool: %v" , err ) )
}
2024-03-28 18:23:49 +00:00
repoPool , err := s . Store . GetEntityPool ( context . Background ( ) , entity , pool . ID )
2022-10-14 17:41:31 +03:00
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( repoPool . ID , pool . ID )
}
func ( s * RepoTestSuite ) TestGetRepositoryPoolInvalidRepoID ( ) {
2024-03-28 18:23:49 +00:00
entity := params . GithubEntity {
ID : "dummy-repo-id" ,
EntityType : params . GithubEntityTypeRepository ,
}
_ , err := s . Store . GetEntityPool ( context . Background ( ) , entity , "dummy-pool-id" )
2022-10-14 17:41:31 +03:00
s . Require ( ) . NotNil ( err )
2023-01-30 14:29:55 +00:00
s . Require ( ) . Equal ( "fetching pool: parsing id: invalid request" , err . Error ( ) )
2022-10-14 17:41:31 +03:00
}
func ( s * RepoTestSuite ) TestDeleteRepositoryPool ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-03-28 18:23:49 +00:00
pool , err := s . Store . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-14 17:41:31 +03:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create repo pool: %v" , err ) )
}
2024-03-28 18:23:49 +00:00
err = s . Store . DeleteEntityPool ( context . Background ( ) , entity , pool . ID )
2022-10-14 17:41:31 +03:00
s . Require ( ) . Nil ( err )
2024-03-28 18:23:49 +00:00
_ , err = s . Store . GetEntityPool ( context . Background ( ) , entity , pool . ID )
2023-01-30 14:29:55 +00:00
s . Require ( ) . Equal ( "fetching pool: finding pool: not found" , err . Error ( ) )
2022-10-14 17:41:31 +03:00
}
func ( s * RepoTestSuite ) TestDeleteRepositoryPoolInvalidRepoID ( ) {
2024-03-28 18:23:49 +00:00
entity := params . GithubEntity {
ID : "dummy-repo-id" ,
EntityType : params . GithubEntityTypeRepository ,
}
err := s . Store . DeleteEntityPool ( context . Background ( ) , entity , "dummy-pool-id" )
2022-10-14 17:41:31 +03:00
s . Require ( ) . NotNil ( err )
2024-03-28 18:23:49 +00:00
s . Require ( ) . Equal ( "parsing id: invalid request" , err . Error ( ) )
2022-10-14 17:41:31 +03:00
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) TestDeleteRepositoryPoolDBDeleteErr ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-03-28 18:23:49 +00:00
pool , err := s . Store . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-17 17:41:00 +03:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create repo pool: %v" , err ) )
}
s . Fixtures . SQLMock . ExpectBegin ( )
s . Fixtures . SQLMock .
2024-03-29 18:18:29 +00:00
ExpectExec ( regexp . QuoteMeta ( "DELETE FROM `pools` WHERE id = ? and repo_id = ?" ) ) .
WithArgs ( pool . ID , s . Fixtures . Repos [ 0 ] . ID ) .
2022-10-17 17:41:00 +03:00
WillReturnError ( fmt . Errorf ( "mocked deleting pool error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
2024-03-29 18:18:29 +00:00
err = s . StoreSQLMocked . DeleteEntityPool ( context . Background ( ) , entity , pool . ID )
2022-10-17 17:41:00 +03:00
s . Require ( ) . NotNil ( err )
2024-03-29 18:18:29 +00:00
s . Require ( ) . Equal ( "removing pool: mocked deleting pool error" , err . Error ( ) )
s . assertSQLMockExpectations ( )
2022-10-17 17:41:00 +03:00
}
2022-10-14 17:41:31 +03:00
func ( s * RepoTestSuite ) TestListRepoInstances ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-03-28 18:23:49 +00:00
pool , err := s . Store . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-14 17:41:31 +03:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create repo pool: %v" , err ) )
}
poolInstances := [ ] params . Instance { }
for i := 1 ; i <= 3 ; i ++ {
s . Fixtures . CreateInstanceParams . Name = fmt . Sprintf ( "test-repo-%d" , i )
instance , err := s . Store . CreateInstance ( context . Background ( ) , pool . ID , s . Fixtures . CreateInstanceParams )
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create instance: %s" , err ) )
}
poolInstances = append ( poolInstances , instance )
}
2024-03-29 18:18:29 +00:00
instances , err := s . Store . ListEntityInstances ( context . Background ( ) , entity )
2022-10-14 17:41:31 +03:00
s . Require ( ) . Nil ( err )
s . equalInstancesByID ( poolInstances , instances )
}
2022-10-17 17:41:00 +03:00
func ( s * RepoTestSuite ) TestListRepoInstancesInvalidRepoID ( ) {
2024-03-29 18:18:29 +00:00
entity := params . GithubEntity {
ID : "dummy-repo-id" ,
EntityType : params . GithubEntityTypeRepository ,
}
_ , err := s . Store . ListEntityInstances ( context . Background ( ) , entity )
2022-10-17 17:41:00 +03:00
s . Require ( ) . NotNil ( err )
2024-03-29 18:18:29 +00:00
s . Require ( ) . Equal ( "fetching entity: parsing id: invalid request" , err . Error ( ) )
2022-10-17 17:41:00 +03:00
}
2022-10-14 17:41:31 +03:00
func ( s * RepoTestSuite ) TestUpdateRepositoryPool ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Repos [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-03-28 18:23:49 +00:00
repoPool , err := s . Store . CreateEntityPool ( context . Background ( ) , entity , s . Fixtures . CreatePoolParams )
2022-10-14 17:41:31 +03:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create repo pool: %v" , err ) )
}
2024-03-29 18:18:29 +00:00
pool , err := s . Store . UpdateEntityPool ( context . Background ( ) , entity , repoPool . ID , s . Fixtures . UpdatePoolParams )
2022-10-14 17:41:31 +03:00
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( * s . Fixtures . UpdatePoolParams . MaxRunners , pool . MaxRunners )
s . Require ( ) . Equal ( * s . Fixtures . UpdatePoolParams . MinIdleRunners , pool . MinIdleRunners )
s . Require ( ) . Equal ( s . Fixtures . UpdatePoolParams . Image , pool . Image )
s . Require ( ) . Equal ( s . Fixtures . UpdatePoolParams . Flavor , pool . Flavor )
}
func ( s * RepoTestSuite ) TestUpdateRepositoryPoolInvalidRepoID ( ) {
2024-03-29 18:18:29 +00:00
entity := params . GithubEntity {
ID : "dummy-repo-id" ,
EntityType : params . GithubEntityTypeRepository ,
}
_ , err := s . Store . UpdateEntityPool ( context . Background ( ) , entity , "dummy-repo-id" , s . Fixtures . UpdatePoolParams )
2022-10-14 17:41:31 +03:00
s . Require ( ) . NotNil ( err )
2023-01-30 14:29:55 +00:00
s . Require ( ) . Equal ( "fetching pool: parsing id: invalid request" , err . Error ( ) )
2022-10-14 17:41:31 +03:00
}
2022-10-13 17:56:16 +03:00
func TestRepoTestSuite ( t * testing . T ) {
2023-04-10 00:03:49 +00:00
t . Parallel ( )
2022-10-13 17:56:16 +03:00
suite . Run ( t , new ( RepoTestSuite ) )
}