2016-02-02 21:03:49 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-02-02 21:03:49 +00:00
|
|
|
|
|
|
|
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 apiserver
|
|
|
|
|
|
|
|
import (
|
2016-08-26 03:01:50 +00:00
|
|
|
"crypto/tls"
|
2016-02-02 21:03:49 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2017-02-01 13:18:37 +00:00
|
|
|
"os"
|
2016-02-02 21:03:49 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-08-26 03:01:50 +00:00
|
|
|
"github.com/golang/glog"
|
2016-02-02 21:03:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-02-01 13:18:37 +00:00
|
|
|
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-02-01 13:18:37 +00:00
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
|
|
|
"k8s.io/kubernetes/test/integration/framework"
|
|
|
|
"k8s.io/sample-apiserver/pkg/apis/wardle/v1alpha1"
|
|
|
|
"k8s.io/sample-apiserver/pkg/cmd/server"
|
2016-02-02 21:03:49 +00:00
|
|
|
)
|
|
|
|
|
2017-02-01 13:18:37 +00:00
|
|
|
const securePort = "6444"
|
|
|
|
|
|
|
|
var groupVersion = v1alpha1.SchemeGroupVersion
|
2016-02-02 21:03:49 +00:00
|
|
|
|
2016-12-03 18:57:26 +00:00
|
|
|
var groupVersionForDiscovery = metav1.GroupVersionForDiscovery{
|
2016-02-04 06:39:18 +00:00
|
|
|
GroupVersion: groupVersion.String(),
|
|
|
|
Version: groupVersion.Version,
|
|
|
|
}
|
|
|
|
|
2016-08-26 03:01:50 +00:00
|
|
|
func TestRunServer(t *testing.T) {
|
2017-02-01 13:18:37 +00:00
|
|
|
masterConfig := framework.NewIntegrationTestMasterConfig()
|
|
|
|
_, s := framework.RunAMaster(masterConfig)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
adminKubeConfig := createKubeConfig(masterConfig.GenericConfig.LoopbackClientConfig)
|
|
|
|
kubeconfigFile, _ := ioutil.TempFile("", "")
|
|
|
|
defer os.Remove(kubeconfigFile.Name())
|
|
|
|
clientcmd.WriteToFile(*adminKubeConfig, kubeconfigFile.Name())
|
|
|
|
|
2017-02-16 19:20:38 +00:00
|
|
|
// Avoid default cert-dir of /var/run/kubernetes to allow this to run on darwin
|
|
|
|
certDir, _ := ioutil.TempDir("", "test-integration-apiserver")
|
|
|
|
defer os.Remove(certDir)
|
|
|
|
|
2016-10-21 11:22:43 +00:00
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
2017-02-01 13:18:37 +00:00
|
|
|
cmd := server.NewCommandStartWardleServer(os.Stdout, os.Stderr, stopCh)
|
|
|
|
cmd.SetArgs([]string{
|
|
|
|
"--secure-port", securePort,
|
|
|
|
"--requestheader-username-headers", "",
|
|
|
|
"--authentication-kubeconfig", kubeconfigFile.Name(),
|
|
|
|
"--authorization-kubeconfig", kubeconfigFile.Name(),
|
|
|
|
"--etcd-servers", framework.GetEtcdURLFromEnv(),
|
2017-02-16 19:20:38 +00:00
|
|
|
"--cert-dir", certDir,
|
2017-02-01 13:18:37 +00:00
|
|
|
})
|
|
|
|
go cmd.Execute()
|
|
|
|
|
|
|
|
serverLocation := fmt.Sprintf("https://localhost:%s", securePort)
|
|
|
|
if err := waitForApiserverUp(serverLocation); err != nil {
|
2016-02-02 21:03:49 +00:00
|
|
|
t.Fatalf("%v", err)
|
|
|
|
}
|
2017-02-01 13:18:37 +00:00
|
|
|
|
|
|
|
testAPIGroupList(t, serverLocation)
|
|
|
|
testAPIGroup(t, serverLocation)
|
|
|
|
testAPIResourceList(t, serverLocation)
|
2016-02-02 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 13:18:37 +00:00
|
|
|
func createKubeConfig(clientCfg *rest.Config) *clientcmdapi.Config {
|
|
|
|
clusterNick := "cluster"
|
|
|
|
userNick := "user"
|
|
|
|
contextNick := "context"
|
|
|
|
|
|
|
|
config := clientcmdapi.NewConfig()
|
|
|
|
|
|
|
|
credentials := clientcmdapi.NewAuthInfo()
|
|
|
|
credentials.Token = clientCfg.BearerToken
|
|
|
|
credentials.ClientCertificate = clientCfg.TLSClientConfig.CertFile
|
|
|
|
if len(credentials.ClientCertificate) == 0 {
|
|
|
|
credentials.ClientCertificateData = clientCfg.TLSClientConfig.CertData
|
|
|
|
}
|
|
|
|
credentials.ClientKey = clientCfg.TLSClientConfig.KeyFile
|
|
|
|
if len(credentials.ClientKey) == 0 {
|
|
|
|
credentials.ClientKeyData = clientCfg.TLSClientConfig.KeyData
|
|
|
|
}
|
|
|
|
config.AuthInfos[userNick] = credentials
|
|
|
|
|
|
|
|
cluster := clientcmdapi.NewCluster()
|
|
|
|
cluster.Server = clientCfg.Host
|
|
|
|
cluster.CertificateAuthority = clientCfg.CAFile
|
|
|
|
if len(cluster.CertificateAuthority) == 0 {
|
|
|
|
cluster.CertificateAuthorityData = clientCfg.CAData
|
|
|
|
}
|
|
|
|
cluster.InsecureSkipTLSVerify = clientCfg.Insecure
|
|
|
|
if clientCfg.GroupVersion != nil {
|
|
|
|
cluster.APIVersion = clientCfg.GroupVersion.String()
|
2016-08-26 03:01:50 +00:00
|
|
|
}
|
2017-02-01 13:18:37 +00:00
|
|
|
config.Clusters[clusterNick] = cluster
|
|
|
|
|
|
|
|
context := clientcmdapi.NewContext()
|
|
|
|
context.Cluster = clusterNick
|
|
|
|
context.AuthInfo = userNick
|
|
|
|
config.Contexts[contextNick] = context
|
|
|
|
config.CurrentContext = contextNick
|
|
|
|
|
|
|
|
return config
|
2016-08-26 03:01:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 13:18:37 +00:00
|
|
|
func waitForApiserverUp(serverLocation string) error {
|
|
|
|
for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(5 * time.Second) {
|
|
|
|
glog.Errorf("Waiting for : %#v", serverLocation)
|
2016-08-26 03:01:50 +00:00
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: tr}
|
2017-02-01 13:18:37 +00:00
|
|
|
_, err := client.Get(serverLocation)
|
2016-02-02 21:03:49 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("waiting for apiserver timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
func readResponse(serverURL string) ([]byte, error) {
|
2016-08-26 03:01:50 +00:00
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
response, err := client.Get(serverURL)
|
2016-02-02 21:03:49 +00:00
|
|
|
if err != nil {
|
2016-08-26 03:01:50 +00:00
|
|
|
glog.Errorf("http get err code : %#v", err)
|
2016-02-02 21:03:49 +00:00
|
|
|
return nil, fmt.Errorf("Error in fetching %s: %v", serverURL, err)
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
2016-08-26 03:01:50 +00:00
|
|
|
glog.Errorf("http get response code : %#v", response.StatusCode)
|
2016-02-04 06:39:18 +00:00
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("unexpected status: %d for URL: %s, expected status: %d", response.StatusCode, serverURL, http.StatusOK)
|
|
|
|
}
|
2016-02-02 21:03:49 +00:00
|
|
|
contents, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error reading response from %s: %v", serverURL, err)
|
|
|
|
}
|
|
|
|
return contents, nil
|
|
|
|
}
|
|
|
|
|
2017-02-01 13:18:37 +00:00
|
|
|
func testAPIGroupList(t *testing.T, serverLocation string) {
|
|
|
|
serverURL := serverLocation + "/apis"
|
2016-02-04 06:39:18 +00:00
|
|
|
contents, err := readResponse(serverURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", err)
|
|
|
|
}
|
2017-02-01 13:18:37 +00:00
|
|
|
t.Log(string(contents))
|
2016-12-03 18:57:26 +00:00
|
|
|
var apiGroupList metav1.APIGroupList
|
2016-02-04 06:39:18 +00:00
|
|
|
err = json.Unmarshal(contents, &apiGroupList)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error in unmarshalling response from server %s: %v", serverURL, err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, 1, len(apiGroupList.Groups))
|
2017-02-01 13:18:37 +00:00
|
|
|
assert.Equal(t, groupVersion.Group, apiGroupList.Groups[0].Name)
|
2016-02-04 06:39:18 +00:00
|
|
|
assert.Equal(t, 1, len(apiGroupList.Groups[0].Versions))
|
2017-02-01 13:18:37 +00:00
|
|
|
assert.Equal(t, groupVersionForDiscovery, apiGroupList.Groups[0].Versions[0])
|
|
|
|
assert.Equal(t, groupVersionForDiscovery, apiGroupList.Groups[0].PreferredVersion)
|
2016-02-04 06:39:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 13:18:37 +00:00
|
|
|
func testAPIGroup(t *testing.T, serverLocation string) {
|
|
|
|
serverURL := serverLocation + "/apis/wardle.k8s.io"
|
2016-02-02 21:03:49 +00:00
|
|
|
contents, err := readResponse(serverURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", err)
|
|
|
|
}
|
2017-02-01 13:18:37 +00:00
|
|
|
t.Log(string(contents))
|
2016-12-03 18:57:26 +00:00
|
|
|
var apiGroup metav1.APIGroup
|
2016-02-02 21:03:49 +00:00
|
|
|
err = json.Unmarshal(contents, &apiGroup)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error in unmarshalling response from server %s: %v", serverURL, err)
|
|
|
|
}
|
2017-02-01 13:18:37 +00:00
|
|
|
assert.Equal(t, groupVersion.Group, apiGroup.Name)
|
2016-02-02 21:03:49 +00:00
|
|
|
assert.Equal(t, 1, len(apiGroup.Versions))
|
2017-02-01 13:18:37 +00:00
|
|
|
assert.Equal(t, groupVersion.String(), apiGroup.Versions[0].GroupVersion)
|
|
|
|
assert.Equal(t, groupVersion.Version, apiGroup.Versions[0].Version)
|
|
|
|
assert.Equal(t, apiGroup.PreferredVersion, apiGroup.Versions[0])
|
2016-02-02 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 13:18:37 +00:00
|
|
|
func testAPIResourceList(t *testing.T, serverLocation string) {
|
|
|
|
serverURL := serverLocation + "/apis/wardle.k8s.io/v1alpha1"
|
2016-02-02 21:03:49 +00:00
|
|
|
contents, err := readResponse(serverURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", err)
|
|
|
|
}
|
2017-02-01 13:18:37 +00:00
|
|
|
t.Log(string(contents))
|
2016-12-03 18:57:26 +00:00
|
|
|
var apiResourceList metav1.APIResourceList
|
2016-02-02 21:03:49 +00:00
|
|
|
err = json.Unmarshal(contents, &apiResourceList)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error in unmarshalling response from server %s: %v", serverURL, err)
|
|
|
|
}
|
2017-02-01 13:18:37 +00:00
|
|
|
assert.Equal(t, groupVersion.String(), apiResourceList.GroupVersion)
|
2016-02-02 21:03:49 +00:00
|
|
|
assert.Equal(t, 1, len(apiResourceList.APIResources))
|
2017-02-01 13:18:37 +00:00
|
|
|
assert.Equal(t, "flunders", apiResourceList.APIResources[0].Name)
|
2016-02-02 21:03:49 +00:00
|
|
|
assert.True(t, apiResourceList.APIResources[0].Namespaced)
|
|
|
|
}
|