Просмотр исходного кода

Extra test cases

Signed-off-by: Sean Holcomb <seanholcomb@gmail.com>
Sean Holcomb 2 дней назад
Родитель
Сommit
4ca8849464

+ 98 - 0
core/pkg/compute/kubemodel/pod_test.go

@@ -188,6 +188,104 @@ func TestComputePods(t *testing.T) {
 				},
 			},
 		},
+		{
+			name: "network egress: internet, cross-region, cross-zone traffic types",
+			overrides: map[string]any{
+				source.QueryPodInfo: []*source.PodInfoResult{
+					{UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
+				},
+				source.QueryPodUptime: []*source.UptimeResult{
+					{UID: "pod-1", First: start, Last: end},
+				},
+				source.QueryPodNetworkEgressBytes: []*source.PodNetworkBytesResult{
+					{UID: "pod-1", Internet: true, Service: "svc-a", Value: 100},
+					{UID: "pod-1", Internet: false, SameRegion: false, Service: "svc-b", Value: 200},
+					{UID: "pod-1", Internet: false, SameRegion: true, SameZone: false, Service: "svc-c", Value: 300},
+					{UID: "pod-1", Internet: false, SameRegion: true, SameZone: true, Service: "svc-d", Value: 400}, // no traffic type → skipped
+					{UID: "pod-1", Internet: true, Service: "svc-e", Value: 0},                                      // zero bytes → skipped
+					{UID: "unknown-pod", Internet: true, Service: "svc-f", Value: 500},                              // unknown pod → skipped
+				},
+			},
+			want: map[string]*kubemodel.Pod{
+				"pod-1": {
+					UID:          "pod-1",
+					Name:         "my-pod",
+					NamespaceUID: "ns-1",
+					Start:        start,
+					End:          end,
+					NetworkTrafficDetails: []kubemodel.NetworkTrafficDetail{
+						{PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionEgress, TrafficType: kubemodel.TrafficTypeInternet, Endpoint: "svc-a", Bytes: 100},
+						{PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionEgress, TrafficType: kubemodel.TrafficTypeCrossRegion, Endpoint: "svc-b", Bytes: 200},
+						{PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionEgress, TrafficType: kubemodel.TrafficTypeCrossZone, Endpoint: "svc-c", Bytes: 300},
+					},
+				},
+			},
+		},
+		{
+			name: "unknown uid in supplementary data is ignored",
+			overrides: map[string]any{
+				source.QueryPodInfo: []*source.PodInfoResult{
+					{UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
+				},
+				source.QueryPodUptime: []*source.UptimeResult{
+					{UID: "pod-1", First: start, Last: end},
+				},
+				// all of these carry an unknown UID — hit the !ok warn paths
+				source.QueryPodOwners: []*source.OwnerResult{
+					{UID: "unknown-pod", OwnerUID: "rs-1", OwnerKind: "ReplicaSet", Controller: true},
+				},
+				source.QueryPodPVCVolumes: []*source.PodPVCVolumeResult{
+					{UID: "unknown-pod", PVCUID: "pvc-1", PodVolumeName: "data"},
+				},
+				source.QueryPodLabels: []*source.PodLabelsResult{
+					{UID: "unknown-pod", Labels: map[string]string{"app": "web"}},
+				},
+				source.QueryPodAnnotations: []*source.PodAnnotationsResult{
+					{UID: "unknown-pod", Annotations: map[string]string{"team": "platform"}},
+				},
+				// same-zone+same-region ingress: triggers !ok continue in ingress loop
+				source.QueryPodNetworkIngressBytes: []*source.PodNetworkBytesResult{
+					{UID: "pod-1", Internet: false, SameRegion: true, SameZone: true, Value: 100},
+				},
+			},
+			want: map[string]*kubemodel.Pod{
+				"pod-1": {
+					UID:          "pod-1",
+					Name:         "my-pod",
+					NamespaceUID: "ns-1",
+					Start:        start,
+					End:          end,
+				},
+			},
+		},
+		{
+			name: "network ingress traffic is recorded",
+			overrides: map[string]any{
+				source.QueryPodInfo: []*source.PodInfoResult{
+					{UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
+				},
+				source.QueryPodUptime: []*source.UptimeResult{
+					{UID: "pod-1", First: start, Last: end},
+				},
+				source.QueryPodNetworkIngressBytes: []*source.PodNetworkBytesResult{
+					{UID: "pod-1", Internet: true, NatGateway: true, Service: "svc-a", Value: 512},
+					{UID: "pod-1", Internet: false, SameRegion: false, Service: "svc-b", Value: 256},
+				},
+			},
+			want: map[string]*kubemodel.Pod{
+				"pod-1": {
+					UID:          "pod-1",
+					Name:         "my-pod",
+					NamespaceUID: "ns-1",
+					Start:        start,
+					End:          end,
+					NetworkTrafficDetails: []kubemodel.NetworkTrafficDetail{
+						{PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionIngress, TrafficType: kubemodel.TrafficTypeInternet, IsNatGateway: true, Endpoint: "svc-a", Bytes: 512},
+						{PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionIngress, TrafficType: kubemodel.TrafficTypeCrossRegion, Endpoint: "svc-b", Bytes: 256},
+					},
+				},
+			},
+		},
 	}
 
 	for _, tt := range tests {

+ 172 - 0
core/pkg/compute/kubemodel/resourcequota_test.go

@@ -152,6 +152,178 @@ func TestComputeResourceQuotas(t *testing.T) {
 				},
 			},
 		},
+		{
+			name: "spec cpu and ram limits are populated",
+			overrides: map[string]any{
+				source.QueryResourceQuotaInfo: []*source.ResourceQuotaInfoResult{
+					{UID: "rq-1", ResourceQuota: "compute-quota", NamespaceUID: "ns-1"},
+				},
+				source.QueryResourceQuotaUptime: []*source.UptimeResult{
+					{UID: "rq-1", First: start, Last: end},
+				},
+				source.QueryResourceQuotaSpecCPULimitAverage: []*source.ResourceResult{
+					{UID: "rq-1", Value: 4.0},
+				},
+				source.QueryResourceQuotaSpecCPULimitMax: []*source.ResourceResult{
+					{UID: "rq-1", Value: 8.0},
+				},
+				source.QueryResourceQuotaSpecRAMLimitAverage: []*source.ResourceResult{
+					{UID: "rq-1", Value: 8 * 1024 * 1024 * 1024},
+				},
+				source.QueryResourceQuotaSpecRAMLimitMax: []*source.ResourceResult{
+					{UID: "rq-1", Value: 16 * 1024 * 1024 * 1024},
+				},
+			},
+			want: map[string]*kubemodel.ResourceQuota{
+				"rq-1": {
+					UID:          "rq-1",
+					Name:         "compute-quota",
+					NamespaceUID: "ns-1",
+					Start:        start,
+					End:          end,
+					Spec: &kubemodel.ResourceQuotaSpec{
+						Hard: &kubemodel.ResourceQuotaSpecHard{
+							Limits: kubemodel.ResourceQuantities{
+								kubemodel.ResourceCPU: {
+									Resource: kubemodel.ResourceCPU,
+									Unit:     kubemodel.UnitMillicore,
+									Values:   kubemodel.Stats{kubemodel.StatAvg: 4000, kubemodel.StatMax: 8000},
+								},
+								kubemodel.ResourceMemory: {
+									Resource: kubemodel.ResourceMemory,
+									Unit:     kubemodel.UnitByte,
+									Values:   kubemodel.Stats{kubemodel.StatAvg: 8 * 1024 * 1024 * 1024, kubemodel.StatMax: 16 * 1024 * 1024 * 1024},
+								},
+							},
+						},
+					},
+					Status: &kubemodel.ResourceQuotaStatus{Used: &kubemodel.ResourceQuotaStatusUsed{}},
+				},
+			},
+		},
+		{
+			name: "status used cpu and ram request max are populated",
+			overrides: map[string]any{
+				source.QueryResourceQuotaInfo: []*source.ResourceQuotaInfoResult{
+					{UID: "rq-1", ResourceQuota: "compute-quota", NamespaceUID: "ns-1"},
+				},
+				source.QueryResourceQuotaUptime: []*source.UptimeResult{
+					{UID: "rq-1", First: start, Last: end},
+				},
+				source.QueryResourceQuotaStatusUsedCPURequestMax: []*source.ResourceResult{
+					{UID: "rq-1", Value: 3.0},
+				},
+				source.QueryResourceQuotaStatusUsedRAMRequestMax: []*source.ResourceResult{
+					{UID: "rq-1", Value: 6 * 1024 * 1024 * 1024},
+				},
+			},
+			want: map[string]*kubemodel.ResourceQuota{
+				"rq-1": {
+					UID:          "rq-1",
+					Name:         "compute-quota",
+					NamespaceUID: "ns-1",
+					Start:        start,
+					End:          end,
+					Spec:         &kubemodel.ResourceQuotaSpec{Hard: &kubemodel.ResourceQuotaSpecHard{}},
+					Status: &kubemodel.ResourceQuotaStatus{
+						Used: &kubemodel.ResourceQuotaStatusUsed{
+							Requests: kubemodel.ResourceQuantities{
+								kubemodel.ResourceCPU: {
+									Resource: kubemodel.ResourceCPU,
+									Unit:     kubemodel.UnitMillicore,
+									Values:   kubemodel.Stats{kubemodel.StatMax: 3000},
+								},
+								kubemodel.ResourceMemory: {
+									Resource: kubemodel.ResourceMemory,
+									Unit:     kubemodel.UnitByte,
+									Values:   kubemodel.Stats{kubemodel.StatMax: 6 * 1024 * 1024 * 1024},
+								},
+							},
+						},
+					},
+				},
+			},
+		},
+		{
+			name: "status used cpu and ram limits are populated",
+			overrides: map[string]any{
+				source.QueryResourceQuotaInfo: []*source.ResourceQuotaInfoResult{
+					{UID: "rq-1", ResourceQuota: "compute-quota", NamespaceUID: "ns-1"},
+				},
+				source.QueryResourceQuotaUptime: []*source.UptimeResult{
+					{UID: "rq-1", First: start, Last: end},
+				},
+				source.QueryResourceQuotaStatusUsedCPULimitAverage: []*source.ResourceResult{
+					{UID: "rq-1", Value: 2.0},
+				},
+				source.QueryResourceQuotaStatusUsedCPULimitMax: []*source.ResourceResult{
+					{UID: "rq-1", Value: 4.0},
+				},
+				source.QueryResourceQuotaStatusUsedRAMLimitAverage: []*source.ResourceResult{
+					{UID: "rq-1", Value: 4 * 1024 * 1024 * 1024},
+				},
+				source.QueryResourceQuotaStatusUsedRAMLimitMax: []*source.ResourceResult{
+					{UID: "rq-1", Value: 8 * 1024 * 1024 * 1024},
+				},
+			},
+			want: map[string]*kubemodel.ResourceQuota{
+				"rq-1": {
+					UID:          "rq-1",
+					Name:         "compute-quota",
+					NamespaceUID: "ns-1",
+					Start:        start,
+					End:          end,
+					Spec:         &kubemodel.ResourceQuotaSpec{Hard: &kubemodel.ResourceQuotaSpecHard{}},
+					Status: &kubemodel.ResourceQuotaStatus{
+						Used: &kubemodel.ResourceQuotaStatusUsed{
+							Limits: kubemodel.ResourceQuantities{
+								kubemodel.ResourceCPU: {
+									Resource: kubemodel.ResourceCPU,
+									Unit:     kubemodel.UnitMillicore,
+									Values:   kubemodel.Stats{kubemodel.StatAvg: 2000, kubemodel.StatMax: 4000},
+								},
+								kubemodel.ResourceMemory: {
+									Resource: kubemodel.ResourceMemory,
+									Unit:     kubemodel.UnitByte,
+									Values:   kubemodel.Stats{kubemodel.StatAvg: 4 * 1024 * 1024 * 1024, kubemodel.StatMax: 8 * 1024 * 1024 * 1024},
+								},
+							},
+						},
+					},
+				},
+			},
+		},
+		{
+			name: "metric data for unknown resource quota is ignored",
+			overrides: map[string]any{
+				source.QueryResourceQuotaInfo: []*source.ResourceQuotaInfoResult{
+					{UID: "rq-1", ResourceQuota: "compute-quota", NamespaceUID: "ns-1"},
+				},
+				source.QueryResourceQuotaUptime: []*source.UptimeResult{
+					{UID: "rq-1", First: start, Last: end},
+				},
+				// all 16 metric queries carry an unknown UID — hit every !ok warn path
+				source.QueryResourceQuotaSpecCPURequestAverage:         []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaSpecCPURequestMax:              []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaSpecRAMRequestAverage:          []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaSpecRAMRequestMax:              []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaSpecCPULimitAverage:            []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaSpecCPULimitMax:                []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaSpecRAMLimitAverage:            []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaSpecRAMLimitMax:                []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaStatusUsedCPURequestAverage:    []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaStatusUsedCPURequestMax:        []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaStatusUsedRAMRequestAverage:    []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaStatusUsedRAMRequestMax:        []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaStatusUsedCPULimitAverage:      []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaStatusUsedCPULimitMax:          []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaStatusUsedRAMLimitAverage:      []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+				source.QueryResourceQuotaStatusUsedRAMLimitMax:          []*source.ResourceResult{{UID: "unknown-rq", Value: 1.0}},
+			},
+			want: map[string]*kubemodel.ResourceQuota{
+				"rq-1": emptyRQ("rq-1", "compute-quota", "ns-1"),
+			},
+		},
 		{
 			name: "uptime for unknown resource quota is ignored",
 			overrides: map[string]any{