|
| 1 | +// Copyright 2025 SAP SE |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package kvm |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cobaltcore-dev/cortex/internal/conf" |
| 10 | + "github.com/cobaltcore-dev/cortex/internal/db" |
| 11 | + "github.com/cobaltcore-dev/cortex/internal/features/plugins/kvm" |
| 12 | + "github.com/cobaltcore-dev/cortex/internal/scheduler/plugins" |
| 13 | + testlibDB "github.com/cobaltcore-dev/cortex/testlib/db" |
| 14 | + testlibPlugins "github.com/cobaltcore-dev/cortex/testlib/scheduler/plugins" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAvoidOverloadedHostsStep_Run(t *testing.T) { |
| 18 | + dbEnv := testlibDB.SetupDBEnv(t) |
| 19 | + testDB := db.DB{DbMap: dbEnv.DbMap} |
| 20 | + defer testDB.Close() |
| 21 | + defer dbEnv.Close() |
| 22 | + |
| 23 | + // Create dependency tables |
| 24 | + err := testDB.CreateTable(testDB.AddTable(kvm.NodeExporterHostCPUUsage{})) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("expected no error, got %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + // Insert mock data into the feature_host_cpu_usage table |
| 30 | + _, err = testDB.Exec(` |
| 31 | + INSERT INTO feature_host_cpu_usage (compute_host, avg_cpu_usage, max_cpu_usage) |
| 32 | + VALUES |
| 33 | + ('host1', 15.0, 25.0), |
| 34 | + ('host2', 5.0, 10.0), |
| 35 | + ('host3', 20.0, 30.0) |
| 36 | + `) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("expected no error, got %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + // Create an instance of the step |
| 42 | + opts := conf.NewRawOpts(` |
| 43 | + avgCPUUsageThreshold: 10.0 |
| 44 | + maxCPUUsageThreshold: 20.0 |
| 45 | + activationOnHit: -1.0 |
| 46 | + `) |
| 47 | + step := &AvoidOverloadedHostsStep{} |
| 48 | + if err := step.Init(testDB, opts); err != nil { |
| 49 | + t.Fatalf("expected no error, got %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + scenario plugins.Scenario |
| 55 | + downvotedHosts map[string]struct{} |
| 56 | + }{ |
| 57 | + { |
| 58 | + name: "Non-vmware vm", |
| 59 | + scenario: &testlibPlugins.MockScenario{ |
| 60 | + VMware: false, |
| 61 | + Hosts: []testlibPlugins.MockScenarioHost{ |
| 62 | + {ComputeHost: "host1", HypervisorHostname: "hypervisor1"}, |
| 63 | + {ComputeHost: "host2", HypervisorHostname: "hypervisor2"}, |
| 64 | + {ComputeHost: "host3", HypervisorHostname: "hypervisor3"}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + // Should downvote hosts with high CPU usage |
| 68 | + downvotedHosts: map[string]struct{}{ |
| 69 | + "host1": {}, |
| 70 | + "host3": {}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "VMware vm", |
| 75 | + scenario: &testlibPlugins.MockScenario{ |
| 76 | + VMware: true, |
| 77 | + Hosts: []testlibPlugins.MockScenarioHost{ |
| 78 | + {ComputeHost: "host1", HypervisorHostname: "hypervisor1"}, |
| 79 | + {ComputeHost: "host2", HypervisorHostname: "hypervisor2"}, |
| 80 | + {ComputeHost: "host3", HypervisorHostname: "hypervisor3"}, |
| 81 | + }, |
| 82 | + }, |
| 83 | + // Should not do anything for VMware VMs |
| 84 | + downvotedHosts: map[string]struct{}{}, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "No overloaded hosts", |
| 88 | + scenario: &testlibPlugins.MockScenario{ |
| 89 | + VMware: false, |
| 90 | + Hosts: []testlibPlugins.MockScenarioHost{ |
| 91 | + {ComputeHost: "host4", HypervisorHostname: "hypervisor4"}, |
| 92 | + {ComputeHost: "host5", HypervisorHostname: "hypervisor5"}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + // Should not downvote any hosts |
| 96 | + downvotedHosts: map[string]struct{}{}, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for _, tt := range tests { |
| 101 | + t.Run(tt.name, func(t *testing.T) { |
| 102 | + weights, err := step.Run(tt.scenario) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("expected no error, got %v", err) |
| 105 | + } |
| 106 | + // Check that the weights have decreased |
| 107 | + for host, weight := range weights { |
| 108 | + if _, ok := tt.downvotedHosts[host]; ok { |
| 109 | + if weight >= 0 { |
| 110 | + t.Errorf("expected weight for host %s to be less than 0, got %f", host, weight) |
| 111 | + } |
| 112 | + } else { |
| 113 | + if weight != 0 { |
| 114 | + t.Errorf("expected weight for host %s to be 0, got %f", host, weight) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments