github.com/tiagovtristao/plz@v13.4.0+incompatible/src/cache/http_cache_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http/httptest"
     6  	"path"
     7  	"path/filepath"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/thought-machine/please/src/core"
    12  	"github.com/thought-machine/please/tools/cache/server"
    13  )
    14  
    15  var (
    16  	label     core.BuildLabel
    17  	target    *core.BuildTarget
    18  	httpcache *httpCache
    19  	key       []byte
    20  )
    21  
    22  func init() {
    23  	label = core.NewBuildLabel("pkg/name", "label_name")
    24  	target = core.NewBuildTarget(label)
    25  
    26  	// Arbitrary large numbers so the cleaner never needs to run.
    27  	cache := server.NewCache("src/cache/test_data", 20*time.Hour, 100000, 100000000, 1000000000)
    28  	key, _ = ioutil.ReadFile("src/cache/test_data/testfile")
    29  	testServer := httptest.NewServer(server.BuildRouter(cache))
    30  
    31  	config := core.DefaultConfiguration()
    32  	config.Cache.HTTPURL.UnmarshalFlag(testServer.URL)
    33  	config.Cache.HTTPWriteable = true
    34  	httpcache = newHTTPCache(config)
    35  }
    36  
    37  func TestStore(t *testing.T) {
    38  	target.AddOutput("testfile")
    39  	httpcache.Store(target, []byte("test_key"))
    40  	abs, _ := filepath.Abs(path.Join("src/cache/test_data", core.OsArch, "pkg/name", "label_name"))
    41  	if !core.PathExists(abs) {
    42  		t.Errorf("Test file %s was not stored in cache.", abs)
    43  	}
    44  }
    45  
    46  func TestRetrieve(t *testing.T) {
    47  	if !httpcache.Retrieve(target, []byte("test_key")) {
    48  		t.Error("Artifact expected and not found.")
    49  	}
    50  }
    51  
    52  func TestClean(t *testing.T) {
    53  	httpcache.Clean(target)
    54  	filename := path.Join("src/cache/test_data", core.OsArch, "pkg/name/label_name")
    55  	if core.PathExists(filename) {
    56  		t.Errorf("File %s was not removed from cache.", filename)
    57  	}
    58  }