Skip to content

Commit

Permalink
fix(import/git): fix panic when use non-existent revision on remote
Browse files Browse the repository at this point in the history
Signed-off-by: hainenber <[email protected]>
  • Loading branch information
hainenber committed Jun 1, 2024
1 parent a89fa6c commit 315858a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Main (unreleased)
cluster instance to another could have a staleness marker inserted and result
in a gap in metrics (@thampiotr)

- Fix panic when `import.git` are given with revision non-existent on remote repo. (@hainenber)

### Other changes

- `pyroscope.ebpf`, `pyroscope.java`, `pyroscope.scrape`, `pyroscope.write` and `discovery.process` components are now GA. (@korniltsev)
Expand Down
66 changes: 66 additions & 0 deletions internal/runtime/import_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,69 @@ const contentsMore = `declare "add" {
value = argument.a.value + argument.b.value + 1
}
}`

func TestNoPanicWithNonexistentRevision(t *testing.T) {
testRepo := t.TempDir()

main := `
import.git "testImport" {
repository = "` + testRepo + `"
path = "math.alloy"
pull_frequency = "1s"
revision = "nonexistent"
}
testImport.add "cc" {
a = 1
b = 1
}
`
runGit(t, testRepo, "init", testRepo)

runGit(t, testRepo, "checkout", "-b", "testor")

math := filepath.Join(testRepo, "math.alloy")
err := os.WriteFile(math, []byte(contents), 0666)
require.NoError(t, err)

runGit(t, testRepo, "add", ".")

runGit(t, testRepo, "commit", "-m \"test\"")

defer verifyNoGoroutineLeaks(t)
ctrl, f := setup(t, main)
err = ctrl.LoadSource(f, nil)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())

var wg sync.WaitGroup
defer func() {
cancel()
wg.Wait()
}()

wg.Add(1)
go func() {
defer wg.Done()
ctrl.Run(ctx)
}()

// Check for initial condition
require.Eventually(t, func() bool {
export := getExport[map[string]interface{}](t, ctrl, "", "testImport.add.cc")
return export["sum"] == 2
}, 5*time.Second, 100*time.Millisecond)

err = os.WriteFile(math, []byte(contentsMore), 0666)
require.NoError(t, err)

runGit(t, testRepo, "add", ".")

runGit(t, testRepo, "commit", "-m \"test2\"")

// Check for final condition.
require.Eventually(t, func() bool {
export := getExport[map[string]interface{}](t, ctrl, "", "testImport.add.cc")
return export["sum"] == 3
}, 5*time.Second, 100*time.Millisecond)
}
6 changes: 4 additions & 2 deletions internal/runtime/internal/importsource/import_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

"github.com/go-git/go-git/v5/plumbing"
"github.com/go-kit/log"

"github.com/grafana/alloy/internal/component"
Expand Down Expand Up @@ -194,11 +195,12 @@ func (im *ImportGit) Update(args component.Arguments) (err error) {
if im.repo == nil || !reflect.DeepEqual(repoOpts, im.repoOpts) {
r, err := vcs.NewGitRepo(context.Background(), repoPath, repoOpts)
if err != nil {
if errors.Is(err, plumbing.ErrReferenceNotFound) || !errors.As(err, &vcs.UpdateFailedError{}) {
return err
}
if errors.As(err, &vcs.UpdateFailedError{}) {
level.Error(im.log).Log("msg", "failed to update repository", "err", err)
im.updateHealth(err)
} else {
return err
}
}
im.repo = r
Expand Down

0 comments on commit 315858a

Please sign in to comment.