Skip to content

Commit 391b47c

Browse files
authored
Merge pull request #272 from thockin/reboot-pr-246
ebtables: better version check regex
2 parents 99ec85e + df71e4c commit 391b47c

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

net/ebtables/ebtables.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,17 @@ func getEbtablesVersionString(exec utilexec.Interface) (string, error) {
121121
if err != nil {
122122
return "", err
123123
}
124-
versionMatcher := regexp.MustCompile(`v([0-9]+\.[0-9]+\.[0-9]+)`)
125-
match := versionMatcher.FindStringSubmatch(string(bytes))
124+
return parseVersion(string(bytes))
125+
}
126+
127+
func parseVersion(version string) (string, error) {
128+
// the regular expression contains `v?` at the beginning because
129+
// different OS distros have different version format output i.e
130+
// either starts with `v` or it doesn't
131+
versionMatcher := regexp.MustCompile(`v?([0-9]+\.[0-9]+\.[0-9]+)`)
132+
match := versionMatcher.FindStringSubmatch(version)
126133
if match == nil {
127-
return "", fmt.Errorf("no ebtables version found in string: %s", bytes)
134+
return "", fmt.Errorf("no ebtables version found in string: %s", version)
128135
}
129136
return match[1], nil
130137
}

net/ebtables/ebtables_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,61 @@ Bridge chain: TEST, entries: 0, policy: ACCEPT`), nil, nil
167167
t.Errorf("expected err = nil")
168168
}
169169
}
170+
171+
func Test_parseVersion(t *testing.T) {
172+
tests := []struct {
173+
name string
174+
version string
175+
want string
176+
wantErr bool
177+
}{
178+
{
179+
name: "version starting with `v`",
180+
version: "v2.0.10",
181+
want: "2.0.10",
182+
wantErr: false,
183+
},
184+
{
185+
name: "version without containing `v`",
186+
version: "2.0.10",
187+
want: "2.0.10",
188+
wantErr: false,
189+
},
190+
{
191+
name: "version containing `v` in between the regex expression match",
192+
version: "2.0v.10",
193+
want: "",
194+
wantErr: true,
195+
},
196+
{
197+
name: "version containing `v` after the regex expression match",
198+
version: "2.0.10v",
199+
want: "2.0.10",
200+
wantErr: false,
201+
},
202+
{
203+
name: "version starting with `v` and containing a symbol in between",
204+
version: "v2.0.10-4",
205+
want: "2.0.10",
206+
wantErr: false,
207+
},
208+
{
209+
name: "version starting with `v` and containing a symbol/alphabets in between",
210+
version: "v2.0a.10-4",
211+
want: "",
212+
wantErr: true,
213+
},
214+
}
215+
for _, tt := range tests {
216+
t.Run(tt.name, func(t *testing.T) {
217+
got, err := parseVersion(tt.version)
218+
if (err != nil) != tt.wantErr {
219+
t.Errorf("parseVersion() error = %v, wantErr %v", err, tt.wantErr)
220+
return
221+
}
222+
if got != tt.want {
223+
t.Errorf("parseVersion() got = %v, want %v", got, tt.want)
224+
}
225+
})
226+
}
227+
}

0 commit comments

Comments
 (0)