Commit 3580686e authored by Tomasz Maczukin's avatar Tomasz Maczukin

Improve gitlab-runner-helper commands tests

parent 8214aea0
Pipeline #9417072 passed with stages
in 21 minutes and 3 seconds
......@@ -17,7 +17,8 @@ var downloaderCredentials = common.JobCredentials{
}
func TestArtifactsDownloaderRequirements(t *testing.T) {
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
cmd := ArtifactsDownloaderCommand{}
assert.Panics(t, func() {
......@@ -34,6 +35,9 @@ func TestArtifactsDownloaderNotFound(t *testing.T) {
network: network,
}
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
assert.Panics(t, func() {
cmd.Execute(nil)
})
......@@ -50,6 +54,9 @@ func TestArtifactsDownloaderForbidden(t *testing.T) {
network: network,
}
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
assert.Panics(t, func() {
cmd.Execute(nil)
})
......@@ -69,6 +76,9 @@ func TestArtifactsDownloaderRetry(t *testing.T) {
},
}
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
assert.Panics(t, func() {
cmd.Execute(nil)
})
......
......@@ -18,7 +18,8 @@ var UploaderCredentials = common.JobCredentials{
}
func TestArtifactsUploaderRequirements(t *testing.T) {
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
cmd := ArtifactsUploaderCommand{}
assert.Panics(t, func() {
......@@ -35,6 +36,9 @@ func TestArtifactsUploaderTooLarge(t *testing.T) {
network: network,
}
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
assert.Panics(t, func() {
cmd.Execute(nil)
})
......@@ -51,6 +55,9 @@ func TestArtifactsUploaderForbidden(t *testing.T) {
network: network,
}
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
assert.Panics(t, func() {
cmd.Execute(nil)
})
......@@ -70,6 +77,9 @@ func TestArtifactsUploaderRetry(t *testing.T) {
},
}
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
assert.Panics(t, func() {
cmd.Execute(nil)
})
......
......@@ -21,6 +21,16 @@ type CacheArchiverCommand struct {
File string `long:"file" description:"The path to file"`
URL string `long:"url" description:"URL of remote cache resource"`
Timeout int `long:"timeout" description:"Overall timeout for cache uploading request (in minutes)"`
client *CacheClient
}
func (c *CacheArchiverCommand) getClient() *CacheClient {
if c.client == nil {
c.client = NewCacheClient(c.Timeout)
}
return c.client
}
func (c *CacheArchiverCommand) upload() (bool, error) {
......@@ -45,8 +55,7 @@ func (c *CacheArchiverCommand) upload() (bool, error) {
req.Header.Set("Last-Modified", fi.ModTime().Format(http.TimeFormat))
req.ContentLength = fi.Size()
client := NewCacheClient(c.Timeout)
resp, err := client.Do(req)
resp, err := c.getClient().Do(req)
if err != nil {
return true, err
}
......
package helpers
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
......@@ -8,6 +9,7 @@ import (
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
......@@ -45,7 +47,8 @@ func TestCacheArchiverIsUpToDate(t *testing.T) {
}
func TestCacheArchiverForIfNoFileDefined(t *testing.T) {
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
cmd := CacheArchiverCommand{}
assert.Panics(t, func() {
cmd.Execute(nil)
......@@ -58,6 +61,9 @@ func testCacheUploadHandler(w http.ResponseWriter, r *http.Request) {
return
}
if r.URL.Path != "/cache.zip" {
if r.URL.Path == "/timeout" {
time.Sleep(50 * time.Millisecond)
}
http.NotFound(w, r)
return
}
......@@ -67,7 +73,8 @@ func TestCacheArchiverRemoteServerNotFound(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(testCacheUploadHandler))
defer ts.Close()
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
os.Remove(cacheExtractorArchive)
cmd := CacheArchiverCommand{
File: cacheExtractorArchive,
......@@ -79,11 +86,12 @@ func TestCacheArchiverRemoteServerNotFound(t *testing.T) {
})
}
func TestCacheArchiverRemoteServe(t *testing.T) {
func TestCacheArchiverRemoteServer(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(testCacheUploadHandler))
defer ts.Close()
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
os.Remove(cacheExtractorArchive)
cmd := CacheArchiverCommand{
File: cacheExtractorArchive,
......@@ -95,8 +103,33 @@ func TestCacheArchiverRemoteServe(t *testing.T) {
})
}
func TestCacheArchiverRemoteServerTimedOut(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(testCacheUploadHandler))
defer ts.Close()
output := logrus.StandardLogger().Out
var buf bytes.Buffer
logrus.SetOutput(&buf)
defer logrus.SetOutput(output)
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
os.Remove(cacheExtractorArchive)
cmd := CacheArchiverCommand{
File: cacheExtractorArchive,
URL: ts.URL + "/timeout",
}
cmd.getClient().Timeout = 1 * time.Millisecond
assert.Panics(t, func() {
cmd.Execute(nil)
})
assert.Contains(t, buf.String(), "net/http: request canceled (Client.Timeout")
}
func TestCacheArchiverRemoteServerFailOnInvalidServer(t *testing.T) {
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
os.Remove(cacheExtractorArchive)
cmd := CacheArchiverCommand{
File: cacheExtractorArchive,
......
......@@ -23,6 +23,16 @@ type CacheExtractorCommand struct {
File string `long:"file" description:"The file containing your cache artifacts"`
URL string `long:"url" description:"URL of remote cache resource"`
Timeout int `long:"timeout" description:"Overall timeout for cache downloading request (in minutes)"`
client *CacheClient
}
func (c *CacheExtractorCommand) getClient() *CacheClient {
if c.client == nil {
c.client = NewCacheClient(c.Timeout)
}
return c.client
}
func (c *CacheExtractorCommand) download() (bool, error) {
......@@ -35,8 +45,7 @@ func (c *CacheExtractorCommand) download() (bool, error) {
defer file.Close()
defer os.Remove(file.Name())
client := NewCacheClient(c.Timeout)
resp, err := client.Get(c.URL)
resp, err := c.getClient().Get(c.URL)
if err != nil {
return true, err
}
......
......@@ -2,6 +2,7 @@ package helpers
import (
"archive/zip"
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
......@@ -9,6 +10,7 @@ import (
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
......@@ -43,7 +45,8 @@ func TestCacheExtractorValidArchive(t *testing.T) {
}
func TestCacheExtractorForInvalidArchive(t *testing.T) {
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
ioutil.WriteFile(cacheExtractorArchive, nil, 0600)
defer os.Remove(cacheExtractorArchive)
......@@ -56,7 +59,8 @@ func TestCacheExtractorForInvalidArchive(t *testing.T) {
}
func TestCacheExtractorForIfNoFileDefined(t *testing.T) {
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
cmd := CacheExtractorCommand{}
assert.Panics(t, func() {
cmd.Execute(nil)
......@@ -64,7 +68,8 @@ func TestCacheExtractorForIfNoFileDefined(t *testing.T) {
}
func TestCacheExtractorForNotExistingFile(t *testing.T) {
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
cmd := CacheExtractorCommand{
File: "/../../../test.zip",
}
......@@ -79,6 +84,9 @@ func testServeCache(w http.ResponseWriter, r *http.Request) {
return
}
if r.URL.Path != "/cache.zip" {
if r.URL.Path == "/timeout" {
time.Sleep(50 * time.Millisecond)
}
http.NotFound(w, r)
return
}
......@@ -93,7 +101,8 @@ func TestCacheExtractorRemoteServerNotFound(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(testServeCache))
defer ts.Close()
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
cmd := CacheExtractorCommand{
File: "non-existing-test.zip",
URL: ts.URL + "/invalid-file.zip",
......@@ -106,6 +115,32 @@ func TestCacheExtractorRemoteServerNotFound(t *testing.T) {
assert.Error(t, err)
}
func TestCacheExtractorRemoteServerTimedOut(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(testServeCache))
defer ts.Close()
output := logrus.StandardLogger().Out
var buf bytes.Buffer
logrus.SetOutput(&buf)
defer logrus.SetOutput(output)
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
cmd := CacheExtractorCommand{
File: "non-existing-test.zip",
URL: ts.URL + "/timeout",
}
cmd.getClient().Timeout = 1 * time.Millisecond
assert.Panics(t, func() {
cmd.Execute(nil)
})
assert.Contains(t, buf.String(), "net/http: request canceled (Client.Timeout")
_, err := os.Stat(cacheExtractorTestArchivedFile)
assert.Error(t, err)
}
func TestCacheExtractorRemoteServer(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(testServeCache))
defer ts.Close()
......@@ -115,7 +150,8 @@ func TestCacheExtractorRemoteServer(t *testing.T) {
os.Remove(cacheExtractorArchive)
os.Remove(cacheExtractorTestArchivedFile)
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
cmd := CacheExtractorCommand{
File: cacheExtractorArchive,
URL: ts.URL + "/cache.zip",
......@@ -135,7 +171,8 @@ func TestCacheExtractorRemoteServer(t *testing.T) {
}
func TestCacheExtractorRemoteServerFailOnInvalidServer(t *testing.T) {
helpers.MakeFatalToPanic()
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
os.Remove(cacheExtractorArchive)
cmd := CacheExtractorCommand{
File: cacheExtractorArchive,
......
package helpers
import "github.com/Sirupsen/logrus"
import (
"fmt"
"io"
"github.com/Sirupsen/logrus"
)
type fatalLogHook struct {
output io.Writer
}
func (s *fatalLogHook) Levels() []logrus.Level {
......@@ -12,9 +18,28 @@ func (s *fatalLogHook) Levels() []logrus.Level {
}
func (s *fatalLogHook) Fire(e *logrus.Entry) error {
fmt.Fprint(s.output, e.Message)
panic(e)
}
func MakeFatalToPanic() {
logrus.AddHook(&fatalLogHook{})
func MakeFatalToPanic() func() {
hook := &fatalLogHook{
output: logrus.StandardLogger().Out,
}
logrus.AddHook(hook)
removeHook := func() {
for level, levelHooks := range logrus.StandardLogger().Hooks {
hooks := []logrus.Hook{}
for _, existingHook := range levelHooks {
if existingHook != hook {
hooks = append(hooks, existingHook)
}
}
logrus.StandardLogger().Hooks[level] = hooks
}
}
return removeHook
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment