/*
Copyright 2014 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 unversioned

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"reflect"
	"strings"
	"testing"

	"k8s.io/kubernetes/pkg/api/testapi"
	"k8s.io/kubernetes/pkg/api/unversioned"
)

const (
	rootCACert = `-----BEGIN CERTIFICATE-----
MIIC4DCCAcqgAwIBAgIBATALBgkqhkiG9w0BAQswIzEhMB8GA1UEAwwYMTAuMTMu
MTI5LjEwNkAxNDIxMzU5MDU4MB4XDTE1MDExNTIxNTczN1oXDTE2MDExNTIxNTcz
OFowIzEhMB8GA1UEAwwYMTAuMTMuMTI5LjEwNkAxNDIxMzU5MDU4MIIBIjANBgkq
hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAunDRXGwsiYWGFDlWH6kjGun+PshDGeZX
xtx9lUnL8pIRWH3wX6f13PO9sktaOWW0T0mlo6k2bMlSLlSZgG9H6og0W6gLS3vq
s4VavZ6DbXIwemZG2vbRwsvR+t4G6Nbwelm6F8RFnA1Fwt428pavmNQ/wgYzo+T1
1eS+HiN4ACnSoDSx3QRWcgBkB1g6VReofVjx63i0J+w8Q/41L9GUuLqquFxu6ZnH
60vTB55lHgFiDLjA1FkEz2dGvGh/wtnFlRvjaPC54JH2K1mPYAUXTreoeJtLJKX0
ycoiyB24+zGCniUmgIsmQWRPaOPircexCp1BOeze82BT1LCZNTVaxQIDAQABoyMw
ITAOBgNVHQ8BAf8EBAMCAKQwDwYDVR0TAQH/BAUwAwEB/zALBgkqhkiG9w0BAQsD
ggEBADMxsUuAFlsYDpF4fRCzXXwrhbtj4oQwcHpbu+rnOPHCZupiafzZ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1G2Xg0jHtfM4tz7fyeI
cr90dc8gpli24dkSxzLeg3Tn3wIj/Bu64m2TpZPZEIlukYvgdgArmRIPQVxerYey
OkrfTNkxU1HXsYjLCdGcGXs5lmb+K/kuTcFxaMOs7jZi7La+jEONwf8CgYEAwCs/
rUOOA0klDsWWisbivOiNPII79c9McZCNBqncCBfMUoiGe8uWDEO4TFHN60vFuVk9
8PkwpCfvaBUX+ajvbafIfHxsnfk1M04WLGCeqQ/ym5Q4sQoQOcC1b1y9qc/xEWfg
nIUuia0ukYRpl7qQa3tNg+BNFyjypW8zukUAC/kCgYB1/Kojuxx5q5/oQVPrx73k
2bevD+B3c+DYh9MJqSCNwFtUpYIWpggPxoQan4LwdsmO0PKzocb/ilyNFj4i/vII
NToqSc/WjDFpaDIKyuu9oWfhECye45NqLWhb/6VOuu4QA/Nsj7luMhIBehnEAHW+
GkzTKM8oD1PxpEG3nPKXYQKBgQC6AuMPRt3XBl1NkCrpSBy/uObFlFaP2Enpf39S
3OZ0Gv0XQrnSaL1kP8TMcz68rMrGX8DaWYsgytstR4W+jyy7WvZwsUu+GjTJ5aMG
77uEcEBpIi9CBzivfn7hPccE8ZgqPf+n4i6q66yxBJflW5xhvafJqDtW2LcPNbW/
bvzdmQKBgExALRUXpq+5dbmkdXBHtvXdRDZ6rVmrnjy4nI5bPw+1GqQqk6uAR6B/
F6NmLCQOO4PDG/cuatNHIr2FrwTmGdEL6ObLUGWn9Oer9gJhHVqqsY5I4sEPo4XX
stR0Yiw0buV6DL/moUO0HIM9Bjh96HJp+LxiIS6UCdIhMPp5HoQa
-----END RSA PRIVATE KEY-----`
)

func TestTransportFor(t *testing.T) {
	testCases := map[string]struct {
		Config  *Config
		Err     bool
		TLS     bool
		Default bool
	}{
		"default transport": {
			Default: true,
			Config:  &Config{},
		},

		"ca transport": {
			TLS: true,
			Config: &Config{
				TLSClientConfig: TLSClientConfig{
					CAData: []byte(rootCACert),
				},
			},
		},
		"bad ca file transport": {
			Err: true,
			Config: &Config{
				TLSClientConfig: TLSClientConfig{
					CAFile: "invalid file",
				},
			},
		},
		"ca data overriding bad ca file transport": {
			TLS: true,
			Config: &Config{
				TLSClientConfig: TLSClientConfig{
					CAData: []byte(rootCACert),
					CAFile: "invalid file",
				},
			},
		},

		"cert transport": {
			TLS: true,
			Config: &Config{
				TLSClientConfig: TLSClientConfig{
					CertData: []byte(certData),
					KeyData:  []byte(keyData),
					CAData:   []byte(rootCACert),
				},
			},
		},
		"bad cert data transport": {
			Err: true,
			Config: &Config{
				TLSClientConfig: TLSClientConfig{
					CertData: []byte(certData),
					KeyData:  []byte("bad key data"),
					CAData:   []byte(rootCACert),
				},
			},
		},
		"bad file cert transport": {
			Err: true,
			Config: &Config{
				TLSClientConfig: TLSClientConfig{
					CertData: []byte(certData),
					KeyFile:  "invalid file",
					CAData:   []byte(rootCACert),
				},
			},
		},
		"key data overriding bad file cert transport": {
			TLS: true,
			Config: &Config{
				TLSClientConfig: TLSClientConfig{
					CertData: []byte(certData),
					KeyData:  []byte(keyData),
					KeyFile:  "invalid file",
					CAData:   []byte(rootCACert),
				},
			},
		},
	}
	for k, testCase := range testCases {
		transport, err := TransportFor(testCase.Config)
		switch {
		case testCase.Err && err == nil:
			t.Errorf("%s: unexpected non-error", k)
			continue
		case !testCase.Err && err != nil:
			t.Errorf("%s: unexpected error: %v", k, err)
			continue
		}

		switch {
		case testCase.Default && transport != http.DefaultTransport:
			t.Errorf("%s: expected the default transport, got %#v", k, transport)
			continue
		case !testCase.Default && transport == http.DefaultTransport:
			t.Errorf("%s: expected non-default transport, got %#v", k, transport)
			continue
		}

		// We only know how to check TLSConfig on http.Transports
		if transport, ok := transport.(*http.Transport); ok {
			switch {
			case testCase.TLS && transport.TLSClientConfig == nil:
				t.Errorf("%s: expected TLSClientConfig, got %#v", k, transport)
				continue
			case !testCase.TLS && transport.TLSClientConfig != nil:
				t.Errorf("%s: expected no TLSClientConfig, got %#v", k, transport)
				continue
			}
		}
	}
}

func TestTLSTransportCache(t *testing.T) {
	// Empty the cache
	tlsTransports = map[string]*http.Transport{}
	// Construct several transports (Insecure=true to force a transport with custom tls settings)
	identicalConfigurations := map[string]*Config{
		"empty":          {Insecure: true},
		"host":           {Insecure: true, Host: "foo"},
		"prefix":         {Insecure: true, Prefix: "foo"},
		"version":        {Insecure: true, Version: "foo"},
		"codec":          {Insecure: true, Codec: testapi.Default.Codec()},
		"basic":          {Insecure: true, Username: "bob", Password: "password"},
		"bearer":         {Insecure: true, BearerToken: "token"},
		"user agent":     {Insecure: true, UserAgent: "useragent"},
		"wrap transport": {Insecure: true, WrapTransport: func(http.RoundTripper) http.RoundTripper { return nil }},
		"qps/burst":      {Insecure: true, QPS: 1.0, Burst: 10},
	}
	for k, v := range identicalConfigurations {
		if _, err := TransportFor(v); err != nil {
			t.Errorf("Unexpected error for %q: %v", k, err)
		}
	}
	if len(tlsTransports) != 1 {
		t.Errorf("Expected 1 cached transport, got %d", len(tlsTransports))
	}

	// Empty the cache
	tlsTransports = map[string]*http.Transport{}
	// Construct several transports with custom TLS settings
	// (no normalization is performed on ca/cert/key data, so appending a newline lets us test "different" content)
	uniqueConfigurations := map[string]*Config{
		"insecure":                {Insecure: true},
		"cadata 1":                {TLSClientConfig: TLSClientConfig{CAData: []byte(rootCACert)}},
		"cadata 2":                {TLSClientConfig: TLSClientConfig{CAData: []byte(rootCACert + "\n")}},
		"cert 1, key 1":           {TLSClientConfig: TLSClientConfig{CertData: []byte(certData), KeyData: []byte(keyData)}},
		"cert 1, key 2":           {TLSClientConfig: TLSClientConfig{CertData: []byte(certData), KeyData: []byte(keyData + "\n")}},
		"cert 2, key 1":           {TLSClientConfig: TLSClientConfig{CertData: []byte(certData + "\n"), KeyData: []byte(keyData)}},
		"cert 2, key 2":           {TLSClientConfig: TLSClientConfig{CertData: []byte(certData + "\n"), KeyData: []byte(keyData + "\n")}},
		"cadata 1, cert 1, key 1": {TLSClientConfig: TLSClientConfig{CAData: []byte(rootCACert), CertData: []byte(certData), KeyData: []byte(keyData)}},
	}
	for k, v := range uniqueConfigurations {
		if _, err := TransportFor(v); err != nil {
			t.Errorf("Unexpected error for %q: %v", k, err)
		}
	}
	// All custom configs should result in a cache entry
	if len(tlsTransports) != len(uniqueConfigurations) {
		t.Errorf("Expected %d cached transports, got %d", len(uniqueConfigurations), len(tlsTransports))
	}

	// Empty the cache
	tlsTransports = map[string]*http.Transport{}
	if _, err := TransportFor(&Config{}); err != nil {
		t.Errorf("Unexpected error: %v", err)
	}
	// A client config with no TLS options should use http.DefaultTransport, not a cached custom transport
	if len(tlsTransports) != 0 {
		t.Errorf("Expected no cached transports, got %d", len(tlsTransports))
	}
}

func TestIsConfigTransportTLS(t *testing.T) {
	testCases := []struct {
		Config       *Config
		TransportTLS bool
	}{
		{
			Config:       &Config{},
			TransportTLS: false,
		},
		{
			Config: &Config{
				Host: "https://localhost",
			},
			TransportTLS: true,
		},
		{
			Config: &Config{
				Host: "localhost",
				TLSClientConfig: TLSClientConfig{
					CertFile: "foo",
				},
			},
			TransportTLS: true,
		},
		{
			Config: &Config{
				Host: "///:://localhost",
				TLSClientConfig: TLSClientConfig{
					CertFile: "foo",
				},
			},
			TransportTLS: false,
		},
		{
			Config: &Config{
				Host:     "1.2.3.4:567",
				Insecure: true,
			},
			TransportTLS: true,
		},
	}
	for _, testCase := range testCases {
		if err := SetKubernetesDefaults(testCase.Config); err != nil {
			t.Errorf("setting defaults failed for %#v: %v", testCase.Config, err)
			continue
		}
		useTLS := IsConfigTransportTLS(*testCase.Config)
		if testCase.TransportTLS != useTLS {
			t.Errorf("expected %v for %#v", testCase.TransportTLS, testCase.Config)
		}
	}
}

func TestSetKubernetesDefaults(t *testing.T) {
	testCases := []struct {
		Config Config
		After  Config
		Err    bool
	}{
		{
			Config{},
			Config{
				Prefix:  "/api",
				Version: testapi.Default.Version(),
				Codec:   testapi.Default.Codec(),
				QPS:     5,
				Burst:   10,
			},
			false,
		},
		{
			Config{
				Version: "not_an_api",
			},
			Config{},
			true,
		},
	}
	for _, testCase := range testCases {
		val := &testCase.Config
		err := SetKubernetesDefaults(val)
		val.UserAgent = ""
		switch {
		case err == nil && testCase.Err:
			t.Errorf("expected error but was nil")
			continue
		case err != nil && !testCase.Err:
			t.Errorf("unexpected error %v", err)
			continue
		case err != nil:
			continue
		}
		if !reflect.DeepEqual(*val, testCase.After) {
			t.Errorf("unexpected result object: %#v", val)
		}
	}
}

func TestSetKubernetesDefaultsUserAgent(t *testing.T) {
	config := &Config{}
	if err := SetKubernetesDefaults(config); err != nil {
		t.Errorf("unexpected error: %v", err)
	}
	if !strings.Contains(config.UserAgent, "kubernetes/") {
		t.Errorf("no user agent set: %#v", config)
	}
}

func TestHelperGetServerAPIVersions(t *testing.T) {
	expect := []string{"v1", "v2", "v3"}
	APIVersions := unversioned.APIVersions{Versions: expect}
	expect = append(expect, "group1/v1", "group1/v2", "group2/v1", "group2/v2")
	APIGroupList := unversioned.APIGroupList{
		Groups: []unversioned.APIGroup{
			{
				Versions: []unversioned.GroupVersion{
					{
						GroupVersion: "group1/v1",
					},
					{
						GroupVersion: "group1/v2",
					},
				},
			},
			{
				Versions: []unversioned.GroupVersion{
					{
						GroupVersion: "group2/v1",
					},
					{
						GroupVersion: "group2/v2",
					},
				},
			},
		},
	}
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		var output []byte
		var err error
		switch req.URL.Path {
		case "/api":
			output, err = json.Marshal(APIVersions)

		case "/apis":
			output, err = json.Marshal(APIGroupList)
		}
		if err != nil {
			t.Errorf("unexpected encoding error: %v", err)
			return
		}

		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)
		w.Write(output)
	}))
	got, err := ServerAPIVersions(&Config{Host: server.URL, Version: "invalid version", Codec: testapi.Default.Codec()})
	if err != nil {
		t.Fatalf("unexpected encoding error: %v", err)
	}
	if e, a := expect, got; !reflect.DeepEqual(e, a) {
		t.Errorf("expected %v, got %v", e, a)
	}
}
