Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: method for fetching latest commit from gittest server #636

Merged
merged 7 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/rudderlabs/rudder-go-kit

go 1.22.5
go 1.22.7

replace github.com/gocql/gocql => github.com/scylladb/gocql v1.14.2

Expand Down
22 changes: 18 additions & 4 deletions testhelper/gittest/gittest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ import (

type Server struct {
*httptest.Server
URL string
URL string
rootPath string
debanjan97 marked this conversation as resolved.
Show resolved Hide resolved
DefaultBranch string
atzoum marked this conversation as resolved.
Show resolved Hide resolved
}

const defaultBranch = "main"

// NewHttpServer creates a new httptest.Server that serves a git repository from the given sourcePath.
func NewHttpServer(t testing.TB, sourcePath string) *Server {
return newServer(t, sourcePath, false)
Expand Down Expand Up @@ -60,7 +64,7 @@ func newServer(t testing.TB, sourcePath string, secure bool) *Server {
require.NoError(t, err, "should be able to find git in PATH")
out, err = execCmd("git", "init", workingDir)
require.NoErrorf(t, err, "should be able to initialize git repository: %s", out)
out, err = execCmd("git", "-C", workingDir, "branch", "-m", "main")
out, err = execCmd("git", "-C", workingDir, "branch", "-m", defaultBranch)
require.NoErrorf(t, err, "should be able to rename the default branch to main: %s", out)
out, err = execCmd("git", "-C", workingDir, "add", ".")
require.NoErrorf(t, err, "should be able to add files to git repository: %s", out)
Expand Down Expand Up @@ -99,8 +103,10 @@ func newServer(t testing.TB, sourcePath string, secure bool) *Server {
serverURL.Host = net.JoinHostPort(getLocalIP(t), port)
url := serverURL.String() + "/" + org + "/" + repo
return &Server{
Server: s,
URL: url,
Server: s,
URL: url,
rootPath: gitRoot,
DefaultBranch: defaultBranch,
}
}

Expand All @@ -109,6 +115,14 @@ func (s *Server) GetServerCA() []byte {
return getServerCA(s.Server)
}

func (s *Server) GetLatestCommitHash(t testing.TB, branch string) string {
cmd := exec.Command("git", "ls-remote", s.URL, fmt.Sprintf("refs/heads/%s", branch))
out, err := cmd.Output()
require.NoError(t, err, "should be able to run the ls-remote command")
commitHash := strings.Split(string(out), "\t")[0]
return commitHash
}

func getServerCA(server *httptest.Server) []byte {
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: server.TLS.Certificates[0].Certificate[0]})
}
Expand Down
8 changes: 8 additions & 0 deletions testhelper/gittest/gittest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -57,6 +58,13 @@ func TestGitServer(t *testing.T) {

out, err = execCmd("git", "-C", tempDir, "push", "origin", "v1.0.0")
require.NoErrorf(t, err, "should be able to push the tag: %s", out)

out, err = execCmd("git", "-C", tempDir, "rev-parse", "HEAD")
require.NoError(t, err, "should be able to get the HEAD commit")
require.NotEmpty(t, out, "HEAD commit should not be empty")

latestCommit := s.GetLatestCommitHash(t, "develop")
require.Equal(t, strings.TrimSpace(out), latestCommit, "HEAD commit should match the latest commit")
})

t.Run("https", func(t *testing.T) {
Expand Down
Loading