go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luciexe/build/errors_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 build
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  
    23  	bbpb "go.chromium.org/luci/buildbucket/proto"
    24  	"go.chromium.org/luci/common/errors"
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  )
    27  
    28  func TestErrors(t *testing.T) {
    29  	Convey(`Errors`, t, func() {
    30  		Convey(`nil`, func() {
    31  			var err error
    32  
    33  			So(AttachStatus(err, bbpb.Status_INFRA_FAILURE, &bbpb.StatusDetails{
    34  				ResourceExhaustion: &bbpb.StatusDetails_ResourceExhaustion{},
    35  			}), ShouldBeNil)
    36  
    37  			status, details := ExtractStatus(err)
    38  			So(status, ShouldResemble, bbpb.Status_SUCCESS)
    39  			So(details, ShouldBeNil)
    40  		})
    41  
    42  		Convey(`err`, func() {
    43  			Convey(`generic`, func() {
    44  				err := errors.New("some error")
    45  				status, details := ExtractStatus(err)
    46  				So(status, ShouldResemble, bbpb.Status_FAILURE)
    47  				So(details, ShouldBeNil)
    48  			})
    49  
    50  			Convey(`AttachStatus`, func() {
    51  				err := errors.New("some error")
    52  				err2 := AttachStatus(err, bbpb.Status_INFRA_FAILURE, &bbpb.StatusDetails{
    53  					ResourceExhaustion: &bbpb.StatusDetails_ResourceExhaustion{},
    54  				})
    55  
    56  				status, details := ExtractStatus(err2)
    57  				So(status, ShouldResemble, bbpb.Status_INFRA_FAILURE)
    58  				So(details, ShouldResembleProto, &bbpb.StatusDetails{
    59  					ResourceExhaustion: &bbpb.StatusDetails_ResourceExhaustion{},
    60  				})
    61  			})
    62  
    63  			Convey(`context`, func() {
    64  				status, details := ExtractStatus(context.Canceled)
    65  				So(status, ShouldResemble, bbpb.Status_CANCELED)
    66  				So(details, ShouldBeNil)
    67  
    68  				status, details = ExtractStatus(context.DeadlineExceeded)
    69  				So(status, ShouldResemble, bbpb.Status_INFRA_FAILURE)
    70  				So(details, ShouldResembleProto, &bbpb.StatusDetails{
    71  					Timeout: &bbpb.StatusDetails_Timeout{},
    72  				})
    73  			})
    74  
    75  		})
    76  
    77  		Convey(`AttachStatus panics for bad status`, func() {
    78  			So(func() {
    79  				AttachStatus(nil, bbpb.Status_STARTED, nil)
    80  			}, ShouldPanicLike, "cannot be used with non-terminal status \"STARTED\"")
    81  		})
    82  	})
    83  }