github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/robots/issue-creator/testowner/owner_test.go (about)

     1  /*
     2  Copyright 2016 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 testowner
    18  
    19  import (
    20  	"bufio"
    21  	"bytes"
    22  	"io/ioutil"
    23  	"os"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  func TestNormalize(t *testing.T) {
    29  	tests := map[string]string{
    30  		"A":                                    "a",
    31  		"Perf [Performance]":                   "perf",
    32  		"[k8s.io] test [performance] stuff":    "test stuff",
    33  		"[k8s.io] blah {Kubernetes e2e suite}": "blah",
    34  	}
    35  	for input, output := range tests {
    36  		result := normalize(input)
    37  		if result != output {
    38  			t.Errorf("normalize(%s) != %s (got %s)", input, output, result)
    39  		}
    40  	}
    41  }
    42  
    43  func TestOwnerList(t *testing.T) {
    44  	list := NewOwnerList(map[string]*OwnerInfo{"Perf [performance]": {
    45  		User: "me",
    46  		SIG:  "group",
    47  	}})
    48  	owner := list.TestOwner("perf [flaky]")
    49  	if owner != "me" {
    50  		t.Error("Unexpected return value ", owner)
    51  	}
    52  	sig := list.TestSIG("perf [flaky]")
    53  	if sig != "group" {
    54  		t.Error("Unexpected sig: ", sig)
    55  	}
    56  	owner = list.TestOwner("Unknown test")
    57  	if owner != "" {
    58  		t.Error("Unexpected return value ", owner)
    59  	}
    60  	sig = list.TestSIG("Unknown test")
    61  	if sig != "" {
    62  		t.Error("Unexpected sig: ", sig)
    63  	}
    64  }
    65  
    66  func TestOwnerGlob(t *testing.T) {
    67  	list := NewOwnerList(map[string]*OwnerInfo{"blah * [performance] test *": {
    68  		User: "me",
    69  		SIG:  "group",
    70  	}})
    71  	owner := list.TestOwner("blah 200 test foo")
    72  	if owner != "me" {
    73  		t.Error("Unexpected return value ", owner)
    74  	}
    75  	sig := list.TestSIG("blah 200 test foo")
    76  	if sig != "group" {
    77  		t.Error("Unexpected sig: ", sig)
    78  	}
    79  	owner = list.TestOwner("Unknown test")
    80  	if owner != "" {
    81  		t.Error("Unexpected return value ", owner)
    82  	}
    83  	sig = list.TestSIG("Unknown test")
    84  	if sig != "" {
    85  		t.Error("Unexpected sig: ", sig)
    86  	}
    87  }
    88  
    89  func TestOwnerListRandom(t *testing.T) {
    90  	list := NewOwnerList(map[string]*OwnerInfo{"testname": {
    91  		User: "a/b/c/d",
    92  	}})
    93  	counts := map[string]int{"a": 0, "b": 0, "c": 0, "d": 0}
    94  	for i := 0; i < 1000; i++ {
    95  		counts[list.TestOwner("testname")]++
    96  	}
    97  	for name, count := range counts {
    98  		if count <= 200 {
    99  			t.Errorf("Too few assigments to %s: only %d, expected > 200", name, count)
   100  		}
   101  	}
   102  }
   103  
   104  func TestOwnerListFromCsv(t *testing.T) {
   105  	r := bytes.NewReader([]byte(",,,header nonsense,\n" +
   106  		",owner,suggested owner,name,sig\n" +
   107  		",foo,other,Test name,Node\n" +
   108  		", bar,foo,other test, Windows\n"))
   109  	list, err := NewOwnerListFromCsv(r)
   110  	if err != nil {
   111  		t.Error(err)
   112  	}
   113  	if owner := list.TestOwner("test name"); owner != "foo" {
   114  		t.Error("unexpected return value ", owner)
   115  	}
   116  	if sig := list.TestSIG("test name"); sig != "Node" {
   117  		t.Error("unexpected sig value ", sig)
   118  	}
   119  	if owner := list.TestOwner("other test"); owner != "bar" {
   120  		t.Error("unexpected return value ", owner)
   121  	}
   122  	if sig := list.TestSIG("other test"); sig != "Windows" {
   123  		t.Error("unexpected sig value ", sig)
   124  	}
   125  }
   126  
   127  func TestReloadingOwnerList(t *testing.T) {
   128  	cases := []struct {
   129  		name   string
   130  		csv    string
   131  		lookup string
   132  		owner  string
   133  		sig    string
   134  		err    bool
   135  	}{
   136  		{
   137  			name:   "owner and sig",
   138  			csv:    "owner,name,sig\nfoo,flake,Scheduling\n",
   139  			lookup: "flake",
   140  			owner:  "foo",
   141  			sig:    "Scheduling",
   142  		},
   143  		{
   144  			name:   "missing sig returns BadCsv",
   145  			csv:    "owner,name,sig\nfoo,flake\n",
   146  			lookup: "flake",
   147  			err:    true,
   148  		},
   149  	}
   150  	tempfile, err := ioutil.TempFile(os.TempDir(), "ownertest")
   151  	if err != nil {
   152  		t.Error(err)
   153  	}
   154  	defer os.Remove(tempfile.Name())
   155  	defer tempfile.Close()
   156  	writer := bufio.NewWriter(tempfile)
   157  
   158  	for _, tc := range cases {
   159  		// Assuming millisecond resolution on our FS, this sleep
   160  		// ensures the mtime will change with the next write.
   161  		time.Sleep(5 * time.Millisecond)
   162  		// Clear file and reset writing offset
   163  		tempfile.Truncate(0)
   164  		tempfile.Seek(0, os.SEEK_SET)
   165  		writer.Reset(tempfile)
   166  		_, err = writer.WriteString(tc.csv)
   167  		if err != nil {
   168  			t.Error(err)
   169  		}
   170  		err = writer.Flush()
   171  		if err != nil {
   172  			t.Error(err)
   173  		}
   174  		list, err := NewReloadingOwnerList(tempfile.Name())
   175  		if err != nil && !tc.err {
   176  			t.Errorf("%s: unexpected error: %v", tc.name, err)
   177  		}
   178  		if tc.err {
   179  			if err == nil {
   180  				t.Errorf("%s: expected an error", tc.name)
   181  			}
   182  			_, ok := err.(BadCsv)
   183  			if !ok {
   184  				t.Errorf("%s: error type is not BadCsv: %v", tc.name, err)
   185  			}
   186  			if list == nil {
   187  				t.Errorf("%s: did not return a list during BadCsv", tc.name)
   188  			}
   189  		}
   190  		if owner := list.TestOwner(tc.lookup); owner != tc.owner {
   191  			t.Errorf("%s: bad owner %s != %s", tc.name, owner, tc.owner)
   192  		}
   193  		if sig := list.TestSIG(tc.lookup); sig != tc.sig {
   194  			t.Errorf("%s: bad sig %s != %s", tc.name, sig, tc.sig)
   195  		}
   196  	}
   197  }