blob: 832c258d78cb3fb735e363a7086976e4334a54bd [file] [log] [blame]
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"context"
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"go.fuchsia.dev/infra/cmd/gcs-util/types"
)
func TestGetBlobs(t *testing.T) {
t.Parallel()
tests := []struct {
name string
manifest []types.Upload
expected []types.Upload
}{
{
name: "only versioned blobs are returned",
manifest: []types.Upload{
{
Source: "abc",
Destination: "blobs/1/abc",
},
{
Source: "abc",
Destination: "blobs/999/abc",
},
{
Source: "abc",
Destination: "blobs/abc",
},
{
Source: "not-a-blob",
Destination: "not-a-blob",
},
},
expected: []types.Upload{
{
Source: "abc",
Destination: "blobs/1/abc",
},
{
Source: "abc",
Destination: "blobs/999/abc",
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
blobs, err := getBlobs(ctx, test.manifest)
if err != nil {
t.Fatalf("got unexpected error: %s", err)
}
if diff := cmp.Diff(test.expected, blobs); diff != "" {
t.Errorf("output differs (-want, +got):\n%s", diff)
}
})
}
}
func TestVerifyBlob(t *testing.T) {
t.Parallel()
tests := []struct {
name string
blob types.Upload
f merkleRoot
expectErr bool
}{
{
name: "matching merkle root",
blob: types.Upload{
Source: "abc",
Destination: "blobs/1/abc",
},
f: func(blob types.Upload, tool string) (string, error) {
return "abc", nil
},
expectErr: false,
},
{
name: "mismatched merkle root",
blob: types.Upload{
Source: "abc",
Destination: "blobs/1/abc",
},
f: func(blob types.Upload, tool string) (string, error) {
return "def", nil
},
expectErr: true,
},
{
name: "tool crash",
blob: types.Upload{
Source: "abc",
Destination: "blobs/1/abc",
},
f: func(blob types.Upload, tool string) (string, error) {
return "", errors.New("failed to compute merkle root")
},
expectErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
err := verifyBlob(ctx, test.blob, "tool", test.f)
if err == nil {
if test.expectErr {
t.Errorf("expected error, got nil")
}
} else if !test.expectErr {
t.Errorf("got unexpected error: %s", err)
}
})
}
}
func TestVerifyBlobs(t *testing.T) {
t.Parallel()
tests := []struct {
name string
blobs []types.Upload
f merkleRoot
expectErr bool
}{
{
name: "matching merkle roots",
blobs: []types.Upload{
{
Source: "abc",
Destination: "blobs/1/abc",
},
{
Source: "def",
Destination: "blobs/1/def",
},
{
Source: "ghi",
Destination: "blobs/1/ghi",
},
},
f: func(blob types.Upload, tool string) (string, error) {
return blob.Source, nil
},
expectErr: false,
},
{
name: "one mismatched merkle root",
blobs: []types.Upload{
{
Source: "abc",
Destination: "blobs/1/abc",
},
{
Source: "def",
Destination: "blobs/1/def",
},
},
f: func(blob types.Upload, tool string) (string, error) {
return "abc", nil
},
expectErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
err := verifyBlobs(ctx, test.blobs, "tool", test.f, 2)
if err == nil {
if test.expectErr {
t.Errorf("expected error, got nil")
}
} else if !test.expectErr {
t.Errorf("got unexpected error: %s", err)
}
})
}
}