Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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 flow |
| 18 | |
| 19 | import ( |
| 20 | "bufio" |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 21 | "context" |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 22 | "fmt" |
| 23 | "os" |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 24 | "path/filepath" |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 25 | "regexp" |
| 26 | "strconv" |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 27 | "testing" |
| 28 | ) |
| 29 | |
| 30 | // TestLoadersIdentical ensures that the group, flow, and meter loaders always have an identical implementation. |
| 31 | func TestLoadersIdentical(t *testing.T) { |
khenaidoo | 1a0d622 | 2021-06-30 16:48:44 -0400 | [diff] [blame] | 32 | types := []string{"flow", "group"} |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 33 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 34 | identical := [][]string{ |
khenaidoo | 1a0d622 | 2021-06-30 16:48:44 -0400 | [diff] [blame] | 35 | {`ofp\.OfpFlowStats`, `ofp\.OfpGroupEntry`}, |
| 36 | {`\.Id`, `\.Desc\.GroupId`}, |
| 37 | {`uint64`, `uint32`}, |
| 38 | {`Flow`, `Group`}, |
| 39 | {`flow`, `group`}, |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 42 | regexes := make([][]*regexp.Regexp, len(identical[0])) |
| 43 | for i := range regexes { |
| 44 | regexes[i] = make([]*regexp.Regexp, len(identical)) |
| 45 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 46 | for i, group := range identical { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 47 | for j, regexStr := range group { |
| 48 | // convert from column-wise to row-wise for convenience |
| 49 | regexes[j][i] = regexp.MustCompile(regexStr) |
| 50 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 53 | for i := 1; i < len(types); i++ { |
| 54 | if err := compare(regexes[0], regexes[i], |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 55 | "../"+types[0]+"/cache.go", |
| 56 | "../"+types[i]+"/cache.go"); err != nil { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 57 | t.Error(err) |
| 58 | return |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 63 | func compare(regexesA, regexesB []*regexp.Regexp, fileNameA, fileNameB string) error { |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 64 | fileA, err := os.Open(filepath.Clean(fileNameA)) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 65 | if err != nil { |
| 66 | return err |
| 67 | } |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 68 | defer func() { |
| 69 | err := fileA.Close() |
| 70 | if err != nil { |
| 71 | logger.Errorf(context.Background(), "failed to close file: %v", err) |
| 72 | } |
| 73 | }() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 74 | |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 75 | fileB, err := os.Open(filepath.Clean(fileNameB)) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 76 | if err != nil { |
| 77 | return err |
| 78 | } |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 79 | defer func() { |
| 80 | err := fileB.Close() |
| 81 | if err != nil { |
| 82 | logger.Errorf(context.Background(), "failed to close file: %v", err) |
| 83 | } |
| 84 | }() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 85 | |
| 86 | scannerA, scannerB := bufio.NewScanner(fileA), bufio.NewScanner(fileB) |
| 87 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 88 | // treat any number of spaces as a single space |
| 89 | spaceRegex := regexp.MustCompile(` +`) |
| 90 | // extra lines are permitted before a "blank" line, or before a lock/unlock |
| 91 | spacerRegex := regexp.MustCompile(`^(?:[^a-z]*|.*Lock\(\)|.*Unlock\(\))$`) |
| 92 | // ignore import type differences |
| 93 | libGoImportRegex := regexp.MustCompile(`^.*github\.com/opencord/voltha-protos/.*$`) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 94 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 95 | lineA, lineB := 1, 1 |
| 96 | linesLoop: |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 97 | for { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 98 | if continueA, continueB := scannerA.Scan(), scannerB.Scan(); !continueA || !continueB { |
| 99 | // EOF |
| 100 | break linesLoop |
| 101 | } |
| 102 | textA, textB := scannerA.Text(), scannerB.Text() |
| 103 | |
| 104 | // allow any number of "extra" lines just before a spacer line |
| 105 | for { |
| 106 | isSpacerA, isSpacerB := spacerRegex.MatchString(textA), spacerRegex.MatchString(textB) |
| 107 | if isSpacerA && !isSpacerB { |
| 108 | if !scannerB.Scan() { |
| 109 | // EOF |
| 110 | break linesLoop |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 111 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 112 | lineB++ |
| 113 | textB = scannerB.Text() |
| 114 | continue |
| 115 | } else if isSpacerB && !isSpacerA { |
| 116 | if !scannerA.Scan() { |
| 117 | // EOF |
| 118 | break linesLoop |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 119 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 120 | lineA++ |
| 121 | textA = scannerA.Text() |
| 122 | continue |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 123 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 124 | break |
| 125 | } |
| 126 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 127 | replacedA, replacedB := textA, textB |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 128 | for i := range regexesA { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 129 | replacement := "{{type" + strconv.Itoa(i) + "}}" |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 130 | replacedA, replacedB = regexesA[i].ReplaceAllString(replacedA, replacement), regexesB[i].ReplaceAllString(replacedB, replacement) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // replace multiple spaces with single space |
| 134 | replacedA, replacedB = spaceRegex.ReplaceAllString(replacedA, " "), spaceRegex.ReplaceAllString(replacedB, " ") |
| 135 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 136 | // ignore voltha-protos import of ofp vs voltha |
| 137 | replacedA, replacedB = libGoImportRegex.ReplaceAllString(replacedA, "{{lib-go-import}}"), libGoImportRegex.ReplaceAllString(replacedB, "{{lib-go-import}}") |
| 138 | |
| 139 | if replacedA != replacedB && textA != textB { |
| 140 | return fmt.Errorf("files which must be identical do not match: \n %s:%d\n %s\n %s:%d\n %s\n\n\t%s\n\t%s", fileNameA, lineA, textA, fileNameB, lineB, textB, replacedA, replacedB) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 143 | lineA++ |
| 144 | lineB++ |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | if err := scannerA.Err(); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | if err := scannerB.Err(); err != nil { |
| 151 | return err |
| 152 | } |
| 153 | return nil |
| 154 | } |