Skip to content

Commit

Permalink
Modifications to strongSwan unit tests (submariner-io#162)
Browse files Browse the repository at this point in the history
Modified the tests for the port env variable config processing to invoke
NewStrongSwan instead of calling envconfig.Process directly. This makes the
tests more blackbox and yields more coverage.

Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
  • Loading branch information
tpantelis authored Oct 28, 2019
1 parent 3958b07 commit db4e232
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions pkg/cableengine/ipsec/strongswan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (
"os"
"testing"

"github.com/kelseyhightower/envconfig"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/submariner-io/submariner/pkg/types"
)

var _ = Describe("Strongswan", func() {
Describe("Charon port configuration", testCharonConfigPortsGen)
Describe("Charon port configuration", testCharonPortConfiguration)
})

func testCharonConfigPortsGen() {
Context("config rendering", func() {
It("should render config correctly out of strongSwan parameters", func() {
func testCharonPortConfiguration() {
When("renderCharonConfigTemplate is called", func() {
It("should render the config file properties correctly from the strongSwan fields", func() {
ss := strongSwan{ipSecIKEPort: "500", ipSecNATTPort: "4500"}
buf := new(bytes.Buffer)

Expand All @@ -33,35 +32,46 @@ func testCharonConfigPortsGen() {
})
})

Context("environment variable processing", func() {
It("should get the defaults from the specification definition", func() {
checkEnvVarParsingPorts("500", "4500")
When("NewStrongSwan is called with no port environment variables set", func() {
It("should set the port fields from the defaults in the specification definition", func() {
checkStrongSwanPorts(defaultIKEPort, defaultNATTPort)
})
})

When("NewStrongSwan is called with port environment variables set", func() {
const (
ikePort = "555"
nattPort = "4555"
ikePortEnvVar = "CE_IPSEC_IKEPORT"
nattPortEnvVar = "CE_IPSEC_NATTPORT"
)

It("should get the right environment variable names", func() {
const (
ikePort = "555"
nattPort = "4555"
ikePortEnvVar = "CE_IPSEC_IKEPORT"
nattPortEnvVar = "CE_IPSEC_NATTPORT"
)
BeforeEach(func() {
os.Setenv(ikePortEnvVar, ikePort)
os.Setenv(nattPortEnvVar, nattPort)
})

checkEnvVarParsingPorts(ikePort, nattPort)

AfterEach(func() {
os.Unsetenv(ikePortEnvVar)
os.Unsetenv(nattPortEnvVar)
})

It("should set the port fields from the environment variables", func() {
checkStrongSwanPorts(ikePort, nattPort)
})
})
}

func checkEnvVarParsingPorts(ikePort string, nattPort string) {
ipSecSpec := specification{}
err := envconfig.Process(ipsecSpecEnvVarPrefix, &ipSecSpec)
func createStrongSwan() *strongSwan {
ss, err := NewStrongSwan([]string{}, types.SubmarinerEndpoint{})
Expect(err).NotTo(HaveOccurred())
Expect(ipSecSpec.IKEPort).To(Equal(ikePort))
Expect(ipSecSpec.NATTPort).To(Equal(nattPort))
return ss.(*strongSwan)
}

func checkStrongSwanPorts(ikePort string, nattPort string) {
ss := createStrongSwan()
Expect(ss.ipSecIKEPort).To(Equal(ikePort))
Expect(ss.ipSecNATTPort).To(Equal(nattPort))
}

func TestStrongswan(t *testing.T) {
Expand Down

0 comments on commit db4e232

Please sign in to comment.