Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
gps: Tests for adaptive git repo recovery/cleanup
Browse files Browse the repository at this point in the history
This covers three basic cases - untracked files, modified files, and
some corruption in the .git directory. The first two are plausible; the
third is less so, as we don't know much about what real git failure
patterns could look like in the .git directory itself. However, it's
an adequate test inasmuch as it triggers failure in the basic git calls
we make, thereby triggering the recovery procedure.
  • Loading branch information
sdboyer committed Oct 26, 2017
1 parent d596e65 commit 90abc13
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
6 changes: 0 additions & 6 deletions internal/gps/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@ func TestSourceInit(t *testing.T) {

os.Stat(filepath.Join(cpath, "metadata", "github.com", "sdboyer", "gpkt", "cache.json"))

// TODO(sdboyer) disabled until we get caching working
//_, err = os.Stat(filepath.Join(cpath, "metadata", "github.com", "sdboyer", "gpkt", "cache.json"))
//if err != nil {
//t.Error("Metadata cache json file does not exist in expected location")
//}

// Ensure source existence values are what we expect
var exists bool
exists, err = sm.SourceExists(id)
Expand Down
2 changes: 1 addition & 1 deletion internal/gps/maybe_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (m maybeGitSource) try(ctx context.Context, cachedir string, c singleSource
if err := superv.do(ctx, "git", ctValidateLocal, func(ctx context.Context) error {
// If repository already exists on disk, make a pass to be sure
// everything's clean.
return src.cleanup(ctx)
return src.ensureClean(ctx)
}); err != nil {
return nil, 0, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/gps/vcs_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ type gitSource struct {
}

// ensureClean sees to it that a git repository is clean and in working order,
// or returns an error if it can't.
func (s *gitSource) cleanup(ctx context.Context) error {
// or returns an error if the adaptive recovery attempts fail.
func (s *gitSource) ensureClean(ctx context.Context) error {
r := s.repo.(*gitRepo)
cmd := commandContext(
ctx,
Expand Down
96 changes: 96 additions & 0 deletions internal/gps/vcs_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gps
import (
"context"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
Expand Down Expand Up @@ -647,6 +648,101 @@ func TestGitSourceListVersionsNoDupes(t *testing.T) {
}
}

func TestGitSourceAdaptiveCleanup(t *testing.T) {
t.Parallel()

// This test is slowish, skip it on -short
if testing.Short() {
t.Skip("Skipping git adaptive failure recovery test in short mode")
}
requiresBins(t, "git")

cpath, err := ioutil.TempDir("", "smcache")
if err != nil {
t.Fatalf("Failed to create temp dir: %s", err)
}

var sm *SourceMgr
mkSM := func() {
// If sm is already set, make sure it's released, then create a new one.
if sm != nil {
sm.Release()
}

var err error
sm, err = NewSourceManager(SourceManagerConfig{
Cachedir: cpath,
Logger: log.New(test.Writer{TB: t}, "", 0),
})
if err != nil {
t.Fatalf("Unexpected error on SourceManager creation: %s", err)
}
}

mkSM()
id := mkPI("github.com/sdboyer/gpkt")
err = sm.SyncSourceFor(id)
if err != nil {
t.Fatal(err)
}

repodir := filepath.Join(sm.cachedir, "sources", "https---github.com-sdboyer-gpkt")
if _, err := os.Stat(repodir); err != nil {
if os.IsNotExist(err) {
t.Fatalf("expected location for repodir did not exist: %q", repodir)
} else {
t.Fatal(err)
}
}

// Create a file that git will see as untracked.
untrackedPath := filepath.Join(repodir, "untrackedfile")
f, err := os.Create(untrackedPath)
if err != nil {
t.Fatal(err)
}
f.Close()

mkSM()
err = sm.SyncSourceFor(id)
if err != nil {
t.Fatalf("choked after adding dummy file: %q", err)
}

if _, err := os.Stat(untrackedPath); err == nil {
t.Fatal("untracked file still existed after cleanup should've been triggered")
}

// Remove a file that we know exists, which `git status` checks should catch.
readmePath := filepath.Join(repodir, "README.md")
os.Remove(readmePath)

mkSM()
err = sm.SyncSourceFor(id)
if err != nil {
t.Fatalf("choked after removing known file: %q", err)
}

if _, err := os.Stat(readmePath); err != nil {
t.Fatal("README was still absent after cleanup should've been triggered")
}

// Remove .git/objects directory, which should make git bite it.
err = os.RemoveAll(filepath.Join(repodir, ".git", "objects"))
if err != nil {
t.Fatal(err)
}

mkSM()
err = sm.SyncSourceFor(id)
if err != nil {
t.Fatalf("choked after removing .git/objects directory: %q", err)
}

sm.Release()
os.RemoveAll(cpath)
}

func Test_bzrSource_exportRevisionTo_removeVcsFiles(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 90abc13

Please sign in to comment.