Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Nov 18, 2018
1 parent 654eced commit af2dce9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
40 changes: 24 additions & 16 deletions internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ import (
)

const (
ngxHealthPath = "/healthz"
ngxHealthPath = "/healthz"
nginxStreamSocket = "/tmp/ingress-stream.sock"
)

var (
Expand Down Expand Up @@ -794,13 +795,6 @@ func configureDynamically(pcfg *ingress.Configuration, port int, isDynamicCertif
return err
}

streamSocket := "/tmp/ingress-stream.sock"
conn, err := net.Dial("unix", streamSocket)
if err != nil {
return err
}
defer conn.Close()

streams := make([]ingress.Backend, 0)
for _, ep := range pcfg.TCPEndpoints {
key := fmt.Sprintf("tcp-%v-%v-%v", ep.Backend.Namespace, ep.Backend.Name, ep.Backend.Port.String())
Expand All @@ -819,6 +813,28 @@ func configureDynamically(pcfg *ingress.Configuration, port int, isDynamicCertif
})
}

err = updateStreamConfiguration(streams)
if err != nil {
return err
}

if isDynamicCertificatesEnabled {
err = configureCertificates(pcfg, port)
if err != nil {
return err
}
}

return nil
}

func updateStreamConfiguration(streams []ingress.Backend) error {
conn, err := net.Dial("unix", nginxStreamSocket)
if err != nil {
return err
}
defer conn.Close()

buf, err := json.Marshal(streams)
if err != nil {
return err
Expand All @@ -832,14 +848,6 @@ func configureDynamically(pcfg *ingress.Configuration, port int, isDynamicCertif
if err != nil {
return err
}
defer conn.Close()

if isDynamicCertificatesEnabled {
err = configureCertificates(pcfg, port)
if err != nil {
return err
}
}

return nil
}
Expand Down
27 changes: 27 additions & 0 deletions internal/ingress/controller/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"

jsoniter "github.com/json-iterator/go"
apiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -146,7 +147,33 @@ func TestIsDynamicConfigurationEnough(t *testing.T) {
}
}

func mockUnixSocket(t *testing.T) net.Listener {
l, err := net.Listen("unix", nginxStreamSocket)
if err != nil {
t.Fatalf("unexpected error creating unix socket: %v", err)
}
if l == nil {
t.Fatalf("expected a listener but none returned")
}

go func() {
for {
conn, err := l.Accept()
if err != nil {
continue
}

time.Sleep(100 * time.Millisecond)
defer conn.Close()
}
}()

return l
}
func TestConfigureDynamically(t *testing.T) {
l := mockUnixSocket(t)
defer l.Close()

target := &apiv1.ObjectReference{}

backends := []*ingress.Backend{{
Expand Down
3 changes: 2 additions & 1 deletion internal/ingress/types_equals.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (c1 *Configuration) Equal(c2 *Configuration) bool {
return false
}
}

if len(c1.UDPEndpoints) != len(c2.UDPEndpoints) {
return false
}
Expand All @@ -83,10 +84,10 @@ func (c1 *Configuration) Equal(c2 *Configuration) bool {
return false
}
}

if len(c1.PassthroughBackends) != len(c2.PassthroughBackends) {
return false
}

for _, ptb1 := range c1.PassthroughBackends {
found := false
for _, ptb2 := range c2.PassthroughBackends {
Expand Down
9 changes: 1 addition & 8 deletions rootfs/etc/nginx/lua/tcp_udp_balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,7 @@ function _M.balance()
return
end

local ctx = ngx.ctx
if not ctx.has_run then
ctx.has_run = true
local _, err = ngx_balancer.set_more_tries(1)
if err then
ngx.log(ngx.ERR, "failed to set more tries: ", err)
end
end
ngx_balancer.set_more_tries(1)

local ok, err = ngx_balancer.set_current_peer(peer)
if not ok then
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tcpudp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var _ = framework.IngressNginxDescribe("TCP Feature", func() {
resp, _, errs := gorequest.New().
Get(fmt.Sprintf("http://%v:%v", ip, port)).
End()
Expect(len(errs)).Should(BeNumerically("==", 0))
Expect(len(errs)).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(200))
})
})

0 comments on commit af2dce9

Please sign in to comment.