|
| 1 | +/* |
| 2 | +Copyright (c) Microsoft Corporation. |
| 3 | +Licensed under the MIT license. |
| 4 | +*/ |
| 5 | + |
| 6 | +package defaulter |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "k8s.io/utils/ptr" |
| 13 | + |
| 14 | + fleetnetv1alpha1 "go.goms.io/fleet-networking/api/v1alpha1" |
| 15 | +) |
| 16 | + |
| 17 | +func TestSetTrafficManagerProfile(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + obj *fleetnetv1alpha1.TrafficManagerProfile |
| 21 | + want *fleetnetv1alpha1.TrafficManagerProfile |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "TrafficManagerProfile with nil spec", |
| 25 | + obj: &fleetnetv1alpha1.TrafficManagerProfile{ |
| 26 | + Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{}, |
| 27 | + }, |
| 28 | + want: &fleetnetv1alpha1.TrafficManagerProfile{ |
| 29 | + Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ |
| 30 | + MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ |
| 31 | + IntervalInSeconds: ptr.To(int64(30)), |
| 32 | + Path: ptr.To("/"), |
| 33 | + Port: ptr.To(int64(80)), |
| 34 | + Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTP), |
| 35 | + TimeoutInSeconds: ptr.To(int64(10)), |
| 36 | + ToleratedNumberOfFailures: ptr.To(int64(3)), |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "TrafficManagerProfile with 10 IntervalInSeconds and nil TimeoutInSeconds", |
| 43 | + obj: &fleetnetv1alpha1.TrafficManagerProfile{ |
| 44 | + Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ |
| 45 | + MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ |
| 46 | + IntervalInSeconds: ptr.To(int64(10)), |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + want: &fleetnetv1alpha1.TrafficManagerProfile{ |
| 51 | + Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ |
| 52 | + MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ |
| 53 | + IntervalInSeconds: ptr.To(int64(10)), |
| 54 | + Path: ptr.To("/"), |
| 55 | + Port: ptr.To(int64(80)), |
| 56 | + Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTP), |
| 57 | + TimeoutInSeconds: ptr.To(int64(9)), |
| 58 | + ToleratedNumberOfFailures: ptr.To(int64(3)), |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "TrafficManagerProfile with values", |
| 65 | + obj: &fleetnetv1alpha1.TrafficManagerProfile{ |
| 66 | + Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ |
| 67 | + MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ |
| 68 | + IntervalInSeconds: ptr.To(int64(10)), |
| 69 | + Path: ptr.To("/healthz"), |
| 70 | + Port: ptr.To(int64(8080)), |
| 71 | + Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTPS), |
| 72 | + TimeoutInSeconds: ptr.To(int64(9)), |
| 73 | + ToleratedNumberOfFailures: ptr.To(int64(4)), |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + want: &fleetnetv1alpha1.TrafficManagerProfile{ |
| 78 | + Spec: fleetnetv1alpha1.TrafficManagerProfileSpec{ |
| 79 | + MonitorConfig: &fleetnetv1alpha1.MonitorConfig{ |
| 80 | + IntervalInSeconds: ptr.To(int64(10)), |
| 81 | + Path: ptr.To("/healthz"), |
| 82 | + Port: ptr.To(int64(8080)), |
| 83 | + Protocol: ptr.To(fleetnetv1alpha1.TrafficManagerMonitorProtocolHTTPS), |
| 84 | + TimeoutInSeconds: ptr.To(int64(9)), |
| 85 | + ToleratedNumberOfFailures: ptr.To(int64(4)), |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + for _, tc := range tests { |
| 92 | + t.Run(tc.name, func(t *testing.T) { |
| 93 | + SetDefaultsTrafficManagerProfile(tc.obj) |
| 94 | + if diff := cmp.Diff(tc.want, tc.obj); diff != "" { |
| 95 | + t.Errorf("SetDefaultsTrafficManagerProfile() mismatch (-want +got):\n%s", diff) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments