summaryrefslogtreecommitdiff
path: root/go/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'go/cmd')
-rw-r--r--go/cmd/gitaly-upload-archive/main.go45
-rw-r--r--go/cmd/gitaly-upload-archive/main_test.go70
2 files changed, 115 insertions, 0 deletions
diff --git a/go/cmd/gitaly-upload-archive/main.go b/go/cmd/gitaly-upload-archive/main.go
new file mode 100644
index 0000000..c988baa
--- /dev/null
+++ b/go/cmd/gitaly-upload-archive/main.go
@@ -0,0 +1,45 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/handler"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/logger"
+
+ pb "gitlab.com/gitlab-org/gitaly-proto/go"
+)
+
+func init() {
+ logger.ProgName = "gitaly-upload-archive"
+}
+
+type uploadArchiveHandler func(gitalyAddress string, request *pb.SSHUploadArchiveRequest) (int32, error)
+
+func main() {
+ if err := handler.Prepare(); err != nil {
+ logger.Fatal("preparation failed", err)
+ }
+
+ code, err := uploadArchive(handler.UploadArchive, os.Args)
+
+ if err != nil {
+ logger.Fatal("upload-archive failed", err)
+ }
+
+ os.Exit(int(code))
+}
+
+func uploadArchive(handler uploadArchiveHandler, args []string) (int32, error) {
+ if n := len(args); n != 3 {
+ return 0, fmt.Errorf("wrong number of arguments: expected 2 arguments, got %v", args)
+ }
+
+ var request pb.SSHUploadArchiveRequest
+ if err := json.Unmarshal([]byte(args[2]), &request); err != nil {
+ return 0, fmt.Errorf("unmarshaling request json failed: %v", err)
+ }
+
+ return handler(args[1], &request)
+}
diff --git a/go/cmd/gitaly-upload-archive/main_test.go b/go/cmd/gitaly-upload-archive/main_test.go
new file mode 100644
index 0000000..b17da7b
--- /dev/null
+++ b/go/cmd/gitaly-upload-archive/main_test.go
@@ -0,0 +1,70 @@
+package main
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+
+ pb "gitlab.com/gitlab-org/gitaly-proto/go"
+)
+
+var testGitalyAddress = "unix:gitaly.socket"
+
+func TestUploadArchiveSuccess(t *testing.T) {
+ testRelativePath := "myrepo.git"
+ requestJSON := fmt.Sprintf(`{"repository":{"relative_path":"%s"}}`, testRelativePath)
+ mockHandler := func(gitalyAddress string, request *pb.SSHUploadArchiveRequest) (int32, error) {
+ if gitalyAddress != testGitalyAddress {
+ t.Fatalf("Expected gitaly address %s got %v", testGitalyAddress, gitalyAddress)
+ }
+ if relativePath := request.Repository.RelativePath; relativePath != testRelativePath {
+ t.Fatalf("Expected repository with relative path %s got %v", testRelativePath, request)
+ }
+ return 0, nil
+ }
+
+ code, err := uploadArchive(mockHandler, []string{"git-upload-archive", testGitalyAddress, requestJSON})
+
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if code != 0 {
+ t.Fatalf("Expected exit code 0, got %v", code)
+ }
+}
+
+func TestUploadArchiveFailure(t *testing.T) {
+ mockHandler := func(_ string, _ *pb.SSHUploadArchiveRequest) (int32, error) {
+ t.Fatal("Expected handler not to be called")
+
+ return 0, nil
+ }
+
+ tests := []struct {
+ desc string
+ args []string
+ err string
+ }{
+ {
+ desc: "With an invalid request json",
+ args: []string{"git-upload-archive", testGitalyAddress, "hello"},
+ err: "unmarshaling request json failed",
+ },
+ {
+ desc: "With an invalid argument count",
+ args: []string{"git-upload-archive", testGitalyAddress, "{}", "extra arg"},
+ err: "wrong number of arguments: expected 2 arguments",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.desc, func(t *testing.T) {
+ _, err := uploadArchive(mockHandler, test.args)
+
+ if !strings.Contains(err.Error(), test.err) {
+ t.Fatalf("Expected error %v, got %v", test.err, err)
+ }
+ })
+ }
+}