|
| 1 | +package expose |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | + "k8s.io/kubectl/pkg/cmd/expose" |
| 8 | + |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 11 | + |
| 12 | + routev1 "github.com/openshift/api/route/v1" |
| 13 | +) |
| 14 | + |
| 15 | +func TestExposeOptions_FinalizeRoute(t *testing.T) { |
| 16 | + route := &routev1.Route{ |
| 17 | + TypeMeta: metav1.TypeMeta{APIVersion: routev1.GroupVersion.String(), Kind: "Route"}, |
| 18 | + ObjectMeta: metav1.ObjectMeta{ |
| 19 | + Name: "example-api", |
| 20 | + Labels: map[string]string{ |
| 21 | + "app": "example", |
| 22 | + }, |
| 23 | + }, |
| 24 | + Spec: routev1.RouteSpec{ |
| 25 | + To: routev1.RouteTargetReference{ |
| 26 | + Name: "example-api", |
| 27 | + }, |
| 28 | + Port: &routev1.RoutePort{ |
| 29 | + TargetPort: intstr.FromInt32(8443), |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + newExpectedRoute := func(hook func(*routev1.Route)) *routev1.Route { |
| 35 | + r := route.DeepCopy() |
| 36 | + if hook != nil { |
| 37 | + hook(r) |
| 38 | + } |
| 39 | + return r |
| 40 | + } |
| 41 | + |
| 42 | + testCases := []struct { |
| 43 | + name string |
| 44 | + opts *ExposeOptions |
| 45 | + expectedRoute *routev1.Route |
| 46 | + expectedErrMsg string |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "happy path", |
| 50 | + opts: &ExposeOptions{ |
| 51 | + Hostname: "example.com", |
| 52 | + Path: "/api", |
| 53 | + WildcardPolicy: string(routev1.WildcardPolicyNone), |
| 54 | + }, |
| 55 | + expectedRoute: newExpectedRoute(func(r *routev1.Route) { |
| 56 | + r.Spec.Host = "example.com" |
| 57 | + r.Spec.Path = "/api" |
| 58 | + r.Spec.WildcardPolicy = routev1.WildcardPolicyNone |
| 59 | + }), |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "custom labels added", |
| 63 | + opts: &ExposeOptions{ |
| 64 | + ExposeServiceOptions: &expose.ExposeServiceOptions{ |
| 65 | + Labels: "label1=value1,label2=value2", |
| 66 | + }, |
| 67 | + }, |
| 68 | + expectedRoute: newExpectedRoute(func(r *routev1.Route) { |
| 69 | + r.ObjectMeta.Labels["label1"] = "value1" |
| 70 | + r.ObjectMeta.Labels["label2"] = "value2" |
| 71 | + }), |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "invalid labels option", |
| 75 | + opts: &ExposeOptions{ |
| 76 | + ExposeServiceOptions: &expose.ExposeServiceOptions{ |
| 77 | + Labels: "label1,label2", |
| 78 | + }, |
| 79 | + }, |
| 80 | + expectedRoute: newExpectedRoute(nil), |
| 81 | + expectedErrMsg: "unable to parse labels: unexpected label spec: label1", |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tc := range testCases { |
| 86 | + t.Run(tc.name, func(t *testing.T) { |
| 87 | + got := route.DeepCopy() |
| 88 | + err := tc.opts.finalizeRoute(got) |
| 89 | + if !cmp.Equal(got, tc.expectedRoute) { |
| 90 | + t.Errorf("Unexpected route:\n%s", cmp.Diff(tc.expectedRoute, got)) |
| 91 | + } |
| 92 | + |
| 93 | + var errMsg string |
| 94 | + if err != nil { |
| 95 | + errMsg = err.Error() |
| 96 | + } |
| 97 | + if tc.expectedErrMsg != errMsg { |
| 98 | + t.Errorf("Unexpected error: want = %q, got = %q", tc.expectedErrMsg, errMsg) |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
0 commit comments