2013-06-09 22:01:28 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-07-20 00:19:05 -04:00
|
|
|
"strings"
|
2013-06-09 22:01:28 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-06-10 01:00:47 -04:00
|
|
|
type TestCache struct{}
|
|
|
|
|
|
|
|
func (TestCache) Lock(string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (TestCache) Unlock(string) {}
|
|
|
|
|
|
|
|
func (TestCache) RLock(string) (string, bool) {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (TestCache) RUnlock(string) {}
|
|
|
|
|
2013-06-09 22:01:28 -04:00
|
|
|
func TestFileCache_Implements(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &FileCache{}
|
|
|
|
if _, ok := raw.(Cache); !ok {
|
|
|
|
t.Fatal("FileCache must be a Cache")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileCache(t *testing.T) {
|
|
|
|
cacheDir, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temporary dir: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(cacheDir)
|
|
|
|
|
|
|
|
cache := &FileCache{CacheDir: cacheDir}
|
2013-12-19 21:25:01 -05:00
|
|
|
|
|
|
|
// Test path with no extension (GH-716)
|
|
|
|
path := cache.Lock("/foo.bar/baz")
|
|
|
|
defer cache.Unlock("/foo.bar/baz")
|
|
|
|
if strings.Contains(path, ".bar") {
|
|
|
|
t.Fatalf("bad: %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test paths with a ?
|
|
|
|
path = cache.Lock("foo.ext?foo=bar.foo")
|
2013-07-20 22:20:29 -04:00
|
|
|
defer cache.Unlock("foo.ext?foo=bar.foo")
|
2013-07-20 19:53:55 -04:00
|
|
|
if !strings.HasSuffix(path, ".ext") {
|
|
|
|
t.Fatalf("bad extension with question mark: %s", path)
|
|
|
|
}
|
2013-07-20 00:19:05 -04:00
|
|
|
|
2013-12-19 21:25:01 -05:00
|
|
|
// Test normal paths
|
2013-07-20 19:53:55 -04:00
|
|
|
path = cache.Lock("foo.iso")
|
2013-07-20 00:19:05 -04:00
|
|
|
if !strings.HasSuffix(path, ".iso") {
|
|
|
|
t.Fatalf("path doesn't end with suffix '%s': '%s'", ".iso", path)
|
|
|
|
}
|
2013-07-20 19:50:18 -04:00
|
|
|
|
2013-06-09 22:01:28 -04:00
|
|
|
err = ioutil.WriteFile(path, []byte("data"), 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error writing: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-07-20 00:19:05 -04:00
|
|
|
cache.Unlock("foo.iso")
|
2013-06-09 22:01:28 -04:00
|
|
|
|
2013-07-20 00:19:05 -04:00
|
|
|
path, ok := cache.RLock("foo.iso")
|
2013-06-09 22:01:28 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("cache says key doesn't exist")
|
|
|
|
}
|
2013-07-20 00:19:05 -04:00
|
|
|
defer cache.RUnlock("foo.iso")
|
2013-06-09 22:01:28 -04:00
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(data) != "data" {
|
|
|
|
t.Fatalf("unknown data: %s", data)
|
|
|
|
}
|
|
|
|
}
|