|
| 1 | +/* |
| 2 | +Copyright 2016 The Kubernetes Authors All rights reserved. |
| 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 | + "encoding/csv" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + "regexp" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/golang/glog" |
| 28 | +) |
| 29 | + |
| 30 | +var tagRegex = regexp.MustCompile(`\[.*?\]|\{.*?\}`) |
| 31 | +var whiteSpaceRegex = regexp.MustCompile(`\s+`) |
| 32 | + |
| 33 | +// Turn a test name into a canonical form (without tags, lowercase, etc.) |
| 34 | +func normalize(name string) string { |
| 35 | + tagLess := tagRegex.ReplaceAll([]byte(name), []byte("")) |
| 36 | + squeezed := whiteSpaceRegex.ReplaceAll(tagLess, []byte(" ")) |
| 37 | + return strings.ToLower(strings.TrimSpace(string(squeezed))) |
| 38 | +} |
| 39 | + |
| 40 | +// OwnerList uses a map to get owners for a given test name. |
| 41 | +type OwnerList struct { |
| 42 | + mapping map[string]string |
| 43 | +} |
| 44 | + |
| 45 | +// TestOwner returns the owner for a test, or the empty string if none is found. |
| 46 | +func (o *OwnerList) TestOwner(testName string) string { |
| 47 | + name := normalize(testName) |
| 48 | + owner, _ := o.mapping[name] |
| 49 | + return owner |
| 50 | +} |
| 51 | + |
| 52 | +// NewOwnerList constructs an OwnerList given a mapping from test names to test owners. |
| 53 | +func NewOwnerList(mapping map[string]string) *OwnerList { |
| 54 | + list := OwnerList{} |
| 55 | + list.mapping = make(map[string]string) |
| 56 | + for input, output := range mapping { |
| 57 | + list.mapping[normalize(input)] = output |
| 58 | + } |
| 59 | + return &list |
| 60 | +} |
| 61 | + |
| 62 | +// NewOwnerListFromCsv constructs an OwnerList given a CSV file that includes |
| 63 | +// 'owner' and 'test name' columns. |
| 64 | +func NewOwnerListFromCsv(r io.Reader) (*OwnerList, error) { |
| 65 | + reader := csv.NewReader(r) |
| 66 | + records, err := reader.ReadAll() |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + mapping := make(map[string]string) |
| 71 | + ownerCol := -1 |
| 72 | + nameCol := -1 |
| 73 | + for _, record := range records { |
| 74 | + if ownerCol == -1 || nameCol == -1 { |
| 75 | + for col, val := range record { |
| 76 | + switch strings.ToLower(val) { |
| 77 | + case "owner": |
| 78 | + ownerCol = col |
| 79 | + case "test name": |
| 80 | + nameCol = col |
| 81 | + } |
| 82 | + } |
| 83 | + } else { |
| 84 | + mapping[record[nameCol]] = record[ownerCol] |
| 85 | + } |
| 86 | + } |
| 87 | + return NewOwnerList(mapping), nil |
| 88 | +} |
| 89 | + |
| 90 | +// ReloadingOwnerList maps test names to owners, reloading the mapping when the |
| 91 | +// underlying file is changed. |
| 92 | +type ReloadingOwnerList struct { |
| 93 | + path string |
| 94 | + mtime time.Time |
| 95 | + ownerList *OwnerList |
| 96 | +} |
| 97 | + |
| 98 | +// NewReloadingOwnerList creates a ReloadingOwnerList given a path to a CSV |
| 99 | +// file containing owner mapping information. |
| 100 | +func NewReloadingOwnerList(path string) (*ReloadingOwnerList, error) { |
| 101 | + ownerList := &ReloadingOwnerList{path: path} |
| 102 | + err := ownerList.reload() |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + return ownerList, nil |
| 107 | +} |
| 108 | + |
| 109 | +// TestOwner returns the owner for a test, or the empty string if none is found. |
| 110 | +func (o *ReloadingOwnerList) TestOwner(testName string) string { |
| 111 | + err := o.reload() |
| 112 | + if err != nil { |
| 113 | + glog.Errorf("Unable to reload test owners at %s: %v", o.path, err) |
| 114 | + // Process using the previous data. |
| 115 | + } |
| 116 | + return o.ownerList.TestOwner(testName) |
| 117 | +} |
| 118 | + |
| 119 | +func (o *ReloadingOwnerList) reload() error { |
| 120 | + info, err := os.Stat(o.path) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + if info.ModTime() == o.mtime { |
| 125 | + return nil |
| 126 | + } |
| 127 | + file, err := os.Open(o.path) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + defer file.Close() |
| 132 | + ownerList, err := NewOwnerListFromCsv(file) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + o.ownerList = ownerList |
| 137 | + o.mtime = info.ModTime() |
| 138 | + return nil |
| 139 | +} |
0 commit comments