summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Newdigate <andrew@gitlab.com>2019-03-06 17:11:07 +0200
committerAndrew Newdigate <andrew@gitlab.com>2019-03-18 14:03:49 +0200
commit517e83acf87dac0c3b9b3ba5e78043ded5d248d9 (patch)
tree86e892020a9a1fbe24e0b273b1d60c5d340eba2b
parentf237aba6df1c1873f1f9d5ba18c3b8924d85cb51 (diff)
downloadgitlab-shell-an-improve-test-coverage.tar.gz
Better testing for JSON deserialisationan-improve-test-coverage
Adds tests to ensure that JSON deserialisation from program arguments takes place. This was a testing blind spot, which led to a brief regression: https://gitlab.com/gitlab-org/gitlab-shell/merge_requests/280
-rw-r--r--go/cmd/gitaly-receive-pack/main_test.go59
-rw-r--r--go/cmd/gitaly-upload-archive/main_test.go59
-rw-r--r--go/cmd/gitaly-upload-pack/main_test.go59
3 files changed, 177 insertions, 0 deletions
diff --git a/go/cmd/gitaly-receive-pack/main_test.go b/go/cmd/gitaly-receive-pack/main_test.go
new file mode 100644
index 0000000..e98b2f6
--- /dev/null
+++ b/go/cmd/gitaly-receive-pack/main_test.go
@@ -0,0 +1,59 @@
+package main
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ pb "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
+)
+
+func Test_deserialize(t *testing.T) {
+ tests := []struct {
+ name string
+ requestJSON string
+ want *pb.SSHReceivePackRequest
+ wantErr bool
+ }{
+ {
+ name: "empty",
+ requestJSON: "",
+ want: nil,
+ wantErr: true,
+ },
+ {
+ name: "empty_hash",
+ requestJSON: "{}",
+ want: &pb.SSHReceivePackRequest{},
+ wantErr: false,
+ },
+ {
+ name: "nil",
+ requestJSON: "null",
+ want: &pb.SSHReceivePackRequest{},
+ wantErr: false,
+ },
+ {
+ name: "values",
+ requestJSON: `{"gl_id": "1234"}`,
+ want: &pb.SSHReceivePackRequest{GlId: "1234"},
+ wantErr: false,
+ },
+ {
+ name: "invalid_json",
+ requestJSON: `{"gl_id": "1234`,
+ want: nil,
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := deserialize(tt.requestJSON)
+ require.EqualValues(t, got, tt.want, "Got %+v, wanted %+v", got, tt.want)
+ if tt.wantErr {
+ require.Error(t, err, "Wanted an error, got %+v", err)
+ } else {
+ require.NoError(t, err, "Wanted no error, got %+v", err)
+ }
+ })
+ }
+}
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..e1fd919
--- /dev/null
+++ b/go/cmd/gitaly-upload-archive/main_test.go
@@ -0,0 +1,59 @@
+package main
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ pb "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
+)
+
+func Test_deserialize(t *testing.T) {
+ tests := []struct {
+ name string
+ requestJSON string
+ want *pb.SSHUploadArchiveRequest
+ wantErr bool
+ }{
+ {
+ name: "empty",
+ requestJSON: "",
+ want: nil,
+ wantErr: true,
+ },
+ {
+ name: "empty_hash",
+ requestJSON: "{}",
+ want: &pb.SSHUploadArchiveRequest{},
+ wantErr: false,
+ },
+ {
+ name: "nil",
+ requestJSON: "null",
+ want: &pb.SSHUploadArchiveRequest{},
+ wantErr: false,
+ },
+ {
+ name: "values",
+ requestJSON: `{"repository": { "storage_name": "12345"} }`,
+ want: &pb.SSHUploadArchiveRequest{Repository: &pb.Repository{StorageName: "12345"}},
+ wantErr: false,
+ },
+ {
+ name: "invalid_json",
+ requestJSON: `{"gl_id": "1234`,
+ want: nil,
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := deserialize(tt.requestJSON)
+ require.EqualValues(t, got, tt.want, "Got %+v, wanted %+v", got, tt.want)
+ if tt.wantErr {
+ require.Error(t, err, "Wanted an error, got %+v", err)
+ } else {
+ require.NoError(t, err, "Wanted no error, got %+v", err)
+ }
+ })
+ }
+}
diff --git a/go/cmd/gitaly-upload-pack/main_test.go b/go/cmd/gitaly-upload-pack/main_test.go
new file mode 100644
index 0000000..7bd6765
--- /dev/null
+++ b/go/cmd/gitaly-upload-pack/main_test.go
@@ -0,0 +1,59 @@
+package main
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ pb "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
+)
+
+func Test_deserialize(t *testing.T) {
+ tests := []struct {
+ name string
+ requestJSON string
+ want *pb.SSHUploadPackRequest
+ wantErr bool
+ }{
+ {
+ name: "empty",
+ requestJSON: "",
+ want: nil,
+ wantErr: true,
+ },
+ {
+ name: "empty_hash",
+ requestJSON: "{}",
+ want: &pb.SSHUploadPackRequest{},
+ wantErr: false,
+ },
+ {
+ name: "nil",
+ requestJSON: "null",
+ want: &pb.SSHUploadPackRequest{},
+ wantErr: false,
+ },
+ {
+ name: "values",
+ requestJSON: `{"repository": { "storage_name": "12345"} }`,
+ want: &pb.SSHUploadPackRequest{Repository: &pb.Repository{StorageName: "12345"}},
+ wantErr: false,
+ },
+ {
+ name: "invalid_json",
+ requestJSON: `{"gl_id": "1234`,
+ want: nil,
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := deserialize(tt.requestJSON)
+ require.EqualValues(t, got, tt.want, "Got %+v, wanted %+v", got, tt.want)
+ if tt.wantErr {
+ require.Error(t, err, "Wanted an error, got %+v", err)
+ } else {
+ require.NoError(t, err, "Wanted no error, got %+v", err)
+ }
+ })
+ }
+}