aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/vcs/linux_patches_test.go
blob: 663cc35442165b2acedbbdec17219f890dfc4b73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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.Commit(HEAD)

	// 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.Commit(HEAD)

	// 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.Commit(HEAD)
	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")
	}
}