go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/appengine/backend/common_test.go (about)

     1  // Copyright 2023 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 backend
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	computealpha "google.golang.org/api/compute/v0.alpha"
    22  	compute "google.golang.org/api/compute/v1"
    23  )
    24  
    25  // TestGetErrors tests getting the errors out of a combined stable+alpha Operation type.
    26  func TestGetErrors(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	cases := []struct {
    30  		name   string
    31  		input  Operation
    32  		output []CommonOpError
    33  	}{
    34  		{
    35  			name:   "empty operation",
    36  			input:  Operation{},
    37  			output: nil,
    38  		},
    39  		{
    40  			name: "stable operation",
    41  			input: Operation{
    42  				Stable: &compute.Operation{
    43  					Error: &compute.OperationError{
    44  						Errors: []*compute.OperationErrorErrors{
    45  							{
    46  								Code:    "hi",
    47  								Message: "bye",
    48  							},
    49  						},
    50  					},
    51  				},
    52  			},
    53  			output: []CommonOpError{
    54  				{
    55  					Code:    "hi",
    56  					Message: "bye",
    57  				},
    58  			},
    59  		},
    60  		{
    61  			name: "alpha operation",
    62  			input: Operation{
    63  				Alpha: &computealpha.Operation{
    64  					Error: &computealpha.OperationError{
    65  						Errors: []*computealpha.OperationErrorErrors{
    66  							{
    67  								Code:    "hi",
    68  								Message: "bye",
    69  							},
    70  						},
    71  					},
    72  				},
    73  			},
    74  			output: []CommonOpError{
    75  				{
    76  					Code:    "hi",
    77  					Message: "bye",
    78  				},
    79  			},
    80  		},
    81  	}
    82  
    83  	for _, tt := range cases {
    84  		tt := tt
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			t.Parallel()
    87  
    88  			expected := tt.output
    89  			actual := tt.input.GetErrors()
    90  			if diff := cmp.Diff(expected, actual); diff != "" {
    91  				t.Errorf("unexpected diff (-want +got) %s", diff)
    92  			}
    93  		})
    94  	}
    95  }