proxy_test.go 3.0 KB
Newer Older
M
Medya Gh 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// +build integration

/*
Copyright 2019 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package integration

import (
	"context"
	"fmt"
	"os"
	"strings"
	"testing"
	"time"

M
Medya Gh 已提交
29 30
	"net/http"

M
Medya Gh 已提交
31 32 33 34 35 36
	"github.com/elazarl/goproxy"
	"github.com/phayes/freeport"
	"github.com/pkg/errors"
)

// setUpProxy runs a local http proxy and sets the env vars for it.
M
Medya Gh 已提交
37
func setUpProxy(t *testing.T) (*http.Server, error) {
M
Medya Gh 已提交
38 39
	port, err := freeport.GetFreePort()
	if err != nil {
M
Medya Gh 已提交
40
		return nil, errors.Wrap(err, "Failed to get an open port")
M
Medya Gh 已提交
41 42
	}

M
Medya Gh 已提交
43
	addr := fmt.Sprintf("localhost:%d", port)
M
Medya Gh 已提交
44 45
	err = os.Setenv("NO_PROXY", "")
	if err != nil {
M
Medya Gh 已提交
46
		return nil, errors.Wrap(err, "Failed to set no proxy env")
M
Medya Gh 已提交
47 48 49
	}
	err = os.Setenv("HTTP_PROXY", addr)
	if err != nil {
M
Medya Gh 已提交
50
		return nil, errors.Wrap(err, "Failed to set http proxy env")
M
Medya Gh 已提交
51 52 53
	}

	proxy := goproxy.NewProxyHttpServer()
M
Medya Gh 已提交
54 55 56 57 58 59 60
	srv := &http.Server{Addr: addr, Handler: proxy}
	go func(s *http.Server, t *testing.T) {
		if err := s.ListenAndServe(); err != http.ErrServerClosed {
			t.Errorf("Failed to start http server for proxy mock")
		}
	}(srv, t)
	return srv, nil
M
Medya Gh 已提交
61 62 63
}

func TestProxy(t *testing.T) {
M
Medya Gh 已提交
64 65 66 67 68 69
	srv, err := setUpProxy(t)
	defer func(t *testing.T) { // shutting down the http proxy after tests
		err := srv.Shutdown(context.TODO())
		if err != nil {
			t.Errorf("Error shutting down the http proxy")
		}
M
Medya Gh 已提交
70 71
	}(t)

M
Medya Gh 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85
	if err != nil {
		t.Fatalf("Failed to set up the test proxy: %s", err)
	}
	t.Run("ConsoleWarnning", testProxyWarning)
	t.Run("DashboardProxy", testDashboard)

}

// testProxyWarning checks user is warned correctly about the proxy related env vars
func testProxyWarning(t *testing.T) {
	mk := NewMinikubeRunner(t)
	// Start a timer for all remaining commands, to display failure output before a panic.
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
	defer cancel()
M
Medya Gh 已提交
86
	startCmd := fmt.Sprintf("start %s %s %s", mk.StartArgs, mk.Args, "--alsologtostderr --v=5")
M
Medya Gh 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	stdout, stderr, err := mk.RunWithContext(ctx, startCmd)
	if err != nil {
		t.Fatalf("start: %v\nstdout: %s\nstderr: %s", err, stdout, stderr)
	}
	mk.EnsureRunning()

	// Pre-cleanup: this usually fails, because no instance is running.
	// mk.RunWithContext(ctx, "delete")
	msg := "Found network options:"
	if !strings.Contains(stdout, msg) {
		t.Errorf("Proxy wranning (%s) is missing from the output: %s", msg, stderr)
	}

	msg = "You appear to be using a proxy"
	if !strings.Contains(stderr, msg) {
		t.Errorf("Proxy wranning (%s) is missing from the output: %s", msg, stderr)
	}

}