github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/gerrit/client/client_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 client
    18  
    19  import (
    20  	"reflect"
    21  	"sort"
    22  	"strings"
    23  	"testing"
    24  	"time"
    25  
    26  	gerrit "github.com/andygrunwald/go-gerrit"
    27  )
    28  
    29  type fgc struct {
    30  	instance string
    31  	changes  map[string][]gerrit.ChangeInfo
    32  }
    33  
    34  func (f *fgc) QueryChanges(opt *gerrit.QueryChangeOptions) (*[]gerrit.ChangeInfo, *gerrit.Response, error) {
    35  	changes := []gerrit.ChangeInfo{}
    36  
    37  	changeInfos, ok := f.changes[f.instance]
    38  	if !ok {
    39  		return &changes, nil, nil
    40  	}
    41  
    42  	project := ""
    43  	for _, query := range opt.Query {
    44  		for _, q := range strings.Split(query, "+") {
    45  			if strings.HasPrefix(q, "project:") {
    46  				project = q[8:]
    47  			}
    48  		}
    49  	}
    50  
    51  	for idx, change := range changeInfos {
    52  		if idx >= opt.Start && len(changes) <= opt.Limit {
    53  			if project == change.Project {
    54  				changes = append(changes, change)
    55  			}
    56  		}
    57  	}
    58  
    59  	return &changes, nil, nil
    60  }
    61  
    62  func (f *fgc) SetReview(changeID, revisionID string, input *gerrit.ReviewInput) (*gerrit.ReviewResult, *gerrit.Response, error) {
    63  	return nil, nil, nil
    64  }
    65  
    66  func TestQueryChange(t *testing.T) {
    67  	now := time.Now().UTC()
    68  	layout := "2006-01-02 15:04:05"
    69  
    70  	var testcases = []struct {
    71  		name       string
    72  		lastUpdate time.Time
    73  		changes    map[string][]gerrit.ChangeInfo
    74  		revisions  map[string][]string
    75  	}{
    76  		{
    77  			name:       "no changes",
    78  			lastUpdate: now,
    79  			revisions:  map[string][]string{},
    80  		},
    81  		{
    82  			name:       "one outdated change",
    83  			lastUpdate: now,
    84  			changes: map[string][]gerrit.ChangeInfo{
    85  				"foo": {
    86  					{
    87  						Project:         "bar",
    88  						ID:              "1",
    89  						CurrentRevision: "1-1",
    90  						Updated:         now.Add(-time.Hour).Format(layout),
    91  						Revisions: map[string]gerrit.RevisionInfo{
    92  							"1-1": {
    93  								Created: now.Add(-time.Hour).Format(layout),
    94  							},
    95  						},
    96  						Status: "NEW",
    97  					},
    98  				},
    99  			},
   100  			revisions: map[string][]string{},
   101  		},
   102  		{
   103  			name:       "one up-to-date change",
   104  			lastUpdate: now.Add(-time.Minute),
   105  			changes: map[string][]gerrit.ChangeInfo{
   106  				"foo": {
   107  					{
   108  						Project:         "bar",
   109  						ID:              "1",
   110  						CurrentRevision: "1-1",
   111  						Updated:         now.Format(layout),
   112  						Revisions: map[string]gerrit.RevisionInfo{
   113  							"1-1": {
   114  								Created: now.Format(layout),
   115  							},
   116  						},
   117  						Status: "NEW",
   118  					},
   119  				},
   120  			},
   121  			revisions: map[string][]string{
   122  				"foo": {"1-1"},
   123  			},
   124  		},
   125  		{
   126  			name:       "one up-to-date change, same timestamp",
   127  			lastUpdate: now.Truncate(time.Second),
   128  			changes: map[string][]gerrit.ChangeInfo{
   129  				"foo": {
   130  					{
   131  						Project:         "bar",
   132  						ID:              "1",
   133  						CurrentRevision: "1-1",
   134  						Updated:         now.Format(layout),
   135  						Revisions: map[string]gerrit.RevisionInfo{
   136  							"1-1": {
   137  								Created: now.Format(layout),
   138  							},
   139  						},
   140  						Status: "NEW",
   141  					},
   142  				},
   143  			},
   144  			revisions: map[string][]string{
   145  				"foo": {"1-1"},
   146  			},
   147  		},
   148  		{
   149  			name:       "one up-to-date change but stale commit",
   150  			lastUpdate: now.Add(-time.Minute),
   151  			changes: map[string][]gerrit.ChangeInfo{
   152  				"foo": {
   153  					{
   154  						Project:         "bar",
   155  						ID:              "1",
   156  						CurrentRevision: "1-1",
   157  						Updated:         now.Format(layout),
   158  						Revisions: map[string]gerrit.RevisionInfo{
   159  							"1-1": {
   160  								Created: now.Add(-time.Hour).Format(layout),
   161  							},
   162  						},
   163  						Status: "NEW",
   164  					},
   165  				},
   166  			},
   167  			revisions: map[string][]string{},
   168  		},
   169  		{
   170  			name:       "one up-to-date change, wrong instance",
   171  			lastUpdate: now.Add(-time.Minute),
   172  			changes: map[string][]gerrit.ChangeInfo{
   173  				"evil": {
   174  					{
   175  						Project:         "bar",
   176  						ID:              "1",
   177  						CurrentRevision: "1-1",
   178  						Updated:         now.Format(layout),
   179  						Revisions: map[string]gerrit.RevisionInfo{
   180  							"1-1": {
   181  								Created: now.Format(layout),
   182  							},
   183  						},
   184  						Status: "NEW",
   185  					},
   186  				},
   187  			},
   188  			revisions: map[string][]string{},
   189  		},
   190  		{
   191  			name:       "one up-to-date change, wrong project",
   192  			lastUpdate: now.Add(-time.Minute),
   193  			changes: map[string][]gerrit.ChangeInfo{
   194  				"foo": {
   195  					{
   196  						Project:         "evil",
   197  						ID:              "1",
   198  						CurrentRevision: "1-1",
   199  						Updated:         now.Format(layout),
   200  						Revisions: map[string]gerrit.RevisionInfo{
   201  							"1-1": {
   202  								Created: now.Format(layout),
   203  							},
   204  						},
   205  						Status: "NEW",
   206  					},
   207  				},
   208  			},
   209  			revisions: map[string][]string{},
   210  		},
   211  		{
   212  			name:       "two up-to-date changes, two projects",
   213  			lastUpdate: now.Add(-time.Minute),
   214  			changes: map[string][]gerrit.ChangeInfo{
   215  				"foo": {
   216  					{
   217  						Project:         "bar",
   218  						ID:              "1",
   219  						CurrentRevision: "1-1",
   220  						Updated:         now.Format(layout),
   221  						Revisions: map[string]gerrit.RevisionInfo{
   222  							"1-1": {
   223  								Created: now.Format(layout),
   224  							},
   225  						},
   226  						Status: "NEW",
   227  					},
   228  					{
   229  						Project:         "bar",
   230  						ID:              "2",
   231  						CurrentRevision: "2-1",
   232  						Updated:         now.Format(layout),
   233  						Revisions: map[string]gerrit.RevisionInfo{
   234  							"2-1": {
   235  								Created: now.Format(layout),
   236  							},
   237  						},
   238  						Status: "NEW",
   239  					},
   240  				},
   241  			},
   242  			revisions: map[string][]string{
   243  				"foo": {"1-1", "2-1"},
   244  			},
   245  		},
   246  		{
   247  			name:       "one good one bad",
   248  			lastUpdate: now.Add(-time.Minute),
   249  			changes: map[string][]gerrit.ChangeInfo{
   250  				"foo": {
   251  					{
   252  						Project:         "bar",
   253  						ID:              "1",
   254  						CurrentRevision: "1-1",
   255  						Updated:         now.Format(layout),
   256  						Revisions: map[string]gerrit.RevisionInfo{
   257  							"1-1": {
   258  								Created: now.Format(layout),
   259  							},
   260  						},
   261  						Status: "NEW",
   262  					},
   263  					{
   264  						Project:         "bar",
   265  						ID:              "2",
   266  						CurrentRevision: "2-1",
   267  						Updated:         now.Add(-time.Hour).Format(layout),
   268  						Revisions: map[string]gerrit.RevisionInfo{
   269  							"2-1": {
   270  								Created: now.Add(-time.Hour).Format(layout),
   271  							},
   272  						},
   273  						Status: "NEW",
   274  					},
   275  				},
   276  			},
   277  			revisions: map[string][]string{
   278  				"foo": {"1-1"},
   279  			},
   280  		},
   281  		{
   282  			name:       "multiple up-to-date changes",
   283  			lastUpdate: now.Add(-time.Minute),
   284  			changes: map[string][]gerrit.ChangeInfo{
   285  				"foo": {
   286  					{
   287  						Project:         "bar",
   288  						ID:              "1",
   289  						CurrentRevision: "1-1",
   290  						Updated:         now.Format(layout),
   291  						Revisions: map[string]gerrit.RevisionInfo{
   292  							"1-1": {
   293  								Created: now.Format(layout),
   294  							},
   295  						},
   296  						Status: "NEW",
   297  					},
   298  					{
   299  						Project:         "bar",
   300  						ID:              "2",
   301  						CurrentRevision: "2-1",
   302  						Updated:         now.Format(layout),
   303  						Revisions: map[string]gerrit.RevisionInfo{
   304  							"2-1": {
   305  								Created: now.Format(layout),
   306  							},
   307  						},
   308  						Status: "NEW",
   309  					},
   310  				},
   311  				"baz": {
   312  					{
   313  						Project:         "boo",
   314  						ID:              "3",
   315  						CurrentRevision: "3-2",
   316  						Updated:         now.Format(layout),
   317  						Revisions: map[string]gerrit.RevisionInfo{
   318  							"3-2": {
   319  								Created: now.Format(layout),
   320  							},
   321  							"3-1": {
   322  								Created: now.Format(layout),
   323  							},
   324  						},
   325  						Status: "NEW",
   326  					},
   327  					{
   328  						Project:         "evil",
   329  						ID:              "4",
   330  						CurrentRevision: "4-1",
   331  						Updated:         now.Add(-time.Hour).Format(layout),
   332  						Revisions: map[string]gerrit.RevisionInfo{
   333  							"4-1": {
   334  								Created: now.Add(-time.Hour).Format(layout),
   335  							},
   336  						},
   337  						Status: "NEW",
   338  					},
   339  				},
   340  			},
   341  			revisions: map[string][]string{
   342  				"foo": {"1-1", "2-1"},
   343  				"baz": {"3-2"},
   344  			},
   345  		},
   346  		{
   347  			name:       "one up-to-date merged change",
   348  			lastUpdate: now.Add(-time.Minute),
   349  			changes: map[string][]gerrit.ChangeInfo{
   350  				"foo": {
   351  					{
   352  						Project:         "bar",
   353  						ID:              "1",
   354  						CurrentRevision: "1-1",
   355  						Updated:         now.Format(layout),
   356  						Submitted:       now.Format(layout),
   357  						Status:          "MERGED",
   358  					},
   359  				},
   360  			},
   361  			revisions: map[string][]string{
   362  				"foo": {"1-1"},
   363  			},
   364  		},
   365  		{
   366  			name:       "one up-to-date abandoned change",
   367  			lastUpdate: now.Add(-time.Minute),
   368  			changes: map[string][]gerrit.ChangeInfo{
   369  				"foo": {
   370  					{
   371  						Project:         "bar",
   372  						ID:              "1",
   373  						CurrentRevision: "1-1",
   374  						Updated:         now.Format(layout),
   375  						Submitted:       now.Format(layout),
   376  						Status:          "ABANDONED",
   377  					},
   378  				},
   379  			},
   380  			revisions: map[string][]string{},
   381  		},
   382  		{
   383  			name:       "merged change recently updated but submitted before last update",
   384  			lastUpdate: now.Add(-time.Minute),
   385  			changes: map[string][]gerrit.ChangeInfo{
   386  				"foo": {
   387  					{
   388  						Project:         "bar",
   389  						ID:              "1",
   390  						CurrentRevision: "1-1",
   391  						Updated:         now.Format(layout),
   392  						Submitted:       now.Add(-2 * time.Minute).Format(layout),
   393  						Status:          "MERGED",
   394  					},
   395  				},
   396  			},
   397  			revisions: map[string][]string{},
   398  		},
   399  		{
   400  			name:       "one abandoned, one merged",
   401  			lastUpdate: now.Add(-time.Minute),
   402  			changes: map[string][]gerrit.ChangeInfo{
   403  				"foo": {
   404  					{
   405  						Project:         "bar",
   406  						ID:              "1",
   407  						CurrentRevision: "1-1",
   408  						Updated:         now.Format(layout),
   409  						Status:          "ABANDONED",
   410  					},
   411  					{
   412  						Project:         "bar",
   413  						ID:              "2",
   414  						CurrentRevision: "2-1",
   415  						Updated:         now.Format(layout),
   416  						Submitted:       now.Format(layout),
   417  						Status:          "MERGED",
   418  					},
   419  				},
   420  			},
   421  			revisions: map[string][]string{
   422  				"foo": {"2-1"},
   423  			},
   424  		},
   425  	}
   426  
   427  	for _, tc := range testcases {
   428  		client := &Client{
   429  			handlers: map[string]*gerritInstanceHandler{
   430  				"foo": {
   431  					instance: "foo",
   432  					projects: []string{"bar"},
   433  					changeService: &fgc{
   434  						changes:  tc.changes,
   435  						instance: "foo",
   436  					},
   437  				},
   438  				"baz": {
   439  					instance: "baz",
   440  					projects: []string{"boo"},
   441  					changeService: &fgc{
   442  						changes:  tc.changes,
   443  						instance: "baz",
   444  					},
   445  				},
   446  			},
   447  		}
   448  
   449  		changes := client.QueryChanges(tc.lastUpdate, 5)
   450  
   451  		revisions := map[string][]string{}
   452  		for instance, changes := range changes {
   453  			revisions[instance] = []string{}
   454  			for _, change := range changes {
   455  				revisions[instance] = append(revisions[instance], change.CurrentRevision)
   456  			}
   457  			sort.Strings(revisions[instance])
   458  		}
   459  
   460  		if !reflect.DeepEqual(revisions, tc.revisions) {
   461  			t.Errorf("tc %s - wrong revisions: got %#v, expect %#v", tc.name, revisions, tc.revisions)
   462  		}
   463  	}
   464  }