...

Source file src/github.com/sigstore/rekor/pkg/indexstorage/redis/redis_test.go

Documentation: github.com/sigstore/rekor/pkg/indexstorage/redis

     1  // Copyright 2023 The Sigstore Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package redis
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"testing"
    21  
    22  	"github.com/go-redis/redismock/v9"
    23  	"github.com/google/go-cmp/cmp"
    24  	"github.com/google/go-cmp/cmp/cmpopts"
    25  	"go.uber.org/goleak"
    26  )
    27  
    28  func TestLookupIndices(t *testing.T) {
    29  	keys := []string{"87c1b129fbadd7b6e9abc0a9ef7695436d767aece042bec198a97e949fcbe14c"}
    30  	value := []string{"1e1f2c881ae0608ec77ebf88a75c66d3099113a7343238f2f7a0ebb91a4ed335"}
    31  	redisClient, mock := redismock.NewClientMock()
    32  	mock.Regexp().ExpectLRange(keys[0], 0, -1).SetVal(value)
    33  
    34  	isp := IndexStorageProvider{redisClient}
    35  
    36  	indices, err := isp.LookupIndices(context.Background(), keys)
    37  	if err != nil {
    38  		t.Error(err)
    39  	}
    40  
    41  	less := func(a, b string) bool { return a < b }
    42  	if cmp.Diff(value, indices, cmpopts.SortSlices(less)) != "" {
    43  		t.Errorf("expected %s, got %s", value, indices)
    44  	}
    45  
    46  	if err := mock.ExpectationsWereMet(); err != nil {
    47  		t.Error(err)
    48  	}
    49  
    50  	mock.ClearExpect()
    51  	errRedis := errors.New("redis error")
    52  	mock.Regexp().ExpectLRange(keys[0], 0, -1).SetErr(errRedis)
    53  	if _, err := isp.LookupIndices(context.Background(), keys); err == nil {
    54  		t.Error("unexpected success")
    55  	}
    56  	if err := mock.ExpectationsWereMet(); err != nil {
    57  		t.Error(err)
    58  	}
    59  }
    60  
    61  func TestWriteIndex(t *testing.T) {
    62  	keys := []string{"87c1b129fbadd7b6e9abc0a9ef7695436d767aece042bec198a97e949fcbe14c"}
    63  	value := []string{"1e1f2c881ae0608ec77ebf88a75c66d3099113a7343238f2f7a0ebb91a4ed335"}
    64  	redisClient, mock := redismock.NewClientMock()
    65  	mock.Regexp().ExpectLPush(keys[0], value).SetVal(1)
    66  
    67  	isp := IndexStorageProvider{redisClient}
    68  	if err := isp.WriteIndex(context.Background(), keys, value[0]); err != nil {
    69  		t.Error(err)
    70  	}
    71  
    72  	if err := mock.ExpectationsWereMet(); err != nil {
    73  		t.Error(err)
    74  	}
    75  
    76  	mock.ClearExpect()
    77  	errRedis := errors.New("redis error")
    78  	mock.Regexp().ExpectLPush(keys[0], value).SetErr(errRedis)
    79  	if err := isp.WriteIndex(context.Background(), keys, value[0]); err == nil {
    80  		t.Error("unexpected success")
    81  	}
    82  	if err := mock.ExpectationsWereMet(); err != nil {
    83  		t.Error(err)
    84  	}
    85  }
    86  
    87  func TestUninitializedClient(t *testing.T) {
    88  	// this is not initialized with a real Redis client
    89  	isp := IndexStorageProvider{}
    90  	if _, err := isp.LookupIndices(context.Background(), []string{"key"}); err == nil {
    91  		t.Error("unexpected success")
    92  	}
    93  	if err := isp.WriteIndex(context.Background(), []string{"key"}, "value"); err == nil {
    94  		t.Error("unexpected success")
    95  	}
    96  }
    97  
    98  func TestMain(m *testing.M) {
    99  	goleak.VerifyTestMain(m)
   100  }
   101  

View as plain text