github.com/kubeflow/training-operator@v1.7.0/pkg/controller.v1/control/pod_control_test.go (about) 1 // Copyright 2019 The Kubeflow Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package control 16 17 import ( 18 "encoding/json" 19 "net/http/httptest" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 corev1 "k8s.io/api/core/v1" 24 apiequality "k8s.io/apimachinery/pkg/api/equality" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/runtime" 27 clientset "k8s.io/client-go/kubernetes" 28 clientscheme "k8s.io/client-go/kubernetes/scheme" 29 restclient "k8s.io/client-go/rest" 30 "k8s.io/client-go/tools/record" 31 utiltesting "k8s.io/client-go/util/testing" 32 33 testutilv1 "github.com/kubeflow/training-operator/test_job/test_util/v1" 34 ) 35 36 func TestCreatePods(t *testing.T) { 37 ns := metav1.NamespaceDefault 38 body := runtime.EncodeOrDie( 39 clientscheme.Codecs.LegacyCodec(corev1.SchemeGroupVersion), 40 &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "empty_pod"}}) 41 fakeHandler := utiltesting.FakeHandler{ 42 StatusCode: 200, 43 ResponseBody: body, 44 } 45 testServer := httptest.NewServer(&fakeHandler) 46 defer testServer.Close() 47 k8sClient := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &corev1.SchemeGroupVersion}}) 48 49 podControl := RealPodControl{ 50 KubeClient: k8sClient, 51 Recorder: &record.FakeRecorder{}, 52 } 53 54 testJob := testutilv1.NewTestJob(1) 55 56 testName := "pod-name" 57 podTemplate := testutilv1.NewTestReplicaSpecTemplate() 58 podTemplate.Name = testName 59 podTemplate.Labels = testutilv1.GenLabels(testJob.Name) 60 podTemplate.SetOwnerReferences([]metav1.OwnerReference{}) 61 62 // Make sure createReplica sends a POST to the apiserver with a pod from the controllers pod template 63 err := podControl.CreatePods(ns, &podTemplate, testJob) 64 assert.NoError(t, err, "unexpected error: %v", err) 65 66 expectedPod := corev1.Pod{ 67 ObjectMeta: metav1.ObjectMeta{ 68 Labels: testutilv1.GenLabels(testJob.Name), 69 Name: testName, 70 }, 71 Spec: podTemplate.Spec, 72 } 73 fakeHandler.ValidateRequest(t, 74 "/api/v1/namespaces/default/pods", "POST", nil) 75 var actualPod = &corev1.Pod{} 76 err = json.Unmarshal([]byte(fakeHandler.RequestBody), actualPod) 77 assert.NoError(t, err, "unexpected error: %v", err) 78 assert.True(t, apiequality.Semantic.DeepDerivative(&expectedPod, actualPod), 79 "Body: %s", fakeHandler.RequestBody) 80 }