|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | + |
| 10 | + . "gopkg.in/check.v1" |
| 11 | + |
| 12 | + chisel "github.com/canonical/chisel/cmd/chisel" |
| 13 | +) |
| 14 | + |
| 15 | +var cacheExpiryTests = []struct { |
| 16 | + value string |
| 17 | + expiry time.Duration |
| 18 | + err string |
| 19 | +}{ |
| 20 | + {value: "", expiry: 7 * 24 * time.Hour}, |
| 21 | + {value: "0", expiry: 0}, |
| 22 | + {value: "1", expiry: 24 * time.Hour}, |
| 23 | + {value: "30", expiry: 30 * 24 * time.Hour}, |
| 24 | + {value: "65535", expiry: 65535 * 24 * time.Hour}, |
| 25 | + {value: "65536", err: `invalid CHISEL_CACHE_EXPIRY "65536", expected a number of days`}, |
| 26 | + {value: "-1", err: `invalid CHISEL_CACHE_EXPIRY "-1", expected a number of days`}, |
| 27 | + {value: "7d", err: `invalid CHISEL_CACHE_EXPIRY "7d", expected a number of days`}, |
| 28 | + {value: "1.5", err: `invalid CHISEL_CACHE_EXPIRY "1.5", expected a number of days`}, |
| 29 | + {value: " 7", err: `invalid CHISEL_CACHE_EXPIRY " 7", expected a number of days`}, |
| 30 | +} |
| 31 | + |
| 32 | +func (s *ChiselSuite) TestCacheExpiry(c *C) { |
| 33 | + for _, test := range cacheExpiryTests { |
| 34 | + c.Logf("CHISEL_CACHE_EXPIRY=%q", test.value) |
| 35 | + restore := fakeEnv("CHISEL_CACHE_EXPIRY", test.value) |
| 36 | + expiry, err := chisel.CacheExpiry() |
| 37 | + restore() |
| 38 | + if test.err != "" { |
| 39 | + c.Assert(err, ErrorMatches, test.err) |
| 40 | + continue |
| 41 | + } |
| 42 | + c.Assert(err, IsNil) |
| 43 | + c.Assert(expiry, Equals, test.expiry) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +var expireCacheTests = []struct { |
| 48 | + value string |
| 49 | + expired bool |
| 50 | + log string |
| 51 | +}{ |
| 52 | + {value: "", expired: true}, |
| 53 | + {value: "7", expired: true}, |
| 54 | + {value: "30", expired: false}, |
| 55 | + {value: "0", expired: false}, |
| 56 | + {value: "never", expired: false, log: "Warning: invalid CHISEL_CACHE_EXPIRY \"never\", expected a number of days; the cache is not expired\n"}, |
| 57 | +} |
| 58 | + |
| 59 | +func (s *ChiselSuite) TestExpireCache(c *C) { |
| 60 | + var logs bytes.Buffer |
| 61 | + chisel.SetLogger(log.New(&logs, "", 0)) |
| 62 | + defer chisel.SetLogger(nil) |
| 63 | + |
| 64 | + for _, test := range expireCacheTests { |
| 65 | + c.Logf("CHISEL_CACHE_EXPIRY=%q", test.value) |
| 66 | + logs.Reset() |
| 67 | + |
| 68 | + dir := c.MkDir() |
| 69 | + entry := filepath.Join(dir, "sha256", "5b41362bc82b7f3d56edc5a306db22105707d01ff4819e26faef9724a2d406c9") |
| 70 | + err := os.MkdirAll(filepath.Dir(entry), 0755) |
| 71 | + c.Assert(err, IsNil) |
| 72 | + err = os.WriteFile(entry, []byte("data1"), 0644) |
| 73 | + c.Assert(err, IsNil) |
| 74 | + lastUsed := time.Now().Add(-8 * 24 * time.Hour) |
| 75 | + err = os.Chtimes(entry, lastUsed, lastUsed) |
| 76 | + c.Assert(err, IsNil) |
| 77 | + |
| 78 | + restore := fakeEnv("CHISEL_CACHE_EXPIRY", test.value) |
| 79 | + chisel.ExpireCache(dir) |
| 80 | + restore() |
| 81 | + |
| 82 | + _, err = os.Stat(entry) |
| 83 | + c.Assert(os.IsNotExist(err), Equals, test.expired) |
| 84 | + c.Assert(logs.String(), Equals, test.log) |
| 85 | + } |
| 86 | +} |
0 commit comments