Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 75eb378

Browse files
committed
add unit tests for caching
1 parent 2c19994 commit 75eb378

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

util/file_cache_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright 2017 Google, Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"io/ioutil"
23+
"os"
24+
"testing"
25+
26+
"github.com/GoogleCloudPlatform/container-diff/pkg/cache"
27+
)
28+
29+
var c *cache.FileCache
30+
31+
func TestCacheData(t *testing.T) {
32+
data := "this is a test of caching some bytes. this could be any data."
33+
cacheAndTest(t, data, "sha256:realdata")
34+
}
35+
36+
func TestCacheEmpty(t *testing.T) {
37+
cacheAndTest(t, "", "sha256:emptydata")
38+
}
39+
40+
func cacheAndTest(t *testing.T, testStr string, layerId string) {
41+
byteArr := []byte(testStr)
42+
r := bytes.NewReader(byteArr)
43+
44+
if c.HasLayer(layerId) {
45+
t.Errorf("cache already has test layer %s", layerId)
46+
}
47+
c.SetLayer(layerId, r)
48+
49+
if !c.HasLayer(layerId) {
50+
t.Errorf("layer %s not successfully cached", layerId)
51+
}
52+
cachedLayer, err := c.GetLayer(layerId)
53+
if err != nil {
54+
t.Errorf(err.Error())
55+
}
56+
cachedData, err := ioutil.ReadAll(cachedLayer)
57+
cachedStr := string(cachedData)
58+
if cachedStr != testStr {
59+
t.Errorf("cached data %s does not match original: %s", cachedStr, testStr)
60+
}
61+
}
62+
63+
func TestMain(m *testing.M) {
64+
var err error
65+
cacheDir, err := ioutil.TempDir("", ".cache")
66+
if err != nil {
67+
fmt.Printf(err.Error())
68+
os.Exit(1)
69+
}
70+
defer os.RemoveAll(cacheDir)
71+
c, err = cache.NewFileCache(cacheDir)
72+
if err != nil {
73+
fmt.Printf(err.Error())
74+
os.Exit(1)
75+
}
76+
os.Exit(m.Run())
77+
}

0 commit comments

Comments
 (0)