| // Copyright 2019 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 ( |
| "bytes" |
| "encoding/base64" |
| "reflect" |
| "strings" |
| "testing" |
| |
| "github.com/golang/protobuf/proto" |
| |
| buildbucketpb "go.chromium.org/luci/buildbucket/proto" |
| ) |
| |
| func TestLoadBuildProto(t *testing.T) { |
| input := &buildbucketpb.Build{ |
| Input: &buildbucketpb.Build_Input{ |
| GerritChanges: []*buildbucketpb.GerritChange{{ |
| Host: "foo-review.googlesource.com", |
| Project: "foo", |
| Change: 1234567, |
| Patchset: 4, |
| }}, |
| }, |
| } |
| unencoded, err := proto.Marshal(input) |
| if err != nil { |
| t.Errorf("failed to marshal proto: %v\ninput: %v", err, input) |
| } |
| encoded := base64.StdEncoding.EncodeToString([]byte(unencoded)) |
| output, err := loadBuildProto(strings.NewReader(encoded)) |
| if err != nil { |
| t.Fatal(encoded, err) |
| } |
| |
| inputAsText := proto.MarshalTextString(input) |
| outputAsText := proto.MarshalTextString(output) |
| if !reflect.DeepEqual(inputAsText, outputAsText) { |
| t.Errorf("wanted:\n%v\ngot:\n%v\n", inputAsText, outputAsText) |
| } |
| } |
| |
| func TestLoadBuildProtoWithUnencodedInput(t *testing.T) { |
| input := &buildbucketpb.Build{ |
| Input: &buildbucketpb.Build_Input{ |
| GerritChanges: []*buildbucketpb.GerritChange{{ |
| Host: "foo-review.googlesource.com", |
| Project: "foo", |
| Change: 1234567, |
| Patchset: 4, |
| }}, |
| }, |
| } |
| unencoded, err := proto.Marshal(input) |
| if err != nil { |
| t.Errorf("failed to marshal proto: %v\ninput: %v", err, input) |
| } |
| output, err := loadBuildProto(bytes.NewReader(unencoded)) |
| if err == nil { |
| t.Errorf("expected an error but got %v", output) |
| } |
| } |
| |
| func TestLoadBuildProtoWithInvalidMessage(t *testing.T) { |
| output, err := loadBuildProto(strings.NewReader("not a build.proto")) |
| if err == nil { |
| t.Errorf("expected an error but got %v", output) |
| } |
| } |
| |
| func TestValidateBuild(t *testing.T) { |
| tests := []struct { |
| name string |
| build *buildbucketpb.Build |
| expectError bool |
| }{ |
| { |
| name: "should err if builder property is nil", |
| build: &buildbucketpb.Build{ |
| Input: &buildbucketpb.Build_Input{ |
| GitilesCommit: &buildbucketpb.GitilesCommit{ |
| Host: "host", |
| Project: "project", |
| Id: "id", |
| }, |
| }, |
| }, |
| expectError: true, |
| }, |
| { |
| name: "should err if builder bucket is empty", |
| build: &buildbucketpb.Build{ |
| Builder: &buildbucketpb.BuilderID{ |
| Project: "project", |
| Builder: "builder", |
| }, |
| Input: &buildbucketpb.Build_Input{ |
| GitilesCommit: &buildbucketpb.GitilesCommit{ |
| Host: "host", |
| Project: "project", |
| Id: "id", |
| }, |
| }, |
| }, |
| expectError: true, |
| }, |
| { |
| name: "should err if builder name is empty", |
| build: &buildbucketpb.Build{ |
| Builder: &buildbucketpb.BuilderID{ |
| Project: "project", |
| Bucket: "bucket", |
| }, |
| Input: &buildbucketpb.Build_Input{ |
| GitilesCommit: &buildbucketpb.GitilesCommit{ |
| Host: "host", |
| Project: "project", |
| Id: "id", |
| }, |
| }, |
| }, |
| expectError: true, |
| }, |
| { |
| name: "should err if the build input is nil", |
| build: &buildbucketpb.Build{ |
| Builder: &buildbucketpb.BuilderID{ |
| Project: "project", |
| Builder: "builder", |
| Bucket: "bucket", |
| }, |
| }, |
| expectError: true, |
| }, |
| { |
| name: "should err if the build input has no GitilesCommit", |
| build: &buildbucketpb.Build{ |
| Builder: &buildbucketpb.BuilderID{ |
| Project: "project", |
| Builder: "builder", |
| Bucket: "bucket", |
| }, |
| Input: &buildbucketpb.Build_Input{}, |
| }, |
| expectError: true, |
| }, |
| { |
| name: "should err if the build input has a GerritChange but no GitilesCommit", |
| build: &buildbucketpb.Build{ |
| Builder: &buildbucketpb.BuilderID{ |
| Project: "project", |
| Builder: "builder", |
| Bucket: "bucket", |
| }, |
| Input: &buildbucketpb.Build_Input{ |
| GerritChanges: []*buildbucketpb.GerritChange{{}}, |
| }, |
| }, |
| expectError: true, |
| }, |
| { |
| name: "should return nil if all fields are non-nil and non-empty", |
| build: &buildbucketpb.Build{ |
| Builder: &buildbucketpb.BuilderID{ |
| Project: "project", |
| Bucket: "bucket", |
| Builder: "builder", |
| }, |
| Input: &buildbucketpb.Build_Input{ |
| GitilesCommit: &buildbucketpb.GitilesCommit{ |
| Host: "host", |
| Project: "project", |
| Id: "id", |
| }, |
| }, |
| }, |
| expectError: false, |
| }, |
| } |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| err := validateBuild(*tt.build) |
| switch { |
| case err != nil && !tt.expectError: |
| t.Errorf("unexpected error: %v", err) |
| case err == nil && tt.expectError: |
| t.Errorf("wanted error but got nil") |
| } |
| }) |
| } |
| } |