Path: blob/main/components/registry-facade/pkg/registry/cache_test.go
2499 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package registry56import (7"context"8"testing"9"time"1011"github.com/containerd/containerd/content"12"github.com/go-redis/redismock/v9"13"github.com/google/go-cmp/cmp"14"github.com/opencontainers/go-digest"15ociv1 "github.com/opencontainers/image-spec/specs-go/v1"16redis "github.com/redis/go-redis/v9"17)1819func TestRedisBlobStore_Info(t *testing.T) {20ctx := context.Background()2122type Expectation struct {23Info content.Info24Error string25}26tests := []struct {27Name string28Content string29Expectation Expectation30}{31{32Name: "not found",33Expectation: Expectation{34Error: "not found",35},36},37{38Name: "invalid JSON",39Content: "foo",40Expectation: Expectation{41Error: "cannot unmarshal blob info: invalid character 'o' in literal false (expecting 'a')",42},43},44{45Name: "valid",46Content: `{"Digest":"digest", "Size": 1234, "CreatedAt": 7899, "UpdatedAt": 9999, "Labels": {"foo":"bar"}}`,47Expectation: Expectation{48Info: content.Info{49Digest: "digest",50Size: 1234,51CreatedAt: time.Unix(7899, 0),52UpdatedAt: time.Unix(9999, 0),53Labels: map[string]string{"foo": "bar"},54},55},56},57}5859for _, test := range tests {60t.Run(test.Name, func(t *testing.T) {61client, mock := redismock.NewClientMock()6263dgst := digest.FromString(test.Content)64if test.Content == "" {65mock.ExpectGet("nfo." + string(dgst)).SetErr(redis.Nil)66} else {67mock.ExpectGet("nfo." + string(dgst)).SetVal(test.Content)68}6970var (71act Expectation72err error73store = &RedisBlobStore{Client: client}74)75act.Info, err = store.Info(ctx, dgst)76if err != nil {77act.Error = err.Error()78}7980if diff := cmp.Diff(test.Expectation, act); diff != "" {81t.Errorf("Info() mismatch (-want +got):\n%s", diff)82}83})84}85}8687func TestRedisBlobStore_Writer(t *testing.T) {88cnt := []byte("hello world")89dgst := digest.FromBytes(cnt)9091client, mock := redismock.NewClientMock()92mock.ExpectExists("cnt."+string(dgst), "nfo."+string(dgst)).SetVal(0)93mock.ExpectMSet(94"cnt."+string(dgst), string(cnt),95"nfo."+string(dgst), `{"Digest":"`+string(dgst)+`","Size":11,"CreatedAt":1,"UpdatedAt":1,"Labels":{"foo":"bar"}}`,96).SetVal("OK")9798store := &RedisBlobStore{Client: client}99w, err := store.Writer(context.Background(), content.WithDescriptor(ociv1.Descriptor{100Digest: dgst,101MediaType: ociv1.MediaTypeImageConfig,102}))103if err != nil {104t.Fatal(err)105}106w.(*redisBlobWriter).forTestingOnlyTime = time.Unix(1, 1)107_, _ = w.Write(cnt)108w.Close()109err = w.Commit(context.Background(), int64(len(cnt)), dgst, content.WithLabels(map[string]string{"foo": "bar"}))110if err != nil {111t.Fatal(err)112}113}114115116