|
| 1 | +package cluster |
| 2 | + |
| 3 | +// Copyright (c) Microsoft Corporation. |
| 4 | +// Licensed under the Apache License 2.0. |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "go.uber.org/mock/gomock" |
| 11 | + |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + |
| 14 | + configv1 "github.com/openshift/api/config/v1" |
| 15 | + configfake "github.com/openshift/client-go/config/clientset/versioned/fake" |
| 16 | + |
| 17 | + "github.com/Azure/ARO-RP/pkg/api" |
| 18 | + mock_metrics "github.com/Azure/ARO-RP/pkg/util/mocks/metrics" |
| 19 | + testlog "github.com/Azure/ARO-RP/test/util/log" |
| 20 | +) |
| 21 | + |
| 22 | +func TestEmitNetworkMTU(t *testing.T) { |
| 23 | + ctx := context.Background() |
| 24 | + |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + oc *api.OpenShiftCluster |
| 28 | + networkConfig *configv1.Network |
| 29 | + expectedMetric metricExpectation |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "MTU 1500 cluster with OpenShiftSDN", |
| 33 | + oc: &api.OpenShiftCluster{ |
| 34 | + Properties: api.OpenShiftClusterProperties{ |
| 35 | + NetworkProfile: api.NetworkProfile{ |
| 36 | + MTUSize: api.MTU1500, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + networkConfig: &configv1.Network{ |
| 41 | + ObjectMeta: metav1.ObjectMeta{ |
| 42 | + Name: "cluster", |
| 43 | + }, |
| 44 | + Spec: configv1.NetworkSpec{ |
| 45 | + NetworkType: "OpenShiftSDN", |
| 46 | + }, |
| 47 | + Status: configv1.NetworkStatus{ |
| 48 | + ClusterNetworkMTU: 1500, |
| 49 | + }, |
| 50 | + }, |
| 51 | + expectedMetric: metricExpectation{ |
| 52 | + name: "network.mtu", |
| 53 | + value: 1, |
| 54 | + labels: map[string]string{ |
| 55 | + "mtu": "1500", |
| 56 | + "network_type": "OpenShiftSDN", |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "MTU 1400 cluster with OVNKubernetes", |
| 62 | + oc: &api.OpenShiftCluster{ |
| 63 | + Properties: api.OpenShiftClusterProperties{ |
| 64 | + NetworkProfile: api.NetworkProfile{ |
| 65 | + MTUSize: api.MTU1500, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + networkConfig: &configv1.Network{ |
| 70 | + ObjectMeta: metav1.ObjectMeta{ |
| 71 | + Name: "cluster", |
| 72 | + }, |
| 73 | + Spec: configv1.NetworkSpec{ |
| 74 | + NetworkType: "OVNKubernetes", |
| 75 | + }, |
| 76 | + Status: configv1.NetworkStatus{ |
| 77 | + ClusterNetworkMTU: 1400, |
| 78 | + }, |
| 79 | + }, |
| 80 | + expectedMetric: metricExpectation{ |
| 81 | + name: "network.mtu", |
| 82 | + value: 1, |
| 83 | + labels: map[string]string{ |
| 84 | + "mtu": "1400", |
| 85 | + "network_type": "OVNKubernetes", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, tt := range tests { |
| 92 | + t.Run(tt.name, func(t *testing.T) { |
| 93 | + controller := gomock.NewController(t) |
| 94 | + m := mock_metrics.NewMockEmitter(controller) |
| 95 | + _, log := testlog.New() |
| 96 | + |
| 97 | + // Create fake config client with network config |
| 98 | + configcli := configfake.NewSimpleClientset(tt.networkConfig) |
| 99 | + |
| 100 | + mon := &Monitor{ |
| 101 | + oc: tt.oc, |
| 102 | + configcli: configcli, |
| 103 | + m: m, |
| 104 | + log: log, |
| 105 | + } |
| 106 | + |
| 107 | + // Set expectation for the MTU metric |
| 108 | + m.EXPECT().EmitGauge(tt.expectedMetric.name, tt.expectedMetric.value, tt.expectedMetric.labels) |
| 109 | + |
| 110 | + err := mon.emitNetworkMTU(ctx) |
| 111 | + if err != nil { |
| 112 | + t.Errorf("emitNetworkMTU() error = %v", err) |
| 113 | + } |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestEmitNetworkMTUError(t *testing.T) { |
| 119 | + ctx := context.Background() |
| 120 | + |
| 121 | + // Test case where network config is not found |
| 122 | + controller := gomock.NewController(t) |
| 123 | + m := mock_metrics.NewMockEmitter(controller) |
| 124 | + _, log := testlog.New() |
| 125 | + |
| 126 | + oc := &api.OpenShiftCluster{ |
| 127 | + Properties: api.OpenShiftClusterProperties{ |
| 128 | + NetworkProfile: api.NetworkProfile{ |
| 129 | + MTUSize: api.MTU1500, |
| 130 | + }, |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + // Empty client with no network config |
| 135 | + configcli := configfake.NewSimpleClientset() |
| 136 | + |
| 137 | + mon := &Monitor{ |
| 138 | + oc: oc, |
| 139 | + configcli: configcli, |
| 140 | + m: m, |
| 141 | + log: log, |
| 142 | + } |
| 143 | + |
| 144 | + err := mon.emitNetworkMTU(ctx) |
| 145 | + if err == nil { |
| 146 | + t.Error("expected error when network config is not found, got nil") |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// metricExpectation represents an expected metric emission |
| 151 | +type metricExpectation struct { |
| 152 | + name string |
| 153 | + value int64 |
| 154 | + labels map[string]string |
| 155 | +} |
0 commit comments