Add more tests

This commit is contained in:
Mustafa Gezen 2023-09-07 03:49:42 +02:00
parent 76b71be278
commit d7b81bacea
Signed by: mustafa
GPG Key ID: DCDF010D946438C1
9 changed files with 276 additions and 8 deletions

View File

@ -61,6 +61,7 @@ go_test(
"main_test.go",
"process_rpm_test.go",
"retract_entry_test.go",
"utils_test.go",
"workflows_test.go",
],
data = glob(["testdata/**"]),
@ -73,11 +74,15 @@ go_test(
"//tools/mothership/migrations",
"//tools/mothership/proto/v1:pb",
"//tools/mothership/worker_server/forge",
"//tools/mothership/worker_server/srpm_import",
"//vendor/github.com/go-git/go-billy/v5/memfs",
"//vendor/github.com/go-git/go-billy/v5/osfs",
"//vendor/github.com/go-git/go-git/v5:go-git",
"//vendor/github.com/go-git/go-git/v5/plumbing",
"//vendor/github.com/go-git/go-git/v5/plumbing/cache",
"//vendor/github.com/go-git/go-git/v5/plumbing/transport/http",
"//vendor/github.com/go-git/go-git/v5/storage/filesystem",
"//vendor/github.com/go-git/go-git/v5/storage/memory",
"//vendor/github.com/stretchr/testify/mock",
"//vendor/github.com/stretchr/testify/require",
"//vendor/github.com/stretchr/testify/suite",

View File

@ -27,6 +27,7 @@ import (
base "go.resf.org/peridot/base/go"
mshipadminpb "go.resf.org/peridot/tools/mothership/admin/pb"
mothership_db "go.resf.org/peridot/tools/mothership/db"
"go.resf.org/peridot/tools/mothership/worker_server/forge"
"go.temporal.io/sdk/temporal"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@ -151,7 +152,7 @@ func clonePatchesToTemporaryFS(currentFS billy.Filesystem) (billy.Filesystem, er
return fs, nil
}
func resetRepoToPoint(repo *git.Repository, commit string) error {
func resetRepoToPoint(repo *git.Repository, authenticator *forge.Authenticator, commit string) error {
wt, err := repo.Worktree()
if err != nil {
return errors.Wrap(err, "failed to get worktree")
@ -263,9 +264,9 @@ func resetRepoToPoint(repo *git.Repository, commit string) error {
// Create the commit
_, err = wt.Commit(commitMsg, &git.CommitOptions{
Committer: &object.Signature{
Name: targetCommit.Author.Name,
Email: targetCommit.Author.Email,
Author: &object.Signature{
Name: authenticator.AuthorName,
Email: authenticator.AuthorEmail,
When: time.Now(),
},
})
@ -322,7 +323,7 @@ func (w *Worker) RetractEntry(name string) (*mshipadminpb.RetractEntryResponse,
}
// Reset the repo to the commit before the commit we want to revert
err = resetRepoToPoint(repo, entry.CommitHash)
err = resetRepoToPoint(repo, auth, entry.CommitHash)
if err != nil {
base.LogErrorf("failed to reset repo: %v", err)
return nil, status.Error(codes.Internal, "failed to reset repo")

View File

@ -21,6 +21,7 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/stretchr/testify/require"
@ -28,6 +29,7 @@ import (
storage_memory "go.resf.org/peridot/base/go/storage/memory"
mothership_db "go.resf.org/peridot/tools/mothership/db"
mothershippb "go.resf.org/peridot/tools/mothership/pb"
"go.resf.org/peridot/tools/mothership/worker_server/forge"
"go.resf.org/peridot/tools/mothership/worker_server/srpm_import"
"io"
"os"
@ -193,7 +195,11 @@ func TestResetRepoToPoint_TwoCommits(t *testing.T) {
})
require.Nil(t, err)
err = resetRepoToPoint(repo, secondImport.Commit.Hash.String())
err = resetRepoToPoint(
repo,
&forge.Authenticator{AuthorName: "test", AuthorEmail: "test@rockylinux.org"},
secondImport.Commit.Hash.String(),
)
require.Nil(t, err)
// Check that only the first commit exists
@ -209,6 +215,99 @@ func TestResetRepoToPoint_TwoCommits(t *testing.T) {
require.Nil(t, commit)
}
func TestResetRepoToPoint_TwoCommits_CommitAfterRetractPoint(t *testing.T) {
s, err := srpm_import.FromFile("testdata/efi-rpm-macros-3-3.el8.src.rpm", false)
require.Nil(t, err)
tempDir, err := os.MkdirTemp("", "peridot-srpm-import-test-*")
require.Nil(t, err)
// Create a bare repo in tempDir
osfsTemp := osfs.New(tempDir)
dot, err := osfsTemp.Chroot(".git")
require.Nil(t, err)
filesystemTemp := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
require.Nil(t, filesystemTemp.Init())
_, err = git.Init(filesystemTemp, nil)
require.Nil(t, err)
opts := &git.CloneOptions{
URL: tempDir,
}
storer := memory.NewStorage()
fs := memfs.New()
lookaside := storage_memory.New(osfs.New("/"))
firstImport, err := s.Import(opts, storer, fs, lookaside, "")
require.Nil(t, err)
storer2 := memory.NewStorage()
fs2 := memfs.New()
secondImport, err := s.Import(opts, storer2, fs2, lookaside, "")
require.Nil(t, err)
repo, err := getRepo("file://"+tempDir, nil)
require.Nil(t, err)
// Get wt and checkout the correct branch
wt, err := repo.Worktree()
require.Nil(t, err)
err = wt.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(secondImport.Branch),
Force: true,
})
require.Nil(t, err)
// Create a commit after the commit we want to reset to
f, err := wt.Filesystem.Create("PATCHES/test.cfg")
require.Nil(t, err)
_, err = f.Write([]byte("lookaside: { file: \"test.png\" }"))
require.Nil(t, err)
err = f.Close()
require.Nil(t, err)
_, err = wt.Add("PATCHES/test.cfg")
require.Nil(t, err)
stableTime := time.Now()
_, err = wt.Commit("test commit", &git.CommitOptions{
Author: &object.Signature{
Name: "Mustafa Gezen",
Email: "mustafa@rockylinux.org",
When: stableTime,
},
})
require.Nil(t, err)
err = resetRepoToPoint(
repo,
&forge.Authenticator{AuthorName: "test", AuthorEmail: "test@rockylinux.org"},
secondImport.Commit.Hash.String(),
)
require.Nil(t, err)
// Check that only the first commit exists
log, err := repo.Log(&git.LogOptions{})
require.Nil(t, err)
commit, err := log.Next()
require.Nil(t, err)
require.NotNil(t, commit)
msg := `Retract "import efi-rpm-macros-3-3.el8"
Fast-forwarded following commits:
test commit
Co-authored-by: Mustafa Gezen <mustafa@rockylinux.org>
`
require.Equal(t, msg, commit.Message)
commit, err = log.Next()
require.Nil(t, err)
require.Equal(t, firstImport.Commit.Hash.String(), commit.Hash.String())
commit, err = log.Next()
require.NotNil(t, err)
require.Equal(t, "EOF", err.Error())
require.Nil(t, commit)
}
func TestWorker_RetractEntry(t *testing.T) {
s, err := srpm_import.FromFile("testdata/efi-rpm-macros-3-3.el8.src.rpm", false)
require.Nil(t, err)

View File

@ -44,7 +44,10 @@ go_library(
go_test(
name = "srpm_import_test",
size = "small",
srcs = ["srpm_import_test.go"],
srcs = [
"srpm_import_test.go",
"srpmproc_compat_test.go",
],
data = glob(["testdata/**"]),
embed = [":srpm_import"],
deps = [

View File

@ -828,6 +828,9 @@ spec_change {
message: "test patch1"
}
}
lookaside {
file: "test.png"
}
`))
require.Nil(t, err)

View File

@ -0,0 +1,57 @@
package srpm_import
import (
"github.com/go-git/go-billy/v5/memfs"
"github.com/rocky-linux/srpmproc/pkg/data"
"github.com/stretchr/testify/require"
storage_memory "go.resf.org/peridot/base/go/storage/memory"
"testing"
)
func TestSrpmprocBlobCompat_Write(t *testing.T) {
lookaside := storage_memory.New(memfs.New())
s := &srpmprocBlobCompat{lookaside}
require.Nil(t, s.Write("test", []byte("test")))
x, err := lookaside.Get("test")
require.Nil(t, err)
require.Equal(t, []byte("test"), x)
}
func TestSrpmprocBlobCompat_Read(t *testing.T) {
lookaside := storage_memory.New(memfs.New())
s := &srpmprocBlobCompat{lookaside}
_, err := lookaside.PutBytes("test", []byte("test"))
require.Nil(t, err)
x, err := s.Read("test")
require.Nil(t, err)
require.Equal(t, []byte("test"), x)
}
func TestSrpmprocImportModeCompat_ImportName(t *testing.T) {
s := &srpmprocImportModeCompat{}
pd := &data.ProcessData{
ImportBranchPrefix: "r",
Version: 9,
RpmLocation: "bash",
}
md := &data.ModeData{
SourcesToIgnore: []*data.IgnoredSource{},
TagBranch: "refs/tags/imports/r9/bash-5.1.8-4.el9",
}
require.Equal(t, "bash-5.1.8-4.el9", s.ImportName(pd, md))
}
// todo(mustafa): actually recall what this mode was useful for in srpmproc. like what is this??
func TestSrpmprocImportModeCompat_ImportName_NoTag(t *testing.T) {
s := &srpmprocImportModeCompat{}
pd := &data.ProcessData{
ImportBranchPrefix: "el",
Version: 9,
RpmLocation: "bash",
}
md := &data.ModeData{
SourcesToIgnore: []*data.IgnoredSource{},
TagBranch: "refs/heads/el9",
}
require.Equal(t, "el9", s.ImportName(pd, md))
}

View File

@ -4,6 +4,7 @@ import (
"github.com/pkg/errors"
"go.temporal.io/sdk/temporal"
"net/url"
"strings"
)
func getObjectPath(uri string) (string, error) {
@ -22,7 +23,7 @@ func getObjectPath(uri string) (string, error) {
// S3 for example must include bucket, while memory:// does not.
// So memory://test.rpm would be parsed as host=test.rpm, path="".
// While s3://mship/test.rpm would be parsed as host=mship, path=test.rpm.
object := parsed.Path
object := strings.TrimPrefix(parsed.Path, "/")
if object == "" {
object = parsed.Host
}

View File

@ -0,0 +1,23 @@
package mothership_worker_server
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestGetObjectPath_Path_S3(t *testing.T) {
object, err := getObjectPath("s3://mship/test.rpm")
require.Nil(t, err)
require.Equal(t, "test.rpm", object)
}
func TestGetObjectPath_Host_Memory(t *testing.T) {
object, err := getObjectPath("memory://test.rpm")
require.Nil(t, err)
require.Equal(t, "test.rpm", object)
}
func TestGetObjectPath_InvalidURI(t *testing.T) {
_, err := getObjectPath("test://test:test/")
require.NotNil(t, err)
}

View File

@ -19,8 +19,10 @@ import (
"errors"
"github.com/stretchr/testify/mock"
base "go.resf.org/peridot/base/go"
mshipadminpb "go.resf.org/peridot/tools/mothership/admin/pb"
mothership_db "go.resf.org/peridot/tools/mothership/db"
mothershippb "go.resf.org/peridot/tools/mothership/pb"
"go.temporal.io/sdk/temporal"
"time"
)
@ -247,3 +249,77 @@ func (s *UnitTestSuite) TestProcessRPMWorkflow_OnHold_Error() {
s.True(s.env.IsWorkflowCompleted())
s.Error(s.env.GetWorkflowError())
}
func (s *UnitTestSuite) TestProcessRPMWorkflow_Error_DeleteEntry() {
s.env.OnActivity(testW.VerifyResourceExists, "memory://efi-rpm-macros-3-3.el8.src.rpm").Return(nil)
s.env.OnActivity(testW.SetWorkerLastCheckinTime, mock.Anything).Return(nil)
entry := (&mothership_db.Entry{
Name: base.NameGen("entries"),
CreateTime: time.Now(),
OSRelease: "Rocky Linux release 8.8 (Green Obsidian)",
Sha256Sum: "518a9418fec1deaeb4c636615d8d81fb60146883c431ea15ab1127893d075d28",
RepositoryName: "BaseOS",
WorkerID: sql.NullString{
String: "test-worker",
Valid: true,
},
State: mothershippb.Entry_ARCHIVING,
}).ToPB()
s.env.OnActivity(testW.CreateEntry, mock.Anything).Return(entry, nil)
checksumErr := temporal.NewNonRetryableApplicationError(
"checksum does not match",
"checksumDoesNotMatch",
errors.New("client submitted a checksum that does not match the resource"),
)
s.env.OnActivity(testW.SetEntryIDFromRPM, entry.Name, "memory://efi-rpm-macros-3-3.el8.src.rpm", entry.Sha256Sum).Return(nil, checksumErr)
s.env.OnActivity(testW.DeleteEntry, entry.Name).Return(nil)
args := &mothershippb.ProcessRPMArgs{
Request: &mothershippb.ProcessRPMRequest{
RpmUri: "memory://efi-rpm-macros-3-3.el8.src.rpm",
OsRelease: "Rocky Linux release 8.8 (Green Obsidian)",
Checksum: entry.Sha256Sum,
Repository: "BaseOS",
},
InternalRequest: &mothershippb.ProcessRPMInternalRequest{
WorkerId: "test-worker",
},
}
s.env.ExecuteWorkflow(ProcessRPMWorkflow, args)
s.True(s.env.IsWorkflowCompleted())
s.Error(s.env.GetWorkflowError())
}
func (s *UnitTestSuite) TestRetractEntryWorkflow_Success() {
entry := base.NameGen("entries")
s.env.OnActivity(testW.SetEntryState, entry, mothershippb.Entry_RETRACTING, mock.Anything).Return(nil, nil)
res := &mshipadminpb.RetractEntryResponse{
Name: entry,
}
s.env.OnActivity(testW.RetractEntry, entry).Return(res, nil)
s.env.OnActivity(testW.SetEntryState, entry, mothershippb.Entry_RETRACTED, mock.Anything).Return(nil, nil)
s.env.ExecuteWorkflow(RetractEntryWorkflow, entry)
s.True(s.env.IsWorkflowCompleted())
s.NoError(s.env.GetWorkflowError())
}
func (s *UnitTestSuite) TestRetractEntryWorkflow_Failed_RevertToArchived() {
entry := base.NameGen("entries")
s.env.OnActivity(testW.SetEntryState, entry, mothershippb.Entry_RETRACTING, mock.Anything).Return(nil, nil)
anyErr := errors.New("any error")
s.env.OnActivity(testW.RetractEntry, entry).Return(nil, anyErr)
s.env.OnActivity(testW.SetEntryState, entry, mothershippb.Entry_ARCHIVED, mock.Anything).Return(nil, nil)
s.env.ExecuteWorkflow(RetractEntryWorkflow, entry)
s.True(s.env.IsWorkflowCompleted())
s.Error(s.env.GetWorkflowError())
}