diff --git a/shell/detail/__tests__/autoscaling.horizontalpodautoscaler.test.ts b/shell/detail/__tests__/autoscaling.horizontalpodautoscaler.test.ts index ee82c5fa261..387c86fb444 100644 --- a/shell/detail/__tests__/autoscaling.horizontalpodautoscaler.test.ts +++ b/shell/detail/__tests__/autoscaling.horizontalpodautoscaler.test.ts @@ -194,4 +194,55 @@ describe('view: autoscaling.horizontalpodautoscaler', () => { }); }); }); + + describe('with malformed metrics:', () => { + const valueWithMalformedMetrics = { + status: { + currentMetrics: [], + }, + spec: { + metrics: [ + { + type: 'Pods', + // Missing 'pods' property - causes metricValue to be undefined + }, + { + type: 'External', + external: { + // Missing 'metric' property - metricValue.metric is undefined + target: { averageValue: 100, type: 'AverageValue' } + } + }, + { + type: 'Resource', + // Missing 'resource' property + } + ] + } + }; + + it('should not throw TypeError when metrics are malformed', () => { + expect(() => { + mount(HorizontalPodAutoScaler, { + props: { value: valueWithMalformedMetrics }, + global: { mocks, stubs }, + }); + }).not.toThrow(); + }); + + it('should gracefully handle malformed metrics with null values', () => { + const wrapper = mount(HorizontalPodAutoScaler, { + props: { value: valueWithMalformedMetrics }, + global: { mocks, stubs }, + }); + + expect(wrapper.exists()).toBe(true); + expect(wrapper.vm.mappedMetrics).toHaveLength(3); + + // Metric names should be null for malformed metrics + expect(wrapper.vm.mappedMetrics[0].metricName).toBeNull(); + expect(wrapper.vm.mappedMetrics[1].metricName).toBeNull(); + expect(wrapper.vm.mappedMetrics[2].metricName).toBeNull(); + }); + }); }); diff --git a/shell/detail/autoscaling.horizontalpodautoscaler/index.vue b/shell/detail/autoscaling.horizontalpodautoscaler/index.vue index e65596a7a00..862d15a0687 100644 --- a/shell/detail/autoscaling.horizontalpodautoscaler/index.vue +++ b/shell/detail/autoscaling.horizontalpodautoscaler/index.vue @@ -52,9 +52,9 @@ export default { // The format is different between 'Resource' metrics and others. // See for examples: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#appendix-horizontal-pod-autoscaler-status-conditions if (metricType !== 'resource') { - currentMatch = findBy(currentMetrics, `${ metricType }.metric.name`, metricValue.metric.name); + currentMatch = findBy(currentMetrics, `${ metricType }.metric.name`, metricValue?.metric?.name); } else { - currentMatch = findBy(currentMetrics, 'resource.name', metric.resource.name); + currentMatch = findBy(currentMetrics, 'resource.name', metric.resource?.name); } const current = currentMatch ? get(currentMatch, `${ camelCase(metric.type) }.current`) : null;