github.phpd.cn/thought-machine/please@v12.2.0+incompatible/tools/cache/server/cache_test.go (about)

     1  // Tests for the core cache functionality
     2  package server
     3  
     4  import (
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  
    14  	"core"
    15  )
    16  
    17  var cache *Cache
    18  
    19  const cachePath = "tools/cache/server/test_data"
    20  
    21  func init() {
    22  	cache = newCache(cachePath)
    23  	core.NewBuildState(1, nil, 4, core.DefaultConfiguration())
    24  }
    25  
    26  func TestFilesToClean(t *testing.T) {
    27  	c := newCache("test_files_to_clean")
    28  	c.cachedFiles.Set("test/artifact/1", &cachedFile{
    29  		lastReadTime: time.Now().AddDate(0, 0, -2),
    30  		readCount:    0,
    31  		size:         1000,
    32  	})
    33  	c.cachedFiles.Set("test/artifact/2", &cachedFile{
    34  		lastReadTime: time.Now().AddDate(0, 0, -5),
    35  		readCount:    0,
    36  		size:         1000,
    37  	})
    38  	c.cachedFiles.Set("test/artifact/3", &cachedFile{
    39  		lastReadTime: time.Now().AddDate(0, 0, -1),
    40  		readCount:    0,
    41  		size:         1000,
    42  	})
    43  	c.totalSize = 3000
    44  
    45  	paths := c.filesToClean(1700)
    46  	assert.Equal(t, 2, len(paths))
    47  }
    48  
    49  func TestCleanOldFiles(t *testing.T) {
    50  	c := newCache("test_clean_old_files")
    51  	c.cachedFiles.Set("test/artifact/1", &cachedFile{
    52  		lastReadTime: time.Now().AddDate(0, 0, -2),
    53  		readCount:    0,
    54  		size:         1000,
    55  	})
    56  	c.cachedFiles.Set("test/artifact/2", &cachedFile{
    57  		lastReadTime: time.Now().AddDate(0, 0, -5),
    58  		readCount:    0,
    59  		size:         1000,
    60  	})
    61  	c.cachedFiles.Set("test/artifact/3", &cachedFile{
    62  		lastReadTime: time.Now().AddDate(0, 0, -1),
    63  		readCount:    0,
    64  		size:         1000,
    65  	})
    66  	c.totalSize = 3000
    67  	assert.True(t, c.cleanOldFiles(72*time.Hour))
    68  	assert.Equal(t, 2, c.cachedFiles.Count())
    69  }
    70  
    71  func TestRetrieve(t *testing.T) {
    72  	artifact, err := cache.RetrieveArtifact("darwin_amd64/pack/label/hash/label.ext")
    73  	assert.NoError(t, err)
    74  	if artifact == nil {
    75  		t.Error("Expected artifact and found nil.")
    76  	}
    77  }
    78  
    79  func TestRetrieveError(t *testing.T) {
    80  	artifact, err := cache.RetrieveArtifact(cachePath + "/darwin_amd64/somepack/somelabel/somehash/somelabel.ext")
    81  	if artifact != nil {
    82  		t.Error("Expected nil and found artifact.")
    83  	}
    84  	if err == nil {
    85  		t.Error("Expected error and found nil.")
    86  	}
    87  }
    88  
    89  func TestGlob(t *testing.T) {
    90  	ret, err := cache.RetrieveArtifact("darwin_amd64/**/*.ext")
    91  	assert.NoError(t, err)
    92  	assert.Equal(t, 1, len(ret))
    93  	assert.Equal(t, ret[0].File, "darwin_amd64/pack/label/hash/label.ext")
    94  }
    95  
    96  func TestStore(t *testing.T) {
    97  	fileContent := "This is a newly created file."
    98  	reader := strings.NewReader(fileContent)
    99  	key, err := ioutil.ReadAll(reader)
   100  
   101  	err = cache.StoreArtifact("/darwin_amd64/somepack/somelabel/somehash/somelabel.ext", key, "")
   102  	if err != nil {
   103  		t.Error(err)
   104  	}
   105  }
   106  
   107  func TestDeleteArtifact(t *testing.T) {
   108  	err := cache.DeleteArtifact("/linux_amd64/otherpack/label")
   109  	assert.NoError(t, err)
   110  	absPath, _ := filepath.Abs(cachePath + "/linux_amd64/otherpack/label")
   111  	if _, err := os.Stat(absPath); err == nil {
   112  		t.Errorf("%s was not removed from cache.", absPath)
   113  	}
   114  }
   115  
   116  func TestDeleteAll(t *testing.T) {
   117  	err := cache.DeleteAllArtifacts()
   118  	assert.NoError(t, err)
   119  	absPath, _ := filepath.Abs(cachePath)
   120  	if files, _ := ioutil.ReadDir(absPath); len(files) != 0 {
   121  
   122  		t.Error(files[0].Name())
   123  		t.Error("The cache was not cleaned.")
   124  	}
   125  }