go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/autocorrelator/check_try_test.go (about)

     1  // Copyright 2021 The Fuchsia Authors.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"testing"
     9  
    10  	buildbucketpb "go.chromium.org/luci/buildbucket/proto"
    11  	"go.fuchsia.dev/infra/cmd/autocorrelator/findings"
    12  	"google.golang.org/protobuf/types/known/structpb"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestCheckTry(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	tests := []struct {
    21  		name               string
    22  		builds             []*buildbucketpb.Build
    23  		changeNum          int64
    24  		status             buildbucketpb.Status
    25  		summaryMarkdown    string
    26  		ignoreSkippedBuild bool
    27  		ignoreSkippedTests bool
    28  		ignoreFailedBuild  bool
    29  		expected           []*findings.SummarySimilarity
    30  	}{
    31  		{
    32  			name: "many past builds",
    33  			builds: []*buildbucketpb.Build{
    34  				{
    35  					Id:              int64(999999),
    36  					Status:          buildbucketpb.Status_SUCCESS,
    37  					SummaryMarkdown: "",
    38  					Input: &buildbucketpb.Build_Input{
    39  						GerritChanges: []*buildbucketpb.GerritChange{
    40  							{
    41  								Change: int64(111111),
    42  							},
    43  						},
    44  					},
    45  					Output: &buildbucketpb.Build_Output{
    46  						Properties: &structpb.Struct{
    47  							Fields: map[string]*structpb.Value{},
    48  						},
    49  					},
    50  				},
    51  				{
    52  					Id:              int64(999998),
    53  					Status:          buildbucketpb.Status_FAILURE,
    54  					SummaryMarkdown: "ERROR",
    55  					Input: &buildbucketpb.Build_Input{
    56  						GerritChanges: []*buildbucketpb.GerritChange{
    57  							{
    58  								Change: int64(222222),
    59  							},
    60  						},
    61  					},
    62  					Output: &buildbucketpb.Build_Output{
    63  						Properties: &structpb.Struct{
    64  							Fields: map[string]*structpb.Value{},
    65  						},
    66  					},
    67  				},
    68  				// This has an unaffected build graph, and should be ignored.
    69  				{
    70  					Id:              int64(999997),
    71  					Status:          buildbucketpb.Status_SUCCESS,
    72  					SummaryMarkdown: "",
    73  					Input: &buildbucketpb.Build_Input{
    74  						GerritChanges: []*buildbucketpb.GerritChange{
    75  							{
    76  								Change: int64(222222),
    77  							},
    78  						},
    79  					},
    80  					Output: &buildbucketpb.Build_Output{
    81  						Properties: &structpb.Struct{
    82  							Fields: map[string]*structpb.Value{
    83  								"skipped_because_unaffected": {
    84  									Kind: &structpb.Value_BoolValue{
    85  										BoolValue: true,
    86  									},
    87  								},
    88  							},
    89  						},
    90  					},
    91  				},
    92  				// This has no affected tests, and should be ignored.
    93  				{
    94  					Id:              int64(999996),
    95  					Status:          buildbucketpb.Status_SUCCESS,
    96  					SummaryMarkdown: "",
    97  					Input: &buildbucketpb.Build_Input{
    98  						GerritChanges: []*buildbucketpb.GerritChange{
    99  							{
   100  								Change: int64(222222),
   101  							},
   102  						},
   103  					},
   104  					Output: &buildbucketpb.Build_Output{
   105  						Properties: &structpb.Struct{
   106  							Fields: map[string]*structpb.Value{
   107  								"affected_tests_no_work": {
   108  									Kind: &structpb.Value_BoolValue{
   109  										BoolValue: true,
   110  									},
   111  								},
   112  							},
   113  						},
   114  					},
   115  				},
   116  				// This failed to build, and should be ignored.
   117  				{
   118  					Id:              int64(999995),
   119  					Status:          buildbucketpb.Status_SUCCESS,
   120  					SummaryMarkdown: "",
   121  					Input: &buildbucketpb.Build_Input{
   122  						GerritChanges: []*buildbucketpb.GerritChange{
   123  							{
   124  								Change: int64(222222),
   125  							},
   126  						},
   127  					},
   128  					Output: &buildbucketpb.Build_Output{
   129  						Properties: &structpb.Struct{
   130  							Fields: map[string]*structpb.Value{
   131  								"failed_to_build": {
   132  									Kind: &structpb.Value_BoolValue{
   133  										BoolValue: true,
   134  									},
   135  								},
   136  							},
   137  						},
   138  					},
   139  				},
   140  				// This matches the input change number, and should be ignored.
   141  				{
   142  					Id:              int64(999994),
   143  					Status:          buildbucketpb.Status_FAILURE,
   144  					SummaryMarkdown: "ERROR",
   145  					Input: &buildbucketpb.Build_Input{
   146  						GerritChanges: []*buildbucketpb.GerritChange{
   147  							{
   148  								Change: int64(123456),
   149  							},
   150  						},
   151  					},
   152  					Output: &buildbucketpb.Build_Output{
   153  						Properties: &structpb.Struct{
   154  							Fields: map[string]*structpb.Value{},
   155  						},
   156  					},
   157  				},
   158  				// This does not match the input status of FAILURE, and should
   159  				// be ignored.
   160  				{
   161  					Id:              int64(999993),
   162  					Status:          buildbucketpb.Status_INFRA_FAILURE,
   163  					SummaryMarkdown: "ERROR: checkout failed",
   164  					Input: &buildbucketpb.Build_Input{
   165  						GerritChanges: []*buildbucketpb.GerritChange{
   166  							{
   167  								Change: int64(333333),
   168  							},
   169  						},
   170  					},
   171  					Output: &buildbucketpb.Build_Output{
   172  						Properties: &structpb.Struct{
   173  							Fields: map[string]*structpb.Value{},
   174  						},
   175  					},
   176  				},
   177  				// This has no output properties and should be ignored.
   178  				{
   179  					Id:              int64(999992),
   180  					Status:          buildbucketpb.Status_FAILURE,
   181  					SummaryMarkdown: "failed for unknown reasons",
   182  					Input: &buildbucketpb.Build_Input{
   183  						GerritChanges: []*buildbucketpb.GerritChange{
   184  							{
   185  								Change: int64(333333),
   186  							},
   187  						},
   188  					},
   189  					Output: &buildbucketpb.Build_Output{
   190  						Properties: nil,
   191  					},
   192  				},
   193  				// This is a CANCELED build which should be ignored.
   194  				{
   195  					Id:              int64(999991),
   196  					Status:          buildbucketpb.Status_CANCELED,
   197  					SummaryMarkdown: "canceled",
   198  					Input: &buildbucketpb.Build_Input{
   199  						GerritChanges: []*buildbucketpb.GerritChange{
   200  							{
   201  								Change: int64(333333),
   202  							},
   203  						},
   204  					},
   205  					Output: &buildbucketpb.Build_Output{},
   206  				},
   207  			},
   208  			status:             buildbucketpb.Status_FAILURE,
   209  			changeNum:          int64(123456),
   210  			summaryMarkdown:    "ERROR",
   211  			ignoreSkippedBuild: true,
   212  			ignoreSkippedTests: true,
   213  			ignoreFailedBuild:  true,
   214  			expected: []*findings.SummarySimilarity{
   215  				{
   216  					Score:   0.0,
   217  					BuildId: "999999",
   218  					IsGreen: true,
   219  				},
   220  				{
   221  					Score:   1.0,
   222  					BuildId: "999998",
   223  					IsGreen: false,
   224  				},
   225  			},
   226  		},
   227  		{
   228  			name: "past build with no gerrit_changes",
   229  			builds: []*buildbucketpb.Build{
   230  				{
   231  					Id:              int64(999999),
   232  					Status:          buildbucketpb.Status_SUCCESS,
   233  					SummaryMarkdown: "",
   234  					Input:           &buildbucketpb.Build_Input{},
   235  					Output: &buildbucketpb.Build_Output{
   236  						Properties: &structpb.Struct{
   237  							Fields: map[string]*structpb.Value{},
   238  						},
   239  					},
   240  				},
   241  			},
   242  			status:             buildbucketpb.Status_FAILURE,
   243  			changeNum:          int64(123456),
   244  			summaryMarkdown:    "ERROR",
   245  			ignoreSkippedBuild: true,
   246  			ignoreSkippedTests: true,
   247  			ignoreFailedBuild:  true,
   248  			expected:           nil,
   249  		},
   250  	}
   251  
   252  	for _, test := range tests {
   253  		t.Run(test.name, func(t *testing.T) {
   254  			comparator := mockComparator{}
   255  			ss := checkTry(test.builds, test.changeNum, test.status, test.summaryMarkdown, comparator, test.ignoreSkippedBuild, test.ignoreSkippedTests, test.ignoreFailedBuild, "", "")
   256  			if diff := cmp.Diff(test.expected, ss); diff != "" {
   257  				t.Fatalf("different (-want +got):\n%s", diff)
   258  			}
   259  		})
   260  	}
   261  }