github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/tide/history/history_test.go (about)

     1  /*
     2  Copyright 2018 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 history
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"reflect"
    23  	"testing"
    24  	"time"
    25  )
    26  
    27  func TestHistory(t *testing.T) {
    28  	var nowTime = time.Now()
    29  	oldNow := now
    30  	now = func() time.Time { return nowTime }
    31  	defer func() { now = oldNow }()
    32  
    33  	const logSizeLimit = 3
    34  	nextTime := func() time.Time {
    35  		nowTime = nowTime.Add(time.Minute)
    36  		return nowTime
    37  	}
    38  
    39  	testMeta := func(num int, author string) PRMeta {
    40  		return PRMeta{
    41  			Num:    num,
    42  			Title:  fmt.Sprintf("PR #%d", num),
    43  			SHA:    fmt.Sprintf("SHA for %d", num),
    44  			Author: author,
    45  		}
    46  	}
    47  
    48  	hist := New(logSizeLimit)
    49  	time1 := nextTime()
    50  	hist.Record("pool A", "TRIGGER", "sha A", "", []PRMeta{testMeta(1, "bob")})
    51  	nextTime()
    52  	hist.Record("pool B", "MERGE", "sha B1", "", []PRMeta{testMeta(2, "joe")})
    53  	time3 := nextTime()
    54  	hist.Record("pool B", "MERGE", "sha B2", "", []PRMeta{testMeta(3, "jeff")})
    55  	time4 := nextTime()
    56  	hist.Record("pool B", "MERGE_BATCH", "sha B3", "", []PRMeta{testMeta(4, "joe"), testMeta(5, "jim")})
    57  	time5 := nextTime()
    58  	hist.Record("pool C", "TRIGGER_BATCH", "sha C1", "", []PRMeta{testMeta(6, "joe"), testMeta(8, "me")})
    59  	time6 := nextTime()
    60  	hist.Record("pool B", "TRIGGER", "sha B4", "", []PRMeta{testMeta(7, "abe")})
    61  
    62  	expected := map[string][]*Record{
    63  		"pool A": {
    64  			&Record{
    65  				Time:    time1,
    66  				BaseSHA: "sha A",
    67  				Action:  "TRIGGER",
    68  				Target: []PRMeta{
    69  					testMeta(1, "bob"),
    70  				},
    71  			},
    72  		},
    73  		"pool B": {
    74  			&Record{
    75  				Time:    time6,
    76  				BaseSHA: "sha B4",
    77  				Action:  "TRIGGER",
    78  				Target: []PRMeta{
    79  					testMeta(7, "abe"),
    80  				},
    81  			},
    82  			&Record{
    83  				Time:    time4,
    84  				BaseSHA: "sha B3",
    85  				Action:  "MERGE_BATCH",
    86  				Target: []PRMeta{
    87  					testMeta(4, "joe"),
    88  					testMeta(5, "jim"),
    89  				},
    90  			},
    91  			&Record{
    92  				Time:    time3,
    93  				BaseSHA: "sha B2",
    94  				Action:  "MERGE",
    95  				Target: []PRMeta{
    96  					testMeta(3, "jeff"),
    97  				},
    98  			},
    99  		},
   100  		"pool C": {
   101  			&Record{
   102  				Time:    time5,
   103  				BaseSHA: "sha C1",
   104  				Action:  "TRIGGER_BATCH",
   105  				Target: []PRMeta{
   106  					testMeta(6, "joe"),
   107  					testMeta(8, "me"),
   108  				},
   109  			},
   110  		},
   111  	}
   112  
   113  	if got := hist.AllRecords(); !reflect.DeepEqual(got, expected) {
   114  		es, _ := json.Marshal(expected)
   115  		gs, _ := json.Marshal(got)
   116  		t.Errorf("Expected history \n%s, but got \n%s.", es, gs)
   117  		t.Logf("strs equal: %v.", string(es) == string(gs))
   118  	}
   119  }