2022-12-31 22:39:55 +00: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.enterprise/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"
"flag"
"fmt"
"regexp"
"sort"
2023-01-18 17:27:53 +01:00
"testing"
2022-12-31 22:39:55 +00:00
"github.com/stretchr/testify/suite"
"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
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
dbCommon "github.com/cloudbase/garm/database/common"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
2022-12-31 22:39:55 +00:00
)
type EnterpriseTestFixtures struct {
Enterprises [ ] params . Enterprise
CreateEnterpriseParams params . CreateEnterpriseParams
CreatePoolParams params . CreatePoolParams
CreateInstanceParams params . CreateInstanceParams
2023-07-04 23:47:55 +00:00
UpdateRepoParams params . UpdateEntityParams
2022-12-31 22:39:55 +00:00
UpdatePoolParams params . UpdatePoolParams
SQLMock sqlmock . Sqlmock
}
type EnterpriseTestSuite struct {
suite . Suite
Store dbCommon . Store
StoreSQLMocked * sqlDatabase
Fixtures * EnterpriseTestFixtures
2024-04-16 17:05:18 +00:00
adminCtx context . Context
testCreds params . GithubCredentials
secondaryTestCreds params . GithubCredentials
githubEndpoint params . GithubEndpoint
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) equalInstancesByName ( expected , actual [ ] params . Instance ) {
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 )
}
}
func ( s * EnterpriseTestSuite ) assertSQLMockExpectations ( ) {
err := s . Fixtures . SQLMock . ExpectationsWereMet ( )
if err != nil {
s . FailNow ( fmt . Sprintf ( "failed to meet sqlmock expectations, got error: %v" , err ) )
}
}
func ( s * EnterpriseTestSuite ) 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
2024-04-16 17:05:18 +00:00
adminCtx := garmTesting . ImpersonateAdminContext ( context . Background ( ) , db , s . T ( ) )
s . adminCtx = adminCtx
s . githubEndpoint = garmTesting . CreateDefaultGithubEndpoint ( adminCtx , db , s . T ( ) )
s . testCreds = garmTesting . CreateTestGithubCredentials ( adminCtx , "new-creds" , db , s . T ( ) , s . githubEndpoint )
s . secondaryTestCreds = garmTesting . CreateTestGithubCredentials ( adminCtx , "secondary-creds" , db , s . T ( ) , s . githubEndpoint )
2022-12-31 22:39:55 +00:00
// create some enterprise objects in the database, for testing purposes
enterprises := [ ] params . Enterprise { }
for i := 1 ; i <= 3 ; i ++ {
enterprise , err := db . CreateEnterprise (
2024-04-16 17:05:18 +00:00
s . adminCtx ,
2022-12-31 22:39:55 +00:00
fmt . Sprintf ( "test-enterprise-%d" , i ) ,
2024-04-16 17:05:18 +00:00
s . testCreds . Name ,
2022-12-31 22:39:55 +00:00
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-12-31 22:39:55 +00:00
)
if err != nil {
2024-04-16 17:05:18 +00:00
s . FailNow ( fmt . Sprintf ( "failed to create database object (test-enterprise-%d): %q" , i , err ) )
2022-12-31 22:39:55 +00:00
}
enterprises = append ( enterprises , enterprise )
}
// 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-12-31 22:39:55 +00: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 ( ) ) ,
}
// setup test fixtures
var maxRunners uint = 30
var minIdleRunners uint = 10
fixtures := & EnterpriseTestFixtures {
Enterprises : enterprises ,
CreateEnterpriseParams : params . CreateEnterpriseParams {
Name : "new-test-enterprise" ,
2024-04-16 17:05:18 +00:00
CredentialsName : s . testCreds . Name ,
2022-12-31 22:39:55 +00:00
WebhookSecret : "new-webhook-secret" ,
} ,
CreatePoolParams : params . CreatePoolParams {
ProviderName : "test-provider" ,
MaxRunners : 3 ,
MinIdleRunners : 1 ,
2023-03-31 15:03:08 +00:00
Enabled : true ,
2022-12-31 22:39:55 +00:00
Image : "test-image" ,
Flavor : "test-flavor" ,
OSType : "linux" ,
OSArch : "amd64" ,
Tags : [ ] string { "self-hosted" , "arm64" , "linux" } ,
} ,
CreateInstanceParams : params . CreateInstanceParams {
Name : "test-instance-name" ,
OSType : "linux" ,
} ,
2023-07-04 23:47:55 +00:00
UpdateRepoParams : params . UpdateEntityParams {
2024-04-16 17:05:18 +00:00
CredentialsName : s . secondaryTestCreds . Name ,
2022-12-31 22:39:55 +00:00
WebhookSecret : "test-update-repo-webhook-secret" ,
} ,
UpdatePoolParams : params . UpdatePoolParams {
MaxRunners : & maxRunners ,
MinIdleRunners : & minIdleRunners ,
Image : "test-update-image" ,
Flavor : "test-update-flavor" ,
} ,
SQLMock : sqlMock ,
}
s . Fixtures = fixtures
}
func ( s * EnterpriseTestSuite ) TestCreateEnterprise ( ) {
// call tested function
enterprise , err := s . Store . CreateEnterprise (
2024-04-16 17:05:18 +00:00
s . adminCtx ,
2022-12-31 22:39:55 +00:00
s . Fixtures . CreateEnterpriseParams . Name ,
s . Fixtures . CreateEnterpriseParams . CredentialsName ,
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
s . Fixtures . CreateEnterpriseParams . WebhookSecret ,
params . PoolBalancerTypeRoundRobin )
2022-12-31 22:39:55 +00:00
// assertions
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
storeEnterprise , err := s . Store . GetEnterpriseByID ( s . adminCtx , enterprise . ID )
2022-12-31 22:39:55 +00:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "failed to get enterprise by id: %v" , err ) )
}
s . Require ( ) . Equal ( storeEnterprise . Name , enterprise . Name )
2024-04-15 08:32:19 +00:00
s . Require ( ) . Equal ( storeEnterprise . Credentials . Name , enterprise . Credentials . Name )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Equal ( storeEnterprise . WebhookSecret , enterprise . WebhookSecret )
}
func ( s * EnterpriseTestSuite ) TestCreateEnterpriseInvalidDBPassphrase ( ) {
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-12-31 22:39:55 +00:00
sqlDB := & sqlDatabase {
conn : conn ,
cfg : cfg ,
}
_ , err = sqlDB . CreateEnterprise (
2024-04-16 17:05:18 +00:00
s . adminCtx ,
2022-12-31 22:39:55 +00:00
s . Fixtures . CreateEnterpriseParams . Name ,
s . Fixtures . CreateEnterpriseParams . CredentialsName ,
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
s . Fixtures . CreateEnterpriseParams . WebhookSecret ,
params . PoolBalancerTypeRoundRobin )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
2023-01-18 17:27:53 +01:00
s . Require ( ) . Equal ( "encoding secret: invalid passphrase length (expected length 32 characters)" , err . Error ( ) )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestCreateEnterpriseDBCreateErr ( ) {
s . Fixtures . SQLMock . ExpectBegin ( )
2024-04-16 17:05:18 +00:00
s . Fixtures . SQLMock .
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `github_credentials` WHERE name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT 1" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . CredentialsName ) .
2024-04-18 16:50:46 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" , "endpoint_name" } ) . AddRow ( s . testCreds . ID , s . testCreds . Endpoint . Name ) )
2024-04-17 12:10:00 +00:00
s . Fixtures . SQLMock . ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL" ) ) .
2024-04-18 16:50:46 +00:00
WithArgs ( s . testCreds . Endpoint . Name ) .
2024-04-17 12:10:00 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "name" } ) .
2024-04-18 16:50:46 +00:00
AddRow ( s . testCreds . Endpoint . Name ) )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "INSERT INTO `enterprises`" ) ) .
WillReturnError ( fmt . Errorf ( "creating enterprise mock error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
_ , err := s . StoreSQLMocked . CreateEnterprise (
2024-04-16 17:05:18 +00:00
s . adminCtx ,
2022-12-31 22:39:55 +00:00
s . Fixtures . CreateEnterpriseParams . Name ,
s . Fixtures . CreateEnterpriseParams . CredentialsName ,
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
s . Fixtures . CreateEnterpriseParams . WebhookSecret ,
params . PoolBalancerTypeRoundRobin )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
2024-04-16 17:05:18 +00:00
s . Require ( ) . Equal ( "creating enterprise: creating enterprise: creating enterprise mock error" , err . Error ( ) )
s . assertSQLMockExpectations ( )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestGetEnterprise ( ) {
2024-04-16 17:05:18 +00:00
enterprise , err := s . Store . GetEnterprise ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . Name )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( s . Fixtures . Enterprises [ 0 ] . Name , enterprise . Name )
s . Require ( ) . Equal ( s . Fixtures . Enterprises [ 0 ] . ID , enterprise . ID )
}
func ( s * EnterpriseTestSuite ) TestGetEnterpriseCaseInsensitive ( ) {
2024-04-16 17:05:18 +00:00
enterprise , err := s . Store . GetEnterprise ( s . adminCtx , "TeSt-eNtErPriSe-1" )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( "test-enterprise-1" , enterprise . Name )
}
func ( s * EnterpriseTestSuite ) TestGetEnterpriseNotFound ( ) {
2024-04-16 17:05:18 +00:00
_ , err := s . Store . GetEnterprise ( s . adminCtx , "dummy-name" )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching enterprise: not found" , err . Error ( ) )
}
func ( s * EnterpriseTestSuite ) TestGetEnterpriseDBDecryptingErr ( ) {
2023-01-23 17:43:32 +01:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE name = ? COLLATE NOCASE AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . Name , 1 ) .
2023-01-23 17:43:32 +01:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "name" } ) . AddRow ( s . Fixtures . Enterprises [ 0 ] . Name ) )
2022-12-31 22:39:55 +00:00
2024-04-16 17:05:18 +00:00
_ , err := s . StoreSQLMocked . GetEnterprise ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . Name )
2022-12-31 22:39:55 +00:00
2023-01-23 17:43:32 +01:00
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching enterprise: missing secret" , err . Error ( ) )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestListEnterprises ( ) {
2024-04-16 17:05:18 +00:00
enterprises , err := s . Store . ListEnterprises ( s . adminCtx )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
garmTesting . EqualDBEntityByName ( s . T ( ) , s . Fixtures . Enterprises , enterprises )
}
func ( s * EnterpriseTestSuite ) TestListEnterprisesDBFetchErr ( ) {
s . Fixtures . SQLMock .
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE `enterprises`.`deleted_at` IS NULL" ) ) .
WillReturnError ( fmt . Errorf ( "fetching user from database mock error" ) )
2024-04-16 17:05:18 +00:00
_ , err := s . StoreSQLMocked . ListEnterprises ( s . adminCtx )
2022-12-31 22:39:55 +00:00
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
2023-01-18 17:27:53 +01:00
s . Require ( ) . Equal ( "fetching enterprises: fetching user from database mock error" , err . Error ( ) )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestDeleteEnterprise ( ) {
2024-04-16 17:05:18 +00:00
err := s . Store . DeleteEnterprise ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
_ , err = s . Store . GetEnterpriseByID ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching enterprise: not found" , err . Error ( ) )
}
func ( s * EnterpriseTestSuite ) TestDeleteEnterpriseInvalidEnterpriseID ( ) {
2024-04-16 17:05:18 +00:00
err := s . Store . DeleteEnterprise ( s . adminCtx , "dummy-enterprise-id" )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching enterprise: parsing id: invalid request" , err . Error ( ) )
}
func ( s * EnterpriseTestSuite ) TestDeleteEnterpriseDBDeleteErr ( ) {
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Enterprises [ 0 ] . ID ) )
s . Fixtures . SQLMock . ExpectBegin ( )
s . Fixtures . SQLMock .
ExpectExec ( regexp . QuoteMeta ( "DELETE FROM `enterprises`" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID ) .
WillReturnError ( fmt . Errorf ( "mocked delete enterprise error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
2024-04-16 17:05:18 +00:00
err := s . StoreSQLMocked . DeleteEnterprise ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "deleting enterprise: mocked delete enterprise error" , err . Error ( ) )
2024-04-16 17:05:18 +00:00
s . assertSQLMockExpectations ( )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestUpdateEnterprise ( ) {
2024-04-16 17:05:18 +00:00
enterprise , err := s . Store . UpdateEnterprise ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID , s . Fixtures . UpdateRepoParams )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
2024-04-15 08:32:19 +00:00
s . Require ( ) . Equal ( s . Fixtures . UpdateRepoParams . CredentialsName , enterprise . Credentials . Name )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Equal ( s . Fixtures . UpdateRepoParams . WebhookSecret , enterprise . WebhookSecret )
}
func ( s * EnterpriseTestSuite ) TestUpdateEnterpriseInvalidEnterpriseID ( ) {
2024-04-16 17:05:18 +00:00
_ , err := s . Store . UpdateEnterprise ( s . adminCtx , "dummy-enterprise-id" , s . Fixtures . UpdateRepoParams )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
2024-04-16 17:05:18 +00:00
s . Require ( ) . Equal ( "updating enterprise: fetching enterprise: parsing id: invalid request" , err . Error ( ) )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestUpdateEnterpriseDBEncryptErr ( ) {
2024-02-22 17:20:05 +01:00
s . StoreSQLMocked . cfg . Passphrase = wrongPassphrase
2024-04-16 17:05:18 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2024-04-17 12:10:00 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" , "endpoint_name" } ) .
AddRow ( s . Fixtures . Enterprises [ 0 ] . ID , s . Fixtures . Enterprises [ 0 ] . Endpoint . Name ) )
2024-04-16 17:05:18 +00:00
s . Fixtures . SQLMock .
2024-04-17 12:10:00 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `github_credentials` WHERE name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?" ) ) .
WithArgs ( s . secondaryTestCreds . Name , 1 ) .
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" , "endpoint_name" } ) .
2024-04-18 16:50:46 +00:00
AddRow ( s . secondaryTestCreds . ID , s . secondaryTestCreds . Endpoint . Name ) )
2024-04-17 12:10:00 +00:00
s . Fixtures . SQLMock . ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL" ) ) .
2024-04-18 16:50:46 +00:00
WithArgs ( s . testCreds . Endpoint . Name ) .
2024-04-17 12:10:00 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "name" } ) .
2024-04-18 16:50:46 +00:00
AddRow ( s . secondaryTestCreds . Endpoint . Name ) )
2024-04-16 17:05:18 +00:00
s . Fixtures . SQLMock . ExpectRollback ( )
2022-12-31 22:39:55 +00:00
2024-04-16 17:05:18 +00:00
_ , err := s . StoreSQLMocked . UpdateEnterprise ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID , s . Fixtures . UpdateRepoParams )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
2024-04-16 17:05:18 +00:00
s . Require ( ) . Equal ( "updating enterprise: encoding secret: invalid passphrase length (expected length 32 characters)" , err . Error ( ) )
s . assertSQLMockExpectations ( )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestUpdateEnterpriseDBSaveErr ( ) {
2024-04-16 17:05:18 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2024-04-17 12:10:00 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" , "endpoint_name" } ) .
AddRow ( s . Fixtures . Enterprises [ 0 ] . ID , s . Fixtures . Enterprises [ 0 ] . Endpoint . Name ) )
2024-04-16 17:05:18 +00:00
s . Fixtures . SQLMock .
2024-04-17 12:10:00 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `github_credentials` WHERE name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?" ) ) .
WithArgs ( s . secondaryTestCreds . Name , 1 ) .
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" , "endpoint_name" } ) .
2024-04-18 16:50:46 +00:00
AddRow ( s . secondaryTestCreds . ID , s . secondaryTestCreds . Endpoint . Name ) )
2024-04-17 12:10:00 +00:00
s . Fixtures . SQLMock . ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL" ) ) .
2024-04-18 16:50:46 +00:00
WithArgs ( s . testCreds . Endpoint . Name ) .
2024-04-17 12:10:00 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "name" } ) .
2024-04-18 16:50:46 +00:00
AddRow ( s . secondaryTestCreds . Endpoint . Name ) )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
ExpectExec ( ( "UPDATE `enterprises` SET" ) ) .
WillReturnError ( fmt . Errorf ( "saving enterprise mock error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
2024-04-16 17:05:18 +00:00
_ , err := s . StoreSQLMocked . UpdateEnterprise ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID , s . Fixtures . UpdateRepoParams )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
2024-04-16 17:05:18 +00:00
s . Require ( ) . Equal ( "updating enterprise: saving enterprise: saving enterprise mock error" , err . Error ( ) )
s . assertSQLMockExpectations ( )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestUpdateEnterpriseDBDecryptingErr ( ) {
2024-02-22 17:20:05 +01:00
s . StoreSQLMocked . cfg . Passphrase = wrongPassphrase
s . Fixtures . UpdateRepoParams . WebhookSecret = webhookSecret
2023-01-23 17:43:32 +01:00
2024-04-16 17:05:18 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2023-01-23 17:43:32 +01:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2024-04-17 12:10:00 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" , "endpoint_name" } ) .
AddRow ( s . Fixtures . Enterprises [ 0 ] . ID , s . Fixtures . Enterprises [ 0 ] . Endpoint . Name ) )
2024-04-16 17:05:18 +00:00
s . Fixtures . SQLMock .
2024-04-17 12:10:00 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `github_credentials` WHERE name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?" ) ) .
WithArgs ( s . secondaryTestCreds . Name , 1 ) .
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" , "endpoint_name" } ) .
2024-04-18 16:50:46 +00:00
AddRow ( s . secondaryTestCreds . ID , s . secondaryTestCreds . Endpoint . Name ) )
2024-04-17 12:10:00 +00:00
s . Fixtures . SQLMock . ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL" ) ) .
2024-04-18 16:50:46 +00:00
WithArgs ( s . testCreds . Endpoint . Name ) .
2024-04-17 12:10:00 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "name" } ) .
2024-04-18 16:50:46 +00:00
AddRow ( s . secondaryTestCreds . Endpoint . Name ) )
2024-04-16 17:05:18 +00:00
s . Fixtures . SQLMock . ExpectRollback ( )
2023-01-23 17:43:32 +01:00
2024-04-16 17:05:18 +00:00
_ , err := s . StoreSQLMocked . UpdateEnterprise ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID , s . Fixtures . UpdateRepoParams )
2023-01-23 17:43:32 +01:00
s . Require ( ) . NotNil ( err )
2024-04-16 17:05:18 +00:00
s . Require ( ) . Equal ( "updating enterprise: encoding secret: invalid passphrase length (expected length 32 characters)" , err . Error ( ) )
s . assertSQLMockExpectations ( )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestGetEnterpriseByID ( ) {
2024-04-16 17:05:18 +00:00
enterprise , err := s . Store . GetEnterpriseByID ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( s . Fixtures . Enterprises [ 0 ] . ID , enterprise . ID )
}
func ( s * EnterpriseTestSuite ) TestGetEnterpriseByIDInvalidEnterpriseID ( ) {
2024-04-16 17:05:18 +00:00
_ , err := s . Store . GetEnterpriseByID ( s . adminCtx , "dummy-enterprise-id" )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching enterprise: parsing id: invalid request" , err . Error ( ) )
}
func ( s * EnterpriseTestSuite ) TestGetEnterpriseByIDDBDecryptingErr ( ) {
2023-01-23 17:43:32 +01:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2023-01-23 17:43:32 +01:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Enterprises [ 0 ] . ID ) )
s . Fixtures . SQLMock .
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `pools` WHERE `pools`.`enterprise_id` = ? AND `pools`.`deleted_at` IS NULL" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID ) .
WillReturnRows ( sqlmock . NewRows ( [ ] string { "enterprise_id" } ) . AddRow ( s . Fixtures . Enterprises [ 0 ] . ID ) )
2024-04-16 17:05:18 +00:00
_ , err := s . StoreSQLMocked . GetEnterpriseByID ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID )
2023-01-23 17:43:32 +01:00
s . assertSQLMockExpectations ( )
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "fetching enterprise: missing secret" , err . Error ( ) )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestCreateEnterprisePool ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
pool , err := s . Store . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
enterprise , err := s . Store . GetEnterpriseByID ( s . adminCtx , s . Fixtures . Enterprises [ 0 ] . ID )
2022-12-31 22:39:55 +00:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot get enterprise by ID: %v" , err ) )
}
s . Require ( ) . Equal ( 1 , len ( enterprise . Pools ) )
s . Require ( ) . Equal ( pool . ID , enterprise . Pools [ 0 ] . ID )
s . Require ( ) . Equal ( s . Fixtures . CreatePoolParams . ProviderName , enterprise . Pools [ 0 ] . ProviderName )
s . Require ( ) . Equal ( s . Fixtures . CreatePoolParams . MaxRunners , enterprise . Pools [ 0 ] . MaxRunners )
s . Require ( ) . Equal ( s . Fixtures . CreatePoolParams . MinIdleRunners , enterprise . Pools [ 0 ] . MinIdleRunners )
}
func ( s * EnterpriseTestSuite ) TestCreateEnterprisePoolMissingTags ( ) {
s . Fixtures . CreatePoolParams . Tags = [ ] string { }
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
_ , err = s . Store . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( "no tags specified" , err . Error ( ) )
}
func ( s * EnterpriseTestSuite ) TestCreateEnterprisePoolInvalidEnterpriseID ( ) {
2024-03-28 18:23:49 +00:00
entity := params . GithubEntity {
ID : "dummy-enterprise-id" ,
EntityType : params . GithubEntityTypeEnterprise ,
}
2024-04-16 17:05:18 +00:00
_ , err := s . Store . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
2024-03-28 18:23:49 +00:00
s . Require ( ) . Equal ( "parsing id: invalid request" , err . Error ( ) )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestCreateEnterprisePoolDBCreateErr ( ) {
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Enterprises [ 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 enterprise_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-12-31 22:39:55 +00:00
WillReturnError ( fmt . Errorf ( "mocked creating pool error" ) )
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
_ , err = s . StoreSQLMocked . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestCreateEnterpriseDBPoolAlreadyExistErr ( ) {
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Enterprises [ 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 enterprise_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-12-31 22:39:55 +00: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 . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "enterprise_id" , "provider_name" , "image" , "flavor" } ) .
AddRow (
s . Fixtures . Enterprises [ 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 . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
_ , err = s . StoreSQLMocked . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
s . Require ( ) . Equal ( runnerErrors . NewConflictError ( "pool with the same image and flavor already exists on this provider" ) , err )
2024-03-29 18:18:29 +00:00
s . assertSQLMockExpectations ( )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestCreateEnterprisePoolDBFetchTagErr ( ) {
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Enterprises [ 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 enterprise_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-12-31 22:39:55 +00: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 . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "enterprise_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-12-31 22:39:55 +00:00
WillReturnError ( fmt . Errorf ( "mocked fetching tag error" ) )
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
_ , err = s . StoreSQLMocked . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestCreateEnterprisePoolDBAddingPoolErr ( ) {
s . Fixtures . CreatePoolParams . Tags = [ ] string { "linux" }
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Enterprises [ 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 enterprise_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-12-31 22:39:55 +00: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 . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "enterprise_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-12-31 22:39:55 +00: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 . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
_ , err = s . StoreSQLMocked . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestCreateEnterprisePoolDBSaveTagErr ( ) {
s . Fixtures . CreatePoolParams . Tags = [ ] string { "linux" }
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Enterprises [ 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 enterprise_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-12-31 22:39:55 +00: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 . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "enterprise_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-12-31 22:39:55 +00: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 . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
_ , err = s . StoreSQLMocked . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestCreateEnterprisePoolDBFetchPoolErr ( ) {
s . Fixtures . CreatePoolParams . Tags = [ ] string { "linux" }
2024-03-29 18:18:29 +00:00
s . Fixtures . SQLMock . ExpectBegin ( )
2022-12-31 22:39:55 +00:00
s . Fixtures . SQLMock .
2024-04-22 13:38:51 +00:00
ExpectQuery ( regexp . QuoteMeta ( "SELECT * FROM `enterprises` WHERE id = ? AND `enterprises`.`deleted_at` IS NULL ORDER BY `enterprises`.`id` LIMIT ?" ) ) .
WithArgs ( s . Fixtures . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) . AddRow ( s . Fixtures . Enterprises [ 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 enterprise_id = ?) AND `pools`.`deleted_at` IS NULL ORDER BY `pools`.`id` LIMIT ?" ) ) .
2022-12-31 22:39:55 +00: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 . Enterprises [ 0 ] . ID , 1 ) .
2022-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "enterprise_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-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
WillReturnRows ( sqlmock . NewRows ( [ ] string { "id" } ) )
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
_ , err = s . StoreSQLMocked . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestListEnterprisePools ( ) {
enterprisePools := [ ] params . Pool { }
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2022-12-31 22:39:55 +00:00
for i := 1 ; i <= 2 ; i ++ {
s . Fixtures . CreatePoolParams . Flavor = fmt . Sprintf ( "test-flavor-%v" , i )
2024-04-16 17:05:18 +00:00
pool , err := s . Store . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create enterprise pool: %v" , err ) )
}
enterprisePools = append ( enterprisePools , pool )
}
2024-04-16 17:05:18 +00:00
pools , err := s . Store . ListEntityPools ( s . adminCtx , entity )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
garmTesting . EqualDBEntityID ( s . T ( ) , enterprisePools , pools )
}
func ( s * EnterpriseTestSuite ) TestListEnterprisePoolsInvalidEnterpriseID ( ) {
2024-03-29 18:18:29 +00:00
entity := params . GithubEntity {
ID : "dummy-enterprise-id" ,
EntityType : params . GithubEntityTypeEnterprise ,
}
2024-04-16 17:05:18 +00:00
_ , err := s . Store . ListEntityPools ( s . adminCtx , entity )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestGetEnterprisePool ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
pool , err := s . Store . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create enterprise pool: %v" , err ) )
}
2024-04-16 17:05:18 +00:00
enterprisePool , err := s . Store . GetEntityPool ( s . adminCtx , entity , pool . ID )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
s . Require ( ) . Equal ( enterprisePool . ID , pool . ID )
}
func ( s * EnterpriseTestSuite ) TestGetEnterprisePoolInvalidEnterpriseID ( ) {
2024-03-28 18:23:49 +00:00
entity := params . GithubEntity {
ID : "dummy-enterprise-id" ,
EntityType : params . GithubEntityTypeEnterprise ,
}
2024-04-16 17:05:18 +00:00
_ , err := s . Store . GetEntityPool ( s . adminCtx , entity , "dummy-pool-id" )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestDeleteEnterprisePool ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
pool , err := s . Store . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create enterprise pool: %v" , err ) )
}
2024-04-16 17:05:18 +00:00
err = s . Store . DeleteEntityPool ( s . adminCtx , entity , pool . ID )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
_ , err = s . Store . GetEntityPool ( s . adminCtx , entity , pool . ID )
2023-01-30 00:40:01 +00:00
s . Require ( ) . Equal ( "fetching pool: finding pool: not found" , err . Error ( ) )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestDeleteEnterprisePoolInvalidEnterpriseID ( ) {
2024-03-28 18:23:49 +00:00
entity := params . GithubEntity {
ID : "dummy-enterprise-id" ,
EntityType : params . GithubEntityTypeEnterprise ,
}
2024-04-16 17:05:18 +00:00
err := s . Store . DeleteEntityPool ( s . adminCtx , entity , "dummy-pool-id" )
2022-12-31 22:39:55 +00:00
s . Require ( ) . NotNil ( err )
2024-03-28 18:23:49 +00:00
s . Require ( ) . Equal ( "parsing id: invalid request" , err . Error ( ) )
2022-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestDeleteEnterprisePoolDBDeleteErr ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
pool , err := s . Store . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create enterprise 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 enterprise_id = ?" ) ) .
WithArgs ( pool . ID , s . Fixtures . Enterprises [ 0 ] . ID ) .
2022-12-31 22:39:55 +00:00
WillReturnError ( fmt . Errorf ( "mocked deleting pool error" ) )
s . Fixtures . SQLMock . ExpectRollback ( )
2024-04-16 17:05:18 +00:00
err = s . StoreSQLMocked . DeleteEntityPool ( s . adminCtx , entity , pool . ID )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestListEnterpriseInstances ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
pool , err := s . Store . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create enterprise pool: %v" , err ) )
}
poolInstances := [ ] params . Instance { }
for i := 1 ; i <= 3 ; i ++ {
s . Fixtures . CreateInstanceParams . Name = fmt . Sprintf ( "test-enterprise-%v" , i )
2024-04-16 17:05:18 +00:00
instance , err := s . Store . CreateInstance ( s . adminCtx , pool . ID , s . Fixtures . CreateInstanceParams )
2022-12-31 22:39:55 +00:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create instance: %s" , err ) )
}
poolInstances = append ( poolInstances , instance )
}
2024-04-16 17:05:18 +00:00
instances , err := s . Store . ListEntityInstances ( s . adminCtx , entity )
2022-12-31 22:39:55 +00:00
s . Require ( ) . Nil ( err )
s . equalInstancesByName ( poolInstances , instances )
}
func ( s * EnterpriseTestSuite ) TestListEnterpriseInstancesInvalidEnterpriseID ( ) {
2024-03-29 18:18:29 +00:00
entity := params . GithubEntity {
ID : "dummy-enterprise-id" ,
EntityType : params . GithubEntityTypeEnterprise ,
}
2024-04-16 17:05:18 +00:00
_ , err := s . Store . ListEntityInstances ( s . adminCtx , entity )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func ( s * EnterpriseTestSuite ) TestUpdateEnterprisePool ( ) {
2024-03-29 18:18:29 +00:00
entity , err := s . Fixtures . Enterprises [ 0 ] . GetEntity ( )
s . Require ( ) . Nil ( err )
2024-04-16 17:05:18 +00:00
pool , err := s . Store . CreateEntityPool ( s . adminCtx , entity , s . Fixtures . CreatePoolParams )
2022-12-31 22:39:55 +00:00
if err != nil {
s . FailNow ( fmt . Sprintf ( "cannot create enterprise pool: %v" , err ) )
}
2024-04-16 17:05:18 +00:00
pool , err = s . Store . UpdateEntityPool ( s . adminCtx , entity , pool . ID , s . Fixtures . UpdatePoolParams )
2022-12-31 22:39:55 +00: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 * EnterpriseTestSuite ) TestUpdateEnterprisePoolInvalidEnterpriseID ( ) {
2024-03-29 18:18:29 +00:00
entity := params . GithubEntity {
ID : "dummy-enterprise-id" ,
EntityType : params . GithubEntityTypeEnterprise ,
}
2024-04-16 17:05:18 +00:00
_ , err := s . Store . UpdateEntityPool ( s . adminCtx , entity , "dummy-pool-id" , s . Fixtures . UpdatePoolParams )
2022-12-31 22:39:55 +00: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-12-31 22:39:55 +00:00
}
func TestEnterpriseTestSuite ( t * testing . T ) {
2023-04-10 00:03:49 +00:00
t . Parallel ( )
2022-12-31 22:39:55 +00:00
suite . Run ( t , new ( EnterpriseTestSuite ) )
}