go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/artifacts/artifact_test.go (about)

     1  // Copyright 2020 The LUCI 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 artifacts
    16  
    17  import (
    18  	"testing"
    19  
    20  	"google.golang.org/grpc/codes"
    21  
    22  	"go.chromium.org/luci/server/span"
    23  
    24  	"go.chromium.org/luci/resultdb/internal/invocations"
    25  	"go.chromium.org/luci/resultdb/internal/testutil"
    26  	"go.chromium.org/luci/resultdb/internal/testutil/insert"
    27  	pb "go.chromium.org/luci/resultdb/proto/v1"
    28  
    29  	. "github.com/smartystreets/goconvey/convey"
    30  	. "go.chromium.org/luci/common/testing/assertions"
    31  )
    32  
    33  func TestMustParseName(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	Convey(`MustParseName`, t, func() {
    37  		Convey(`Parse`, func() {
    38  			invID, testID, resultID, artifactID := MustParseName(
    39  				"invocations/a/tests/ninja:%2F%2Fchrome%2Ftest:foo_tests%2FBarTest.DoBaz/results/result5/artifacts/a")
    40  			So(invID, ShouldEqual, invocations.ID("a"))
    41  			So(testID, ShouldEqual, "ninja://chrome/test:foo_tests/BarTest.DoBaz")
    42  			So(resultID, ShouldEqual, "result5")
    43  			So(artifactID, ShouldEqual, "a")
    44  		})
    45  
    46  		Convey(`Invalid`, func() {
    47  			invalidNames := []string{
    48  				"invocations/a/tests/b",
    49  				"invocations/a/tests/b/exonerations/c",
    50  			}
    51  			for _, name := range invalidNames {
    52  				name := name
    53  				So(func() { MustParseName(name) }, ShouldPanic)
    54  			}
    55  		})
    56  	})
    57  }
    58  
    59  func TestParentID(t *testing.T) {
    60  	t.Parallel()
    61  	Convey(`TestParentID`, t, func() {
    62  		Convey(`Invocation level`, func() {
    63  			id := ParentID("", "")
    64  			So(id, ShouldEqual, "")
    65  		})
    66  		Convey(`Test result level`, func() {
    67  			id := ParentID("t t", "r")
    68  			So(id, ShouldEqual, "tr/t t/r")
    69  		})
    70  	})
    71  }
    72  
    73  func TestRead(t *testing.T) {
    74  	Convey(`TestRead`, t, func() {
    75  		ctx := testutil.SpannerTestContext(t)
    76  		ctx, cancel := span.ReadOnlyTransaction(ctx)
    77  		defer cancel()
    78  
    79  		testutil.MustApply(ctx, insert.Invocation("inv", pb.Invocation_FINALIZED, nil))
    80  
    81  		Convey(`Does not exist`, func() {
    82  			_, err := Read(ctx, "invocations/i/artifacts/a")
    83  			So(err, ShouldHaveAppStatus, codes.NotFound, "invocations/i/artifacts/a not found")
    84  		})
    85  
    86  		Convey(`Exists`, func() {
    87  			testutil.MustApply(ctx, insert.Artifact("inv", "tr/t/r", "a", map[string]any{
    88  				"ContentType": "text/plain",
    89  				"Size":        "54",
    90  			}))
    91  			const name = "invocations/inv/tests/t/results/r/artifacts/a"
    92  			a, err := Read(ctx, name)
    93  			So(err, ShouldBeNil)
    94  			So(a, ShouldResembleProto, &pb.Artifact{
    95  				Name:        name,
    96  				ArtifactId:  "a",
    97  				ContentType: "text/plain",
    98  				SizeBytes:   54,
    99  			})
   100  		})
   101  
   102  		Convey(`Exists with GcsURI`, func() {
   103  			testutil.MustApply(ctx, insert.Artifact("inv", "tr/t/r", "b", map[string]any{
   104  				"ContentType": "text/plain",
   105  				"Size":        "54",
   106  				"GcsURI":      "gs://test",
   107  			}))
   108  			const name = "invocations/inv/tests/t/results/r/artifacts/b"
   109  			a, err := Read(ctx, name)
   110  			So(err, ShouldBeNil)
   111  			So(a, ShouldResembleProto, &pb.Artifact{
   112  				Name:        name,
   113  				ArtifactId:  "b",
   114  				ContentType: "text/plain",
   115  				SizeBytes:   54,
   116  				GcsUri:      "gs://test",
   117  			})
   118  		})
   119  	})
   120  }