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 f21a9b8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 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
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
28 changes: 13 additions & 15 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 @@ -250,8 +250,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 +396,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 +420,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 +429,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 +440,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 +451,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
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 f21a9b8

Please sign in to comment.