go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/pbutil/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 pbutil
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  
    22  	. "go.chromium.org/luci/common/testing/assertions"
    23  )
    24  
    25  func TestParseArtifactName(t *testing.T) {
    26  	t.Parallel()
    27  	Convey(`ParseArtifactName`, t, func() {
    28  		Convey(`Invocation level`, func() {
    29  			Convey(`Success`, func() {
    30  				invocationID, testID, resultID, artifactID, err := ParseArtifactName("invocations/inv/artifacts/a")
    31  				So(err, ShouldBeNil)
    32  				So(invocationID, ShouldEqual, "inv")
    33  				So(testID, ShouldEqual, "")
    34  				So(resultID, ShouldEqual, "")
    35  				So(artifactID, ShouldEqual, "a")
    36  			})
    37  
    38  			Convey(`With a slash`, func() {
    39  				_, _, _, artifactID, err := ParseArtifactName("invocations/inv/artifacts/a%2Fb")
    40  				So(err, ShouldBeNil)
    41  				So(artifactID, ShouldEqual, "a/b")
    42  			})
    43  		})
    44  
    45  		Convey(`Test result level`, func() {
    46  			Convey(`Success`, func() {
    47  				invocationID, testID, resultID, artifactID, err := ParseArtifactName("invocations/inv/tests/t/results/r/artifacts/a")
    48  				So(err, ShouldBeNil)
    49  				So(invocationID, ShouldEqual, "inv")
    50  				So(testID, ShouldEqual, "t")
    51  				So(resultID, ShouldEqual, "r")
    52  				So(artifactID, ShouldEqual, "a")
    53  			})
    54  
    55  			Convey(`With a slash in test ID`, func() {
    56  				_, testID, _, _, err := ParseArtifactName("invocations/inv/tests/t%2F/results/r/artifacts/a/b")
    57  				So(err, ShouldBeNil)
    58  				So(testID, ShouldEqual, "t/")
    59  			})
    60  
    61  			Convey(`With a slash`, func() {
    62  				_, _, _, artifactID, err := ParseArtifactName("invocations/inv/tests/t/results/r/artifacts/a%2Fb")
    63  				So(err, ShouldBeNil)
    64  				So(artifactID, ShouldEqual, "a/b")
    65  			})
    66  		})
    67  	})
    68  }
    69  
    70  func TestArtifactName(t *testing.T) {
    71  	t.Parallel()
    72  	Convey(`ArtifactName`, t, func() {
    73  		Convey(`Invocation level`, func() {
    74  			Convey(`Success`, func() {
    75  				name := InvocationArtifactName("inv", "a")
    76  				So(name, ShouldEqual, "invocations/inv/artifacts/a")
    77  			})
    78  			Convey(`With a slash`, func() {
    79  				name := InvocationArtifactName("inv", "a/b")
    80  				So(name, ShouldEqual, "invocations/inv/artifacts/a%2Fb")
    81  			})
    82  		})
    83  
    84  		Convey(`Test result level`, func() {
    85  			Convey(`Success`, func() {
    86  				name := TestResultArtifactName("inv", "t r", "r", "a")
    87  				So(name, ShouldEqual, "invocations/inv/tests/t%20r/results/r/artifacts/a")
    88  			})
    89  			Convey(`With a slash`, func() {
    90  				name := TestResultArtifactName("inv", "t r", "r", "a/b")
    91  				So(name, ShouldEqual, "invocations/inv/tests/t%20r/results/r/artifacts/a%2Fb")
    92  			})
    93  		})
    94  	})
    95  }
    96  
    97  func TestValidateArtifactName(t *testing.T) {
    98  	t.Parallel()
    99  	Convey(`ValidateArtifactName`, t, func() {
   100  		Convey(`Invocation level`, func() {
   101  			err := ValidateArtifactName("invocations/inv/artifacts/a/b")
   102  			So(err, ShouldBeNil)
   103  		})
   104  		Convey(`Test result level`, func() {
   105  			err := ValidateArtifactName("invocations/inv/tests/t/results/r/artifacts/a")
   106  			So(err, ShouldBeNil)
   107  		})
   108  		Convey(`Invalid`, func() {
   109  			err := ValidateArtifactName("abc")
   110  			So(err, ShouldErrLike, "does not match")
   111  		})
   112  	})
   113  }
   114  
   115  func TestArtifactId(t *testing.T) {
   116  	t.Parallel()
   117  	Convey(`ValidateArtifactID`, t, func() {
   118  		Convey(`ASCII printable`, func() {
   119  			err := ValidateArtifactID("ascii.txt")
   120  			So(err, ShouldBeNil)
   121  		})
   122  		Convey(`Unicode printable`, func() {
   123  			err := ValidateArtifactID("unicode ©.txt")
   124  			So(err, ShouldBeNil)
   125  		})
   126  		Convey(`Unprintable`, func() {
   127  			err := ValidateArtifactID("unprintable \a.txt")
   128  			So(err, ShouldErrLike, "does not match")
   129  		})
   130  		Convey(`Starts with dot`, func() {
   131  			err := ValidateArtifactID(".arc.log")
   132  			So(err, ShouldBeNil)
   133  		})
   134  
   135  	})
   136  }
   137  
   138  func TestIsTextArtifact(t *testing.T) {
   139  	t.Parallel()
   140  	Convey("IsTextArtifact", t, func() {
   141  		Convey("empty content type", func() {
   142  			So(IsTextArtifact(""), ShouldBeFalse)
   143  		})
   144  		Convey("text artifact", func() {
   145  			So(IsTextArtifact("text/plain"), ShouldBeTrue)
   146  		})
   147  		Convey("non text artifact", func() {
   148  			So(IsTextArtifact("image/png"), ShouldBeFalse)
   149  		})
   150  	})
   151  }