Skip to content

Commit

Permalink
Allocate new ports for Envoy after restart. (istio#4465)
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue.

Allocate new ports for Envoy after restart.

Fixes istio#4444
  • Loading branch information
JimmyCYJ authored and istio-merge-robot committed Mar 22, 2018
1 parent 4a9a42a commit 6ff2777
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions mixer/test/client/check_report/check_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func TestCheckReportAttributes(t *testing.T) {
s.ReStartEnvoy()

// Issues a POST request.
url = fmt.Sprintf("http://localhost:%d/echo", s.Ports().ClientProxyPort)
if _, _, err := env.HTTPPost(url, "text/plain", "Hello World!"); err != nil {
t.Errorf("Failed in request %s: %v", tag, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestCheckReportDisable(t *testing.T) {
s.ReStartEnvoy()

tag = "Check Only"
url = fmt.Sprintf("http://localhost:%d/echo", s.Ports().ClientProxyPort)
if _, _, err := env.HTTPGet(url); err != nil {
t.Errorf("Failed in request %s: %v", tag, err)
}
Expand All @@ -66,6 +67,7 @@ func TestCheckReportDisable(t *testing.T) {

// wait for 2 second to wait for envoy to come up
tag = "Report Only"
url = fmt.Sprintf("http://localhost:%d/echo", s.Ports().ClientProxyPort)
if _, _, err := env.HTTPGet(url); err != nil {
t.Errorf("Failed in request %s: %v", tag, err)
}
Expand Down
41 changes: 34 additions & 7 deletions mixer/test/client/env/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,24 @@ const (
portBase uint16 = 20000
// Maximum number of ports used in each test.
portNum uint16 = 6
// Number of ports used by Envoy in each test.
envoyPortNum uint16 = 4
)

// Ports stores all used ports
type Ports struct {
ClientProxyPort uint16
ServerProxyPort uint16
TCPProxyPort uint16
AdminPort uint16
MixerPort uint16
BackendPort uint16
AdminPort uint16
}

func allocPortBase(name uint16) uint16 {
base := portBase + name*portNum
for i := 0; i < 10; i++ {
if allPortFree(base) {
if allPortFree(base, portNum) {
return base
}
base += maxTestNum * portNum
Expand All @@ -73,8 +75,20 @@ func allocPortBase(name uint16) uint16 {
return base
}

func allPortFree(base uint16) bool {
for port := base; port < base+portNum; port++ {
func allocEnvoyPortBase(name uint16) uint16 {
base := portBase + name*portNum
for i := 0; i < 10; i++ {
if allPortFree(base, envoyPortNum) {
return base
}
base += maxTestNum * portNum
}
log.Println("could not find free ports, continue the test...")
return base
}

func allPortFree(base uint16, ports uint16) bool {
for port := base; port < base+ports; port++ {
if IsPortUsed(port) {
log.Println("port is used ", port)
return false
Expand All @@ -90,8 +104,21 @@ func NewPorts(name uint16) *Ports {
ClientProxyPort: base,
ServerProxyPort: base + 1,
TCPProxyPort: base + 2,
MixerPort: base + 3,
BackendPort: base + 4,
AdminPort: base + 5,
AdminPort: base + 3,
MixerPort: base + 4,
BackendPort: base + 5,
}
}

// NewEnvoyPorts allocate ports for Envoy
func NewEnvoyPorts(ports *Ports, name uint16) *Ports {
base := allocEnvoyPortBase(name)
return &Ports{
ClientProxyPort: base,
ServerProxyPort: base + 1,
TCPProxyPort: base + 2,
AdminPort: base + 3,
MixerPort: ports.MixerPort,
BackendPort: ports.BackendPort,
}
}
18 changes: 11 additions & 7 deletions mixer/test/client/env/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ import (

// TestSetup store data for a test.
type TestSetup struct {
t *testing.T
stress bool
faultInject bool
noMixer bool
mfConf *MixerFilterConf
ports *Ports
t *testing.T
epoch int
mfConf *MixerFilterConf
ports *Ports

envoy *Envoy
mixer *MixerServer
backend *HTTPServer
epoch int
testName uint16
stress bool
faultInject bool
noMixer bool
mfConfVersion string
}

Expand All @@ -55,6 +56,7 @@ func NewTestSetup(name uint16, t *testing.T) *TestSetup {
t: t,
mfConf: GetDefaultMixerFilterConf(),
ports: NewPorts(name),
testName: name,
mfConfVersion: MixerFilterConfigV2,
}
}
Expand Down Expand Up @@ -159,6 +161,8 @@ func (s *TestSetup) TearDown() {
// ReStartEnvoy restarts Envoy
func (s *TestSetup) ReStartEnvoy() {
_ = s.envoy.Stop()
s.ports = NewEnvoyPorts(s.ports, s.testName)
log.Printf("new allocated ports are %v:", s.ports)
var err error
s.epoch++
s.envoy, err = NewEnvoy(s.stress, s.faultInject, s.mfConf, s.ports, s.epoch, s.mfConfVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestMixerInternalFail(t *testing.T) {

tag = "Fail-Close"
// Use fail close policy.
url = fmt.Sprintf("http://localhost:%d/echo", s.Ports().ServerProxyPort)
code, _, err = env.HTTPGet(url)
if err != nil {
t.Errorf("Failed in request %s: %v", tag, err)
Expand Down
1 change: 1 addition & 0 deletions mixer/test/client/network_policy/network_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestNetworkFailure(t *testing.T) {
s.ReStartEnvoy()

tag = "Fail-Close"
url = fmt.Sprintf("http://localhost:%d/echo", s.Ports().ServerProxyPort)
// Use fail close policy.
code, _, err = env.HTTPGet(url)
if err != nil {
Expand Down

0 comments on commit 6ff2777

Please sign in to comment.