gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/commands/helpers/cache_extractor_test.go (about)

     1  package helpers
     2  
     3  import (
     4  	"archive/zip"
     5  	"bytes"
     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 cacheExtractorArchive = "archive.zip"
    19  const cacheExtractorTestArchivedFile = "archive_file"
    20  
    21  func TestCacheExtractorValidArchive(t *testing.T) {
    22  	file, err := os.Create(cacheExtractorArchive)
    23  	assert.NoError(t, err)
    24  	defer file.Close()
    25  	defer os.Remove(file.Name())
    26  	defer os.Remove(cacheExtractorTestArchivedFile)
    27  
    28  	archive := zip.NewWriter(file)
    29  	archive.Create(cacheExtractorTestArchivedFile)
    30  	archive.Close()
    31  
    32  	_, err = os.Stat(cacheExtractorTestArchivedFile)
    33  	assert.Error(t, err)
    34  
    35  	cmd := CacheExtractorCommand{
    36  		File: cacheExtractorArchive,
    37  	}
    38  	assert.NotPanics(t, func() {
    39  		cmd.Execute(nil)
    40  	})
    41  
    42  	_, err = os.Stat(cacheExtractorTestArchivedFile)
    43  	assert.NoError(t, err)
    44  }
    45  
    46  func TestCacheExtractorForInvalidArchive(t *testing.T) {
    47  	removeHook := helpers.MakeFatalToPanic()
    48  	defer removeHook()
    49  	writeTestFile(t, cacheExtractorArchive)
    50  	defer os.Remove(cacheExtractorArchive)
    51  
    52  	cmd := CacheExtractorCommand{
    53  		File: cacheExtractorArchive,
    54  	}
    55  	assert.Panics(t, func() {
    56  		cmd.Execute(nil)
    57  	})
    58  }
    59  
    60  func TestCacheExtractorForIfNoFileDefined(t *testing.T) {
    61  	removeHook := helpers.MakeFatalToPanic()
    62  	defer removeHook()
    63  	cmd := CacheExtractorCommand{}
    64  	assert.Panics(t, func() {
    65  		cmd.Execute(nil)
    66  	})
    67  }
    68  
    69  func TestCacheExtractorForNotExistingFile(t *testing.T) {
    70  	removeHook := helpers.MakeFatalToPanic()
    71  	defer removeHook()
    72  	cmd := CacheExtractorCommand{
    73  		File: "/../../../test.zip",
    74  	}
    75  	assert.NotPanics(t, func() {
    76  		cmd.Execute(nil)
    77  	})
    78  }
    79  
    80  func testServeCache(w http.ResponseWriter, r *http.Request) {
    81  	if r.Method != "GET" {
    82  		http.Error(w, "408 Method not allowed", 408)
    83  		return
    84  	}
    85  	if r.URL.Path != "/cache.zip" {
    86  		if r.URL.Path == "/timeout" {
    87  			time.Sleep(50 * time.Millisecond)
    88  		}
    89  		http.NotFound(w, r)
    90  		return
    91  	}
    92  
    93  	w.Header().Set("Last-Modified", time.Now().Format(http.TimeFormat))
    94  	archive := zip.NewWriter(w)
    95  	archive.Create(cacheExtractorTestArchivedFile)
    96  	archive.Close()
    97  }
    98  
    99  func TestCacheExtractorRemoteServerNotFound(t *testing.T) {
   100  	ts := httptest.NewServer(http.HandlerFunc(testServeCache))
   101  	defer ts.Close()
   102  
   103  	removeHook := helpers.MakeFatalToPanic()
   104  	defer removeHook()
   105  	cmd := CacheExtractorCommand{
   106  		File:    "non-existing-test.zip",
   107  		URL:     ts.URL + "/invalid-file.zip",
   108  		Timeout: 0,
   109  	}
   110  	assert.Panics(t, func() {
   111  		cmd.Execute(nil)
   112  	})
   113  	_, err := os.Stat(cacheExtractorTestArchivedFile)
   114  	assert.Error(t, err)
   115  }
   116  
   117  func TestCacheExtractorRemoteServerTimedOut(t *testing.T) {
   118  	ts := httptest.NewServer(http.HandlerFunc(testServeCache))
   119  	defer ts.Close()
   120  
   121  	output := logrus.StandardLogger().Out
   122  	var buf bytes.Buffer
   123  	logrus.SetOutput(&buf)
   124  	defer logrus.SetOutput(output)
   125  	removeHook := helpers.MakeFatalToPanic()
   126  	defer removeHook()
   127  
   128  	cmd := CacheExtractorCommand{
   129  		File: "non-existing-test.zip",
   130  		URL:  ts.URL + "/timeout",
   131  	}
   132  	cmd.getClient().Timeout = 1 * time.Millisecond
   133  
   134  	assert.Panics(t, func() {
   135  		cmd.Execute(nil)
   136  	})
   137  	assert.Contains(t, buf.String(), "Client.Timeout")
   138  
   139  	_, err := os.Stat(cacheExtractorTestArchivedFile)
   140  	assert.Error(t, err)
   141  }
   142  
   143  func TestCacheExtractorRemoteServer(t *testing.T) {
   144  	ts := httptest.NewServer(http.HandlerFunc(testServeCache))
   145  	defer ts.Close()
   146  
   147  	defer os.Remove(cacheExtractorArchive)
   148  	defer os.Remove(cacheExtractorTestArchivedFile)
   149  	os.Remove(cacheExtractorArchive)
   150  	os.Remove(cacheExtractorTestArchivedFile)
   151  
   152  	removeHook := helpers.MakeFatalToPanic()
   153  	defer removeHook()
   154  	cmd := CacheExtractorCommand{
   155  		File:    cacheExtractorArchive,
   156  		URL:     ts.URL + "/cache.zip",
   157  		Timeout: 0,
   158  	}
   159  	assert.NotPanics(t, func() {
   160  		cmd.Execute(nil)
   161  	})
   162  
   163  	_, err := os.Stat(cacheExtractorTestArchivedFile)
   164  	assert.NoError(t, err)
   165  
   166  	os.Chtimes(cacheExtractorArchive, time.Now().Add(time.Hour), time.Now().Add(time.Hour))
   167  	assert.NotPanics(t, func() {
   168  		cmd.Execute(nil)
   169  	}, "archive is up to date")
   170  }
   171  
   172  func TestCacheExtractorRemoteServerFailOnInvalidServer(t *testing.T) {
   173  	removeHook := helpers.MakeFatalToPanic()
   174  	defer removeHook()
   175  	os.Remove(cacheExtractorArchive)
   176  	cmd := CacheExtractorCommand{
   177  		File:    cacheExtractorArchive,
   178  		URL:     "http://localhost:65333/cache.zip",
   179  		Timeout: 0,
   180  	}
   181  	assert.Panics(t, func() {
   182  		cmd.Execute(nil)
   183  	})
   184  
   185  	_, err := os.Stat(cacheExtractorTestArchivedFile)
   186  	assert.Error(t, err)
   187  }