Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/registry-facade/pkg/registry/cache_test.go
2499 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package registry
6
7
import (
8
"context"
9
"testing"
10
"time"
11
12
"github.com/containerd/containerd/content"
13
"github.com/go-redis/redismock/v9"
14
"github.com/google/go-cmp/cmp"
15
"github.com/opencontainers/go-digest"
16
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
17
redis "github.com/redis/go-redis/v9"
18
)
19
20
func TestRedisBlobStore_Info(t *testing.T) {
21
ctx := context.Background()
22
23
type Expectation struct {
24
Info content.Info
25
Error string
26
}
27
tests := []struct {
28
Name string
29
Content string
30
Expectation Expectation
31
}{
32
{
33
Name: "not found",
34
Expectation: Expectation{
35
Error: "not found",
36
},
37
},
38
{
39
Name: "invalid JSON",
40
Content: "foo",
41
Expectation: Expectation{
42
Error: "cannot unmarshal blob info: invalid character 'o' in literal false (expecting 'a')",
43
},
44
},
45
{
46
Name: "valid",
47
Content: `{"Digest":"digest", "Size": 1234, "CreatedAt": 7899, "UpdatedAt": 9999, "Labels": {"foo":"bar"}}`,
48
Expectation: Expectation{
49
Info: content.Info{
50
Digest: "digest",
51
Size: 1234,
52
CreatedAt: time.Unix(7899, 0),
53
UpdatedAt: time.Unix(9999, 0),
54
Labels: map[string]string{"foo": "bar"},
55
},
56
},
57
},
58
}
59
60
for _, test := range tests {
61
t.Run(test.Name, func(t *testing.T) {
62
client, mock := redismock.NewClientMock()
63
64
dgst := digest.FromString(test.Content)
65
if test.Content == "" {
66
mock.ExpectGet("nfo." + string(dgst)).SetErr(redis.Nil)
67
} else {
68
mock.ExpectGet("nfo." + string(dgst)).SetVal(test.Content)
69
}
70
71
var (
72
act Expectation
73
err error
74
store = &RedisBlobStore{Client: client}
75
)
76
act.Info, err = store.Info(ctx, dgst)
77
if err != nil {
78
act.Error = err.Error()
79
}
80
81
if diff := cmp.Diff(test.Expectation, act); diff != "" {
82
t.Errorf("Info() mismatch (-want +got):\n%s", diff)
83
}
84
})
85
}
86
}
87
88
func TestRedisBlobStore_Writer(t *testing.T) {
89
cnt := []byte("hello world")
90
dgst := digest.FromBytes(cnt)
91
92
client, mock := redismock.NewClientMock()
93
mock.ExpectExists("cnt."+string(dgst), "nfo."+string(dgst)).SetVal(0)
94
mock.ExpectMSet(
95
"cnt."+string(dgst), string(cnt),
96
"nfo."+string(dgst), `{"Digest":"`+string(dgst)+`","Size":11,"CreatedAt":1,"UpdatedAt":1,"Labels":{"foo":"bar"}}`,
97
).SetVal("OK")
98
99
store := &RedisBlobStore{Client: client}
100
w, err := store.Writer(context.Background(), content.WithDescriptor(ociv1.Descriptor{
101
Digest: dgst,
102
MediaType: ociv1.MediaTypeImageConfig,
103
}))
104
if err != nil {
105
t.Fatal(err)
106
}
107
w.(*redisBlobWriter).forTestingOnlyTime = time.Unix(1, 1)
108
_, _ = w.Write(cnt)
109
w.Close()
110
err = w.Commit(context.Background(), int64(len(cnt)), dgst, content.WithLabels(map[string]string{"foo": "bar"}))
111
if err != nil {
112
t.Fatal(err)
113
}
114
}
115
116