Skip to content

Commit 1a7338b

Browse files
committed
Move test to main
1 parent 36e50fc commit 1a7338b

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

cmd/gateway/main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
6+
"net"
57
"os"
68

79
flag "github.com/spf13/pflag"
@@ -38,6 +40,17 @@ var (
3840
podIP = os.Getenv("POD_IP")
3941
)
4042

43+
func validateIP(podIP string) error {
44+
if podIP == "" {
45+
return errors.New("IP address must be set")
46+
}
47+
if net.ParseIP(podIP) == nil {
48+
return fmt.Errorf("%q must be a valid IP address", podIP)
49+
}
50+
51+
return nil
52+
}
53+
4154
func main() {
4255
flag.Parse()
4356

@@ -47,8 +60,8 @@ func main() {
4760
GatewayClassParam(),
4861
)
4962

50-
if err := ValidatePodIP(podIP); err != nil {
51-
fmt.Println(err.Error())
63+
if err := validateIP(podIP); err != nil {
64+
fmt.Printf("error validating POD_IP environment variable: %v\n", err)
5265
os.Exit(1)
5366
}
5467

cmd/gateway/main_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("Main", func() {
9+
type testCase struct {
10+
expSubMsg string
11+
podIP string
12+
expErr bool
13+
}
14+
DescribeTable("should validate an IP address",
15+
func(tc testCase) {
16+
err := validateIP(tc.podIP)
17+
if !tc.expErr {
18+
Expect(err).ToNot(HaveOccurred())
19+
} else {
20+
Expect(err.Error()).To(ContainSubstring(tc.expSubMsg))
21+
}
22+
},
23+
Entry("var not set", testCase{podIP: "", expErr: true, expSubMsg: "must be set"}),
24+
Entry("var set to invalid value", testCase{podIP: "invalid", expErr: true, expSubMsg: "must be a valid"}),
25+
Entry("var set to valid value", testCase{podIP: "1.2.3.4", expErr: false}),
26+
) // should validate an IP address
27+
})

cmd/gateway/setup.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"errors"
55
"fmt"
6-
"net"
76
"os"
87
"regexp"
98
"strings"
@@ -121,14 +120,3 @@ func MustValidateArguments(flagset *flag.FlagSet, validators ...ValidatorContext
121120
os.Exit(1)
122121
}
123122
}
124-
125-
func ValidatePodIP(podIP string) error {
126-
if podIP == "" {
127-
return errors.New("POD_IP environment variable must be set")
128-
}
129-
if net.ParseIP(podIP) == nil {
130-
return fmt.Errorf("POD_IP %q must be a valid IP address", podIP)
131-
}
132-
133-
return nil
134-
}

cmd/gateway/setup_test.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func MockValidator(name string, called *int, succeed bool) ValidatorContext {
2424
}
2525
}
2626

27-
var _ = Describe("Main", func() {
27+
var _ = Describe("Main Setup", func() {
2828
Describe("Generic Validator", func() {
2929
var mockFlags *flag.FlagSet
3030
BeforeEach(func() {
@@ -263,25 +263,4 @@ var _ = Describe("Main", func() {
263263
}) // should fail with invalid name
264264
}) // gatewayclass validation
265265
}) // CLI argument validation
266-
267-
Describe("environment variable validaton", func() {
268-
type testCase struct {
269-
expSubMsg string
270-
podIP string
271-
expErr bool
272-
}
273-
DescribeTable("should validate the POD_IP env var",
274-
func(tc testCase) {
275-
err := ValidatePodIP(tc.podIP)
276-
if !tc.expErr {
277-
Expect(err).ToNot(HaveOccurred())
278-
} else {
279-
Expect(err.Error()).To(ContainSubstring(tc.expSubMsg))
280-
}
281-
},
282-
Entry("var not set", testCase{podIP: "", expErr: true, expSubMsg: "must be set"}),
283-
Entry("var set to invalid value", testCase{podIP: "invalid", expErr: true, expSubMsg: "must be a valid"}),
284-
Entry("var set to valid value", testCase{podIP: "1.2.3.4", expErr: false}),
285-
)
286-
}) // environment variable validation
287266
}) // end Main

internal/status/updater_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var _ = Describe("Updater", func() {
5555
Client: client,
5656
Logger: zap.New(),
5757
Clock: fakeClock,
58+
PodIP: "1.2.3.4",
5859
})
5960
})
6061

@@ -123,7 +124,8 @@ var _ = Describe("Updater", func() {
123124
createExpectedGwWithGeneration = func(generation int64) *v1beta1.Gateway {
124125
ipAddrType := v1beta1.IPAddressType
125126
addr := v1beta1.GatewayAddress{
126-
Type: &ipAddrType,
127+
Type: &ipAddrType,
128+
Value: "1.2.3.4",
127129
}
128130

129131
return &v1beta1.Gateway{

0 commit comments

Comments
 (0)