Skip to content

Commit

Permalink
refactor testapi and test scripts to prepare for multiple API groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chao Xu committed Sep 5, 2015
1 parent 49702f9 commit 9fc79e9
Show file tree
Hide file tree
Showing 109 changed files with 1,010 additions and 714 deletions.
25 changes: 15 additions & 10 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"k8s.io/kubernetes/pkg/api"
apierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/latest"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/apiserver"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/record"
Expand Down Expand Up @@ -69,8 +70,6 @@ import (

var (
fakeDocker1, fakeDocker2 dockertools.FakeDockerClient
// API version that should be used by the client to talk to the server.
apiVersion string
// Limit the number of concurrent tests.
maxConcurrency int
)
Expand All @@ -93,7 +92,7 @@ func (h *delegateHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNotFound)
}

func startComponents(firstManifestURL, secondManifestURL, apiVersion string) (string, string) {
func startComponents(firstManifestURL, secondManifestURL string) (string, string) {
// Setup
servers := []string{}
glog.Infof("Creating etcd client pointing to %v", servers)
Expand Down Expand Up @@ -126,13 +125,17 @@ func startComponents(firstManifestURL, secondManifestURL, apiVersion string) (st
glog.Fatalf("Failed to connect to etcd")
}

cl := client.NewOrDie(&client.Config{Host: apiServer.URL, Version: apiVersion})
cl := client.NewOrDie(&client.Config{Host: apiServer.URL, Version: testapi.Default.Version()})

etcdStorage, err := master.NewEtcdStorage(etcdClient, latest.InterfacesFor, latest.Version, etcdtest.PathPrefix())
// TODO: caesarxuchao: hacky way to specify version of Experimental client.
// We will fix this by supporting multiple group versions in Config
cl.ExperimentalClient = client.NewExperimentalOrDie(&client.Config{Host: apiServer.URL, Version: testapi.Experimental.Version()})

etcdStorage, err := master.NewEtcdStorage(etcdClient, latest.InterfacesFor, testapi.Default.Version(), etcdtest.PathPrefix())
if err != nil {
glog.Fatalf("Unable to get etcd storage: %v", err)
}
expEtcdStorage, err := master.NewEtcdStorage(etcdClient, explatest.InterfacesFor, explatest.Version, etcdtest.PathPrefix())
expEtcdStorage, err := master.NewEtcdStorage(etcdClient, explatest.InterfacesFor, testapi.Experimental.Version(), etcdtest.PathPrefix())
if err != nil {
glog.Fatalf("Unable to get etcd storage for experimental: %v", err)
}
Expand Down Expand Up @@ -891,7 +894,6 @@ func runSchedulerNoPhantomPodsTest(client *client.Client) {
type testFunc func(*client.Client)

func addFlags(fs *pflag.FlagSet) {
fs.StringVar(&apiVersion, "api-version", latest.Version, "API version that should be used by the client for communicating with the server")
fs.IntVar(
&maxConcurrency, "max-concurrency", -1, "Maximum number of tests to be run simultaneously. Unlimited if set to negative.")
}
Expand All @@ -911,18 +913,21 @@ func main() {
glog.Fatalf("This test has timed out.")
}()

glog.Infof("Running tests for APIVersion: %s", apiVersion)
glog.Infof("Running tests for APIVersion: %s", os.Getenv("KUBE_TEST_API"))

firstManifestURL := ServeCachedManifestFile(testPodSpecFile)
secondManifestURL := ServeCachedManifestFile(testPodSpecFile)
apiServerURL, _ := startComponents(firstManifestURL, secondManifestURL, apiVersion)
apiServerURL, _ := startComponents(firstManifestURL, secondManifestURL)

// Ok. we're good to go.
glog.Infof("API Server started on %s", apiServerURL)
// Wait for the synchronization threads to come up.
time.Sleep(time.Second * 10)

kubeClient := client.NewOrDie(&client.Config{Host: apiServerURL, Version: apiVersion})
kubeClient := client.NewOrDie(&client.Config{Host: apiServerURL, Version: testapi.Default.Version()})
// TODO: caesarxuchao: hacky way to specify version of Experimental client.
// We will fix this by supporting multiple group versions in Config
kubeClient.ExperimentalClient = client.NewExperimentalOrDie(&client.Config{Host: apiServerURL, Version: testapi.Experimental.Version()})

// Run tests in parallel
testFuncs := []testFunc{
Expand Down
16 changes: 8 additions & 8 deletions contrib/mesos/pkg/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func TestExecutorLaunchAndKillTask(t *testing.T) {
Updates: updates,
APIClient: client.NewOrDie(&client.Config{
Host: testApiServer.server.URL,
Version: testapi.Version(),
Version: testapi.Default.Version(),
}),
Kubelet: &fakeKubelet{
Kubelet: &kubelet.Kubelet{},
Expand Down Expand Up @@ -355,7 +355,7 @@ func TestExecutorLaunchAndKillTask(t *testing.T) {
assert.Equal(t, nil, err, "must be able to create a task from a pod")

taskInfo := podTask.BuildTaskInfo()
data, err := testapi.Codec().Encode(pod)
data, err := testapi.Default.Codec().Encode(pod)
assert.Equal(t, nil, err, "must be able to encode a pod's spec data")
taskInfo.Data = data
var statusUpdateCalls sync.WaitGroup
Expand Down Expand Up @@ -484,7 +484,7 @@ func TestExecutorStaticPods(t *testing.T) {
Updates: make(chan interface{}, 1), // allow kube-executor source to proceed past init
APIClient: client.NewOrDie(&client.Config{
Host: testApiServer.server.URL,
Version: testapi.Version(),
Version: testapi.Default.Version(),
}),
Kubelet: &kubelet.Kubelet{},
PodStatusFunc: func(kl KubeletInterface, pod *api.Pod) (*api.PodStatus, error) {
Expand Down Expand Up @@ -565,7 +565,7 @@ func TestExecutorFrameworkMessage(t *testing.T) {
Updates: make(chan interface{}, 1024),
APIClient: client.NewOrDie(&client.Config{
Host: testApiServer.server.URL,
Version: testapi.Version(),
Version: testapi.Default.Version(),
}),
Kubelet: &fakeKubelet{
Kubelet: &kubelet.Kubelet{},
Expand Down Expand Up @@ -602,7 +602,7 @@ func TestExecutorFrameworkMessage(t *testing.T) {
*pod, &mesosproto.ExecutorInfo{})

taskInfo := podTask.BuildTaskInfo()
data, _ := testapi.Codec().Encode(pod)
data, _ := testapi.Default.Codec().Encode(pod)
taskInfo.Data = data

mockDriver.On(
Expand Down Expand Up @@ -660,11 +660,11 @@ func TestExecutorFrameworkMessage(t *testing.T) {
func NewTestPod(i int) *api.Pod {
name := fmt.Sprintf("pod%d", i)
return &api.Pod{
TypeMeta: api.TypeMeta{APIVersion: testapi.Version()},
TypeMeta: api.TypeMeta{APIVersion: testapi.Default.Version()},
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: api.NamespaceDefault,
SelfLink: testapi.SelfLink("pods", string(i)),
SelfLink: testapi.Default.SelfLink("pods", string(i)),
},
Spec: api.PodSpec{
Containers: []api.Container{
Expand Down Expand Up @@ -710,7 +710,7 @@ func NewTestServer(t *testing.T, namespace string, pods *api.PodList) *TestServe
}
mux := http.NewServeMux()

mux.HandleFunc(testapi.ResourcePath("bindings", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(testapi.Default.ResourcePath("bindings", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand Down
14 changes: 7 additions & 7 deletions contrib/mesos/pkg/scheduler/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func NewTestServer(t *testing.T, namespace string, mockPodListWatch *MockPodsLis
}
mux := http.NewServeMux()

mux.HandleFunc(testapi.ResourcePath("pods", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(testapi.Default.ResourcePath("pods", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
pods := mockPodListWatch.Pods()
w.Write([]byte(runtime.EncodeOrDie(testapi.Codec(), &pods)))
w.Write([]byte(runtime.EncodeOrDie(testapi.Default.Codec(), &pods)))
})

podsPrefix := testapi.ResourcePath("pods", namespace, "") + "/"
podsPrefix := testapi.Default.ResourcePath("pods", namespace, "") + "/"
mux.HandleFunc(podsPrefix, func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[len(podsPrefix):]

Expand All @@ -79,13 +79,13 @@ func NewTestServer(t *testing.T, namespace string, mockPodListWatch *MockPodsLis
p := mockPodListWatch.GetPod(name)
if p != nil {
w.WriteHeader(http.StatusOK)
w.Write([]byte(runtime.EncodeOrDie(testapi.Codec(), p)))
w.Write([]byte(runtime.EncodeOrDie(testapi.Default.Codec(), p)))
return
}
w.WriteHeader(http.StatusNotFound)
})

mux.HandleFunc(testapi.ResourcePath("events", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(testapi.Default.ResourcePath("events", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand Down Expand Up @@ -196,7 +196,7 @@ func NewTestPod() (*api.Pod, int) {
currentPodNum = currentPodNum + 1
name := fmt.Sprintf("pod%d", currentPodNum)
return &api.Pod{
TypeMeta: api.TypeMeta{APIVersion: testapi.Version()},
TypeMeta: api.TypeMeta{APIVersion: testapi.Default.Version()},
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: api.NamespaceDefault,
Expand Down Expand Up @@ -398,7 +398,7 @@ func TestPlugin_LifeCycle(t *testing.T) {
podtask.NewDefaultProcurement(mresource.DefaultDefaultContainerCPULimit, mresource.DefaultDefaultContainerMemLimit))
testScheduler := New(Config{
Executor: executor,
Client: client.NewOrDie(&client.Config{Host: testApiServer.server.URL, Version: testapi.Version()}),
Client: client.NewOrDie(&client.Config{Host: testApiServer.server.URL, Version: testapi.Default.Version()}),
Scheduler: NewFCFSPodScheduler(as),
Schedcfg: *schedcfg.CreateDefaultConfig(),
})
Expand Down
21 changes: 15 additions & 6 deletions hack/test-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ KUBE_COVERPROCS=${KUBE_COVERPROCS:-4}
KUBE_RACE=${KUBE_RACE:-} # use KUBE_RACE="-race" to enable race testing
# Set to the goveralls binary path to report coverage results to Coveralls.io.
KUBE_GOVERALLS_BIN=${KUBE_GOVERALLS_BIN:-}
# Comma separated list of API Versions that should be tested.
KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1"}
# Lists of API Versions of each groups that should be tested, groups are
# separated by comma, lists are separated by semicolon. e.g.,
# "v1,compute/v1alpha1,experimental/v1alpha2;v1,compute/v2,experimental/v1alpha3"
# TODO: It's going to be:
# KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1,experimental/v1alpha1"}
KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1,experimental/v1"}
# once we have multiple group supports
# Run tests with the standard (registry) and a custom etcd prefix
# (kubernetes.io/registry).
KUBE_TEST_ETCD_PREFIXES=${KUBE_TEST_ETCD_PREFIXES:-"registry,kubernetes.io/registry"}
Expand Down Expand Up @@ -131,7 +136,8 @@ junitFilenamePrefix() {
return
fi
mkdir -p "${KUBE_JUNIT_REPORT_DIR}"
echo "${KUBE_JUNIT_REPORT_DIR}/junit_${KUBE_API_VERSION}_$(kube::util::sortable_date)"
local KUBE_TEST_API_NO_SLASH=echo "${KUBE_TEST_API//\//-}"
echo "${KUBE_JUNIT_REPORT_DIR}/junit_${KUBE_TEST_API_NO_SLASH}_$(kube::util::sortable_date)"
}

produceJUnitXMLReport() {
Expand Down Expand Up @@ -205,7 +211,7 @@ runTests() {
fi

# Create coverage report directories.
cover_report_dir="/tmp/k8s_coverage/${KUBE_API_VERSION}/$(kube::util::sortable_date)"
cover_report_dir="/tmp/k8s_coverage/${KUBE_TEST_API}/$(kube::util::sortable_date)"
cover_profile="coverage.out" # Name for each individual coverage profile
kube::log::status "Saving coverage output in '${cover_report_dir}'"
mkdir -p "${@+${@/#/${cover_report_dir}/}}"
Expand Down Expand Up @@ -266,15 +272,18 @@ reportCoverageToCoveralls() {
}

# Convert the CSVs to arrays.
IFS=',' read -a apiVersions <<< "${KUBE_TEST_API_VERSIONS}"
IFS=';' read -a apiVersions <<< "${KUBE_TEST_API_VERSIONS}"
IFS=',' read -a etcdPrefixes <<< "${KUBE_TEST_ETCD_PREFIXES}"
apiVersionsCount=${#apiVersions[@]}
etcdPrefixesCount=${#etcdPrefixes[@]}
for (( i=0, j=0; ; )); do
apiVersion=${apiVersions[i]}
etcdPrefix=${etcdPrefixes[j]}
echo "Running tests for APIVersion: $apiVersion with etcdPrefix: $etcdPrefix"
KUBE_API_VERSION="${apiVersion}" KUBE_API_VERSIONS="v1" ETCD_PREFIX=${etcdPrefix} runTests "$@"
# KUBE_TEST_API sets the version of each group to be tested. KUBE_API_VERSIONS
# register the groups/versions as supported by k8s. So KUBE_API_VERSIONS
# needs to be the superset of KUBE_TEST_API.
KUBE_TEST_API="${apiVersion}" KUBE_API_VERSIONS="v1" ETCD_PREFIX=${etcdPrefix} runTests "$@"
i=${i}+1
j=${j}+1
if [[ i -eq ${apiVersionsCount} ]] && [[ j -eq ${etcdPrefixesCount} ]]; then
Expand Down
14 changes: 9 additions & 5 deletions hack/test-integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ set -o pipefail

KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
# Comma separated list of API Versions that should be tested.
KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1"}
# Lists of API Versions of each groups that should be tested, groups are
# separated by comma, lists are separated by semicolon. e.g.,
# "v1,compute/v1alpha1,experimental/v1alpha2;v1,compute/v2,experimental/v1alpha3"
# TODO: It's going to be:
# KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1,experimental/v1alpha1"}
KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1,experimental/v1"}

KUBE_INTEGRATION_TEST_MAX_CONCURRENCY=${KUBE_INTEGRATION_TEST_MAX_CONCURRENCY:-"-1"}
LOG_LEVEL=${LOG_LEVEL:-2}
Expand All @@ -48,8 +52,8 @@ runTests() {

kube::log::status "Running integration test scenario"

KUBE_API_VERSIONS="v1" "${KUBE_OUTPUT_HOSTBIN}/integration" --v=${LOG_LEVEL} --api-version="$1" \
--max-concurrency="${KUBE_INTEGRATION_TEST_MAX_CONCURRENCY}"
KUBE_API_VERSIONS="v1" KUBE_TEST_API_VERSIONS="$1" "${KUBE_OUTPUT_HOSTBIN}/integration" --v=${LOG_LEVEL} \
--max-concurrency="${KUBE_INTEGRATION_TEST_MAX_CONCURRENCY}"

cleanup
}
Expand All @@ -60,7 +64,7 @@ KUBE_API_VERSIONS="v1" "${KUBE_ROOT}/hack/build-go.sh" "$@" cmd/integration
trap cleanup EXIT

# Convert the CSV to an array of API versions to test
IFS=',' read -a apiVersions <<< "${KUBE_TEST_API_VERSIONS}"
IFS=';' read -a apiVersions <<< "${KUBE_TEST_API_VERSIONS}"
for apiVersion in "${apiVersions[@]}"; do
runTests "${apiVersion}"
done
6 changes: 3 additions & 3 deletions pkg/api/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func BenchmarkPodConversion(b *testing.B) {
scheme := api.Scheme.Raw()
var result *api.Pod
for i := 0; i < b.N; i++ {
versionedObj, err := scheme.ConvertToVersion(&pod, testapi.Version())
versionedObj, err := scheme.ConvertToVersion(&pod, testapi.Default.Version())
if err != nil {
b.Fatalf("Conversion error: %v", err)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func BenchmarkNodeConversion(b *testing.B) {
scheme := api.Scheme.Raw()
var result *api.Node
for i := 0; i < b.N; i++ {
versionedObj, err := scheme.ConvertToVersion(&node, testapi.Version())
versionedObj, err := scheme.ConvertToVersion(&node, testapi.Default.Version())
if err != nil {
b.Fatalf("Conversion error: %v", err)
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func BenchmarkReplicationControllerConversion(b *testing.B) {
scheme := api.Scheme.Raw()
var result *api.ReplicationController
for i := 0; i < b.N; i++ {
versionedObj, err := scheme.ConvertToVersion(&replicationController, testapi.Version())
versionedObj, err := scheme.ConvertToVersion(&replicationController, testapi.Default.Version())
if err != nil {
b.Fatalf("Conversion error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func TestDeepCopyApiObjects(t *testing.T) {
for i := 0; i < *fuzzIters; i++ {
for _, version := range []string{"", testapi.Version()} {
for _, version := range []string{"", testapi.Default.Version()} {
f := apitesting.FuzzerFor(t, version, rand.NewSource(rand.Int63()))
for kind := range api.Scheme.KnownTypes(version) {
item, err := api.Scheme.New(version, kind)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ func roundTripSame(t *testing.T, item runtime.Object, except ...string) {
set := util.NewStringSet(except...)
seed := rand.Int63()
fuzzInternalObject(t, "", item, seed)
version := testapi.Version()
version := testapi.Default.Version()
if !set.Has(version) {
fuzzInternalObject(t, version, item, seed)
roundTrip(t, testapi.Codec(), item)
roundTrip(t, testapi.Default.Codec(), item)
}
}

Expand Down
Loading

0 comments on commit 9fc79e9

Please sign in to comment.