|
| 1 | +package apiserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | + |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/client-go/tools/cache" |
| 10 | + "k8s.io/utils/clock" |
| 11 | + |
| 12 | + configv1 "github.com/openshift/api/config/v1" |
| 13 | + configlistersv1 "github.com/openshift/client-go/config/listers/config/v1" |
| 14 | + "github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/configobservation" |
| 15 | + "github.com/openshift/library-go/pkg/operator/events" |
| 16 | +) |
| 17 | + |
| 18 | +func TestObserveGoawayChance(t *testing.T) { |
| 19 | + scenarios := []struct { |
| 20 | + name string |
| 21 | + existingKubeAPIConfig map[string]interface{} |
| 22 | + expectedKubeAPIConfig map[string]interface{} |
| 23 | + controlPlaneTopology configv1.TopologyMode |
| 24 | + }{ |
| 25 | + |
| 26 | + // scenario 1 - HA unset |
| 27 | + { |
| 28 | + name: "ha: defaults to 0.001", |
| 29 | + controlPlaneTopology: configv1.HighlyAvailableTopologyMode, |
| 30 | + expectedKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 31 | + "goaway-chance": []interface{}{"0.001"}, |
| 32 | + }}, |
| 33 | + }, |
| 34 | + |
| 35 | + // scenario 3 - HA, update not required |
| 36 | + { |
| 37 | + name: "ha: update not required", |
| 38 | + controlPlaneTopology: configv1.HighlyAvailableTopologyMode, |
| 39 | + existingKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 40 | + "goaway-chance": []interface{}{"0.001"}, |
| 41 | + }}, |
| 42 | + expectedKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 43 | + "goaway-chance": []interface{}{"0.001"}, |
| 44 | + }}, |
| 45 | + }, |
| 46 | + |
| 47 | + // scenario 4 - HA, update required |
| 48 | + { |
| 49 | + name: "ha: update required", |
| 50 | + controlPlaneTopology: configv1.HighlyAvailableTopologyMode, |
| 51 | + existingKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 52 | + "goaway-chance": []interface{}{"0.2"}, |
| 53 | + }}, |
| 54 | + expectedKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 55 | + "goaway-chance": []interface{}{"0.001"}, |
| 56 | + }}, |
| 57 | + }, |
| 58 | + |
| 59 | + // scenario 5 - SNO |
| 60 | + { |
| 61 | + name: "sno: defaults to 0", |
| 62 | + controlPlaneTopology: configv1.SingleReplicaTopologyMode, |
| 63 | + expectedKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 64 | + "goaway-chance": []interface{}{"0"}, |
| 65 | + }}, |
| 66 | + }, |
| 67 | + |
| 68 | + // scenario 6 - SNO, update required |
| 69 | + { |
| 70 | + name: "sno: update required", |
| 71 | + controlPlaneTopology: configv1.SingleReplicaTopologyMode, |
| 72 | + existingKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 73 | + "goaway-chance": []interface{}{"0.001"}, |
| 74 | + }}, |
| 75 | + expectedKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 76 | + "goaway-chance": []interface{}{"0"}, |
| 77 | + }}, |
| 78 | + }, |
| 79 | + |
| 80 | + // scenario 7 - SNO, update not required |
| 81 | + { |
| 82 | + name: "sno: update not required", |
| 83 | + controlPlaneTopology: configv1.SingleReplicaTopologyMode, |
| 84 | + existingKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 85 | + "goaway-chance": []interface{}{"0"}, |
| 86 | + }}, |
| 87 | + expectedKubeAPIConfig: map[string]interface{}{"apiServerArguments": map[string]interface{}{ |
| 88 | + "goaway-chance": []interface{}{"0"}, |
| 89 | + }}, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, scenario := range scenarios { |
| 94 | + t.Run(scenario.name, func(t *testing.T) { |
| 95 | + // test data |
| 96 | + eventRecorder := events.NewInMemoryRecorder("", clock.RealClock{}) |
| 97 | + infrastructureIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}) |
| 98 | + infrastructureIndexer.Add(&configv1.Infrastructure{ |
| 99 | + ObjectMeta: metav1.ObjectMeta{Name: "cluster"}, |
| 100 | + Status: configv1.InfrastructureStatus{ControlPlaneTopology: scenario.controlPlaneTopology}, |
| 101 | + }) |
| 102 | + listers := configobservation.Listers{ |
| 103 | + InfrastructureLister_: configlistersv1.NewInfrastructureLister(infrastructureIndexer), |
| 104 | + } |
| 105 | + |
| 106 | + // act |
| 107 | + observedKubeAPIConfig, err := ObserveGoawayChance(listers, eventRecorder, scenario.existingKubeAPIConfig) |
| 108 | + |
| 109 | + // validate |
| 110 | + if len(err) > 0 { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + if diff := cmp.Diff(scenario.expectedKubeAPIConfig, observedKubeAPIConfig); diff != "" { |
| 114 | + t.Fatalf("unexpected configuration, diff = %s", diff) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestObserveGoawayChanceErrors(t *testing.T) { |
| 121 | + scenarios := []struct { |
| 122 | + name string |
| 123 | + infraIndexerFunc func(cache.Indexer) |
| 124 | + expectedErrs []error |
| 125 | + }{ |
| 126 | + { |
| 127 | + name: "happy path", |
| 128 | + infraIndexerFunc: func(indexer cache.Indexer) { |
| 129 | + indexer.Add(&configv1.Infrastructure{ |
| 130 | + ObjectMeta: metav1.ObjectMeta{Name: "cluster"}, |
| 131 | + Status: configv1.InfrastructureStatus{ControlPlaneTopology: configv1.HighlyAvailableTopologyMode}, |
| 132 | + }) |
| 133 | + }, |
| 134 | + expectedErrs: nil, |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "no cluster infra", |
| 138 | + infraIndexerFunc: nil, |
| 139 | + expectedErrs: nil, |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + for _, scenario := range scenarios { |
| 144 | + t.Run(scenario.name, func(t *testing.T) { |
| 145 | + // test data |
| 146 | + eventRecorder := events.NewInMemoryRecorder("", clock.RealClock{}) |
| 147 | + infrastructureIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}) |
| 148 | + if scenario.infraIndexerFunc != nil { |
| 149 | + scenario.infraIndexerFunc(infrastructureIndexer) |
| 150 | + } |
| 151 | + listers := configobservation.Listers{ |
| 152 | + InfrastructureLister_: configlistersv1.NewInfrastructureLister(infrastructureIndexer), |
| 153 | + } |
| 154 | + |
| 155 | + // act |
| 156 | + _, errs := ObserveGoawayChance(listers, eventRecorder, map[string]interface{}{}) |
| 157 | + |
| 158 | + // validate |
| 159 | + if diff := cmp.Diff(scenario.expectedErrs, errs); diff != "" { |
| 160 | + t.Fatalf("unexpected errs, diff = %s", diff) |
| 161 | + } |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments