Skip to content

Commit

Permalink
vultr: fix port parsing regression; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
squat committed Jun 1, 2018
1 parent 441156b commit 237b7f7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vultr/resource_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ func resourceFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error
}

func splitFirewallRule(portRange string) (int, int, error) {
if len(portRange) == 0 {
return 0, 0, nil
}
ports := strings.Split(portRange, "-")
from, err := strconv.Atoi(strings.TrimSpace(ports[0]))
if err != nil {
Expand Down
77 changes: 77 additions & 0 deletions vultr/resource_firewall_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package vultr

import (
"testing"
)

func TestSplitFirewallRule(t *testing.T) {
cases := []struct {
portRange string
from int
to int
err bool
}{
{
portRange: "",
from: 0,
to: 0,
err: false,
},
{
portRange: ":",
from: 0,
to: 0,
err: true,
},
{
portRange: "-",
from: 0,
to: 0,
err: true,
},
{
portRange: "22",
from: 22,
to: 22,
err: false,
},
{
portRange: "foo",
from: 0,
to: 0,
err: true,
},
{
portRange: "22:23",
from: 0,
to: 0,
err: true,
},
{
portRange: "22 - 23",
from: 22,
to: 23,
err: false,
},
{
portRange: "80-81",
from: 80,
to: 81,
err: false,
},
}

for i, c := range cases {
from, to, err := splitFirewallRule(c.portRange)
if (err != nil) != c.err {
no := "no"
if c.err {
no = "an"
}
t.Errorf("test case %d: expected %s error, got %v", i, no, err)
}
if from != c.from || to != c.to {
t.Errorf("test case %d: expected range %d:%d, got %d:%d", i, c.from, c.to, from, to)
}
}
}

0 comments on commit 237b7f7

Please sign in to comment.