aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/vcs
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2023-08-04 15:18:36 +0200
committerAleksandr Nogikh <nogikh@google.com>2023-08-04 14:04:53 +0000
commitf390e60878ac1f757d680dbbbcc6b6d4c579baad (patch)
tree49ee02378f75ada4253e2d1510a966e1aa43e7d1 /pkg/vcs
parent7728e95980db0a7f7f2a6305d4e2a7f3e919be91 (diff)
pkg/vcs: add tests for automatic backports
Also, slightly refactor the code to facilitate testing.
Diffstat (limited to 'pkg/vcs')
-rw-r--r--pkg/vcs/linux_patches.go12
-rw-r--r--pkg/vcs/linux_patches_test.go108
2 files changed, 118 insertions, 2 deletions
diff --git a/pkg/vcs/linux_patches.go b/pkg/vcs/linux_patches.go
index 2ec3e0c63..88c3741f2 100644
--- a/pkg/vcs/linux_patches.go
+++ b/pkg/vcs/linux_patches.go
@@ -21,8 +21,16 @@ type BackportCommit struct {
// linuxFixBackports() cherry-picks the commits necessary to compile/run older Linux kernel releases.
func linuxFixBackports(repo *git, extraCommits ...BackportCommit) error {
- list := append([]BackportCommit{}, pickLinuxCommits...)
- for _, info := range append(list, extraCommits...) {
+ return applyFixBackports(repo,
+ append(
+ append([]BackportCommit{}, pickLinuxCommits...),
+ extraCommits...,
+ ),
+ )
+}
+
+func applyFixBackports(repo *git, commits []BackportCommit) error {
+ for _, info := range commits {
if info.GuiltyHash != "" {
contains, err := repo.Contains(info.GuiltyHash)
if err != nil {
diff --git a/pkg/vcs/linux_patches_test.go b/pkg/vcs/linux_patches_test.go
new file mode 100644
index 000000000..f5a27219d
--- /dev/null
+++ b/pkg/vcs/linux_patches_test.go
@@ -0,0 +1,108 @@
+// Copyright 2023 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+package vcs
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/google/syzkaller/pkg/osutil"
+)
+
+func TestFixBackport(t *testing.T) {
+ baseDir := t.TempDir()
+ repo := MakeTestRepo(t, baseDir)
+
+ repo.Git("checkout", "-b", "main")
+ repo.Git("commit", "--no-edit", "--allow-empty", "-m", "starting commit")
+
+ // Let the fix stay in a separate branch.
+ repo.Git("checkout", "-b", "branch-with-a-fix")
+ filePath := filepath.Join(baseDir, "object.txt")
+ if err := os.WriteFile(filePath, []byte("content"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ repo.Git("add", "object.txt")
+ repo.Git("commit", "--no-edit", "-m", "fix title")
+ fixCommit, _ := repo.repo.HeadCommit()
+
+ // Return to the original branch.
+ repo.Git("checkout", "main")
+
+ // Check the test is sane.
+ if osutil.IsExist(filePath) {
+ t.Fatalf("we have switched the branch, object.txt should not be present")
+ }
+
+ // Verify that the fix gets backported.
+ err := applyFixBackports(repo.repo, []BackportCommit{
+ {
+ FixHash: fixCommit.Hash,
+ FixTitle: `fix title`,
+ },
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !osutil.IsExist(filePath) {
+ t.Fatalf("the commit was not backported, but should have")
+ }
+}
+
+func TestConditionalFixBackport(t *testing.T) {
+ baseDir := t.TempDir()
+ repo := MakeTestRepo(t, baseDir)
+
+ repo.Git("checkout", "-b", "main")
+ repo.Git("commit", "--no-edit", "--allow-empty", "-m", "starting commit")
+
+ // Let the fix stay in a separate branch.
+ repo.Git("checkout", "-b", "branch-with-fix")
+ filePath := filepath.Join(baseDir, "object.txt")
+ if err := os.WriteFile(filePath, []byte("content"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ repo.Git("add", "object.txt")
+ repo.Git("commit", "--no-edit", "-m", "fix title")
+ fixCommit, _ := repo.repo.HeadCommit()
+
+ // Create a branch without a bug.
+ repo.Git("checkout", "main")
+ repo.Git("checkout", "-b", "branch-no-bug")
+ repo.Git("commit", "--no-edit", "--allow-empty", "-m", "some commit")
+
+ // Create a branch with a bug.
+ repo.Git("checkout", "-b", "branch-with-bug")
+ repo.Git("commit", "--no-edit", "--allow-empty", "-m", "bad commit")
+ badCommit, _ := repo.repo.HeadCommit()
+ repo.Git("commit", "--no-edit", "--allow-empty", "-m", "some other commit")
+
+ // Ensure we do not cherry-pick the fix when there's no bug.
+ repo.Git("checkout", "branch-no-bug")
+ rules := []BackportCommit{
+ {
+ GuiltyHash: badCommit.Hash,
+ FixHash: fixCommit.Hash,
+ FixTitle: `fix title`,
+ },
+ }
+ err := applyFixBackports(repo.repo, rules)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if osutil.IsExist(filePath) {
+ t.Fatalf("the commit was backported, but shouldn't have been")
+ }
+
+ // .. but we do cherry-pick otherwise.
+ repo.Git("checkout", "branch-with-bug")
+ err = applyFixBackports(repo.repo, rules)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !osutil.IsExist(filePath) {
+ t.Fatalf("the commit was not backported, but should have been")
+ }
+}