Skip to content

Commit

Permalink
Remove unused code, update test
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
  • Loading branch information
gabriel-samfira committed Mar 15, 2024
1 parent ac29af6 commit 206fe42
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 43 deletions.
6 changes: 5 additions & 1 deletion runner/enterprises_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprise() {
s.Require().Nil(err)
s.Require().Equal(s.Fixtures.CreateEnterpriseParams.Name, enterprise.Name)
s.Require().Equal(s.Fixtures.Credentials[s.Fixtures.CreateEnterpriseParams.CredentialsName].Name, enterprise.CredentialsName)
s.Require().Equal(params.PoolBalancerTypeRoundRobin, enterprise.PoolBalancerType)
}

func (s *EnterpriseTestSuite) TestCreateEnterpriseErrUnauthorized() {
Expand Down Expand Up @@ -294,13 +295,16 @@ func (s *EnterpriseTestSuite) TestUpdateEnterprise() {
s.Fixtures.PoolMgrCtrlMock.On("UpdateEnterprisePoolManager", s.Fixtures.AdminContext, mock.AnythingOfType("params.Enterprise")).Return(s.Fixtures.PoolMgrMock, nil)
s.Fixtures.PoolMgrMock.On("Status").Return(params.PoolManagerStatus{IsRunning: true}, nil)

org, err := s.Runner.UpdateEnterprise(s.Fixtures.AdminContext, s.Fixtures.StoreEnterprises["test-enterprise-1"].ID, s.Fixtures.UpdateRepoParams)
param := s.Fixtures.UpdateRepoParams
param.PoolBalancerType = params.PoolBalancerTypePack
org, err := s.Runner.UpdateEnterprise(s.Fixtures.AdminContext, s.Fixtures.StoreEnterprises["test-enterprise-1"].ID, param)

s.Fixtures.PoolMgrMock.AssertExpectations(s.T())
s.Fixtures.PoolMgrCtrlMock.AssertExpectations(s.T())
s.Require().Nil(err)
s.Require().Equal(s.Fixtures.UpdateRepoParams.CredentialsName, org.CredentialsName)
s.Require().Equal(s.Fixtures.UpdateRepoParams.WebhookSecret, org.WebhookSecret)
s.Require().Equal(params.PoolBalancerTypePack, org.PoolBalancerType)
}

func (s *EnterpriseTestSuite) TestUpdateEnterpriseErrUnauthorized() {
Expand Down
29 changes: 29 additions & 0 deletions runner/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,20 @@ func (s *OrgTestSuite) TestCreateOrganization() {
s.Require().Nil(err)
s.Require().Equal(s.Fixtures.CreateOrgParams.Name, org.Name)
s.Require().Equal(s.Fixtures.Credentials[s.Fixtures.CreateOrgParams.CredentialsName].Name, org.CredentialsName)
s.Require().Equal(params.PoolBalancerTypeRoundRobin, org.PoolBalancerType)
}

func (s *OrgTestSuite) TestCreateOrganizationPoolBalancerTypePack() {
s.Fixtures.CreateOrgParams.PoolBalancerType = params.PoolBalancerTypePack
s.Fixtures.PoolMgrMock.On("Start").Return(nil)
s.Fixtures.PoolMgrCtrlMock.On("CreateOrgPoolManager", s.Fixtures.AdminContext, mock.AnythingOfType("params.Organization"), s.Fixtures.Providers, s.Fixtures.Store).Return(s.Fixtures.PoolMgrMock, nil)

org, err := s.Runner.CreateOrganization(s.Fixtures.AdminContext, s.Fixtures.CreateOrgParams)

s.Fixtures.PoolMgrMock.AssertExpectations(s.T())
s.Fixtures.PoolMgrCtrlMock.AssertExpectations(s.T())
s.Require().Nil(err)
s.Require().Equal(params.PoolBalancerTypePack, org.PoolBalancerType)
}

func (s *OrgTestSuite) TestCreateOrganizationErrUnauthorized() {
Expand Down Expand Up @@ -303,6 +317,21 @@ func (s *OrgTestSuite) TestUpdateOrganization() {
s.Require().Equal(s.Fixtures.UpdateRepoParams.WebhookSecret, org.WebhookSecret)
}

func (s *OrgTestSuite) TestUpdateRepositoryBalancingType() {
s.Fixtures.UpdateRepoParams.PoolBalancerType = params.PoolBalancerTypePack
s.Fixtures.PoolMgrCtrlMock.On("UpdateOrgPoolManager", s.Fixtures.AdminContext, mock.AnythingOfType("params.Organization")).Return(s.Fixtures.PoolMgrMock, nil)
s.Fixtures.PoolMgrMock.On("Status").Return(params.PoolManagerStatus{IsRunning: true}, nil)

param := s.Fixtures.UpdateRepoParams
param.PoolBalancerType = params.PoolBalancerTypePack
org, err := s.Runner.UpdateOrganization(s.Fixtures.AdminContext, s.Fixtures.StoreOrgs["test-org-1"].ID, param)

s.Fixtures.PoolMgrMock.AssertExpectations(s.T())
s.Fixtures.PoolMgrCtrlMock.AssertExpectations(s.T())
s.Require().Nil(err)
s.Require().Equal(params.PoolBalancerTypePack, org.PoolBalancerType)
}

func (s *OrgTestSuite) TestUpdateOrganizationErrUnauthorized() {
_, err := s.Runner.UpdateOrganization(context.Background(), "dummy-org-id", s.Fixtures.UpdateRepoParams)

Expand Down
12 changes: 0 additions & 12 deletions runner/pool/enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,6 @@ func (e *enterprise) GetJITConfig(ctx context.Context, instance string, pool par
return ret, jitConfig.Runner, nil
}

func (e *enterprise) GithubCLI() common.GithubClient {
return e.ghcli
}

func (e *enterprise) PoolType() params.PoolType {
return params.EnterprisePool
}
Expand Down Expand Up @@ -370,14 +366,6 @@ func (e *enterprise) WebhookSecret() string {
return e.cfg.WebhookSecret
}

func (e *enterprise) FindPoolByTags(labels []string) (params.Pool, error) {
pool, err := e.store.FindEnterprisePoolByTags(e.ctx, e.id, labels)
if err != nil {
return params.Pool{}, errors.Wrap(err, "fetching suitable pool")
}
return pool, nil
}

func (e *enterprise) GetPoolByID(poolID string) (params.Pool, error) {
pool, err := e.store.GetEnterprisePool(e.ctx, e.id, poolID)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions runner/pool/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/cloudbase/garm/params"
"github.com/cloudbase/garm/runner/common"
)

type poolHelper interface {
Expand All @@ -35,16 +34,13 @@ type poolHelper interface {
UninstallHook(ctx context.Context, url string) error
GetHookInfo(ctx context.Context) (params.HookInfo, error)

GithubCLI() common.GithubClient

GetJITConfig(ctx context.Context, instanceName string, pool params.Pool, labels []string) (map[string]string, *github.Runner, error)

FetchDbInstances() ([]params.Instance, error)
ListPools() ([]params.Pool, error)
GithubURL() string
JwtToken() string
String() string
FindPoolByTags(labels []string) (params.Pool, error)
GetPoolByID(poolID string) (params.Pool, error)
ValidateOwner(job params.WorkflowJob) error
UpdateState(param params.UpdatePoolStateParams) error
Expand Down
12 changes: 0 additions & 12 deletions runner/pool/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,6 @@ func (o *organization) GetJITConfig(ctx context.Context, instance string, pool p
return ret, runner, nil
}

func (o *organization) GithubCLI() common.GithubClient {
return o.ghcli
}

func (o *organization) PoolType() params.PoolType {
return params.OrganizationPool
}
Expand Down Expand Up @@ -384,14 +380,6 @@ func (o *organization) WebhookSecret() string {
return o.cfg.WebhookSecret
}

func (o *organization) FindPoolByTags(labels []string) (params.Pool, error) {
pool, err := o.store.FindOrganizationPoolByTags(o.ctx, o.id, labels)
if err != nil {
return params.Pool{}, errors.Wrap(err, "fetching suitable pool")
}
return pool, nil
}

func (o *organization) GetPoolByID(poolID string) (params.Pool, error) {
pool, err := o.store.GetOrganizationPool(o.ctx, o.id, poolID)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion runner/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,15 @@ func (r *basePoolManager) Stop() error {
}

func (r *basePoolManager) RefreshState(param params.UpdatePoolStateParams) error {
return r.helper.UpdateState(param)
if err := r.helper.UpdateState(param); err != nil {
return fmt.Errorf("failed to update pool state: %w", err)
}
// Update the tools as soon as state is updated. This should revive a stopped pool manager
// or stop one if the supplied credentials are not okay.
if err := r.updateTools(); err != nil {
return fmt.Errorf("failed to update tools: %w", err)
}
return nil
}

func (r *basePoolManager) WebhookSecret() string {
Expand Down
12 changes: 0 additions & 12 deletions runner/pool/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ func (r *repository) GetJITConfig(ctx context.Context, instance string, pool par
return ret, runner, nil
}

func (r *repository) GithubCLI() common.GithubClient {
return r.ghcli
}

func (r *repository) PoolType() params.PoolType {
return params.RepositoryPool
}
Expand Down Expand Up @@ -341,14 +337,6 @@ func (r *repository) WebhookSecret() string {
return r.cfg.WebhookSecret
}

func (r *repository) FindPoolByTags(labels []string) (params.Pool, error) {
pool, err := r.store.FindRepositoryPoolByTags(r.ctx, r.id, labels)
if err != nil {
return params.Pool{}, errors.Wrap(err, "fetching suitable pool")
}
return pool, nil
}

func (r *repository) GetPoolByID(poolID string) (params.Pool, error) {
pool, err := r.store.GetRepositoryPool(r.ctx, r.id, poolID)
if err != nil {
Expand Down
39 changes: 38 additions & 1 deletion runner/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ func (s *RepoTestSuite) TestCreateRepository() {
s.Require().Equal(s.Fixtures.CreateRepoParams.Owner, repo.Owner)
s.Require().Equal(s.Fixtures.CreateRepoParams.Name, repo.Name)
s.Require().Equal(s.Fixtures.Credentials[s.Fixtures.CreateRepoParams.CredentialsName].Name, repo.CredentialsName)
s.Require().Equal(params.PoolBalancerTypeRoundRobin, repo.PoolBalancerType)
}

func (s *RepoTestSuite) TestCreareRepositoryPoolBalancerTypePack() {
// setup mocks expectations
s.Fixtures.PoolMgrMock.On("Start").Return(nil)
s.Fixtures.PoolMgrCtrlMock.On("CreateRepoPoolManager", s.Fixtures.AdminContext, mock.AnythingOfType("params.Repository"), s.Fixtures.Providers, s.Fixtures.Store).Return(s.Fixtures.PoolMgrMock, nil)

// call tested function
param := s.Fixtures.CreateRepoParams
param.PoolBalancerType = params.PoolBalancerTypePack
repo, err := s.Runner.CreateRepository(s.Fixtures.AdminContext, param)

// assertions
s.Fixtures.PoolMgrMock.AssertExpectations(s.T())
s.Fixtures.PoolMgrCtrlMock.AssertExpectations(s.T())
s.Require().Nil(err)
s.Require().Equal(param.Owner, repo.Owner)
s.Require().Equal(param.Name, repo.Name)
s.Require().Equal(s.Fixtures.Credentials[s.Fixtures.CreateRepoParams.CredentialsName].Name, repo.CredentialsName)
s.Require().Equal(params.PoolBalancerTypePack, repo.PoolBalancerType)
}

func (s *RepoTestSuite) TestCreateRepositoryErrUnauthorized() {
Expand Down Expand Up @@ -304,11 +325,27 @@ func (s *RepoTestSuite) TestUpdateRepository() {
s.Require().Nil(err)
s.Require().Equal(s.Fixtures.UpdateRepoParams.CredentialsName, repo.CredentialsName)
s.Require().Equal(s.Fixtures.UpdateRepoParams.WebhookSecret, repo.WebhookSecret)
s.Require().Equal(params.PoolBalancerTypeRoundRobin, repo.PoolBalancerType)
}

func (s *RepoTestSuite) TestUpdateRepositoryBalancingType() {
s.Fixtures.PoolMgrCtrlMock.On("UpdateRepoPoolManager", s.Fixtures.AdminContext, mock.AnythingOfType("params.Repository")).Return(s.Fixtures.PoolMgrMock, nil)
s.Fixtures.PoolMgrMock.On("Status").Return(params.PoolManagerStatus{IsRunning: true}, nil)

updateRepoParams := s.Fixtures.UpdateRepoParams
updateRepoParams.PoolBalancerType = params.PoolBalancerTypePack
repo, err := s.Runner.UpdateRepository(s.Fixtures.AdminContext, s.Fixtures.StoreRepos["test-repo-1"].ID, updateRepoParams)

s.Fixtures.PoolMgrCtrlMock.AssertExpectations(s.T())
s.Fixtures.PoolMgrMock.AssertExpectations(s.T())
s.Require().Nil(err)
s.Require().Equal(updateRepoParams.CredentialsName, repo.CredentialsName)
s.Require().Equal(updateRepoParams.WebhookSecret, repo.WebhookSecret)
s.Require().Equal(params.PoolBalancerTypePack, repo.PoolBalancerType)
}

func (s *RepoTestSuite) TestUpdateRepositoryErrUnauthorized() {
_, err := s.Runner.UpdateRepository(context.Background(), "dummy-repo-id", s.Fixtures.UpdateRepoParams)

s.Require().Equal(runnerErrors.ErrUnauthorized, err)
}

Expand Down

0 comments on commit 206fe42

Please sign in to comment.