github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/experimental/batchmap/sumdb/build/pipeline/log_test.go (about)

     1  // Copyright 2020 Google LLC. All Rights Reserved.
     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 pipeline
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/apache/beam/sdks/v2/go/pkg/beam"
    22  	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
    23  	"github.com/apache/beam/sdks/v2/go/pkg/beam/testing/passert"
    24  	"github.com/apache/beam/sdks/v2/go/pkg/beam/testing/ptest"
    25  	"github.com/google/trillian/experimental/batchmap"
    26  )
    27  
    28  const treeID = 12345
    29  
    30  func init() {
    31  	register.Function1x1(testEntryHashToStringFn)
    32  	register.Function1x1(testModuleVersionsFn)
    33  }
    34  
    35  func TestMakeVersionLogs(t *testing.T) {
    36  	tests := []struct {
    37  		name     string
    38  		metadata []Metadata
    39  
    40  		wantCount    int
    41  		wantRoot     string
    42  		wantVersions []string
    43  	}{
    44  		{
    45  			name: "single module single metadata",
    46  			metadata: []Metadata{
    47  				{
    48  					Module:  "foo",
    49  					Version: "v0.0.1",
    50  					ID:      1,
    51  				},
    52  			},
    53  			wantCount:    1,
    54  			wantRoot:     "ab0fa665851a47dec50ff0a51e7dfbab747ff5a548dcfcc7bde213b571a8e6ae",
    55  			wantVersions: []string{"v0.0.1"},
    56  		},
    57  		{
    58  			name: "single module two metadata (in order)",
    59  			metadata: []Metadata{
    60  				{
    61  					Module:  "foo",
    62  					Version: "1",
    63  					ID:      1,
    64  				},
    65  				{
    66  					Module:  "foo",
    67  					Version: "2",
    68  					ID:      2,
    69  				},
    70  			},
    71  			wantCount:    1,
    72  			wantRoot:     "7fadb0db3926ec36f4028452856670df932eacba6f624ed82284c4a63adc5f73",
    73  			wantVersions: []string{"1", "2"},
    74  		},
    75  		{
    76  			name: "single module two metadata (out of order)",
    77  			metadata: []Metadata{
    78  				{
    79  					Module:  "foo",
    80  					Version: "2",
    81  					ID:      2,
    82  				},
    83  				{
    84  					Module:  "foo",
    85  					Version: "1",
    86  					ID:      1,
    87  				},
    88  			},
    89  			wantCount:    1,
    90  			wantRoot:     "7fadb0db3926ec36f4028452856670df932eacba6f624ed82284c4a63adc5f73",
    91  			wantVersions: []string{"1", "2"},
    92  		},
    93  		{
    94  			name: "two modules",
    95  			metadata: []Metadata{
    96  				{
    97  					Module:  "foo",
    98  					Version: "1",
    99  					ID:      1,
   100  				},
   101  				{
   102  					Module:  "bar",
   103  					Version: "1",
   104  					ID:      2,
   105  				},
   106  			},
   107  			wantCount: 2,
   108  		},
   109  	}
   110  
   111  	for _, test := range tests {
   112  		test := test
   113  		t.Run(test.name, func(t *testing.T) {
   114  			p, s := beam.NewPipelineWithRoot()
   115  			metadata := beam.CreateList(s, test.metadata)
   116  
   117  			entries, logs := MakeVersionLogs(s, treeID, metadata)
   118  
   119  			passert.Count(s, entries, "entries", test.wantCount)
   120  			passert.Count(s, logs, "logs", test.wantCount)
   121  			if len(test.wantRoot) > 0 {
   122  				roots := beam.ParDo(s, testEntryHashToStringFn, entries)
   123  				passert.Equals(s, roots, test.wantRoot)
   124  			}
   125  			if len(test.wantVersions) > 0 {
   126  				versions := beam.ParDo(s, testModuleVersionsFn, logs)
   127  				passert.Equals(s, versions, test.wantVersions)
   128  			}
   129  			err := ptest.Run(p)
   130  			if err != nil {
   131  				t.Errorf("unexpected error: %v", err)
   132  			}
   133  		})
   134  	}
   135  }
   136  
   137  func testEntryHashToStringFn(e *batchmap.Entry) string  { return fmt.Sprintf("%x", e.HashValue) }
   138  func testModuleVersionsFn(l *ModuleVersionLog) []string { return l.Versions }