github.com/ssube/gitlab-ci-multi-runner@v1.2.1-0.20160607142738-b8d1285632e6/commands/helpers/cache_extractor_test.go (about)

     1  package helpers
     2  
     3  import (
     4  	"archive/zip"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
    14  	"time"
    15  )
    16  
    17  const cacheExtractorArchive = "archive.zip"
    18  const cacheExtractorTestArchivedFile = "archive_file"
    19  
    20  func TestCacheExtractorValidArchive(t *testing.T) {
    21  	file, err := os.Create(cacheExtractorArchive)
    22  	assert.NoError(t, err)
    23  	defer file.Close()
    24  	defer os.Remove(file.Name())
    25  	defer os.Remove(cacheExtractorTestArchivedFile)
    26  
    27  	archive := zip.NewWriter(file)
    28  	archive.Create(cacheExtractorTestArchivedFile)
    29  	archive.Close()
    30  
    31  	_, err = os.Stat(cacheExtractorTestArchivedFile)
    32  	assert.Error(t, err)
    33  
    34  	cmd := CacheExtractorCommand{
    35  		File: cacheExtractorArchive,
    36  	}
    37  	assert.NotPanics(t, func() {
    38  		cmd.Execute(nil)
    39  	})
    40  
    41  	_, err = os.Stat(cacheExtractorTestArchivedFile)
    42  	assert.NoError(t, err)
    43  }
    44  
    45  func TestCacheExtractorForInvalidArchive(t *testing.T) {
    46  	helpers.MakeFatalToPanic()
    47  	ioutil.WriteFile(cacheExtractorArchive, nil, 0600)
    48  	defer os.Remove(cacheExtractorArchive)
    49  
    50  	cmd := CacheExtractorCommand{
    51  		File: cacheExtractorArchive,
    52  	}
    53  	assert.Panics(t, func() {
    54  		cmd.Execute(nil)
    55  	})
    56  }
    57  
    58  func TestCacheExtractorForIfNoFileDefined(t *testing.T) {
    59  	helpers.MakeFatalToPanic()
    60  	cmd := CacheExtractorCommand{}
    61  	assert.Panics(t, func() {
    62  		cmd.Execute(nil)
    63  	})
    64  }
    65  
    66  func TestCacheExtractorForNotExistingFile(t *testing.T) {
    67  	helpers.MakeFatalToPanic()
    68  	cmd := CacheExtractorCommand{
    69  		File: "/../../../test.zip",
    70  	}
    71  	assert.NotPanics(t, func() {
    72  		cmd.Execute(nil)
    73  	})
    74  }
    75  
    76  func testServeCache(w http.ResponseWriter, r *http.Request) {
    77  	if r.Method != "GET" {
    78  		http.Error(w, "408 Method not allowed", 408)
    79  		return
    80  	}
    81  	if r.URL.Path != "/cache.zip" {
    82  		http.NotFound(w, r)
    83  		return
    84  	}
    85  
    86  	w.Header().Set("Last-Modified", time.Now().Format(http.TimeFormat))
    87  	archive := zip.NewWriter(w)
    88  	archive.Create(cacheExtractorTestArchivedFile)
    89  	archive.Close()
    90  }
    91  
    92  func TestCacheExtractorRemoteServerNotFound(t *testing.T) {
    93  	ts := httptest.NewServer(http.HandlerFunc(testServeCache))
    94  	defer ts.Close()
    95  
    96  	helpers.MakeFatalToPanic()
    97  	cmd := CacheExtractorCommand{
    98  		File: "non-existing-test.zip",
    99  		URL:  ts.URL + "/invalid-file.zip",
   100  	}
   101  	assert.NotPanics(t, func() {
   102  		cmd.Execute(nil)
   103  	})
   104  	_, err := os.Stat(cacheExtractorTestArchivedFile)
   105  	assert.Error(t, err)
   106  }
   107  
   108  func TestCacheExtractorRemoteServer(t *testing.T) {
   109  	ts := httptest.NewServer(http.HandlerFunc(testServeCache))
   110  	defer ts.Close()
   111  
   112  	defer os.Remove(cacheExtractorArchive)
   113  	defer os.Remove(cacheExtractorTestArchivedFile)
   114  	os.Remove(cacheExtractorArchive)
   115  	os.Remove(cacheExtractorTestArchivedFile)
   116  
   117  	helpers.MakeFatalToPanic()
   118  	cmd := CacheExtractorCommand{
   119  		File: cacheExtractorArchive,
   120  		URL:  ts.URL + "/cache.zip",
   121  	}
   122  	assert.NotPanics(t, func() {
   123  		cmd.Execute(nil)
   124  	})
   125  
   126  	_, err := os.Stat(cacheExtractorTestArchivedFile)
   127  	assert.NoError(t, err)
   128  
   129  	os.Chtimes(cacheExtractorArchive, time.Now().Add(time.Hour), time.Now().Add(time.Hour))
   130  	assert.NotPanics(t, func() {
   131  		cmd.Execute(nil)
   132  	}, "archive is up to date")
   133  }
   134  
   135  func TestCacheExtractorRemoteServerDoesntFailOnInvalidServer(t *testing.T) {
   136  	helpers.MakeFatalToPanic()
   137  	os.Remove(cacheExtractorArchive)
   138  	cmd := CacheExtractorCommand{
   139  		File: cacheExtractorArchive,
   140  		URL:  "http://localhost:65333/cache.zip",
   141  	}
   142  	assert.NotPanics(t, func() {
   143  		cmd.Execute(nil)
   144  	})
   145  
   146  	_, err := os.Stat(cacheExtractorTestArchivedFile)
   147  	assert.Error(t, err)
   148  }