go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/gcs-util/verify_blobs_test.go (about)

     1  // Copyright 2023 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"go.fuchsia.dev/infra/cmd/gcs-util/types"
    14  )
    15  
    16  func TestGetBlobs(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	tests := []struct {
    20  		name     string
    21  		manifest []types.Upload
    22  		expected []types.Upload
    23  	}{
    24  		{
    25  			name: "only versioned blobs are returned",
    26  			manifest: []types.Upload{
    27  				{
    28  					Source:      "abc",
    29  					Destination: "blobs/1/abc",
    30  				},
    31  				{
    32  					Source:      "abc",
    33  					Destination: "blobs/999/abc",
    34  				},
    35  				{
    36  					Source:      "abc",
    37  					Destination: "blobs/abc",
    38  				},
    39  				{
    40  					Source:      "not-a-blob",
    41  					Destination: "not-a-blob",
    42  				},
    43  			},
    44  			expected: []types.Upload{
    45  				{
    46  					Source:      "abc",
    47  					Destination: "blobs/1/abc",
    48  				},
    49  				{
    50  					Source:      "abc",
    51  					Destination: "blobs/999/abc",
    52  				},
    53  			},
    54  		},
    55  	}
    56  
    57  	for _, test := range tests {
    58  		t.Run(test.name, func(t *testing.T) {
    59  			ctx := context.Background()
    60  			blobs, err := getBlobs(ctx, test.manifest)
    61  			if err != nil {
    62  				t.Fatalf("got unexpected error: %s", err)
    63  			}
    64  			if diff := cmp.Diff(test.expected, blobs); diff != "" {
    65  				t.Errorf("output differs (-want, +got):\n%s", diff)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestVerifyBlob(t *testing.T) {
    72  	t.Parallel()
    73  
    74  	tests := []struct {
    75  		name      string
    76  		blob      types.Upload
    77  		f         merkleRoot
    78  		expectErr bool
    79  	}{
    80  		{
    81  			name: "matching merkle root",
    82  			blob: types.Upload{
    83  				Source:      "abc",
    84  				Destination: "blobs/1/abc",
    85  			},
    86  			f: func(blob types.Upload, tool string) (string, error) {
    87  				return "abc", nil
    88  			},
    89  			expectErr: false,
    90  		},
    91  		{
    92  			name: "mismatched merkle root",
    93  			blob: types.Upload{
    94  				Source:      "abc",
    95  				Destination: "blobs/1/abc",
    96  			},
    97  			f: func(blob types.Upload, tool string) (string, error) {
    98  				return "def", nil
    99  			},
   100  			expectErr: true,
   101  		},
   102  		{
   103  			name: "tool crash",
   104  			blob: types.Upload{
   105  				Source:      "abc",
   106  				Destination: "blobs/1/abc",
   107  			},
   108  			f: func(blob types.Upload, tool string) (string, error) {
   109  				return "", errors.New("failed to compute merkle root")
   110  			},
   111  			expectErr: true,
   112  		},
   113  	}
   114  
   115  	for _, test := range tests {
   116  		t.Run(test.name, func(t *testing.T) {
   117  			ctx := context.Background()
   118  			err := verifyBlob(ctx, test.blob, "tool", test.f)
   119  			if err == nil {
   120  				if test.expectErr {
   121  					t.Errorf("expected error, got nil")
   122  				}
   123  			} else if !test.expectErr {
   124  				t.Errorf("got unexpected error: %s", err)
   125  			}
   126  		})
   127  	}
   128  }
   129  
   130  func TestVerifyBlobs(t *testing.T) {
   131  	t.Parallel()
   132  
   133  	tests := []struct {
   134  		name      string
   135  		blobs     []types.Upload
   136  		f         merkleRoot
   137  		expectErr bool
   138  	}{
   139  		{
   140  			name: "matching merkle roots",
   141  			blobs: []types.Upload{
   142  				{
   143  					Source:      "abc",
   144  					Destination: "blobs/1/abc",
   145  				},
   146  				{
   147  					Source:      "def",
   148  					Destination: "blobs/1/def",
   149  				},
   150  				{
   151  					Source:      "ghi",
   152  					Destination: "blobs/1/ghi",
   153  				},
   154  			},
   155  			f: func(blob types.Upload, tool string) (string, error) {
   156  				return blob.Source, nil
   157  			},
   158  			expectErr: false,
   159  		},
   160  		{
   161  			name: "one mismatched merkle root",
   162  			blobs: []types.Upload{
   163  				{
   164  					Source:      "abc",
   165  					Destination: "blobs/1/abc",
   166  				},
   167  				{
   168  					Source:      "def",
   169  					Destination: "blobs/1/def",
   170  				},
   171  			},
   172  			f: func(blob types.Upload, tool string) (string, error) {
   173  				return "abc", nil
   174  			},
   175  			expectErr: true,
   176  		},
   177  	}
   178  
   179  	for _, test := range tests {
   180  		t.Run(test.name, func(t *testing.T) {
   181  			ctx := context.Background()
   182  			err := verifyBlobs(ctx, test.blobs, "tool", test.f, 2)
   183  			if err == nil {
   184  				if test.expectErr {
   185  					t.Errorf("expected error, got nil")
   186  				}
   187  			} else if !test.expectErr {
   188  				t.Errorf("got unexpected error: %s", err)
   189  			}
   190  		})
   191  	}
   192  }