summaryrefslogtreecommitdiff
path: root/go/internal/command
diff options
context:
space:
mode:
Diffstat (limited to 'go/internal/command')
-rw-r--r--go/internal/command/command.go7
-rw-r--r--go/internal/command/command_test.go14
-rw-r--r--go/internal/command/healthcheck/healthcheck.go49
-rw-r--r--go/internal/command/healthcheck/healthcheck_test.go90
4 files changed, 160 insertions, 0 deletions
diff --git a/go/internal/command/command.go b/go/internal/command/command.go
index 40d92e0..52393df 100644
--- a/go/internal/command/command.go
+++ b/go/internal/command/command.go
@@ -5,6 +5,7 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/authorizedprincipals"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/discover"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/healthcheck"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/lfsauthenticate"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/receivepack"
@@ -41,6 +42,8 @@ func buildCommand(e *executable.Executable, args commandargs.CommandArgs, config
return buildAuthorizedKeysCommand(args.(*commandargs.AuthorizedKeys), config, readWriter)
case executable.AuthorizedPrincipalsCheck:
return buildAuthorizedPrincipalsCommand(args.(*commandargs.AuthorizedPrincipals), config, readWriter)
+ case executable.Healthcheck:
+ return buildHealthcheckCommand(config, readWriter)
}
return nil
@@ -72,3 +75,7 @@ func buildAuthorizedKeysCommand(args *commandargs.AuthorizedKeys, config *config
func buildAuthorizedPrincipalsCommand(args *commandargs.AuthorizedPrincipals, config *config.Config, readWriter *readwriter.ReadWriter) Command {
return &authorizedprincipals.Command{Config: config, Args: args, ReadWriter: readWriter}
}
+
+func buildHealthcheckCommand(config *config.Config, readWriter *readwriter.ReadWriter) Command {
+ return &healthcheck.Command{Config: config, ReadWriter: readWriter}
+}
diff --git a/go/internal/command/command_test.go b/go/internal/command/command_test.go
index 59f22e2..8f14db3 100644
--- a/go/internal/command/command_test.go
+++ b/go/internal/command/command_test.go
@@ -9,6 +9,7 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/authorizedkeys"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/authorizedprincipals"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/discover"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/healthcheck"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/lfsauthenticate"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/receivepack"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/shared/disallowedcommand"
@@ -23,6 +24,7 @@ import (
var (
authorizedKeysExec = &executable.Executable{Name: executable.AuthorizedKeysCheck}
authorizedPrincipalsExec = &executable.Executable{Name: executable.AuthorizedPrincipalsCheck}
+ checkExec = &executable.Executable{Name: executable.Healthcheck}
gitlabShellExec = &executable.Executable{Name: executable.GitlabShell}
basicConfig = &config.Config{GitlabUrl: "http+unix://gitlab.socket"}
@@ -35,6 +37,13 @@ func buildEnv(command string) map[string]string {
}
}
+func buildCheckAllowedEnv(command string) map[string]string {
+ out := buildEnv(command)
+ out["GITLAB_SHELL_ALLOW_CHECK_COMMAND"] = "1"
+
+ return out
+}
+
func TestNew(t *testing.T) {
testCases := []struct {
desc string
@@ -80,6 +89,11 @@ func TestNew(t *testing.T) {
expectedType: &uploadarchive.Command{},
},
{
+ desc: "it returns a Healthcheck command",
+ executable: checkExec,
+ expectedType: &healthcheck.Command{},
+ },
+ {
desc: "it returns a AuthorizedKeys command",
executable: authorizedKeysExec,
arguments: []string{"git", "git", "key"},
diff --git a/go/internal/command/healthcheck/healthcheck.go b/go/internal/command/healthcheck/healthcheck.go
new file mode 100644
index 0000000..fef981c
--- /dev/null
+++ b/go/internal/command/healthcheck/healthcheck.go
@@ -0,0 +1,49 @@
+package healthcheck
+
+import (
+ "fmt"
+
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/readwriter"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet/healthcheck"
+)
+
+var (
+ apiMessage = "Internal API available"
+ redisMessage = "Redis available via internal API"
+)
+
+type Command struct {
+ Config *config.Config
+ ReadWriter *readwriter.ReadWriter
+}
+
+func (c *Command) Execute() error {
+ response, err := c.runCheck()
+ if err != nil {
+ return fmt.Errorf("%v: FAILED - %v", apiMessage, err)
+ }
+
+ fmt.Fprintf(c.ReadWriter.Out, "%v: OK\n", apiMessage)
+
+ if !response.Redis {
+ return fmt.Errorf("%v: FAILED", redisMessage)
+ }
+
+ fmt.Fprintf(c.ReadWriter.Out, "%v: OK\n", redisMessage)
+ return nil
+}
+
+func (c *Command) runCheck() (*healthcheck.Response, error) {
+ client, err := healthcheck.NewClient(c.Config)
+ if err != nil {
+ return nil, err
+ }
+
+ response, err := client.Check()
+ if err != nil {
+ return nil, err
+ }
+
+ return response, nil
+}
diff --git a/go/internal/command/healthcheck/healthcheck_test.go b/go/internal/command/healthcheck/healthcheck_test.go
new file mode 100644
index 0000000..6c92ebc
--- /dev/null
+++ b/go/internal/command/healthcheck/healthcheck_test.go
@@ -0,0 +1,90 @@
+package healthcheck
+
+import (
+ "bytes"
+ "encoding/json"
+ "net/http"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/readwriter"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet/healthcheck"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet/testserver"
+)
+
+var (
+ okResponse = &healthcheck.Response{
+ APIVersion: "v4",
+ GitlabVersion: "v12.0.0-ee",
+ GitlabRevision: "3b13818e8330f68625d80d9bf5d8049c41fbe197",
+ Redis: true,
+ }
+
+ badRedisResponse = &healthcheck.Response{Redis: false}
+
+ okHandlers = buildTestHandlers(200, okResponse)
+ badRedisHandlers = buildTestHandlers(200, badRedisResponse)
+ brokenHandlers = buildTestHandlers(500, nil)
+)
+
+func buildTestHandlers(code int, rsp *healthcheck.Response) []testserver.TestRequestHandler {
+ return []testserver.TestRequestHandler{
+ {
+ Path: "/api/v4/internal/check",
+ Handler: func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(code)
+ if rsp != nil {
+ json.NewEncoder(w).Encode(rsp)
+ }
+ },
+ },
+ }
+}
+
+func TestExecute(t *testing.T) {
+ url, cleanup := testserver.StartSocketHttpServer(t, okHandlers)
+ defer cleanup()
+
+ buffer := &bytes.Buffer{}
+ cmd := &Command{
+ Config: &config.Config{GitlabUrl: url},
+ ReadWriter: &readwriter.ReadWriter{Out: buffer},
+ }
+
+ err := cmd.Execute()
+
+ require.NoError(t, err)
+ require.Equal(t, "Internal API available: OK\nRedis available via internal API: OK\n", buffer.String())
+}
+
+func TestFailingRedisExecute(t *testing.T) {
+ url, cleanup := testserver.StartSocketHttpServer(t, badRedisHandlers)
+ defer cleanup()
+
+ buffer := &bytes.Buffer{}
+ cmd := &Command{
+ Config: &config.Config{GitlabUrl: url},
+ ReadWriter: &readwriter.ReadWriter{Out: buffer},
+ }
+
+ err := cmd.Execute()
+ require.Error(t, err, "Redis available via internal API: FAILED")
+ require.Equal(t, "Internal API available: OK\n", buffer.String())
+}
+
+func TestFailingAPIExecute(t *testing.T) {
+ url, cleanup := testserver.StartSocketHttpServer(t, brokenHandlers)
+ defer cleanup()
+
+ buffer := &bytes.Buffer{}
+ cmd := &Command{
+ Config: &config.Config{GitlabUrl: url},
+ ReadWriter: &readwriter.ReadWriter{Out: buffer},
+ }
+
+ err := cmd.Execute()
+ require.Empty(t, buffer.String())
+ require.EqualError(t, err, "Internal API available: FAILED - Internal API error (500)")
+}