Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: João Vilaça <jvilaca@redhat.com>
- Loading branch information
1 parent
34d4b39
commit 72bf912
Showing
5 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package libmonitoring | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/machadovilaca/operator-observability/pkg/operatormetrics" | ||
"github.com/onsi/gomega/format" | ||
) | ||
|
||
type MetricMatcher struct { | ||
Metric operatormetrics.Metric | ||
} | ||
|
||
func (matcher *MetricMatcher) FailureMessage(actual interface{}) (message string) { | ||
return format.Message(actual, "to contain metric", matcher.Metric.GetOpts().Name) | ||
} | ||
|
||
func (matcher *MetricMatcher) NegatedFailureMessage(actual interface{}) (message string) { | ||
return format.Message(actual, "not to contain metric", matcher.Metric.GetOpts().Name) | ||
} | ||
|
||
func (matcher *MetricMatcher) Match(actual interface{}) (success bool, err error) { | ||
actualMetric, ok := actual.(promResult) | ||
if !ok { | ||
return false, fmt.Errorf("metric matcher requires a libmonitoring.PromResult") | ||
} | ||
|
||
actualName, ok := actualMetric.Metric["__name__"] | ||
if !ok { | ||
return false, fmt.Errorf("metric matcher requires a map with __name__ key") | ||
} | ||
|
||
nameToMatch := matcher.Metric.GetOpts().Name | ||
if matcher.Metric.GetType() == operatormetrics.HistogramType || matcher.Metric.GetType() == operatormetrics.HistogramVecType { | ||
nameToMatch = nameToMatch + "_bucket" | ||
} | ||
|
||
return actualName == nameToMatch, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package monitoring | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/machadovilaca/operator-observability/pkg/operatormetrics" | ||
"github.com/onsi/gomega/types" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
v1 "kubevirt.io/api/core/v1" | ||
"kubevirt.io/client-go/kubecli" | ||
|
||
virtapi "kubevirt.io/kubevirt/pkg/monitoring/metrics/virt-api" | ||
virtcontroller "kubevirt.io/kubevirt/pkg/monitoring/metrics/virt-controller" | ||
virtoperator "kubevirt.io/kubevirt/pkg/monitoring/metrics/virt-operator" | ||
"kubevirt.io/kubevirt/pkg/pointer" | ||
|
||
"kubevirt.io/kubevirt/tests/decorators" | ||
"kubevirt.io/kubevirt/tests/framework/kubevirt" | ||
"kubevirt.io/kubevirt/tests/libmonitoring" | ||
"kubevirt.io/kubevirt/tests/libvmi" | ||
"kubevirt.io/kubevirt/tests/testsuite" | ||
) | ||
|
||
var _ = FDescribe("[Serial][sig-monitoring]Metrics", Serial, decorators.SigMonitoring, func() { | ||
var virtClient kubecli.KubevirtClient | ||
var metrics *libmonitoring.QueryRequestResult | ||
var err error | ||
|
||
setupVM := func() { | ||
vmi := libvmi.NewCirros( | ||
libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding()), | ||
libvmi.WithNetwork(v1.DefaultPodNetwork()), | ||
) | ||
vmi.Namespace = testsuite.GetTestNamespace(nil) | ||
|
||
By("Creating a running VirtualMachine") | ||
vm := libvmi.NewVirtualMachine(vmi) | ||
vm.Spec.Running = pointer.P(true) | ||
_, err = virtClient.VirtualMachine(vm.Namespace).Create(context.Background(), vm) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
libmonitoring.WaitForMetricValue(virtClient, "kubevirt_number_of_vms", 1) | ||
|
||
By("Deleting the VirtualMachine") | ||
err = virtClient.VirtualMachine(vm.Namespace).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
libmonitoring.WaitForMetricValue(virtClient, "kubevirt_number_of_vms", -1) | ||
} | ||
|
||
BeforeEach(func() { | ||
virtClient = kubevirt.Client() | ||
|
||
setupVM() | ||
|
||
metrics, err = libmonitoring.FetchMetric(virtClient, "{__name__=~\"kubevirt_.*\"}") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(metrics.Status).To(Equal("success")) | ||
Expect(metrics.Data.ResultType).To(Equal("vector")) | ||
Expect(metrics.Data.Result).ToNot(BeEmpty(), "No metrics found") | ||
}) | ||
|
||
containsMetric := func(metric operatormetrics.Metric) types.GomegaMatcher { | ||
return &libmonitoring.MetricMatcher{Metric: metric} | ||
} | ||
|
||
Context("Prometheus metrics", func() { | ||
var excludedMetrics = map[string]bool{ | ||
// virt-api | ||
// can later be added in pre-existing feature tests | ||
"kubevirt_portforward_active_tunnels": true, | ||
"kubevirt_usbredir_active_connections": true, | ||
"kubevirt_vnc_active_connections": true, | ||
|
||
// virt-controller | ||
// needs a migration - ignoring since already tested in - VM Monitoring, VM migration metrics | ||
"kubevirt_vmi_migration_phase_transition_time_from_creation_seconds": true, | ||
"kubevirt_vmi_migrations_in_pending_phase": true, | ||
"kubevirt_vmi_migrations_in_scheduling_phase": true, | ||
"kubevirt_vmi_migrations_in_running_phase": true, | ||
"kubevirt_vmi_migration_succeeded": true, | ||
"kubevirt_vmi_migration_failed": true, | ||
} | ||
|
||
It("should contain virt-operator metrics", func() { | ||
err = virtoperator.SetupMetrics() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
for _, metric := range virtoperator.ListMetrics() { | ||
Expect(metrics.Data.Result).To(ContainElement(containsMetric(metric))) | ||
} | ||
}) | ||
|
||
It("should contain virt-api metrics", func() { | ||
err = virtapi.SetupMetrics() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
for _, metric := range virtapi.ListMetrics() { | ||
if excludedMetrics[metric.GetOpts().Name] { | ||
continue | ||
} | ||
|
||
Expect(metrics.Data.Result).To(ContainElement(containsMetric(metric))) | ||
} | ||
}) | ||
|
||
It("should contain virt-controller metrics", func() { | ||
err = virtcontroller.SetupMetrics(nil, nil, nil, nil, nil, nil, nil, nil) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
for _, metric := range virtcontroller.ListMetrics() { | ||
if excludedMetrics[metric.GetOpts().Name] { | ||
continue | ||
} | ||
|
||
Expect(metrics.Data.Result).To(ContainElement(containsMetric(metric))) | ||
} | ||
}) | ||
}) | ||
}) |