sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/tide/codereview_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tide
    18  
    19  import (
    20  	"encoding/json"
    21  	"regexp"
    22  	"strconv"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  	fuzz "github.com/google/gofuzz"
    28  )
    29  
    30  func TestMinStruct(t *testing.T) {
    31  	var fieldsPopulated bool
    32  	for i := 0; i < 100; i++ {
    33  		seed := time.Now().UnixNano()
    34  		fuzzer := fuzz.NewWithSeed(seed)
    35  
    36  		crc := &CodeReviewCommon{}
    37  		// For unknown reason, including Gerrit field causing timeout, ignoring
    38  		// it, it should be fine as this test is focusing on the fields for
    39  		// the Min struct only.
    40  		fuzzer.SkipFieldsWithPattern(regexp.MustCompile(`Gerrit`)).Fuzz(crc)
    41  
    42  		// Guard against the chance of fuzzer not doing it's job.
    43  		if crc.Title+strconv.Itoa(crc.Number)+crc.HeadRefOID+crc.Mergeable != "0" {
    44  			fieldsPopulated = true
    45  		}
    46  		want := CodeReviewForDeck{
    47  			Title:      crc.Title,
    48  			Number:     crc.Number,
    49  			HeadRefOID: crc.HeadRefOID,
    50  			Mergeable:  crc.Mergeable,
    51  		}
    52  		wantBytes, err := json.Marshal(&want)
    53  		if err != nil {
    54  			t.Fatalf("Unexpected marshal error from want struct: %v", err)
    55  		}
    56  
    57  		casted := MinCodeReviewCommon(*crc)
    58  		gotBytes, err := json.Marshal(&casted)
    59  		if err != nil {
    60  			t.Fatalf("Unexpected marshal error from got struct: %v", err)
    61  		}
    62  		if diff := cmp.Diff(string(wantBytes), string(gotBytes)); diff != "" {
    63  			t.Fatalf("Output mismatch. Want(-), got(+):\n%s", diff)
    64  		}
    65  	}
    66  
    67  	if !fieldsPopulated {
    68  		t.Fatalf("Expecting fuzzer to fuzz Title, Number, HeadRefOID, or Mergeable, but it didn't")
    69  	}
    70  }