Skip to content

Commit

Permalink
Move test to main
Browse files Browse the repository at this point in the history
  • Loading branch information
sjberman committed May 16, 2023
1 parent 36e50fc commit 1a7338b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
17 changes: 15 additions & 2 deletions cmd/gateway/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"errors"
"fmt"
"net"
"os"

flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -38,6 +40,17 @@ var (
podIP = os.Getenv("POD_IP")
)

func validateIP(podIP string) error {
if podIP == "" {
return errors.New("IP address must be set")
}
if net.ParseIP(podIP) == nil {
return fmt.Errorf("%q must be a valid IP address", podIP)
}

return nil
}

func main() {
flag.Parse()

Expand All @@ -47,8 +60,8 @@ func main() {
GatewayClassParam(),
)

if err := ValidatePodIP(podIP); err != nil {
fmt.Println(err.Error())
if err := validateIP(podIP); err != nil {
fmt.Printf("error validating POD_IP environment variable: %v\n", err)
os.Exit(1)
}

Expand Down
27 changes: 27 additions & 0 deletions cmd/gateway/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Main", func() {
type testCase struct {
expSubMsg string
podIP string
expErr bool
}
DescribeTable("should validate an IP address",
func(tc testCase) {
err := validateIP(tc.podIP)
if !tc.expErr {
Expect(err).ToNot(HaveOccurred())
} else {
Expect(err.Error()).To(ContainSubstring(tc.expSubMsg))
}
},
Entry("var not set", testCase{podIP: "", expErr: true, expSubMsg: "must be set"}),
Entry("var set to invalid value", testCase{podIP: "invalid", expErr: true, expSubMsg: "must be a valid"}),
Entry("var set to valid value", testCase{podIP: "1.2.3.4", expErr: false}),
) // should validate an IP address
})
12 changes: 0 additions & 12 deletions cmd/gateway/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"errors"
"fmt"
"net"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -121,14 +120,3 @@ func MustValidateArguments(flagset *flag.FlagSet, validators ...ValidatorContext
os.Exit(1)
}
}

func ValidatePodIP(podIP string) error {
if podIP == "" {
return errors.New("POD_IP environment variable must be set")
}
if net.ParseIP(podIP) == nil {
return fmt.Errorf("POD_IP %q must be a valid IP address", podIP)
}

return nil
}
23 changes: 1 addition & 22 deletions cmd/gateway/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func MockValidator(name string, called *int, succeed bool) ValidatorContext {
}
}

var _ = Describe("Main", func() {
var _ = Describe("Main Setup", func() {
Describe("Generic Validator", func() {
var mockFlags *flag.FlagSet
BeforeEach(func() {
Expand Down Expand Up @@ -263,25 +263,4 @@ var _ = Describe("Main", func() {
}) // should fail with invalid name
}) // gatewayclass validation
}) // CLI argument validation

Describe("environment variable validaton", func() {
type testCase struct {
expSubMsg string
podIP string
expErr bool
}
DescribeTable("should validate the POD_IP env var",
func(tc testCase) {
err := ValidatePodIP(tc.podIP)
if !tc.expErr {
Expect(err).ToNot(HaveOccurred())
} else {
Expect(err.Error()).To(ContainSubstring(tc.expSubMsg))
}
},
Entry("var not set", testCase{podIP: "", expErr: true, expSubMsg: "must be set"}),
Entry("var set to invalid value", testCase{podIP: "invalid", expErr: true, expSubMsg: "must be a valid"}),
Entry("var set to valid value", testCase{podIP: "1.2.3.4", expErr: false}),
)
}) // environment variable validation
}) // end Main
4 changes: 3 additions & 1 deletion internal/status/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var _ = Describe("Updater", func() {
Client: client,
Logger: zap.New(),
Clock: fakeClock,
PodIP: "1.2.3.4",
})
})

Expand Down Expand Up @@ -123,7 +124,8 @@ var _ = Describe("Updater", func() {
createExpectedGwWithGeneration = func(generation int64) *v1beta1.Gateway {
ipAddrType := v1beta1.IPAddressType
addr := v1beta1.GatewayAddress{
Type: &ipAddrType,
Type: &ipAddrType,
Value: "1.2.3.4",
}

return &v1beta1.Gateway{
Expand Down

0 comments on commit 1a7338b

Please sign in to comment.