Skip to content

Commit

Permalink
Fix test failures in OCP prow CI (#2503)
Browse files Browse the repository at this point in the history
* Fix test failures in OCP prow CI

Ensure that ServeRepo() make copy of repo from ./testdata and serves the
repo from that copy.

Signed-off-by: Matej Vašek <[email protected]>

* fixup: typo

Signed-off-by: Matej Vašek <[email protected]>

---------

Signed-off-by: Matej Vašek <[email protected]>
  • Loading branch information
matejvasek committed Sep 16, 2024
1 parent 3cbcc25 commit 42ed4d8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRepository_List(t *testing.T) {
// arguments, respects the repositories' path flag, and the expected name is echoed
// upon subsequent 'list'.
func TestRepository_Add(t *testing.T) {
url := ServeRepo("repository.git#main", t)
url := ServeRepo("repository.git", t) + "#main"
_ = FromTempDirectory(t)
t.Log(url)

Expand Down
2 changes: 1 addition & 1 deletion pkg/functions/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestRepositories_AddWithManifest(t *testing.T) {
// defines a custom language pack and makes full use of the manifest.yaml.
// The manifest.yaml is included which specifies things like custom templates
// location and (appropos to this test) a default name/
uri := ServeRepo("repository-a", t) // ./testdata/repository-a.git
uri := ServeRepo("repository-a.git", t) // ./testdata/repository-a.git
root, rm := Mktemp(t)
defer rm()

Expand Down
62 changes: 50 additions & 12 deletions pkg/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package testing

import (
"fmt"
"io"
"io/fs"
"net"
"net/http"
"net/http/cgi"
Expand Down Expand Up @@ -136,21 +138,57 @@ func cd(t *testing.T, dir string) {
// such as fromTempDirectory(t)
func ServeRepo(name string, t *testing.T) string {
t.Helper()
var (
path = filepath.Join("./testdata", name)
dir = filepath.Dir(path)
abs, _ = filepath.Abs(dir)
repo = filepath.Base(path)
url = RunGitServer(abs, t)
)
// This is to prevent "fatal: detected dubious ownership in repository at <source_reposutory_path>" while executing
// unit tests on other environments (such as Prow CI)
cmd := exec.Command("git", "config", "--global", "--add", "safe.directory", abs)
_, err := cmd.CombinedOutput()

gitRoot := t.TempDir()

// copy repo to the temp dir
err := filepath.Walk(filepath.Join("./testdata", name), func(path string, fi fs.FileInfo, err error) error {
if err != nil {
return err
}

relp, err := filepath.Rel("./testdata", path)
if err != nil {
return fmt.Errorf("cannot get relpath: %v", err)
}

dest := filepath.Join(gitRoot, relp)

switch {
case fi.Mode().IsRegular():
var in, out *os.File
in, err = os.Open(path)
if err != nil {
return fmt.Errorf("cannot open source file: %v", err)
}
defer in.Close()
out, err = os.OpenFile(dest, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("cannot open destination file: %v", err)
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return fmt.Errorf("cannot copy data: %v", err)
}
case fi.IsDir():
err = os.MkdirAll(dest, 0755)
if err != nil {
return fmt.Errorf("cannot mkdir: %v", err)
}
default:
return fmt.Errorf("unsupported file type")
}

return nil
})
if err != nil {
t.Fatal(err)
}
return fmt.Sprintf("%v/%v", url, repo)

url := RunGitServer(gitRoot, t)

return fmt.Sprintf("%v/%v", url, name)
}

// WithExecutable creates an executable of the given name and source in a temp
Expand Down

0 comments on commit 42ed4d8

Please sign in to comment.