|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "github.com/appuio/appuio-cloud-agent/ratio" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + "k8s.io/apimachinery/pkg/api/resource" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/runtime" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | + ctrl "sigs.k8s.io/controller-runtime" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + |
| 18 | + "testing" |
| 19 | + |
| 20 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 21 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 22 | + "k8s.io/client-go/tools/record" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 24 | +) |
| 25 | + |
| 26 | +func TestRatioReconciler_Warn(t *testing.T) { |
| 27 | + recorder := record.NewFakeRecorder(4) |
| 28 | + _, err := prepareTest(t, testCfg{ |
| 29 | + limit: resource.MustParse("4G"), |
| 30 | + fetchMemory: resource.MustParse("4G"), |
| 31 | + fetchCPU: resource.MustParse("1100m"), |
| 32 | + recorder: recorder, |
| 33 | + obj: []client.Object{ |
| 34 | + testNs, |
| 35 | + testPod, |
| 36 | + }, |
| 37 | + }).Reconcile(context.TODO(), ctrl.Request{ |
| 38 | + NamespacedName: types.NamespacedName{ |
| 39 | + Namespace: testNs.Name, |
| 40 | + Name: testPod.Name, |
| 41 | + }, |
| 42 | + }) |
| 43 | + assert.NoError(t, err) |
| 44 | + require.Len(t, recorder.Events, 2) |
| 45 | +} |
| 46 | + |
| 47 | +func TestRatioReconciler_Ok(t *testing.T) { |
| 48 | + recorder := record.NewFakeRecorder(4) |
| 49 | + _, err := prepareTest(t, testCfg{ |
| 50 | + limit: resource.MustParse("4G"), |
| 51 | + fetchMemory: resource.MustParse("4G"), |
| 52 | + fetchCPU: resource.MustParse("900m"), |
| 53 | + recorder: recorder, |
| 54 | + obj: []client.Object{ |
| 55 | + testNs, |
| 56 | + testPod, |
| 57 | + }, |
| 58 | + }).Reconcile(context.TODO(), ctrl.Request{ |
| 59 | + NamespacedName: types.NamespacedName{ |
| 60 | + Namespace: testNs.Name, |
| 61 | + Name: testPod.Name, |
| 62 | + }, |
| 63 | + }) |
| 64 | + assert.NoError(t, err) |
| 65 | + require.Len(t, recorder.Events, 0) |
| 66 | +} |
| 67 | + |
| 68 | +func TestRatioReconciler_Disabled(t *testing.T) { |
| 69 | + recorder := record.NewFakeRecorder(4) |
| 70 | + _, err := prepareTest(t, testCfg{ |
| 71 | + limit: resource.MustParse("4G"), |
| 72 | + fetchErr: ratio.ErrorDisabled, |
| 73 | + recorder: recorder, |
| 74 | + obj: []client.Object{ |
| 75 | + testNs, |
| 76 | + testPod, |
| 77 | + }, |
| 78 | + }).Reconcile(context.TODO(), ctrl.Request{ |
| 79 | + NamespacedName: types.NamespacedName{ |
| 80 | + Namespace: testNs.Name, |
| 81 | + Name: testPod.Name, |
| 82 | + }, |
| 83 | + }) |
| 84 | + assert.NoError(t, err) |
| 85 | + require.Len(t, recorder.Events, 0) |
| 86 | +} |
| 87 | + |
| 88 | +func TestRatioReconciler_Failed(t *testing.T) { |
| 89 | + recorder := record.NewFakeRecorder(4) |
| 90 | + _, err := prepareTest(t, testCfg{ |
| 91 | + limit: resource.MustParse("4G"), |
| 92 | + fetchErr: errors.New("internal"), |
| 93 | + recorder: recorder, |
| 94 | + obj: []client.Object{ |
| 95 | + testNs, |
| 96 | + testPod, |
| 97 | + }, |
| 98 | + }).Reconcile(context.TODO(), ctrl.Request{ |
| 99 | + NamespacedName: types.NamespacedName{ |
| 100 | + Namespace: testNs.Name, |
| 101 | + Name: testPod.Name, |
| 102 | + }, |
| 103 | + }) |
| 104 | + assert.Error(t, err) |
| 105 | + require.Len(t, recorder.Events, 0) |
| 106 | +} |
| 107 | + |
| 108 | +func TestRatioReconciler_RecordFailed(t *testing.T) { |
| 109 | + wrongNs := *testNs |
| 110 | + wrongNs.Name = "bar" |
| 111 | + wrongPod := *testPod |
| 112 | + wrongPod.Name = "asf" |
| 113 | + wrongPod.Namespace = "asf" |
| 114 | + recorder := record.NewFakeRecorder(4) |
| 115 | + _, err := prepareTest(t, testCfg{ |
| 116 | + limit: resource.MustParse("4G"), |
| 117 | + fetchMemory: resource.MustParse("4G"), |
| 118 | + fetchCPU: resource.MustParse("1100m"), |
| 119 | + recorder: recorder, |
| 120 | + obj: []client.Object{ |
| 121 | + &wrongNs, |
| 122 | + &wrongPod, |
| 123 | + }, |
| 124 | + }).Reconcile(context.TODO(), ctrl.Request{ |
| 125 | + NamespacedName: types.NamespacedName{ |
| 126 | + Namespace: testNs.Name, |
| 127 | + Name: testPod.Name, |
| 128 | + }, |
| 129 | + }) |
| 130 | + assert.NoError(t, err) |
| 131 | + if !assert.Len(t, recorder.Events, 0) { |
| 132 | + for i := 0; i < len(recorder.Events); i++ { |
| 133 | + e := <-recorder.Events |
| 134 | + t.Log(e) |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +type testCfg struct { |
| 140 | + limit resource.Quantity |
| 141 | + fetchErr error |
| 142 | + fetchCPU resource.Quantity |
| 143 | + fetchMemory resource.Quantity |
| 144 | + obj []client.Object |
| 145 | + recorder record.EventRecorder |
| 146 | +} |
| 147 | + |
| 148 | +func prepareTest(t *testing.T, cfg testCfg) *RatioReconciler { |
| 149 | + scheme := runtime.NewScheme() |
| 150 | + utilruntime.Must(clientgoscheme.AddToScheme(scheme)) |
| 151 | + |
| 152 | + client := fake.NewClientBuilder(). |
| 153 | + WithScheme(scheme). |
| 154 | + WithObjects(cfg.obj...). |
| 155 | + Build() |
| 156 | + |
| 157 | + if cfg.recorder == nil { |
| 158 | + cfg.recorder = &record.FakeRecorder{} |
| 159 | + } |
| 160 | + |
| 161 | + return &RatioReconciler{ |
| 162 | + Client: client, |
| 163 | + Recorder: cfg.recorder, |
| 164 | + Scheme: scheme, |
| 165 | + Ratio: fakeFetcher{ |
| 166 | + err: cfg.fetchErr, |
| 167 | + ratio: &ratio.Ratio{ |
| 168 | + CPU: cfg.fetchCPU.AsDec(), |
| 169 | + Memory: cfg.fetchMemory.AsDec(), |
| 170 | + }, |
| 171 | + }, |
| 172 | + RatioLimit: &cfg.limit, |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +type fakeFetcher struct { |
| 177 | + err error |
| 178 | + ratio *ratio.Ratio |
| 179 | +} |
| 180 | + |
| 181 | +func (f fakeFetcher) FetchRatio(ctx context.Context, ns string) (*ratio.Ratio, error) { |
| 182 | + return f.ratio, f.err |
| 183 | +} |
| 184 | + |
| 185 | +var testNs = &corev1.Namespace{ |
| 186 | + ObjectMeta: metav1.ObjectMeta{ |
| 187 | + Name: "foo", |
| 188 | + }, |
| 189 | +} |
| 190 | +var testPod = &corev1.Pod{ |
| 191 | + ObjectMeta: metav1.ObjectMeta{ |
| 192 | + Name: "pod", |
| 193 | + Namespace: "foo", |
| 194 | + }, |
| 195 | +} |
0 commit comments