Skip to content

Commit 315f5df

Browse files
Refactoring: Git interfaces un-exposure, and move of test-server to test tooling (kptdev#503)
* Move testing git server to /test/git/pkg * Move all package internal types and functions to private * Prevent test imports during production builds of images * Remove deadcode * Found 2 more methods --------- Co-authored-by: Liam Fallon <35595825+liamfallon@users.noreply.github.com>
1 parent 4509acc commit 315f5df

26 files changed

Lines changed: 441 additions & 402 deletions

pkg/cache/crcache/cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import (
3030
"github.com/nephio-project/porch/pkg/cache/repomap"
3131
cachetypes "github.com/nephio-project/porch/pkg/cache/types"
3232
"github.com/nephio-project/porch/pkg/externalrepo/fake"
33-
"github.com/nephio-project/porch/pkg/externalrepo/git"
3433
externalrepotypes "github.com/nephio-project/porch/pkg/externalrepo/types"
3534
"github.com/nephio-project/porch/pkg/repository"
35+
gitserver "github.com/nephio-project/porch/test/git/pkg"
3636
"github.com/stretchr/testify/assert"
3737
"github.com/stretchr/testify/require"
3838
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -246,7 +246,7 @@ func openRepositoryFromArchive(t *testing.T, ctx context.Context, testPath, name
246246

247247
tempdir := t.TempDir()
248248
tarfile := filepath.Join(testPath, fmt.Sprintf("%s-repository.tar", name))
249-
_, address := git.ServeGitRepository(t, tarfile, tempdir)
249+
_, address := gitserver.ServeGitRepository(t, tarfile, tempdir)
250250
metadataStore := createMetadataStoreFromArchive(t, fmt.Sprintf("%s-metadata.yaml", name), name)
251251

252252
apiRepo := &v1alpha1.Repository{

pkg/externalrepo/git/annotation.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ type gitAnnotation struct {
4646
Task *porchapi.Task `json:"task,omitempty"`
4747
}
4848

49-
// ExtractGitAnnotations reads the gitAnnotations from the given commit.
49+
// extractGitAnnotations reads the gitAnnotations from the given commit.
5050
// If no annotation are found, it returns [], nil
5151
// If an invalid annotation is found, it returns an error.
52-
func ExtractGitAnnotations(commit *object.Commit) ([]gitAnnotation, error) {
52+
func extractGitAnnotations(commit *object.Commit) ([]gitAnnotation, error) {
5353
annotations := []gitAnnotation{}
5454
ec := errors.NewErrorCollector().WithSeparator(";").WithFormat("{%s}")
5555

@@ -69,8 +69,8 @@ func ExtractGitAnnotations(commit *object.Commit) ([]gitAnnotation, error) {
6969
return annotations, ec.Join()
7070
}
7171

72-
// AnnotateCommitMessage adds the gitAnnotation to the commit message.
73-
func AnnotateCommitMessage(message string, annotation *gitAnnotation) (string, error) {
72+
// annotateCommitMessage adds the gitAnnotation to the commit message.
73+
func annotateCommitMessage(message string, annotation *gitAnnotation) (string, error) {
7474
b, err := json.Marshal(annotation)
7575
if err != nil {
7676
return "", pkgerrors.Wrap(err, "error marshaling annotation")

pkg/externalrepo/git/branchcommithash_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestBranchCommitHash(t *testing.T) {
104104
// Validate commit exists if requested
105105
if tc.validateCommit {
106106
gitRepo := repo.(*gitRepository)
107-
err := gitRepo.sharedDir.WithRLock(func(r *gogit.Repository) error {
107+
err := gitRepo.sharedDir.withRLock(func(r *gogit.Repository) error {
108108
_, err := r.CommitObject(plumbing.NewHash(hash))
109109
return err
110110
})

pkg/externalrepo/git/cachedir_pool.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ import (
2424
"k8s.io/klog/v2"
2525
)
2626

27-
// DirectoryPool manages shared access to cached git directories
28-
type DirectoryPool struct {
27+
// directoryPool manages shared access to cached git directories
28+
type directoryPool struct {
2929
directories sync.Map
3030
mutex sync.Mutex
3131
}
3232

33-
type SharedDirectory struct {
33+
type sharedDirectory struct {
3434
repo *git.Repository
3535
mutex sync.RWMutex
3636
refCount int // number of gitRepository instances using this directory
3737
}
3838

39-
var globalDirectoryPool = &DirectoryPool{
39+
var globalDirectoryPool = &directoryPool{
4040
directories: sync.Map{},
4141
}
4242

43-
// GetOrCreateSharedRepository safely initializes or reuses a cached git directory
44-
func (p *DirectoryPool) GetOrCreateSharedRepository(dir, reponame string) (*SharedDirectory, error) {
43+
// getOrCreateSharedRepository safely initializes or reuses a cached git directory
44+
func (p *directoryPool) getOrCreateSharedRepository(dir, reponame string) (*sharedDirectory, error) {
4545
// Fast path: check if directory already exists
4646
if sharedDir, exists := p.directories.Load(dir); exists {
4747
p.mutex.Lock()
48-
shared := sharedDir.(*SharedDirectory)
48+
shared := sharedDir.(*sharedDirectory)
4949
shared.refCount++
5050
klog.V(2).Infof("Repo %s is reusing shared directory %s, refCount now: %d", reponame, dir, shared.refCount)
5151
p.mutex.Unlock()
@@ -58,7 +58,7 @@ func (p *DirectoryPool) GetOrCreateSharedRepository(dir, reponame string) (*Shar
5858

5959
// Double-check after acquiring lock
6060
if sharedDir, exists := p.directories.Load(dir); exists {
61-
shared := sharedDir.(*SharedDirectory)
61+
shared := sharedDir.(*sharedDirectory)
6262
shared.refCount++
6363
klog.V(2).Infof("Repo %s is reusing shared directory %s, refCount now: %d", reponame, dir, shared.refCount)
6464
return shared, nil
@@ -93,7 +93,7 @@ func (p *DirectoryPool) GetOrCreateSharedRepository(dir, reponame string) (*Shar
9393
}
9494
}
9595

96-
shared := &SharedDirectory{
96+
shared := &sharedDirectory{
9797
repo: repo,
9898
refCount: 1,
9999
}
@@ -102,13 +102,13 @@ func (p *DirectoryPool) GetOrCreateSharedRepository(dir, reponame string) (*Shar
102102
return shared, nil
103103
}
104104

105-
// ReleaseSharedRepository decrements reference count and cleans up cached git directory if needed
106-
func (p *DirectoryPool) ReleaseSharedRepository(dir, reponame string) {
105+
// releaseSharedRepository decrements reference count and cleans up cached git directory if needed
106+
func (p *directoryPool) releaseSharedRepository(dir, reponame string) {
107107
p.mutex.Lock()
108108
defer p.mutex.Unlock()
109109

110110
if sharedDir, exists := p.directories.Load(dir); exists {
111-
shared := sharedDir.(*SharedDirectory)
111+
shared := sharedDir.(*sharedDirectory)
112112
shared.refCount--
113113
klog.V(2).Infof("Released repo %s from %s, refCount now: %d", reponame, filepath.Base(dir), shared.refCount)
114114

@@ -131,15 +131,15 @@ func (p *DirectoryPool) ReleaseSharedRepository(dir, reponame string) {
131131
}
132132
}
133133

134-
// WithLock executes function with exclusive access to the cached git directory
135-
func (s *SharedDirectory) WithLock(fn func(*git.Repository) error) error {
134+
// withLock executes function with exclusive access to the cached git directory
135+
func (s *sharedDirectory) withLock(fn func(*git.Repository) error) error {
136136
s.mutex.Lock()
137137
defer s.mutex.Unlock()
138138
return fn(s.repo)
139139
}
140140

141-
// WithRLock executes function with read-only access to the cached git directory
142-
func (s *SharedDirectory) WithRLock(fn func(*git.Repository) error) error {
141+
// withRLock executes function with read-only access to the cached git directory
142+
func (s *sharedDirectory) withRLock(fn func(*git.Repository) error) error {
143143
s.mutex.RLock()
144144
defer s.mutex.RUnlock()
145145
return fn(s.repo)

pkg/externalrepo/git/cachedir_pool_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,48 @@ import (
2626
)
2727

2828
func TestDirectoryPool_GetOrCreateSharedRepository(t *testing.T) {
29-
pool := &DirectoryPool{
29+
pool := &directoryPool{
3030
directories: sync.Map{},
3131
}
3232

3333
tempDir := t.TempDir()
3434
repoDir := filepath.Join(tempDir, "test-repo")
3535

3636
// First call should create new shared directory
37-
shared1, err := pool.GetOrCreateSharedRepository(repoDir, "test-repo")
37+
shared1, err := pool.getOrCreateSharedRepository(repoDir, "test-repo")
3838
require.NoError(t, err)
3939
require.NotNil(t, shared1)
4040
assert.Equal(t, 1, shared1.refCount)
4141

4242
// Second call should reuse existing directory
43-
shared2, err := pool.GetOrCreateSharedRepository(repoDir, "test-repo")
43+
shared2, err := pool.getOrCreateSharedRepository(repoDir, "test-repo")
4444
require.NoError(t, err)
4545
assert.Same(t, shared1, shared2)
4646
assert.Equal(t, 2, shared2.refCount)
4747
}
4848

4949
func TestDirectoryPool_ReleaseSharedRepository(t *testing.T) {
50-
pool := &DirectoryPool{
50+
pool := &directoryPool{
5151
directories: sync.Map{},
5252
}
5353

5454
tempDir := t.TempDir()
5555
repoDir := filepath.Join(tempDir, "test-repo")
5656

5757
// Create shared repository
58-
shared, err := pool.GetOrCreateSharedRepository(repoDir, "test-repo")
58+
shared, err := pool.getOrCreateSharedRepository(repoDir, "test-repo")
5959
require.NoError(t, err)
6060

6161
// Add another reference
62-
_, err = pool.GetOrCreateSharedRepository(repoDir, "test-repo")
62+
_, err = pool.getOrCreateSharedRepository(repoDir, "test-repo")
6363
require.NoError(t, err)
6464

6565
// First release should decrement refCount
66-
pool.ReleaseSharedRepository(repoDir, "test-repo")
66+
pool.releaseSharedRepository(repoDir, "test-repo")
6767
assert.Equal(t, 1, shared.refCount)
6868

6969
// Second release should remove from pool and cleanup directory
70-
pool.ReleaseSharedRepository(repoDir, "test-repo")
70+
pool.releaseSharedRepository(repoDir, "test-repo")
7171
_, exists := pool.directories.Load(repoDir)
7272
assert.False(t, exists)
7373
_, err = os.Stat(repoDir)
@@ -79,13 +79,13 @@ func TestSharedDirectory_WithLock(t *testing.T) {
7979
repo, err := initEmptyRepository(tempDir)
8080
require.NoError(t, err)
8181

82-
shared := &SharedDirectory{
82+
shared := &sharedDirectory{
8383
repo: repo,
8484
refCount: 1,
8585
}
8686

8787
called := false
88-
err = shared.WithLock(func(r *git.Repository) error {
88+
err = shared.withLock(func(r *git.Repository) error {
8989
called = true
9090
assert.Same(t, repo, r)
9191
return nil
@@ -100,13 +100,13 @@ func TestSharedDirectory_WithRLock(t *testing.T) {
100100
repo, err := initEmptyRepository(tempDir)
101101
require.NoError(t, err)
102102

103-
shared := &SharedDirectory{
103+
shared := &sharedDirectory{
104104
repo: repo,
105105
refCount: 1,
106106
}
107107

108108
called := false
109-
err = shared.WithRLock(func(r *git.Repository) error {
109+
err = shared.withRLock(func(r *git.Repository) error {
110110
called = true
111111
assert.Same(t, repo, r)
112112
return nil
@@ -117,7 +117,7 @@ func TestSharedDirectory_WithRLock(t *testing.T) {
117117
}
118118

119119
func TestDirectoryPool_ConcurrentAccess(t *testing.T) {
120-
pool := &DirectoryPool{
120+
pool := &directoryPool{
121121
directories: sync.Map{},
122122
}
123123

@@ -132,15 +132,15 @@ func TestDirectoryPool_ConcurrentAccess(t *testing.T) {
132132
wg.Add(1)
133133
go func(id int) {
134134
defer wg.Done()
135-
_, err := pool.GetOrCreateSharedRepository(repoDir, "concurrent-repo")
135+
_, err := pool.getOrCreateSharedRepository(repoDir, "concurrent-repo")
136136
assert.NoError(t, err)
137137
}(i)
138138
}
139139
wg.Wait()
140140

141141
// Check final refCount
142142
sharedDir, _ := pool.directories.Load(repoDir)
143-
shared := sharedDir.(*SharedDirectory)
143+
shared := sharedDir.(*sharedDirectory)
144144

145145
assert.Equal(t, numGoroutines, shared.refCount)
146146

@@ -149,7 +149,7 @@ func TestDirectoryPool_ConcurrentAccess(t *testing.T) {
149149
wg.Add(1)
150150
go func() {
151151
defer wg.Done()
152-
pool.ReleaseSharedRepository(repoDir, "concurrent-repo")
152+
pool.releaseSharedRepository(repoDir, "concurrent-repo")
153153
}()
154154
}
155155
wg.Wait()
@@ -160,7 +160,7 @@ func TestDirectoryPool_ConcurrentAccess(t *testing.T) {
160160
}
161161

162162
func TestDirectoryPool_InitEmptyRepositoryFailure(t *testing.T) {
163-
pool := &DirectoryPool{
163+
pool := &directoryPool{
164164
directories: sync.Map{},
165165
}
166166

@@ -174,7 +174,7 @@ func TestDirectoryPool_InitEmptyRepositoryFailure(t *testing.T) {
174174
repoDir := filepath.Join(filePath, "subdir") // This will fail because parent is a file
175175

176176
// Should fail to create repository
177-
_, err = pool.GetOrCreateSharedRepository(repoDir, "test-repo")
177+
_, err = pool.getOrCreateSharedRepository(repoDir, "test-repo")
178178
assert.Error(t, err)
179179

180180
// Should not be in pool
@@ -183,12 +183,12 @@ func TestDirectoryPool_InitEmptyRepositoryFailure(t *testing.T) {
183183

184184
// Retry should work if we fix the issue
185185
os.Remove(filePath)
186-
_, err = pool.GetOrCreateSharedRepository(repoDir, "test-repo")
186+
_, err = pool.getOrCreateSharedRepository(repoDir, "test-repo")
187187
assert.NoError(t, err)
188188
}
189189

190190
func TestDirectoryPool_OpenRepositoryFailure(t *testing.T) {
191-
pool := &DirectoryPool{
191+
pool := &directoryPool{
192192
directories: sync.Map{},
193193
}
194194

@@ -203,7 +203,7 @@ func TestDirectoryPool_OpenRepositoryFailure(t *testing.T) {
203203
require.NoError(t, err)
204204

205205
// Should fail to open corrupted repository
206-
_, err = pool.GetOrCreateSharedRepository(repoDir, "test-repo")
206+
_, err = pool.getOrCreateSharedRepository(repoDir, "test-repo")
207207
assert.Error(t, err)
208208
assert.Contains(t, err.Error(), "corrupted cache was removed")
209209

@@ -216,12 +216,12 @@ func TestDirectoryPool_OpenRepositoryFailure(t *testing.T) {
216216
assert.True(t, os.IsNotExist(err))
217217

218218
// Retry should work now that corrupted dir is removed
219-
_, err = pool.GetOrCreateSharedRepository(repoDir, "test-repo")
219+
_, err = pool.getOrCreateSharedRepository(repoDir, "test-repo")
220220
assert.NoError(t, err)
221221
}
222222

223223
func TestDirectoryPool_OpenRepositoryCleanupFailure(t *testing.T) {
224-
pool := &DirectoryPool{
224+
pool := &directoryPool{
225225
directories: sync.Map{},
226226
}
227227

@@ -242,7 +242,7 @@ func TestDirectoryPool_OpenRepositoryCleanupFailure(t *testing.T) {
242242
defer os.Chmod(repoDir, 0755)
243243

244244
// Should fail to open and also fail to cleanup
245-
_, err = pool.GetOrCreateSharedRepository(repoDir, "test-repo")
245+
_, err = pool.getOrCreateSharedRepository(repoDir, "test-repo")
246246
assert.Error(t, err)
247247
// When cleanup fails, error message should mention checking local cache
248248
assert.Contains(t, err.Error(), "check the local git cache")
@@ -257,7 +257,7 @@ func TestDirectoryPool_OpenRepositoryCleanupFailure(t *testing.T) {
257257
}
258258

259259
func TestDirectoryPool_PathIsFile(t *testing.T) {
260-
pool := &DirectoryPool{
260+
pool := &directoryPool{
261261
directories: sync.Map{},
262262
}
263263

@@ -269,7 +269,7 @@ func TestDirectoryPool_PathIsFile(t *testing.T) {
269269
require.NoError(t, err)
270270

271271
// Should fail because path is a file, not a directory
272-
_, err = pool.GetOrCreateSharedRepository(filePath, "test-repo")
272+
_, err = pool.getOrCreateSharedRepository(filePath, "test-repo")
273273
assert.Error(t, err)
274274
assert.Contains(t, err.Error(), "not a directory")
275275

pkg/externalrepo/git/commit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestPackageCommitToMain(t *testing.T) {
9292

9393
// Commit `bucket`` package from drafts/bucket/v1 into main
9494

95-
main := resolveReference(t, gitRepo, DefaultMainReferenceName)
95+
main := resolveReference(t, gitRepo, defaultMainReferenceName)
9696
packagePath := "bucket"
9797

9898
// Confirm no 'bucket' package in main
@@ -143,7 +143,7 @@ func TestCommitWithUser(t *testing.T) {
143143
gitRepo := OpenGitRepositoryFromArchive(t, filepath.Join("testdata", "trivial-repository.tar"), tempdir)
144144

145145
ctx := context.Background()
146-
main := resolveReference(t, gitRepo, DefaultMainReferenceName)
146+
main := resolveReference(t, gitRepo, defaultMainReferenceName)
147147

148148
{
149149
const testEmail = "porch-test@porch-domain.com"

0 commit comments

Comments
 (0)