github.com/alloyci/alloy-runner@v1.0.1-0.20180222164613-925503ccafd6/commands/helpers/cache_archiver_test.go (about)

     1  package helpers
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/Sirupsen/logrus"
    13  	"github.com/stretchr/testify/assert"
    14  
    15  	"gitlab.com/gitlab-org/gitlab-runner/helpers"
    16  )
    17  
    18  const cacheArchiverArchive = "archive.zip"
    19  const cacheArchiverTestArchivedFile = "archive_file"
    20  
    21  func TestCacheArchiverIsUpToDate(t *testing.T) {
    22  	ioutil.WriteFile(cacheArchiverTestArchivedFile, nil, 0600)
    23  	defer os.Remove(cacheArchiverTestArchivedFile)
    24  
    25  	defer os.Remove(cacheArchiverArchive)
    26  	cmd := CacheArchiverCommand{
    27  		File: cacheArchiverArchive,
    28  		fileArchiver: fileArchiver{
    29  			Paths: []string{
    30  				cacheArchiverTestArchivedFile,
    31  			},
    32  		},
    33  	}
    34  	cmd.Execute(nil)
    35  	fi, _ := os.Stat(cacheArchiverArchive)
    36  	cmd.Execute(nil)
    37  	fi2, _ := os.Stat(cacheArchiverArchive)
    38  	assert.Equal(t, fi.ModTime(), fi2.ModTime(), "archive is up to date")
    39  
    40  	// We need to wait one second, since the FS doesn't save milliseconds
    41  	time.Sleep(time.Second)
    42  
    43  	os.Chtimes(cacheArchiverTestArchivedFile, time.Now(), time.Now())
    44  	cmd.Execute(nil)
    45  	fi3, _ := os.Stat(cacheArchiverArchive)
    46  	assert.NotEqual(t, fi.ModTime(), fi3.ModTime(), "archive should get updated")
    47  }
    48  
    49  func TestCacheArchiverForIfNoFileDefined(t *testing.T) {
    50  	removeHook := helpers.MakeFatalToPanic()
    51  	defer removeHook()
    52  	cmd := CacheArchiverCommand{}
    53  	assert.Panics(t, func() {
    54  		cmd.Execute(nil)
    55  	})
    56  }
    57  
    58  func testCacheUploadHandler(w http.ResponseWriter, r *http.Request) {
    59  	if r.Method != "PUT" {
    60  		http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
    61  		return
    62  	}
    63  	if r.URL.Path != "/cache.zip" {
    64  		if r.URL.Path == "/timeout" {
    65  			time.Sleep(50 * time.Millisecond)
    66  		}
    67  		http.NotFound(w, r)
    68  		return
    69  	}
    70  }
    71  
    72  func TestCacheArchiverRemoteServerNotFound(t *testing.T) {
    73  	ts := httptest.NewServer(http.HandlerFunc(testCacheUploadHandler))
    74  	defer ts.Close()
    75  
    76  	removeHook := helpers.MakeFatalToPanic()
    77  	defer removeHook()
    78  	os.Remove(cacheExtractorArchive)
    79  	cmd := CacheArchiverCommand{
    80  		File:    cacheExtractorArchive,
    81  		URL:     ts.URL + "/invalid-file.zip",
    82  		Timeout: 0,
    83  	}
    84  	assert.Panics(t, func() {
    85  		cmd.Execute(nil)
    86  	})
    87  }
    88  
    89  func TestCacheArchiverRemoteServer(t *testing.T) {
    90  	ts := httptest.NewServer(http.HandlerFunc(testCacheUploadHandler))
    91  	defer ts.Close()
    92  
    93  	removeHook := helpers.MakeFatalToPanic()
    94  	defer removeHook()
    95  	os.Remove(cacheExtractorArchive)
    96  	cmd := CacheArchiverCommand{
    97  		File:    cacheExtractorArchive,
    98  		URL:     ts.URL + "/cache.zip",
    99  		Timeout: 0,
   100  	}
   101  	assert.NotPanics(t, func() {
   102  		cmd.Execute(nil)
   103  	})
   104  }
   105  
   106  func TestCacheArchiverRemoteServerTimedOut(t *testing.T) {
   107  	ts := httptest.NewServer(http.HandlerFunc(testCacheUploadHandler))
   108  	defer ts.Close()
   109  
   110  	output := logrus.StandardLogger().Out
   111  	var buf bytes.Buffer
   112  	logrus.SetOutput(&buf)
   113  	defer logrus.SetOutput(output)
   114  	removeHook := helpers.MakeFatalToPanic()
   115  	defer removeHook()
   116  
   117  	os.Remove(cacheExtractorArchive)
   118  	cmd := CacheArchiverCommand{
   119  		File: cacheExtractorArchive,
   120  		URL:  ts.URL + "/timeout",
   121  	}
   122  	cmd.getClient().Timeout = 1 * time.Millisecond
   123  
   124  	assert.Panics(t, func() {
   125  		cmd.Execute(nil)
   126  	})
   127  	assert.Contains(t, buf.String(), "net/http: request canceled (Client.Timeout")
   128  }
   129  
   130  func TestCacheArchiverRemoteServerFailOnInvalidServer(t *testing.T) {
   131  	removeHook := helpers.MakeFatalToPanic()
   132  	defer removeHook()
   133  	os.Remove(cacheExtractorArchive)
   134  	cmd := CacheArchiverCommand{
   135  		File:    cacheExtractorArchive,
   136  		URL:     "http://localhost:65333/cache.zip",
   137  		Timeout: 0,
   138  	}
   139  	assert.Panics(t, func() {
   140  		cmd.Execute(nil)
   141  	})
   142  
   143  	_, err := os.Stat(cacheExtractorTestArchivedFile)
   144  	assert.Error(t, err)
   145  }