github.com/bazelbuild/remote-apis-sdks@v0.0.0-20240425170053-8a36686a6350/go/pkg/client/exec_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/client"
     7  
     8  	repb "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
     9  	oppb "google.golang.org/genproto/googleapis/longrunning"
    10  	spb "google.golang.org/genproto/googleapis/rpc/status"
    11  	anypb "google.golang.org/protobuf/types/known/anypb"
    12  )
    13  
    14  func TestOperationStatus(t *testing.T) {
    15  	respv2, err := anypb.New(&repb.ExecuteResponse{Status: &spb.Status{Code: 2}})
    16  	if err != nil {
    17  		t.Fatalf("Unable to marshal V2 proto: %s", err)
    18  	}
    19  	respOther, err := anypb.New(&spb.Status{Code: 3})
    20  	if err != nil {
    21  		t.Fatalf("Unable to marshal status proto: %s", err)
    22  	}
    23  
    24  	tests := []struct {
    25  		name       string
    26  		op         *oppb.Operation
    27  		wantStatus int
    28  		wantNil    bool
    29  	}{
    30  		{
    31  			name:    "empty operation",
    32  			op:      &oppb.Operation{},
    33  			wantNil: true,
    34  		},
    35  		{
    36  			name:    "no response in operation",
    37  			op:      &oppb.Operation{Result: &oppb.Operation_Error{Error: &spb.Status{Code: 99}}},
    38  			wantNil: true, // we ignore the status in the "Operation.Error" field
    39  		},
    40  		{
    41  			name:       "correct proto present",
    42  			op:         &oppb.Operation{Result: &oppb.Operation_Response{Response: respv2}},
    43  			wantStatus: 2,
    44  		},
    45  		{
    46  			name:    "wrong proto type in response",
    47  			op:      &oppb.Operation{Result: &oppb.Operation_Response{Response: respOther}},
    48  			wantNil: true,
    49  		},
    50  	}
    51  	for _, test := range tests {
    52  		t.Run(test.name, func(t *testing.T) {
    53  			st := client.OperationStatus(test.op)
    54  			if test.wantNil {
    55  				if st != nil {
    56  					t.Errorf("OperationStatus(%v) = %v; want <nil>", test.op, st)
    57  				}
    58  				return
    59  			}
    60  			if st == nil {
    61  				t.Errorf("OperationStatus(%v) = <nil>, want status code %v", test.op, test.wantStatus)
    62  				return
    63  			}
    64  			if int(st.Code()) != test.wantStatus {
    65  				t.Errorf("OperationStatus(%v) = %v, want status code %v", test.op, st, test.wantStatus)
    66  			}
    67  		})
    68  	}
    69  }