|
| 1 | +package apiserver_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 9 | + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" |
| 10 | + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" |
| 11 | + "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/fake" |
| 12 | + "github.com/tektoncd/pipeline/pkg/reconciler/apiserver" |
| 13 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 16 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 17 | + ktesting "k8s.io/client-go/testing" |
| 18 | +) |
| 19 | + |
| 20 | +func TestDryRunCreate_Valid_DifferentGVKs(t *testing.T) { |
| 21 | + tcs := []struct { |
| 22 | + name string |
| 23 | + obj runtime.Object |
| 24 | + wantErr bool |
| 25 | + }{{ |
| 26 | + name: "v1 task", |
| 27 | + obj: &v1.Task{}, |
| 28 | + }, { |
| 29 | + name: "v1beta1 task", |
| 30 | + obj: &v1beta1.Task{}, |
| 31 | + }, { |
| 32 | + name: "v1 pipeline", |
| 33 | + obj: &v1.Pipeline{}, |
| 34 | + }, { |
| 35 | + name: "v1beta1 pipeline", |
| 36 | + obj: &v1beta1.Pipeline{}, |
| 37 | + }, { |
| 38 | + name: "unsupported gvk", |
| 39 | + obj: &v1beta1.ClusterTask{}, |
| 40 | + wantErr: true, |
| 41 | + }} |
| 42 | + for _, tc := range tcs { |
| 43 | + t.Run(tc.name, func(t *testing.T) { |
| 44 | + tektonclient := fake.NewSimpleClientset() |
| 45 | + err := apiserver.DryRunValidate(context.Background(), "default", tc.obj, tektonclient) |
| 46 | + if (err != nil) != tc.wantErr { |
| 47 | + t.Errorf("wantErr was %t but got err %v", tc.wantErr, err) |
| 48 | + } |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestDryRunCreate_Invalid_DifferentGVKs(t *testing.T) { |
| 54 | + tcs := []struct { |
| 55 | + name string |
| 56 | + obj runtime.Object |
| 57 | + wantErr error |
| 58 | + }{{ |
| 59 | + name: "v1 task", |
| 60 | + obj: &v1.Task{}, |
| 61 | + wantErr: apiserver.ErrReferencedObjectValidationFailed, |
| 62 | + }, { |
| 63 | + name: "v1beta1 task", |
| 64 | + obj: &v1beta1.Task{}, |
| 65 | + wantErr: apiserver.ErrReferencedObjectValidationFailed, |
| 66 | + }, { |
| 67 | + name: "v1 pipeline", |
| 68 | + obj: &v1.Pipeline{}, |
| 69 | + wantErr: apiserver.ErrReferencedObjectValidationFailed, |
| 70 | + }, { |
| 71 | + name: "v1beta1 pipeline", |
| 72 | + obj: &v1beta1.Pipeline{}, |
| 73 | + wantErr: apiserver.ErrReferencedObjectValidationFailed, |
| 74 | + }, { |
| 75 | + name: "unsupported gvk", |
| 76 | + obj: &v1beta1.ClusterTask{}, |
| 77 | + wantErr: cmpopts.AnyError, |
| 78 | + }} |
| 79 | + for _, tc := range tcs { |
| 80 | + t.Run(tc.name, func(t *testing.T) { |
| 81 | + tektonclient := fake.NewSimpleClientset() |
| 82 | + tektonclient.PrependReactor("create", "tasks", func(action ktesting.Action) (bool, runtime.Object, error) { |
| 83 | + return true, nil, apierrors.NewBadRequest("bad request") |
| 84 | + }) |
| 85 | + tektonclient.PrependReactor("create", "pipelines", func(action ktesting.Action) (bool, runtime.Object, error) { |
| 86 | + return true, nil, apierrors.NewBadRequest("bad request") |
| 87 | + }) |
| 88 | + err := apiserver.DryRunValidate(context.Background(), "default", tc.obj, tektonclient) |
| 89 | + if d := cmp.Diff(tc.wantErr, err, cmpopts.EquateErrors()); d != "" { |
| 90 | + t.Errorf("wrong error: %s", d) |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestDryRunCreate_DifferentErrTypes(t *testing.T) { |
| 97 | + tcs := []struct { |
| 98 | + name string |
| 99 | + webhookErr error |
| 100 | + wantErr error |
| 101 | + }{{ |
| 102 | + name: "no error", |
| 103 | + wantErr: nil, |
| 104 | + }, { |
| 105 | + name: "bad request", |
| 106 | + webhookErr: apierrors.NewBadRequest("bad request"), |
| 107 | + wantErr: apiserver.ErrReferencedObjectValidationFailed, |
| 108 | + }, { |
| 109 | + name: "invalid", |
| 110 | + webhookErr: apierrors.NewInvalid(schema.GroupKind{Group: "tekton.dev/v1", Kind: "Task"}, "task", field.ErrorList{}), |
| 111 | + wantErr: apiserver.ErrCouldntValidateObjectPermanent, |
| 112 | + }, { |
| 113 | + name: "not supported", |
| 114 | + webhookErr: apierrors.NewMethodNotSupported(schema.GroupResource{}, "create"), |
| 115 | + wantErr: apiserver.ErrCouldntValidateObjectPermanent, |
| 116 | + }, { |
| 117 | + name: "timeout", |
| 118 | + webhookErr: apierrors.NewTimeoutError("timeout", 5), |
| 119 | + wantErr: apiserver.ErrCouldntValidateObjectRetryable, |
| 120 | + }, { |
| 121 | + name: "server timeout", |
| 122 | + webhookErr: apierrors.NewServerTimeout(schema.GroupResource{}, "create", 5), |
| 123 | + wantErr: apiserver.ErrCouldntValidateObjectRetryable, |
| 124 | + }, { |
| 125 | + name: "too many requests", |
| 126 | + webhookErr: apierrors.NewTooManyRequests("foo", 5), |
| 127 | + wantErr: apiserver.ErrCouldntValidateObjectRetryable, |
| 128 | + }} |
| 129 | + for _, tc := range tcs { |
| 130 | + t.Run(tc.name, func(t *testing.T) { |
| 131 | + tektonclient := fake.NewSimpleClientset() |
| 132 | + tektonclient.PrependReactor("create", "tasks", func(action ktesting.Action) (bool, runtime.Object, error) { |
| 133 | + return true, nil, tc.webhookErr |
| 134 | + }) |
| 135 | + err := apiserver.DryRunValidate(context.Background(), "default", &v1.Task{}, tektonclient) |
| 136 | + if d := cmp.Diff(tc.wantErr, err, cmpopts.EquateErrors()); d != "" { |
| 137 | + t.Errorf("wrong error: %s", d) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments