Skip to content
Snippets Groups Projects
Verified Commit fff8685d authored by Tomasz Maczukin's avatar Tomasz Maczukin :speech_balloon:
Browse files

Log error if API v4 is not present (GitLab CE/EE is older than 9.0)

parent f2060a0e
No related branches found
No related tags found
1 merge request!528Log error if API v4 is not present (GitLab CE/EE is older than 9.0)
......@@ -21,6 +21,8 @@ import (
const clientError = -100
var notSupportingGitLabPre90Message = "GitLab Runner >= 9.0 supports ONLY GitLab CE/EE >= 9.0"
type GitLabClient struct {
clients map[string]*client
lock sync.Mutex
......@@ -113,6 +115,12 @@ func (n *GitLabClient) RegisterRunner(runner common.RunnerCredentials, descripti
case 403:
runner.Log().Errorln("Registering runner...", "forbidden (check registration token)")
return nil
case 404:
runner.Log().WithFields(logrus.Fields{
"status": statusText,
"reason": notSupportingGitLabPre90Message,
}).Errorln("Registering runner...", "not-compatible")
return nil
case clientError:
runner.Log().WithField("status", statusText).Errorln("Registering runner...", "error")
return nil
......@@ -137,6 +145,12 @@ func (n *GitLabClient) VerifyRunner(runner common.RunnerCredentials) bool {
case 403:
runner.Log().Errorln("Verifying runner...", "is removed")
return false
case 404:
runner.Log().WithFields(logrus.Fields{
"status": statusText,
"reason": notSupportingGitLabPre90Message,
}).Errorln("Verifying runner...", "not-compatible")
return false
case clientError:
runner.Log().WithField("status", statusText).Errorln("Verifying runner...", "error")
return false
......@@ -151,7 +165,7 @@ func (n *GitLabClient) UnregisterRunner(runner common.RunnerCredentials) bool {
Token: runner.Token,
}
result, statusText, _ := n.doJSON(&runner, "DELETE", "runners", 200, &request, nil)
result, statusText, _ := n.doJSON(&runner, "DELETE", "runners", 204, &request, nil)
switch result {
case 204:
......@@ -160,6 +174,12 @@ func (n *GitLabClient) UnregisterRunner(runner common.RunnerCredentials) bool {
case 403:
runner.Log().Errorln("Deleting runner...", "forbidden")
return false
case 404:
runner.Log().WithFields(logrus.Fields{
"status": statusText,
"reason": notSupportingGitLabPre90Message,
}).Errorln("Deleting runner...", "not-compatible")
return false
case clientError:
runner.Log().WithField("status", statusText).Errorln("Deleting runner...", "error")
return false
......@@ -193,6 +213,12 @@ func (n *GitLabClient) RequestJob(config common.RunnerConfig) (*common.JobRespon
case 204:
config.Log().Debugln("Checking for jobs...", "nothing")
return nil, true
case 404:
config.Log().WithFields(logrus.Fields{
"status": statusText,
"reason": notSupportingGitLabPre90Message,
}).Errorln("checking for jobs...", "not-compatible")
return nil, false
case clientError:
config.Log().WithField("status", statusText).Errorln("Checking for jobs...", "error")
return nil, false
......
......@@ -14,6 +14,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
. "gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
)
......@@ -859,3 +860,77 @@ func TestArtifactsDownload(t *testing.T) {
state = c.DownloadArtifacts(fileNotFoundTokenCredentials, artifactsFileName)
assert.Equal(t, DownloadNotFound, state, "Artifacts should be bit downloaded if it's not found")
}
func prepareAPIv4CompatibilityTestEnvironment(handler func(w http.ResponseWriter, r *http.Request)) (server *httptest.Server, runner RunnerConfig) {
server = httptest.NewServer(http.HandlerFunc(handler))
runner = RunnerConfig{
RunnerCredentials: RunnerCredentials{
URL: server.URL,
Token: "valid",
},
}
return
}
func TestAPIv4Compatibility(t *testing.T) {
serverPre90, runnerPre90 := prepareAPIv4CompatibilityTestEnvironment(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v4/runners":
w.WriteHeader(404)
case "/api/v4/runners/verify":
w.WriteHeader(404)
case "/api/v4/jobs/request":
w.WriteHeader(404)
}
})
defer serverPre90.Close()
server90, runner90 := prepareAPIv4CompatibilityTestEnvironment(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v4/runners":
switch r.Method {
case "POST":
res := make(map[string]interface{})
res["token"] = "token-here"
output, err := json.Marshal(res)
require.NoError(t, err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(201)
w.Write(output)
case "DELETE":
w.WriteHeader(204)
}
case "/api/v4/runners/verify":
w.WriteHeader(200)
case "/api/v4/jobs/request":
w.WriteHeader(204)
}
})
defer server90.Close()
c := NewGitLabClient()
resp := c.RegisterRunner(runnerPre90.RunnerCredentials, "", "", false, false)
assert.Nil(t, resp, "Request against GitLab < 9.0 should not register Runner")
resp = c.RegisterRunner(runner90.RunnerCredentials, "", "", false, false)
assert.NotNil(t, resp, "Request against GitLab < 9.0 should register Runner")
ok := c.VerifyRunner(runnerPre90.RunnerCredentials)
assert.False(t, ok, "Request against GitLab < 9.0 should not verify Runner")
ok = c.VerifyRunner(runner90.RunnerCredentials)
assert.True(t, ok, "Request against GitLab < 9.0 should verify Runner")
ok = c.UnregisterRunner(runnerPre90.RunnerCredentials)
assert.False(t, ok, "Request against GitLab < 9.0 should not unregister Runner")
ok = c.UnregisterRunner(runner90.RunnerCredentials)
assert.True(t, ok, "Request against GitLab < 9.0 should unregister Runner")
_, healthy := c.RequestJob(runnerPre90)
assert.False(t, healthy, "Request against GitLab < 9.0 should mark Runner as unhealthy")
_, healthy = c.RequestJob(runner90)
assert.True(t, healthy, "Request against GitLab >= 9.0 should mark Runner as healthy")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment