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