tools: Add buildbucket package
Wraps the luci package to make it a bit friendlier. Bug: dawn:1342 Change-Id: I80e02344ea1ecd95134a719f21eb8bd2a1cb1997 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/86527 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Kokoro: Kokoro <noreply+kokoro@google.com> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
parent
76b189f5a9
commit
5827191cc5
|
@ -0,0 +1,229 @@
|
||||||
|
// Copyright 2022 The Tint Authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package buildbucket provides helpers for interfacing with build-bucket
|
||||||
|
package buildbucket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"dawn.googlesource.com/dawn/tools/src/gerrit"
|
||||||
|
"dawn.googlesource.com/dawn/tools/src/utils"
|
||||||
|
"go.chromium.org/luci/auth"
|
||||||
|
bbpb "go.chromium.org/luci/buildbucket/proto"
|
||||||
|
"go.chromium.org/luci/grpc/prpc"
|
||||||
|
"go.chromium.org/luci/hardcoded/chromeinfra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Buildbucket is the client to communicate with Buildbucket.
|
||||||
|
type Buildbucket struct {
|
||||||
|
client bbpb.BuildsClient
|
||||||
|
}
|
||||||
|
|
||||||
|
// Builder describes a buildbucket builder
|
||||||
|
type Builder struct {
|
||||||
|
Project string
|
||||||
|
Bucket string
|
||||||
|
Builder string
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildID is a unique identifier of a build
|
||||||
|
type BuildID int64
|
||||||
|
|
||||||
|
// Build describes a buildbucket build
|
||||||
|
type Build struct {
|
||||||
|
ID BuildID
|
||||||
|
Status BuildStatus
|
||||||
|
Builder Builder
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildStatus is the status of a build
|
||||||
|
type BuildStatus string
|
||||||
|
|
||||||
|
// Enumerator values for BuildStatus
|
||||||
|
const (
|
||||||
|
// Unspecified state. Meaning depends on the context.
|
||||||
|
StatusUnknown BuildStatus = "unknown"
|
||||||
|
// Build was scheduled, but did not start or end yet.
|
||||||
|
StatusScheduled BuildStatus = "scheduled"
|
||||||
|
// Build/step has started.
|
||||||
|
StatusStarted BuildStatus = "started"
|
||||||
|
// A build/step ended successfully.
|
||||||
|
// This is a terminal status. It may not transition to another status.
|
||||||
|
StatusSuccess BuildStatus = "success"
|
||||||
|
// A build/step ended unsuccessfully due to its Build.Input,
|
||||||
|
// e.g. tests failed, and NOT due to a build infrastructure failure.
|
||||||
|
// This is a terminal status. It may not transition to another status.
|
||||||
|
StatusFailure BuildStatus = "failure"
|
||||||
|
// A build/step ended unsuccessfully due to a failure independent of the
|
||||||
|
// input, e.g. swarming failed, not enough capacity or the recipe was unable
|
||||||
|
// to read the patch from gerrit.
|
||||||
|
// start_time is not required for this status.
|
||||||
|
// This is a terminal status. It may not transition to another status.
|
||||||
|
StatusInfraFailure BuildStatus = "infra-failure"
|
||||||
|
// A build was cancelled explicitly, e.g. via an RPC.
|
||||||
|
// This is a terminal status. It may not transition to another status.
|
||||||
|
StatusCanceled BuildStatus = "canceled"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Running returns true if the build is still running
|
||||||
|
func (s BuildStatus) Running() bool {
|
||||||
|
switch s {
|
||||||
|
case StatusScheduled, StatusStarted:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pb returns a protobuf BuilderID constructed from the Builder
|
||||||
|
func (b Builder) pb() *bbpb.BuilderID {
|
||||||
|
return &bbpb.BuilderID{
|
||||||
|
Project: b.Project,
|
||||||
|
Bucket: b.Bucket,
|
||||||
|
Builder: b.Builder,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// toBuilder returns a Builder constructed from the protobuf BuilderID
|
||||||
|
func toBuilder(b *bbpb.BuilderID) Builder {
|
||||||
|
return Builder{
|
||||||
|
Project: b.Project,
|
||||||
|
Bucket: b.Bucket,
|
||||||
|
Builder: b.Builder,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// gerritChange returns the protobuf GerritChange from a gerrit.Patchset
|
||||||
|
func gerritChange(ps gerrit.Patchset) *bbpb.GerritChange {
|
||||||
|
host := ps.Host
|
||||||
|
if u, err := url.Parse(ps.Host); err == nil && u.Host != "" {
|
||||||
|
host = u.Host // Strip scheme from URL
|
||||||
|
}
|
||||||
|
return &bbpb.GerritChange{
|
||||||
|
Host: host,
|
||||||
|
Project: ps.Project,
|
||||||
|
Change: int64(ps.Change),
|
||||||
|
Patchset: int64(ps.Patchset),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// toBuildStatus returns a BuildStatus from a protobuf Status
|
||||||
|
func toBuildStatus(s bbpb.Status) BuildStatus {
|
||||||
|
switch s {
|
||||||
|
default:
|
||||||
|
return StatusUnknown
|
||||||
|
case bbpb.Status_SCHEDULED:
|
||||||
|
return StatusScheduled
|
||||||
|
case bbpb.Status_STARTED:
|
||||||
|
return StatusStarted
|
||||||
|
case bbpb.Status_SUCCESS:
|
||||||
|
return StatusSuccess
|
||||||
|
case bbpb.Status_FAILURE:
|
||||||
|
return StatusFailure
|
||||||
|
case bbpb.Status_INFRA_FAILURE:
|
||||||
|
return StatusInfraFailure
|
||||||
|
case bbpb.Status_CANCELED:
|
||||||
|
return StatusCanceled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// toBuild returns a Build from a protobuf Build
|
||||||
|
func toBuild(b *bbpb.Build) Build {
|
||||||
|
return Build{BuildID(b.Id), toBuildStatus(b.Status), toBuilder(b.Builder)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a client to communicate with Buildbucket.
|
||||||
|
func New(ctx context.Context, credentials auth.Options) (*Buildbucket, error) {
|
||||||
|
http, err := auth.NewAuthenticator(ctx, auth.InteractiveLogin, credentials).Client()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
client, err := bbpb.NewBuildsClient(
|
||||||
|
&prpc.Client{
|
||||||
|
C: http,
|
||||||
|
Host: chromeinfra.BuildbucketHost,
|
||||||
|
Options: prpc.DefaultOptions(),
|
||||||
|
}), nil
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Buildbucket{client}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SearchBuilds queries the list of builds performed for the given gerrit change.
|
||||||
|
func (r *Buildbucket) SearchBuilds(ctx context.Context, ps gerrit.Patchset, f func(Build) error) error {
|
||||||
|
pageToken := ""
|
||||||
|
for {
|
||||||
|
rsp, err := r.client.SearchBuilds(ctx, &bbpb.SearchBuildsRequest{
|
||||||
|
Predicate: &bbpb.BuildPredicate{
|
||||||
|
GerritChanges: []*bbpb.GerritChange{gerritChange(ps)},
|
||||||
|
},
|
||||||
|
PageSize: 1000, // Maximum page size.
|
||||||
|
PageToken: pageToken,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, res := range rsp.Builds {
|
||||||
|
if err := f(toBuild(res)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pageToken = rsp.GetNextPageToken()
|
||||||
|
if pageToken == "" {
|
||||||
|
// No more test variants with unexpected result.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartBuild starts a build.
|
||||||
|
func (r *Buildbucket) StartBuild(
|
||||||
|
ctx context.Context,
|
||||||
|
ps gerrit.Patchset,
|
||||||
|
builder Builder,
|
||||||
|
forceBuild bool) (Build, error) {
|
||||||
|
|
||||||
|
id := ""
|
||||||
|
if !forceBuild {
|
||||||
|
id = utils.Hash(ps, builder)
|
||||||
|
}
|
||||||
|
|
||||||
|
build, err := r.client.ScheduleBuild(ctx, &bbpb.ScheduleBuildRequest{
|
||||||
|
RequestId: id,
|
||||||
|
Builder: builder.pb(),
|
||||||
|
GerritChanges: []*bbpb.GerritChange{gerritChange(ps)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return Build{}, fmt.Errorf("failed to start build for patchset %+v on builder %+v: %w", ps, builder, err)
|
||||||
|
}
|
||||||
|
return toBuild(build), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryBuild queries the status of a build.
|
||||||
|
func (r *Buildbucket) QueryBuild(ctx context.Context, id BuildID) (Build, error) {
|
||||||
|
b, err := r.client.GetBuild(ctx, &bbpb.GetBuildRequest{Id: int64(id)})
|
||||||
|
if err != nil {
|
||||||
|
return Build{}, fmt.Errorf("failed to query build with id %v: %w", id, err)
|
||||||
|
}
|
||||||
|
return toBuild(b), nil
|
||||||
|
}
|
|
@ -14,6 +14,7 @@ require (
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
cloud.google.com/go v0.99.0 // indirect
|
||||||
github.com/acomagu/bufpipe v1.0.3 // indirect
|
github.com/acomagu/bufpipe v1.0.3 // indirect
|
||||||
github.com/go-git/gcfg v1.5.0 // indirect
|
github.com/go-git/gcfg v1.5.0 // indirect
|
||||||
github.com/go-git/go-billy/v5 v5.3.1 // indirect
|
github.com/go-git/go-billy/v5 v5.3.1 // indirect
|
||||||
|
@ -30,9 +31,12 @@ require (
|
||||||
github.com/tklauser/go-sysconf v0.3.10 // indirect
|
github.com/tklauser/go-sysconf v0.3.10 // indirect
|
||||||
github.com/tklauser/numcpus v0.4.0 // indirect
|
github.com/tklauser/numcpus v0.4.0 // indirect
|
||||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||||
|
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
||||||
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
|
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
|
||||||
golang.org/x/text v0.3.7 // indirect
|
golang.org/x/text v0.3.7 // indirect
|
||||||
|
google.golang.org/api v0.63.0 // indirect
|
||||||
|
google.golang.org/appengine v1.6.7 // indirect
|
||||||
google.golang.org/genproto v0.0.0-20220216160803-4663080d8bc8 // indirect
|
google.golang.org/genproto v0.0.0-20220216160803-4663080d8bc8 // indirect
|
||||||
google.golang.org/grpc v1.44.0 // indirect
|
google.golang.org/grpc v1.44.0 // indirect
|
||||||
google.golang.org/protobuf v1.27.1 // indirect
|
google.golang.org/protobuf v1.27.1 // indirect
|
||||||
|
|
|
@ -30,6 +30,7 @@ cloud.google.com/go v0.92.3/go.mod h1:8utlLll2EF5XMAV15woO4lSbWQlk8rer9aLOfLh7+Y
|
||||||
cloud.google.com/go v0.93.3/go.mod h1:8utlLll2EF5XMAV15woO4lSbWQlk8rer9aLOfLh7+YI=
|
cloud.google.com/go v0.93.3/go.mod h1:8utlLll2EF5XMAV15woO4lSbWQlk8rer9aLOfLh7+YI=
|
||||||
cloud.google.com/go v0.94.1/go.mod h1:qAlAugsXlC+JWO+Bke5vCtc9ONxjQT3drlTTnAplMW4=
|
cloud.google.com/go v0.94.1/go.mod h1:qAlAugsXlC+JWO+Bke5vCtc9ONxjQT3drlTTnAplMW4=
|
||||||
cloud.google.com/go v0.97.0/go.mod h1:GF7l59pYBVlXQIBLx3a761cZ41F9bBH3JUlihCt2Udc=
|
cloud.google.com/go v0.97.0/go.mod h1:GF7l59pYBVlXQIBLx3a761cZ41F9bBH3JUlihCt2Udc=
|
||||||
|
cloud.google.com/go v0.99.0 h1:y/cM2iqGgGi5D5DQZl6D9STN/3dR/Vx5Mp8s752oJTY=
|
||||||
cloud.google.com/go v0.99.0/go.mod h1:w0Xx2nLzqWJPuozYQX+hFfCSI8WioryfRDzkoI/Y2ZA=
|
cloud.google.com/go v0.99.0/go.mod h1:w0Xx2nLzqWJPuozYQX+hFfCSI8WioryfRDzkoI/Y2ZA=
|
||||||
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
|
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
|
||||||
cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE=
|
cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE=
|
||||||
|
@ -632,6 +633,7 @@ golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914/go.mod h1:KelEdhl1UZF7XfJ
|
||||||
golang.org/x/oauth2 v0.0.0-20210805134026-6f1e6394065a/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
|
golang.org/x/oauth2 v0.0.0-20210805134026-6f1e6394065a/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
|
||||||
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
|
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
|
||||||
golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
|
golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
|
||||||
|
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 h1:RerP+noqYHUQ8CMRcPlC2nvTa4dcBIjegkuWdcUDuqg=
|
||||||
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
|
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
@ -835,6 +837,7 @@ google.golang.org/api v0.56.0/go.mod h1:38yMfeP1kfjsl8isn0tliTjIb1rJXcQi4UXlbqiv
|
||||||
google.golang.org/api v0.57.0/go.mod h1:dVPlbZyBo2/OjBpmvNdpn2GRm6rPy75jyU7bmhdrMgI=
|
google.golang.org/api v0.57.0/go.mod h1:dVPlbZyBo2/OjBpmvNdpn2GRm6rPy75jyU7bmhdrMgI=
|
||||||
google.golang.org/api v0.58.0/go.mod h1:cAbP2FsxoGVNwtgNAmmn3y5G1TWAiVYRmg4yku3lv+E=
|
google.golang.org/api v0.58.0/go.mod h1:cAbP2FsxoGVNwtgNAmmn3y5G1TWAiVYRmg4yku3lv+E=
|
||||||
google.golang.org/api v0.61.0/go.mod h1:xQRti5UdCmoCEqFxcz93fTl338AVqDgyaDRuOZ3hg9I=
|
google.golang.org/api v0.61.0/go.mod h1:xQRti5UdCmoCEqFxcz93fTl338AVqDgyaDRuOZ3hg9I=
|
||||||
|
google.golang.org/api v0.63.0 h1:n2bqqK895ygnBpdPDYetfy23K7fJ22wsrZKCyfuRkkA=
|
||||||
google.golang.org/api v0.63.0/go.mod h1:gs4ij2ffTRXwuzzgJl/56BdwJaA194ijkfn++9tDuPo=
|
google.golang.org/api v0.63.0/go.mod h1:gs4ij2ffTRXwuzzgJl/56BdwJaA194ijkfn++9tDuPo=
|
||||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
|
@ -842,6 +845,7 @@ google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
|
||||||
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
|
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
|
||||||
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
||||||
google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
||||||
|
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
|
||||||
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
||||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||||
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
// Copyright 2022 The Tint Authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Hash returns a hash of the provided values
|
||||||
|
func Hash(values ...any) string {
|
||||||
|
h := sha256.New()
|
||||||
|
for _, v := range values {
|
||||||
|
h.Write([]byte(fmt.Sprintf("%T⬡%+v", v, v)))
|
||||||
|
h.Write([]byte{0})
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%x", h.Sum(nil))
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
// Copyright 2022 The Tint Authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package utils_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"dawn.googlesource.com/dawn/tools/src/utils"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHash(t *testing.T) {
|
||||||
|
type Test struct {
|
||||||
|
a, b []any
|
||||||
|
expectEqual bool
|
||||||
|
}
|
||||||
|
for _, test := range []Test{
|
||||||
|
{a: []any{1}, b: []any{1}, expectEqual: true},
|
||||||
|
{a: []any{1}, b: []any{2}, expectEqual: false},
|
||||||
|
{a: []any{1}, b: []any{1.0}, expectEqual: false},
|
||||||
|
{a: []any{1.0}, b: []any{1.0}, expectEqual: true},
|
||||||
|
{a: []any{'x'}, b: []any{'x'}, expectEqual: true},
|
||||||
|
{a: []any{'x'}, b: []any{'y'}, expectEqual: false},
|
||||||
|
{a: []any{1, 2}, b: []any{1, 2}, expectEqual: true},
|
||||||
|
{a: []any{1, 2}, b: []any{1}, expectEqual: false},
|
||||||
|
{a: []any{1, 2}, b: []any{1, 3}, expectEqual: false},
|
||||||
|
} {
|
||||||
|
hashA := utils.Hash(test.a...)
|
||||||
|
hashB := utils.Hash(test.b...)
|
||||||
|
equal := hashA == hashB
|
||||||
|
if equal != test.expectEqual {
|
||||||
|
t.Errorf("Hash(%v): %v\nHash(%v): %v", test.a, hashA, test.b, hashB)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2022 The Tint Authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package utils contains small miscellaneous utilities.
|
||||||
|
package utils
|
Loading…
Reference in New Issue