Skip to content

Commit

Permalink
make copy of receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
rverdile committed Nov 13, 2023
1 parent e7a3cd6 commit 4279018
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
5 changes: 3 additions & 2 deletions pkg/dao/repository_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func DBErrorToApi(e error) *ce.DaoError {
}

func (r *repositoryConfigDaoImpl) WithContext(ctx context.Context) RepositoryConfigDao {
r.ctx = ctx
return r
cpy := *r
cpy.ctx = ctx
return &cpy
}

func (r repositoryConfigDaoImpl) Create(newRepoReq api.RepositoryRequest) (api.RepositoryResponse, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/dao/repository_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (suite *RepositoryConfigSuite) TestCreateRedHatRepository() {
config.El9,
},
}
dao := GetRepositoryConfigDao(suite.tx)
dao := GetRepositoryConfigDao(suite.tx, suite.mockPulpClient)
_, err := dao.Create(toCreate)
assert.ErrorContains(suite.T(), err, "Creating of Red Hat repositories is not permitted")
}
Expand Down Expand Up @@ -1446,7 +1446,7 @@ func (suite *RepositoryConfigSuite) TestBulkDeleteOneNotFound() {

func (suite *RepositoryConfigSuite) TestBulkDeleteRedhatRepository() {
t := suite.T()
dao := GetRepositoryConfigDao(suite.tx)
dao := GetRepositoryConfigDao(suite.tx, suite.mockPulpClient)
orgID := config.RedHatOrg
repoConfigCount := 5

Expand Down
5 changes: 3 additions & 2 deletions pkg/dao/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func GetSnapshotDao(db *gorm.DB) SnapshotDao {
}

func (sDao *snapshotDaoImpl) WithContext(ctx context.Context) SnapshotDao {
sDao.ctx = ctx
return sDao
cpy := *sDao
cpy.ctx = ctx
return &cpy
}

// Create records a snapshot of a repository
Expand Down
38 changes: 19 additions & 19 deletions pkg/dao/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ func (s *SnapshotsSuite) TestCreateAndList() {
tx := s.tx

mockPulpClient := pulp_client.NewMockPulpClient(t)
sDao := snapshotDaoImpl{db: tx, pulpClient: mockPulpClient}
sDao.WithContext(context.Background())
sDaoImpl := snapshotDaoImpl{db: tx, pulpClient: mockPulpClient}
sDao := sDaoImpl.WithContext(context.Background())

mockPulpClient.WithContextMock().On("GetContentPath").Return(testContentPath, nil)
mockPulpClient.WithContextMock().WithDomainMock().On("GetContentPath").Return(testContentPath, nil)

repoDao := repositoryConfigDaoImpl{db: tx, yumRepo: &mockExt.YumRepositoryMock{}, pulpClient: mockPulpClient}
repoDao.WithContext(context.Background())
repoDaoImpl := repositoryConfigDaoImpl{db: tx, yumRepo: &mockExt.YumRepositoryMock{}, pulpClient: mockPulpClient}
repoDao := repoDaoImpl.WithContext(context.Background())
rConfig := s.createRepository()

pageData := api.PaginationData{
Expand All @@ -133,7 +133,7 @@ func (s *SnapshotsSuite) TestCreateAndList() {

collection, total, err := sDao.List(rConfig.OrgID, rConfig.UUID, pageData, filterData)

repository, _ := repoDao.fetchRepoConfig(rConfig.OrgID, rConfig.UUID, false)
repository, _ := repoDaoImpl.fetchRepoConfig(rConfig.OrgID, rConfig.UUID, false)
repositoryList, repoCount, _ := repoDao.List(rConfig.OrgID, api.PaginationData{Limit: -1}, api.FilterData{})

assert.NoError(t, err)
Expand Down Expand Up @@ -162,10 +162,12 @@ func (s *SnapshotsSuite) TestCreateAndListRedHatRepo() {
tx := s.tx

mockPulpClient := pulp_client.NewMockPulpClient(t)
sDao := snapshotDaoImpl{db: tx, pulpClient: mockPulpClient}
mockPulpClient.On("GetContentPath").Return(testContentPath, nil)
sDaoImpl := snapshotDaoImpl{db: tx, pulpClient: mockPulpClient}
sDao := sDaoImpl.WithContext(context.Background())
mockPulpClient.WithContextMock().WithDomainMock().On("GetContentPath").Return(testContentPath, nil)

repoDao := repositoryConfigDaoImpl{db: tx, yumRepo: &mockExt.YumRepositoryMock{}}
repoDaoImpl := repositoryConfigDaoImpl{db: tx, yumRepo: &mockExt.YumRepositoryMock{}, pulpClient: mockPulpClient}
repoDao := repoDaoImpl.WithContext(context.Background())

redhatRepositoryConfig := s.createRedhatRepository()
redhatSnap := s.createSnapshot(redhatRepositoryConfig)
Expand All @@ -182,7 +184,7 @@ func (s *SnapshotsSuite) TestCreateAndListRedHatRepo() {

collection, total, err := sDao.List("ShouldNotMatter", redhatRepositoryConfig.UUID, pageData, filterData)

repository, _ := repoDao.fetchRepoConfig("ShouldNotMatter", redhatRepositoryConfig.UUID, true)
repository, _ := repoDaoImpl.fetchRepoConfig("ShouldNotMatter", redhatRepositoryConfig.UUID, true)
repositoryList, repoCount, _ := repoDao.List("ShouldNotMatter", api.PaginationData{Limit: -1}, api.FilterData{})

assert.NoError(t, err)
Expand Down Expand Up @@ -250,8 +252,8 @@ func (s *SnapshotsSuite) TestListPageLimit() {
tx := s.tx

mockPulpClient := pulp_client.NewMockPulpClient(t)
sDao := snapshotDaoImpl{db: tx, pulpClient: mockPulpClient}
sDao.WithContext(context.Background())
sDaoImpl := snapshotDaoImpl{db: tx, pulpClient: mockPulpClient}
sDao := sDaoImpl.WithContext(context.Background())

mockPulpClient.WithContextMock().On("GetContentPath").Return(testContentPath, nil)

Expand Down Expand Up @@ -396,21 +398,20 @@ func (s *SnapshotsSuite) TestGetRepositoryConfigurationFile() {

mockPulpClient := pulp_client.NewMockPulpClient(t)
sDao := snapshotDaoImpl{db: tx, pulpClient: mockPulpClient}
sDao.WithContext(context.Background())

repoConfig := s.createRepository()
snapshot := s.createSnapshot(repoConfig)

// Test happy scenario
mockPulpClient.WithContextMock().On("GetContentPath").Return(testContentPath, nil).Once()
repoConfigFile, err := sDao.GetRepositoryConfigurationFile(repoConfig.OrgID, snapshot.UUID, repoConfig.UUID)
repoConfigFile, err := sDao.WithContext(context.Background()).GetRepositoryConfigurationFile(repoConfig.OrgID, snapshot.UUID, repoConfig.UUID)
assert.NoError(t, err)
assert.Contains(t, repoConfigFile, repoConfig.Name)
assert.Contains(t, repoConfigFile, testContentPath)

// Test error from pulp call
mockPulpClient.WithContextMock().On("GetContentPath").Return("", fmt.Errorf("some error")).Once()
repoConfigFile, err = sDao.GetRepositoryConfigurationFile(repoConfig.OrgID, snapshot.UUID, repoConfig.UUID)
repoConfigFile, err = sDao.WithContext(context.Background()).GetRepositoryConfigurationFile(repoConfig.OrgID, snapshot.UUID, repoConfig.UUID)
assert.Error(t, err)
assert.Empty(t, repoConfigFile)
}
Expand All @@ -421,7 +422,6 @@ func (s *SnapshotsSuite) TestGetRepositoryConfigurationFileNotFound() {

mockPulpClient := pulp_client.MockPulpClient{}
sDao := snapshotDaoImpl{db: tx, pulpClient: &mockPulpClient}
sDao.WithContext(context.Background())

repoConfig := s.createRepository()
snapshot := s.createSnapshot(repoConfig)
Expand All @@ -431,7 +431,7 @@ func (s *SnapshotsSuite) TestGetRepositoryConfigurationFileNotFound() {
}

// Test bad repo UUID
repoConfigFile, err := sDao.GetRepositoryConfigurationFile(repoConfig.OrgID, snapshot.UUID, uuid2.NewString())
repoConfigFile, err := sDao.WithContext(context.Background()).GetRepositoryConfigurationFile(repoConfig.OrgID, snapshot.UUID, uuid2.NewString())
assert.Error(t, err)
if err != nil {
daoError, ok := err.(*ce.DaoError)
Expand All @@ -442,7 +442,7 @@ func (s *SnapshotsSuite) TestGetRepositoryConfigurationFileNotFound() {
assert.Empty(t, repoConfigFile)

// Test bad snapshot UUID
repoConfigFile, err = sDao.GetRepositoryConfigurationFile(repoConfig.OrgID, uuid2.NewString(), repoConfig.UUID)
repoConfigFile, err = sDao.WithContext(context.Background()).GetRepositoryConfigurationFile(repoConfig.OrgID, uuid2.NewString(), repoConfig.UUID)
assert.Error(t, err)
if err != nil {
daoError, ok := err.(*ce.DaoError)
Expand All @@ -453,7 +453,7 @@ func (s *SnapshotsSuite) TestGetRepositoryConfigurationFileNotFound() {
assert.Empty(t, repoConfigFile)

// Test bad org ID
repoConfigFile, err = sDao.GetRepositoryConfigurationFile("bad orgID", snapshot.UUID, repoConfig.UUID)
repoConfigFile, err = sDao.WithContext(context.Background()).GetRepositoryConfigurationFile("bad orgID", snapshot.UUID, repoConfig.UUID)
assert.Error(t, err)
if err != nil {
daoError, ok := err.(*ce.DaoError)
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handler
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/mock"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -20,6 +19,7 @@ import (
echo_middleware "github.com/labstack/echo/v4/middleware"
"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)

Expand Down
5 changes: 3 additions & 2 deletions pkg/pulp_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func (p *pulpDaoImpl) WithContext(ctx context.Context) PulpClient {
}

func (p *pulpDaoImpl) WithDomain(domainName string) PulpClient {
p.domainName = domainName
return p
cpy := *p
cpy.domainName = domainName
return &cpy
}

func getPulpImpl(ctx context.Context) pulpDaoImpl {
Expand Down

0 comments on commit 4279018

Please sign in to comment.