github.com/nilium/gitlab-runner@v12.5.0+incompatible/commands/helpers/cache_archiver_test.go (about)

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