Skip to content

Commit 9fc79e9

Browse files
author
Chao Xu
committed
refactor testapi and test scripts to prepare for multiple API groups.
1 parent 49702f9 commit 9fc79e9

File tree

109 files changed

+1010
-714
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+1010
-714
lines changed

cmd/integration/integration.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"k8s.io/kubernetes/pkg/api"
3939
apierrors "k8s.io/kubernetes/pkg/api/errors"
4040
"k8s.io/kubernetes/pkg/api/latest"
41+
"k8s.io/kubernetes/pkg/api/testapi"
4142
"k8s.io/kubernetes/pkg/apiserver"
4243
client "k8s.io/kubernetes/pkg/client/unversioned"
4344
"k8s.io/kubernetes/pkg/client/unversioned/record"
@@ -69,8 +70,6 @@ import (
6970

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

96-
func startComponents(firstManifestURL, secondManifestURL, apiVersion string) (string, string) {
95+
func startComponents(firstManifestURL, secondManifestURL string) (string, string) {
9796
// Setup
9897
servers := []string{}
9998
glog.Infof("Creating etcd client pointing to %v", servers)
@@ -126,13 +125,17 @@ func startComponents(firstManifestURL, secondManifestURL, apiVersion string) (st
126125
glog.Fatalf("Failed to connect to etcd")
127126
}
128127

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

131-
etcdStorage, err := master.NewEtcdStorage(etcdClient, latest.InterfacesFor, latest.Version, etcdtest.PathPrefix())
130+
// TODO: caesarxuchao: hacky way to specify version of Experimental client.
131+
// We will fix this by supporting multiple group versions in Config
132+
cl.ExperimentalClient = client.NewExperimentalOrDie(&client.Config{Host: apiServer.URL, Version: testapi.Experimental.Version()})
133+
134+
etcdStorage, err := master.NewEtcdStorage(etcdClient, latest.InterfacesFor, testapi.Default.Version(), etcdtest.PathPrefix())
132135
if err != nil {
133136
glog.Fatalf("Unable to get etcd storage: %v", err)
134137
}
135-
expEtcdStorage, err := master.NewEtcdStorage(etcdClient, explatest.InterfacesFor, explatest.Version, etcdtest.PathPrefix())
138+
expEtcdStorage, err := master.NewEtcdStorage(etcdClient, explatest.InterfacesFor, testapi.Experimental.Version(), etcdtest.PathPrefix())
136139
if err != nil {
137140
glog.Fatalf("Unable to get etcd storage for experimental: %v", err)
138141
}
@@ -891,7 +894,6 @@ func runSchedulerNoPhantomPodsTest(client *client.Client) {
891894
type testFunc func(*client.Client)
892895

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

914-
glog.Infof("Running tests for APIVersion: %s", apiVersion)
916+
glog.Infof("Running tests for APIVersion: %s", os.Getenv("KUBE_TEST_API"))
915917

916918
firstManifestURL := ServeCachedManifestFile(testPodSpecFile)
917919
secondManifestURL := ServeCachedManifestFile(testPodSpecFile)
918-
apiServerURL, _ := startComponents(firstManifestURL, secondManifestURL, apiVersion)
920+
apiServerURL, _ := startComponents(firstManifestURL, secondManifestURL)
919921

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

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

927932
// Run tests in parallel
928933
testFuncs := []testFunc{

contrib/mesos/pkg/executor/executor_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func TestExecutorLaunchAndKillTask(t *testing.T) {
318318
Updates: updates,
319319
APIClient: client.NewOrDie(&client.Config{
320320
Host: testApiServer.server.URL,
321-
Version: testapi.Version(),
321+
Version: testapi.Default.Version(),
322322
}),
323323
Kubelet: &fakeKubelet{
324324
Kubelet: &kubelet.Kubelet{},
@@ -355,7 +355,7 @@ func TestExecutorLaunchAndKillTask(t *testing.T) {
355355
assert.Equal(t, nil, err, "must be able to create a task from a pod")
356356

357357
taskInfo := podTask.BuildTaskInfo()
358-
data, err := testapi.Codec().Encode(pod)
358+
data, err := testapi.Default.Codec().Encode(pod)
359359
assert.Equal(t, nil, err, "must be able to encode a pod's spec data")
360360
taskInfo.Data = data
361361
var statusUpdateCalls sync.WaitGroup
@@ -484,7 +484,7 @@ func TestExecutorStaticPods(t *testing.T) {
484484
Updates: make(chan interface{}, 1), // allow kube-executor source to proceed past init
485485
APIClient: client.NewOrDie(&client.Config{
486486
Host: testApiServer.server.URL,
487-
Version: testapi.Version(),
487+
Version: testapi.Default.Version(),
488488
}),
489489
Kubelet: &kubelet.Kubelet{},
490490
PodStatusFunc: func(kl KubeletInterface, pod *api.Pod) (*api.PodStatus, error) {
@@ -565,7 +565,7 @@ func TestExecutorFrameworkMessage(t *testing.T) {
565565
Updates: make(chan interface{}, 1024),
566566
APIClient: client.NewOrDie(&client.Config{
567567
Host: testApiServer.server.URL,
568-
Version: testapi.Version(),
568+
Version: testapi.Default.Version(),
569569
}),
570570
Kubelet: &fakeKubelet{
571571
Kubelet: &kubelet.Kubelet{},
@@ -602,7 +602,7 @@ func TestExecutorFrameworkMessage(t *testing.T) {
602602
*pod, &mesosproto.ExecutorInfo{})
603603

604604
taskInfo := podTask.BuildTaskInfo()
605-
data, _ := testapi.Codec().Encode(pod)
605+
data, _ := testapi.Default.Codec().Encode(pod)
606606
taskInfo.Data = data
607607

608608
mockDriver.On(
@@ -660,11 +660,11 @@ func TestExecutorFrameworkMessage(t *testing.T) {
660660
func NewTestPod(i int) *api.Pod {
661661
name := fmt.Sprintf("pod%d", i)
662662
return &api.Pod{
663-
TypeMeta: api.TypeMeta{APIVersion: testapi.Version()},
663+
TypeMeta: api.TypeMeta{APIVersion: testapi.Default.Version()},
664664
ObjectMeta: api.ObjectMeta{
665665
Name: name,
666666
Namespace: api.NamespaceDefault,
667-
SelfLink: testapi.SelfLink("pods", string(i)),
667+
SelfLink: testapi.Default.SelfLink("pods", string(i)),
668668
},
669669
Spec: api.PodSpec{
670670
Containers: []api.Container{
@@ -710,7 +710,7 @@ func NewTestServer(t *testing.T, namespace string, pods *api.PodList) *TestServe
710710
}
711711
mux := http.NewServeMux()
712712

713-
mux.HandleFunc(testapi.ResourcePath("bindings", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
713+
mux.HandleFunc(testapi.Default.ResourcePath("bindings", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
714714
w.WriteHeader(http.StatusOK)
715715
})
716716

contrib/mesos/pkg/scheduler/plugin_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ func NewTestServer(t *testing.T, namespace string, mockPodListWatch *MockPodsLis
6161
}
6262
mux := http.NewServeMux()
6363

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

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

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

88-
mux.HandleFunc(testapi.ResourcePath("events", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
88+
mux.HandleFunc(testapi.Default.ResourcePath("events", namespace, ""), func(w http.ResponseWriter, r *http.Request) {
8989
w.WriteHeader(http.StatusOK)
9090
})
9191

@@ -196,7 +196,7 @@ func NewTestPod() (*api.Pod, int) {
196196
currentPodNum = currentPodNum + 1
197197
name := fmt.Sprintf("pod%d", currentPodNum)
198198
return &api.Pod{
199-
TypeMeta: api.TypeMeta{APIVersion: testapi.Version()},
199+
TypeMeta: api.TypeMeta{APIVersion: testapi.Default.Version()},
200200
ObjectMeta: api.ObjectMeta{
201201
Name: name,
202202
Namespace: api.NamespaceDefault,
@@ -398,7 +398,7 @@ func TestPlugin_LifeCycle(t *testing.T) {
398398
podtask.NewDefaultProcurement(mresource.DefaultDefaultContainerCPULimit, mresource.DefaultDefaultContainerMemLimit))
399399
testScheduler := New(Config{
400400
Executor: executor,
401-
Client: client.NewOrDie(&client.Config{Host: testApiServer.server.URL, Version: testapi.Version()}),
401+
Client: client.NewOrDie(&client.Config{Host: testApiServer.server.URL, Version: testapi.Default.Version()}),
402402
Scheduler: NewFCFSPodScheduler(as),
403403
Schedcfg: *schedcfg.CreateDefaultConfig(),
404404
})

hack/test-go.sh

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ KUBE_COVERPROCS=${KUBE_COVERPROCS:-4}
5252
KUBE_RACE=${KUBE_RACE:-} # use KUBE_RACE="-race" to enable race testing
5353
# Set to the goveralls binary path to report coverage results to Coveralls.io.
5454
KUBE_GOVERALLS_BIN=${KUBE_GOVERALLS_BIN:-}
55-
# Comma separated list of API Versions that should be tested.
56-
KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1"}
55+
# Lists of API Versions of each groups that should be tested, groups are
56+
# separated by comma, lists are separated by semicolon. e.g.,
57+
# "v1,compute/v1alpha1,experimental/v1alpha2;v1,compute/v2,experimental/v1alpha3"
58+
# TODO: It's going to be:
59+
# KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1,experimental/v1alpha1"}
60+
KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1,experimental/v1"}
61+
# once we have multiple group supports
5762
# Run tests with the standard (registry) and a custom etcd prefix
5863
# (kubernetes.io/registry).
5964
KUBE_TEST_ETCD_PREFIXES=${KUBE_TEST_ETCD_PREFIXES:-"registry,kubernetes.io/registry"}
@@ -131,7 +136,8 @@ junitFilenamePrefix() {
131136
return
132137
fi
133138
mkdir -p "${KUBE_JUNIT_REPORT_DIR}"
134-
echo "${KUBE_JUNIT_REPORT_DIR}/junit_${KUBE_API_VERSION}_$(kube::util::sortable_date)"
139+
local KUBE_TEST_API_NO_SLASH=echo "${KUBE_TEST_API//\//-}"
140+
echo "${KUBE_JUNIT_REPORT_DIR}/junit_${KUBE_TEST_API_NO_SLASH}_$(kube::util::sortable_date)"
135141
}
136142

137143
produceJUnitXMLReport() {
@@ -205,7 +211,7 @@ runTests() {
205211
fi
206212

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

268274
# Convert the CSVs to arrays.
269-
IFS=',' read -a apiVersions <<< "${KUBE_TEST_API_VERSIONS}"
275+
IFS=';' read -a apiVersions <<< "${KUBE_TEST_API_VERSIONS}"
270276
IFS=',' read -a etcdPrefixes <<< "${KUBE_TEST_ETCD_PREFIXES}"
271277
apiVersionsCount=${#apiVersions[@]}
272278
etcdPrefixesCount=${#etcdPrefixes[@]}
273279
for (( i=0, j=0; ; )); do
274280
apiVersion=${apiVersions[i]}
275281
etcdPrefix=${etcdPrefixes[j]}
276282
echo "Running tests for APIVersion: $apiVersion with etcdPrefix: $etcdPrefix"
277-
KUBE_API_VERSION="${apiVersion}" KUBE_API_VERSIONS="v1" ETCD_PREFIX=${etcdPrefix} runTests "$@"
283+
# KUBE_TEST_API sets the version of each group to be tested. KUBE_API_VERSIONS
284+
# register the groups/versions as supported by k8s. So KUBE_API_VERSIONS
285+
# needs to be the superset of KUBE_TEST_API.
286+
KUBE_TEST_API="${apiVersion}" KUBE_API_VERSIONS="v1" ETCD_PREFIX=${etcdPrefix} runTests "$@"
278287
i=${i}+1
279288
j=${j}+1
280289
if [[ i -eq ${apiVersionsCount} ]] && [[ j -eq ${etcdPrefixesCount} ]]; then

hack/test-integration.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ set -o pipefail
2424

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

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

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

51-
KUBE_API_VERSIONS="v1" "${KUBE_OUTPUT_HOSTBIN}/integration" --v=${LOG_LEVEL} --api-version="$1" \
52-
--max-concurrency="${KUBE_INTEGRATION_TEST_MAX_CONCURRENCY}"
55+
KUBE_API_VERSIONS="v1" KUBE_TEST_API_VERSIONS="$1" "${KUBE_OUTPUT_HOSTBIN}/integration" --v=${LOG_LEVEL} \
56+
--max-concurrency="${KUBE_INTEGRATION_TEST_MAX_CONCURRENCY}"
5357

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

6266
# Convert the CSV to an array of API versions to test
63-
IFS=',' read -a apiVersions <<< "${KUBE_TEST_API_VERSIONS}"
67+
IFS=';' read -a apiVersions <<< "${KUBE_TEST_API_VERSIONS}"
6468
for apiVersion in "${apiVersions[@]}"; do
6569
runTests "${apiVersion}"
6670
done

pkg/api/conversion_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func BenchmarkPodConversion(b *testing.B) {
3737
scheme := api.Scheme.Raw()
3838
var result *api.Pod
3939
for i := 0; i < b.N; i++ {
40-
versionedObj, err := scheme.ConvertToVersion(&pod, testapi.Version())
40+
versionedObj, err := scheme.ConvertToVersion(&pod, testapi.Default.Version())
4141
if err != nil {
4242
b.Fatalf("Conversion error: %v", err)
4343
}
@@ -65,7 +65,7 @@ func BenchmarkNodeConversion(b *testing.B) {
6565
scheme := api.Scheme.Raw()
6666
var result *api.Node
6767
for i := 0; i < b.N; i++ {
68-
versionedObj, err := scheme.ConvertToVersion(&node, testapi.Version())
68+
versionedObj, err := scheme.ConvertToVersion(&node, testapi.Default.Version())
6969
if err != nil {
7070
b.Fatalf("Conversion error: %v", err)
7171
}
@@ -93,7 +93,7 @@ func BenchmarkReplicationControllerConversion(b *testing.B) {
9393
scheme := api.Scheme.Raw()
9494
var result *api.ReplicationController
9595
for i := 0; i < b.N; i++ {
96-
versionedObj, err := scheme.ConvertToVersion(&replicationController, testapi.Version())
96+
versionedObj, err := scheme.ConvertToVersion(&replicationController, testapi.Default.Version())
9797
if err != nil {
9898
b.Fatalf("Conversion error: %v", err)
9999
}

pkg/api/copy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
func TestDeepCopyApiObjects(t *testing.T) {
3030
for i := 0; i < *fuzzIters; i++ {
31-
for _, version := range []string{"", testapi.Version()} {
31+
for _, version := range []string{"", testapi.Default.Version()} {
3232
f := apitesting.FuzzerFor(t, version, rand.NewSource(rand.Int63()))
3333
for kind := range api.Scheme.KnownTypes(version) {
3434
item, err := api.Scheme.New(version, kind)

pkg/api/serialization_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ func roundTripSame(t *testing.T, item runtime.Object, except ...string) {
9090
set := util.NewStringSet(except...)
9191
seed := rand.Int63()
9292
fuzzInternalObject(t, "", item, seed)
93-
version := testapi.Version()
93+
version := testapi.Default.Version()
9494
if !set.Has(version) {
9595
fuzzInternalObject(t, version, item, seed)
96-
roundTrip(t, testapi.Codec(), item)
96+
roundTrip(t, testapi.Default.Codec(), item)
9797
}
9898
}
9999

0 commit comments

Comments
 (0)