Skip to content

Commit

Permalink
Merge pull request #1439 from dougm/vcsim-listen
Browse files Browse the repository at this point in the history
vcsim: remove httptest.serve flag
  • Loading branch information
dougm authored Apr 22, 2019
2 parents 8a823c5 + 5b5eaa7 commit 088b8fb
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 51 deletions.
2 changes: 1 addition & 1 deletion govc/test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ vcsim_start() {
export GOVC_SIM_ENV
mkfifo "$GOVC_SIM_ENV"

vcsim -httptest.serve=127.0.0.1:0 -E "$GOVC_SIM_ENV" "$@" &
vcsim -l 127.0.0.1:0 -E "$GOVC_SIM_ENV" "$@" &

eval "$(cat "$GOVC_SIM_ENV")"
}
Expand Down
12 changes: 3 additions & 9 deletions govc/test/vcsim.bats
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ load test_helper
port=$(govc env -x GOVC_URL_PORT)
vcsim_stop

vcsim_start -httptest.serve="$url" # reuse free port selection from above
vcsim_start -l "$url" # reuse free port selection from above

run govc object.collect -s -type h host/DC0_H0 summary.config.port
assert_success "$port"
Expand All @@ -71,7 +71,7 @@ load test_helper

vcsim_stop

VCSIM_HOST_PORT_UNIQUE=true vcsim_start -httptest.serve="$url"
VCSIM_HOST_PORT_UNIQUE=true vcsim_start -l "$url"

hosts=$(curl -sk "https://$url/debug/vars" | jq .vcsim.Model.Host)
ports=$(govc object.collect -s -type h / summary.config.port | uniq -u | wc -l)
Expand Down Expand Up @@ -249,13 +249,7 @@ load test_helper
[[ "$url" == *"https://127.0.0.1:"* ]]
vcsim_stop

vcsim_start -dc 0 -httptest.serve 0.0.0.0:0
url=$(govc option.ls vcsim.server.url)
[[ "$url" != *"https://127.0.0.1:"* ]]
[[ "$url" != *"https://[::]:"* ]]
vcsim_stop

vcsim_start -dc 0 -l :0 -httptest.serve ""
vcsim_start -dc 0 -l 0.0.0.0:0
url=$(govc option.ls vcsim.server.url)
[[ "$url" != *"https://127.0.0.1:"* ]]
[[ "$url" != *"https://[::]:"* ]]
Expand Down
29 changes: 9 additions & 20 deletions simulator/internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ package internal
import (
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -51,11 +49,11 @@ type Server struct {
client *http.Client
}

func newLocalListener() net.Listener {
if *serve != "" {
l, err := net.Listen("tcp", *serve)
func newLocalListener(serve string) net.Listener {
if serve != "" {
l, err := net.Listen("tcp", serve)
if err != nil {
panic(fmt.Sprintf("httptest: failed to listen on %v: %v", *serve, err))
panic(fmt.Sprintf("httptest: failed to listen on %v: %v", serve, err))
}
return l
}
Expand All @@ -68,16 +66,10 @@ func newLocalListener() net.Listener {
return l
}

// When debugging a particular http server-based test,
// this flag lets you run
// go test -run=BrokenTest -httptest.serve=127.0.0.1:8000
// to start the broken server so you can interact with it manually.
var serve = flag.String("httptest.serve", "", "if non-empty, httptest.NewServer serves on this address and blocks")

// NewServer starts and returns a new Server.
// The caller should call Close when finished, to shut it down.
func NewServer(handler http.Handler) *Server {
ts := NewUnstartedServer(handler)
ts := NewUnstartedServer(handler, "")
ts.Start()
return ts
}
Expand All @@ -88,9 +80,10 @@ func NewServer(handler http.Handler) *Server {
// StartTLS.
//
// The caller should call Close when finished, to shut it down.
func NewUnstartedServer(handler http.Handler) *Server {
// serve allows the server's listen address to be specified.
func NewUnstartedServer(handler http.Handler, serve string) *Server {
return &Server{
Listener: newLocalListener(),
Listener: newLocalListener(serve),
Config: &http.Server{Handler: handler},
}
}
Expand All @@ -106,10 +99,6 @@ func (s *Server) Start() {
s.URL = "http://" + s.Listener.Addr().String()
s.wrap()
s.goServe()
if *serve != "" {
fmt.Fprintln(os.Stderr, "httptest: serving on", s.URL)
select {}
}
}

// StartTLS starts TLS on a server from NewUnstartedServer.
Expand Down Expand Up @@ -157,7 +146,7 @@ func (s *Server) StartTLS() {
// NewTLSServer starts and returns a new Server using TLS.
// The caller should call Close when finished, to shut it down.
func NewTLSServer(handler http.Handler) *Server {
ts := NewUnstartedServer(handler)
ts := NewUnstartedServer(handler, "")
ts.StartTLS()
return ts
}
Expand Down
12 changes: 2 additions & 10 deletions simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"encoding/base64"
"encoding/json"
"encoding/pem"
"flag"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -71,6 +70,7 @@ type Service struct {

readAll func(io.Reader) ([]byte, error)

Listen string
TLS *tls.Config
ServeMux *http.ServeMux
}
Expand Down Expand Up @@ -601,10 +601,7 @@ func (s *Service) NewServer() *Server {
mux.HandleFunc(nfcPrefix, ServeNFC)
mux.HandleFunc("/about", s.About)

// Using NewUnstartedServer() instead of NewServer(),
// for use in main.go, where Start() blocks, we can still set ServiceHostName
ts := internal.NewUnstartedServer(mux)

ts := internal.NewUnstartedServer(mux, s.Listen)
addr := ts.Listener.Addr().(*net.TCPAddr)
port := strconv.Itoa(addr.Port)
u := &url.URL{
Expand All @@ -619,11 +616,6 @@ func (s *Service) NewServer() *Server {
// Redirect clients to this http server, rather than HostSystem.Name
Map.SessionManager().ServiceHostName = u.Host

if f := flag.Lookup("httptest.serve"); f != nil {
// Avoid the blocking behaviour of httptest.Server.Start() when this flag is set
_ = f.Value.Set("")
}

// Add vcsim config to OptionManager for use by SDK handlers (see lookup/simulator for example)
m := Map.OptionManager()
m.Setting = append(m.Setting,
Expand Down
2 changes: 1 addition & 1 deletion vcsim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ curl -sk https://user:pass@127.0.0.1:8989/about

## Listen address

The default vcsim listen address is `127.0.0.1:8989`. Use the `-httptest.serve` flag to listen on another address:
The default vcsim listen address is `127.0.0.1:8989`. Use the `-l` flag to listen on another address:


``` shell
Expand Down
11 changes: 1 addition & 10 deletions vcsim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,6 @@ func main() {
}
}

f := flag.Lookup("httptest.serve")
serve := f.Value.String()
if serve == "" {
err = f.Value.Set(*listen) // propagate -l unless -httptest.serve is specified
if err != nil {
log.Fatal(err)
}
} else {
*listen = serve // propagate to updateHostTemplate call below
}
if err = updateHostTemplate(*listen); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -147,6 +137,7 @@ func main() {
log.Fatal(err)
}

model.Service.Listen = *listen
if *isTLS {
model.Service.TLS = new(tls.Config)
if *cert != "" {
Expand Down

0 comments on commit 088b8fb

Please sign in to comment.