github.com/abayer/test-infra@v0.0.5/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 TestOwnerListFromCsv(t *testing.T) {
    90  	r := bytes.NewReader([]byte(",,,header nonsense,\n" +
    91  		",owner,suggested owner,name,sig\n" +
    92  		",foo,other,Test name,Node\n" +
    93  		", bar,foo,other test, Windows\n"))
    94  	list, err := NewOwnerListFromCsv(r)
    95  	if err != nil {
    96  		t.Error(err)
    97  	}
    98  	if owner := list.TestOwner("test name"); owner != "foo" {
    99  		t.Error("unexpected return value ", owner)
   100  	}
   101  	if sig := list.TestSIG("test name"); sig != "Node" {
   102  		t.Error("unexpected sig value ", sig)
   103  	}
   104  	if owner := list.TestOwner("other test"); owner != "bar" {
   105  		t.Error("unexpected return value ", owner)
   106  	}
   107  	if sig := list.TestSIG("other test"); sig != "Windows" {
   108  		t.Error("unexpected sig value ", sig)
   109  	}
   110  }
   111  
   112  func TestReloadingOwnerList(t *testing.T) {
   113  	cases := []struct {
   114  		name   string
   115  		csv    string
   116  		lookup string
   117  		owner  string
   118  		sig    string
   119  		err    bool
   120  	}{
   121  		{
   122  			name:   "owner and sig",
   123  			csv:    "owner,name,sig\nfoo,flake,Scheduling\n",
   124  			lookup: "flake",
   125  			owner:  "foo",
   126  			sig:    "Scheduling",
   127  		},
   128  		{
   129  			name:   "missing sig returns badCsv",
   130  			csv:    "owner,name,sig\nfoo,flake\n",
   131  			lookup: "flake",
   132  			err:    true,
   133  		},
   134  	}
   135  	tempfile, err := ioutil.TempFile(os.TempDir(), "ownertest")
   136  	if err != nil {
   137  		t.Error(err)
   138  	}
   139  	defer os.Remove(tempfile.Name())
   140  	defer tempfile.Close()
   141  	writer := bufio.NewWriter(tempfile)
   142  
   143  	for _, tc := range cases {
   144  		// Assuming millisecond resolution on our FS, this sleep
   145  		// ensures the mtime will change with the next write.
   146  		time.Sleep(5 * time.Millisecond)
   147  		// Clear file and reset writing offset
   148  		tempfile.Truncate(0)
   149  		tempfile.Seek(0, os.SEEK_SET)
   150  		writer.Reset(tempfile)
   151  		_, err = writer.WriteString(tc.csv)
   152  		if err != nil {
   153  			t.Error(err)
   154  		}
   155  		err = writer.Flush()
   156  		if err != nil {
   157  			t.Error(err)
   158  		}
   159  		list, err := NewReloadingOwnerList(tempfile.Name())
   160  		if err != nil && !tc.err {
   161  			t.Errorf("%s: unexpected error: %v", tc.name, err)
   162  		}
   163  		if tc.err {
   164  			if err == nil {
   165  				t.Errorf("%s: expected an error", tc.name)
   166  			}
   167  			_, ok := err.(badCsv)
   168  			if !ok {
   169  				t.Errorf("%s: error type is not badCsv: %v", tc.name, err)
   170  			}
   171  			if list == nil {
   172  				t.Errorf("%s: did not return a list during badCsv", tc.name)
   173  			}
   174  		}
   175  		if owner := list.TestOwner(tc.lookup); owner != tc.owner {
   176  			t.Errorf("%s: bad owner %s != %s", tc.name, owner, tc.owner)
   177  		}
   178  		if sig := list.TestSIG(tc.lookup); sig != tc.sig {
   179  			t.Errorf("%s: bad sig %s != %s", tc.name, sig, tc.sig)
   180  		}
   181  	}
   182  }