go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/clustering/clusterid_test.go (about)

     1  // Copyright 2022 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 clustering
    16  
    17  import (
    18  	"encoding/hex"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  	. "go.chromium.org/luci/common/testing/assertions"
    23  )
    24  
    25  func TestValidate(t *testing.T) {
    26  	Convey(`Validate`, t, func() {
    27  		id := ClusterID{
    28  			Algorithm: "blah-v2",
    29  			ID:        hex.EncodeToString([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}),
    30  		}
    31  		Convey(`Algorithm missing`, func() {
    32  			id.Algorithm = ""
    33  			err := id.Validate()
    34  			So(err, ShouldErrLike, `algorithm not valid`)
    35  		})
    36  		Convey("Algorithm invalid", func() {
    37  			id.Algorithm = "!!!"
    38  			err := id.Validate()
    39  			So(err, ShouldErrLike, `algorithm not valid`)
    40  		})
    41  		Convey("ID missing", func() {
    42  			id.ID = ""
    43  			err := id.Validate()
    44  			So(err, ShouldErrLike, `ID is empty`)
    45  		})
    46  		Convey("ID invalid", func() {
    47  			id.ID = "!!!"
    48  			err := id.Validate()
    49  			So(err, ShouldErrLike, `ID is not valid lowercase hexadecimal bytes`)
    50  		})
    51  		Convey("ID not lowercase", func() {
    52  			id.ID = "AA"
    53  			err := id.Validate()
    54  			So(err, ShouldErrLike, `ID is not valid lowercase hexadecimal bytes`)
    55  		})
    56  		Convey("ID too long", func() {
    57  			id.ID = hex.EncodeToString([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17})
    58  			err := id.Validate()
    59  			So(err, ShouldErrLike, `ID is too long (got 17 bytes, want at most 16 bytes)`)
    60  		})
    61  	})
    62  }