...

Source file src/github.com/sigstore/rekor/pkg/sharding/log_index_test.go

Documentation: github.com/sigstore/rekor/pkg/sharding

     1  // Copyright 2021 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 sharding
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestVirtualLogIndex(t *testing.T) {
    22  	tests := []struct {
    23  		description   string
    24  		leafIndex     int64
    25  		tid           int64
    26  		ranges        LogRanges
    27  		expectedIndex int64
    28  	}{
    29  		{
    30  			description:   "no ranges",
    31  			leafIndex:     5,
    32  			ranges:        LogRanges{},
    33  			expectedIndex: 5,
    34  		},
    35  		// Log 100: 0 1 2 3 4
    36  		// Log 300: 5 6 7...
    37  		{
    38  			description: "two shards",
    39  			leafIndex:   2,
    40  			tid:         300,
    41  			ranges: LogRanges{
    42  				inactive: []LogRange{
    43  					{
    44  						TreeID:     100,
    45  						TreeLength: 5,
    46  					}},
    47  				active: 300,
    48  			},
    49  			expectedIndex: 7,
    50  		},
    51  		// Log 100: 0 1 2 3 4
    52  		// Log 300: 5 6 7 8
    53  		// Log 400: ...
    54  		{
    55  			description: "three shards",
    56  			leafIndex:   1,
    57  			tid:         300,
    58  			ranges: LogRanges{
    59  				inactive: []LogRange{
    60  					{
    61  						TreeID:     100,
    62  						TreeLength: 5,
    63  					}, {
    64  						TreeID:     300,
    65  						TreeLength: 4,
    66  					}},
    67  				active: 400,
    68  			},
    69  			expectedIndex: 6,
    70  		},
    71  		// Log 30: 1 2 3...
    72  		{
    73  			description: "only active tree",
    74  			leafIndex:   2,
    75  			tid:         30,
    76  			ranges: LogRanges{
    77  				active: 30,
    78  			},
    79  			expectedIndex: 2,
    80  		}, {
    81  			description: "invalid tid passed in",
    82  			leafIndex:   2,
    83  			tid:         4,
    84  			ranges: LogRanges{
    85  				active: 30,
    86  			},
    87  			expectedIndex: -1,
    88  		},
    89  	}
    90  
    91  	for _, test := range tests {
    92  		t.Run(test.description, func(t *testing.T) {
    93  			got := VirtualLogIndex(test.leafIndex, test.tid, test.ranges)
    94  			if got != test.expectedIndex {
    95  				t.Fatalf("expected %v got %v", test.expectedIndex, got)
    96  			}
    97  		})
    98  	}
    99  }
   100  

View as plain text