kubectl set tests: remove testapi core dependency

This commit is contained in:
Sean Sullivan
2018-10-02 10:50:44 -07:00
parent 7bcdd8b55c
commit 69c0fb74df
5 changed files with 397 additions and 406 deletions

View File

@@ -22,7 +22,6 @@ import (
"io"
"io/ioutil"
"net/http"
"path"
"testing"
"github.com/stretchr/testify/assert"
@@ -30,7 +29,7 @@ import (
appsv1beta1 "k8s.io/api/apps/v1beta1"
appsv1beta2 "k8s.io/api/apps/v1beta2"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
@@ -40,7 +39,6 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions/resource"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/testapi"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/scheme"
)
@@ -85,7 +83,6 @@ func TestSetServiceAccountLocal(t *testing.T) {
cmd := NewCmdServiceAccount(tf, streams)
cmd.Flags().Set("output", outputFormat)
cmd.Flags().Set("local", "true")
testapi.Default = testapi.Groups[input.apiGroup]
saConfig := SetServiceAccountOptions{
PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme),
fileNameOptions: resource.FilenameOptions{
@@ -103,7 +100,6 @@ func TestSetServiceAccountLocal(t *testing.T) {
}
func TestSetServiceAccountMultiLocal(t *testing.T) {
testapi.Default = testapi.Groups[""]
tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
@@ -146,26 +142,26 @@ func TestSetServiceAccountMultiLocal(t *testing.T) {
func TestSetServiceAccountRemote(t *testing.T) {
inputs := []struct {
object runtime.Object
apiPrefix, apiGroup, apiVersion string
testAPIGroup string
args []string
object runtime.Object
groupVersion schema.GroupVersion
path string
args []string
}{
{
object: &extensionsv1beta1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "extensions", apiVersion: "v1beta1",
args: []string{"replicaset", "nginx", serviceAccount},
groupVersion: extensionsv1beta1.SchemeGroupVersion,
path: "/namespaces/test/replicasets/nginx",
args: []string{"replicaset", "nginx", serviceAccount},
},
{
object: &appsv1beta2.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
Spec: appsv1beta2.ReplicaSetSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx",
Image: "nginx",
@@ -175,17 +171,17 @@ func TestSetServiceAccountRemote(t *testing.T) {
},
},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta2",
args: []string{"replicaset", "nginx", serviceAccount},
groupVersion: appsv1beta2.SchemeGroupVersion,
path: "/namespaces/test/replicasets/nginx",
args: []string{"replicaset", "nginx", serviceAccount},
},
{
object: &appsv1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
Spec: appsv1.ReplicaSetSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx",
Image: "nginx",
@@ -195,65 +191,65 @@ func TestSetServiceAccountRemote(t *testing.T) {
},
},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1",
args: []string{"replicaset", "nginx", serviceAccount},
groupVersion: appsv1.SchemeGroupVersion,
path: "/namespaces/test/replicasets/nginx",
args: []string{"replicaset", "nginx", serviceAccount},
},
{
object: &extensionsv1beta1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "extensions", apiVersion: "v1beta1",
args: []string{"daemonset", "nginx", serviceAccount},
groupVersion: extensionsv1beta1.SchemeGroupVersion,
path: "/namespaces/test/daemonsets/nginx",
args: []string{"daemonset", "nginx", serviceAccount},
},
{
object: &appsv1beta2.DaemonSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta2",
args: []string{"daemonset", "nginx", serviceAccount},
groupVersion: appsv1beta2.SchemeGroupVersion,
path: "/namespaces/test/daemonsets/nginx",
args: []string{"daemonset", "nginx", serviceAccount},
},
{
object: &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1",
args: []string{"daemonset", "nginx", serviceAccount},
groupVersion: appsv1.SchemeGroupVersion,
path: "/namespaces/test/daemonsets/nginx",
args: []string{"daemonset", "nginx", serviceAccount},
},
{
object: &extensionsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "extensions", apiVersion: "v1beta1",
args: []string{"deployment", "nginx", serviceAccount},
groupVersion: extensionsv1beta1.SchemeGroupVersion,
path: "/namespaces/test/deployments/nginx",
args: []string{"deployment", "nginx", serviceAccount},
},
{
object: &appsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta1",
args: []string{"deployment", "nginx", serviceAccount},
groupVersion: appsv1beta1.SchemeGroupVersion,
path: "/namespaces/test/deployments/nginx",
args: []string{"deployment", "nginx", serviceAccount},
},
{
object: &appsv1beta2.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta2",
args: []string{"deployment", "nginx", serviceAccount},
groupVersion: appsv1beta2.SchemeGroupVersion,
path: "/namespaces/test/deployments/nginx",
args: []string{"deployment", "nginx", serviceAccount},
},
{
object: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
Spec: appsv1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx",
Image: "nginx",
@@ -263,33 +259,33 @@ func TestSetServiceAccountRemote(t *testing.T) {
},
},
},
testAPIGroup: "extensions",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1",
args: []string{"deployment", "nginx", serviceAccount},
groupVersion: appsv1.SchemeGroupVersion,
path: "/namespaces/test/deployments/nginx",
args: []string{"deployment", "nginx", serviceAccount},
},
{
object: &appsv1beta1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "apps",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta1",
args: []string{"statefulset", "nginx", serviceAccount},
groupVersion: appsv1beta1.SchemeGroupVersion,
path: "/namespaces/test/statefulsets/nginx",
args: []string{"statefulset", "nginx", serviceAccount},
},
{
object: &appsv1beta2.StatefulSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "apps",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta2",
args: []string{"statefulset", "nginx", serviceAccount},
groupVersion: appsv1beta2.SchemeGroupVersion,
path: "/namespaces/test/statefulsets/nginx",
args: []string{"statefulset", "nginx", serviceAccount},
},
{
object: &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
Spec: appsv1.StatefulSetSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx",
Image: "nginx",
@@ -299,43 +295,40 @@ func TestSetServiceAccountRemote(t *testing.T) {
},
},
},
testAPIGroup: "apps",
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1",
args: []string{"statefulset", "nginx", serviceAccount},
groupVersion: appsv1.SchemeGroupVersion,
path: "/namespaces/test/statefulsets/nginx",
args: []string{"statefulset", "nginx", serviceAccount},
},
{
object: &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "batch",
apiPrefix: "/apis", apiGroup: "batch", apiVersion: "v1",
args: []string{"job", "nginx", serviceAccount},
groupVersion: batchv1.SchemeGroupVersion,
path: "/namespaces/test/jobs/nginx",
args: []string{"job", "nginx", serviceAccount},
},
{
object: &v1.ReplicationController{
object: &corev1.ReplicationController{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
},
testAPIGroup: "",
apiPrefix: "/api", apiGroup: "", apiVersion: "v1",
args: []string{"replicationcontroller", "nginx", serviceAccount},
groupVersion: corev1.SchemeGroupVersion,
path: "/namespaces/test/replicationcontrollers/nginx",
args: []string{"replicationcontroller", "nginx", serviceAccount},
},
}
for _, input := range inputs {
t.Run(input.apiPrefix, func(t *testing.T) {
groupVersion := schema.GroupVersion{Group: input.apiGroup, Version: input.apiVersion}
testapi.Default = testapi.Groups[input.testAPIGroup]
for i, input := range inputs {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
tf.Client = &fake.RESTClient{
GroupVersion: groupVersion,
GroupVersion: input.groupVersion,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
resourcePath := testapi.Default.ResourcePath(input.args[0]+"s", "test", input.args[1])
switch p, m := req.URL.Path, req.Method; {
case p == resourcePath && m == http.MethodGet:
case p == input.path && m == http.MethodGet:
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(input.object)}, nil
case p == resourcePath && m == http.MethodPatch:
case p == input.path && m == http.MethodPatch:
stream, err := req.GetBody()
if err != nil {
return nil, err
@@ -351,7 +344,6 @@ func TestSetServiceAccountRemote(t *testing.T) {
return nil, fmt.Errorf("unexpected request")
}
}),
VersionedAPIPath: path.Join(input.apiPrefix, testapi.Default.GroupVersion().String()),
}
outputFormat := "yaml"