Exemplo n.º 1
0
func setupWebsocketProxy() {
	config.Parse()
	config.Config.NumStats = 1
	config.Config.CAdvisorUrl = "http://localhost:8080"
	config.Config.ParsedPublicKey = wsp_utils.ParseTestPublicKey()
	privateKey = wsp_utils.ParseTestPrivateKey()

	conf := test_utils.GetTestConfig(":1111")
	p := &proxy.ProxyStarter{
		BackendPaths:  []string{"/v1/connectbackend"},
		FrontendPaths: []string{"/v1/{logs:logs}/", "/v1/{stats:stats}", "/v1/{stats:stats}/{statsid}", "/v1/exec/"},
		StatsPaths: []string{"/v1/{hoststats:hoststats(\\/project)?(\\/)?}",
			"/v1/{containerstats:containerstats(\\/service)?(\\/)?}",
			"/v1/{containerstats:containerstats}/{containerid}"},
		Config: conf,
	}

	log.Infof("Starting websocket proxy. Listening on [%s], Proxying to cattle API at [%s].",
		conf.ListenAddr, conf.CattleAddr)

	go p.StartProxy()
	time.Sleep(time.Second)
	signedToken := wsp_utils.CreateBackendToken("1", privateKey)

	handlers := make(map[string]backend.Handler)
	handlers["/v1/stats/"] = &StatsHandler{}
	handlers["/v1/hoststats/"] = &HostStatsHandler{}
	handlers["/v1/containerstats/"] = &ContainerStatsHandler{}
	go backend.ConnectToProxy("ws://localhost:1111/v1/connectbackend?token="+signedToken, handlers)
	time.Sleep(300 * time.Millisecond)
}
Exemplo n.º 2
0
func (s *ProxyTestSuite) setupWebsocketProxy() {
	// TODO Deduplicate. This method and the two below are close copies of the ones in logs_test.go.
	config.Parse()
	config.Config.HostUuid = "1"
	config.Config.ParsedPublicKey = wsp_utils.ParseTestPublicKey()
	s.privateKey = wsp_utils.ParseTestPrivateKey()

	conf := test_utils.GetTestConfig(":4444")
	p := &proxy.ProxyStarter{
		BackendPaths:  []string{"/v1/connectbackend"},
		FrontendPaths: []string{"/v1/{dockersocket:dockersocket}/"},
		Config:        conf,
	}

	log.Infof("Starting websocket proxy. Listening on [%s].", conf.ListenAddr)

	go p.StartProxy()
	time.Sleep(time.Second)
	signedToken := wsp_utils.CreateBackendToken("1", s.privateKey)

	handlers := make(map[string]backend.Handler)
	handlers["/v1/dockersocket/"] = &Handler{}
	go backend.ConnectToProxy("ws://localhost:4444/v1/connectbackend?token="+signedToken, handlers)
	s.pullImage("ibuildthecloud/helloworld", "latest")
}
Exemplo n.º 3
0
func TestMain(m *testing.M) {
	c := getTestConfig()
	privateKey = test_utils.ParseTestPrivateKey()

	ps := &ProxyStarter{
		BackendPaths:       []string{"/v1/connectbackend"},
		FrontendPaths:      []string{"/v1/echo", "/v1/oneanddone", "/v1/repeat", "/v1/sendafterclose"},
		CattleWSProxyPaths: []string{"/v1/subscribe"},
		CattleProxyPaths:   []string{"/{cattle-proxy:.*}"},
		Config:             c,
	}
	go ps.StartProxy()

	handlers := make(map[string]backend.Handler)
	handlers["/v1/echo"] = &echoHandler{}
	handlers["/v1/oneanddone"] = &oneAndDoneHandler{}
	handlers["/v1/repeat"] = &repeatingHandler{}
	handlers["/v1/sendafterclose"] = &sendAfterCloseHandler{}
	signedToken := test_utils.CreateBackendToken("1", privateKey)
	url := "ws://localhost:1111/v1/connectbackend?token=" + signedToken
	go backend.ConnectToProxy(url, handlers)

	router := mux.NewRouter()
	router.HandleFunc("/v1/subscribe", getWsHandler())
	router.HandleFunc("/{foo:.*}", func(rw http.ResponseWriter, req *http.Request) {
		rw.Write([]byte("SUCCESS"))
	})
	go http.ListenAndServe("127.0.0.1:3333", router)

	time.Sleep(50 * time.Millisecond) // Give front and back a chance to initialize

	os.Exit(m.Run())
}
Exemplo n.º 4
0
func TestBackendGoesAway(t *testing.T) {
	dialer := &websocket.Dialer{}
	headers := http.Header{}
	signedToken := test_utils.CreateBackendToken("1", privateKey)
	url := "ws://localhost:2222/v1/connectbackend?token=" + signedToken
	backendWs, _, err := dialer.Dial(url, headers)
	if err != nil {
		t.Fatal("Failed to connect to proxy.", err)
	}

	handlers := make(map[string]Handler)
	handlers["/v1/echo"] = &echoHandler{}
	go connectToProxyWS(backendWs, handlers)

	signedToken = test_utils.CreateToken("1", privateKey)
	url = "ws://localhost:2222/v1/echo?token=" + signedToken
	ws := getClientConnection(url, t)

	if err := ws.WriteMessage(1, []byte("a message")); err != nil {
		t.Fatal(err)
	}

	backendWs.Close()

	if _, _, err := ws.ReadMessage(); err != io.EOF {
		t.Fatal("Expected error indicating websocket was closed.")
	}

	dialer = &websocket.Dialer{}
	ws, _, err = dialer.Dial(url, http.Header{})
	if ws != nil || err != websocket.ErrBadHandshake {
		t.Fatal("Should not have been able to connect.")
	}

}
Exemplo n.º 5
0
func (s *LogsTestSuite) setupWebsocketProxy() {
	config.Parse()
	config.Config.HostUuid = "1"
	config.Config.ParsedPublicKey = wsp_utils.ParseTestPublicKey()
	privateKey = wsp_utils.ParseTestPrivateKey()

	conf := test_utils.GetTestConfig(":3333")
	p := &proxy.ProxyStarter{
		BackendPaths:  []string{"/v1/connectbackend"},
		FrontendPaths: []string{"/v1/{logs:logs}/"},
		Config:        conf,
	}

	log.Infof("Starting websocket proxy. Listening on [%s], Proxying to cattle API at [%s].",
		conf.ListenAddr, conf.CattleAddr)

	go p.StartProxy()
	time.Sleep(time.Second)
	signedToken := wsp_utils.CreateBackendToken("1", privateKey)

	handlers := make(map[string]backend.Handler)
	handlers["/v1/logs/"] = &LogsHandler{}
	go backend.ConnectToProxy("ws://localhost:3333/v1/connectbackend?token="+signedToken, handlers)
	s.pullImage("hello-world", "latest")
}