|
| 1 | +package instance |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1" |
| 7 | + "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata" |
| 8 | + "k8s.io/utils/ptr" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/cloudnative-pg/cnpg-i/pkg/metrics" |
| 12 | + barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" |
| 13 | + . "github.com/onsi/ginkgo/v2" |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/runtime" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = Describe("Metrics Collect method", func() { |
| 22 | + const ( |
| 23 | + clusterName = "test-cluster" |
| 24 | + namespace = "test-ns" |
| 25 | + objectStoreName = "test-object-store" |
| 26 | + ) |
| 27 | + |
| 28 | + var ( |
| 29 | + fakeClient client.Client |
| 30 | + m metricsImpl |
| 31 | + ctx context.Context |
| 32 | + req *metrics.CollectMetricsRequest |
| 33 | + objectStore *barmancloudv1.ObjectStore |
| 34 | + ) |
| 35 | + |
| 36 | + BeforeEach(func() { |
| 37 | + ctx = context.Background() |
| 38 | + scheme := runtime.NewScheme() |
| 39 | + Expect(barmancloudv1.AddToScheme(scheme)).To(Succeed()) |
| 40 | + |
| 41 | + // Timestamps for the test |
| 42 | + firstRecoverabilityPoint := metav1.NewTime(time.Now().Add(-24 * time.Hour)) |
| 43 | + lastSuccessfulBackupTime := metav1.NewTime(time.Now()) |
| 44 | + |
| 45 | + // Create a fake ObjectStore with a status |
| 46 | + objectStore = &barmancloudv1.ObjectStore{ |
| 47 | + ObjectMeta: metav1.ObjectMeta{ |
| 48 | + Name: objectStoreName, |
| 49 | + Namespace: namespace, |
| 50 | + }, |
| 51 | + Status: barmancloudv1.ObjectStoreStatus{ |
| 52 | + ServerRecoveryWindow: map[string]barmancloudv1.RecoveryWindow{ |
| 53 | + clusterName: { |
| 54 | + FirstRecoverabilityPoint: &firstRecoverabilityPoint, |
| 55 | + LastSuccessfulBackupTime: &lastSuccessfulBackupTime, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + // Create a fake client with the ObjectStore |
| 62 | + fakeClient = fake.NewClientBuilder(). |
| 63 | + WithScheme(scheme). |
| 64 | + WithStatusSubresource(&barmancloudv1.ObjectStore{}). |
| 65 | + WithObjects(objectStore). |
| 66 | + Build() |
| 67 | + |
| 68 | + m = metricsImpl{Client: fakeClient} |
| 69 | + |
| 70 | + // Create a minimal cluster definition |
| 71 | + clusterDefinition := cnpgv1.Cluster{ |
| 72 | + ObjectMeta: metav1.ObjectMeta{ |
| 73 | + Name: clusterName, |
| 74 | + Namespace: namespace, |
| 75 | + }, |
| 76 | + Spec: cnpgv1.ClusterSpec{ |
| 77 | + Plugins: []cnpgv1.PluginConfiguration{ |
| 78 | + { |
| 79 | + Name: metadata.PluginName, |
| 80 | + Enabled: ptr.To(true), |
| 81 | + Parameters: map[string]string{ |
| 82 | + "serverName": clusterName, |
| 83 | + "barmanObjectName": objectStoreName, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + clusterJSON, err := json.Marshal(clusterDefinition) |
| 90 | + Expect(err).ToNot(HaveOccurred()) |
| 91 | + |
| 92 | + req = &metrics.CollectMetricsRequest{ |
| 93 | + ClusterDefinition: clusterJSON, |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + It("should collect metrics successfully", func() { |
| 98 | + res, err := m.Collect(ctx, req) |
| 99 | + Expect(err).ToNot(HaveOccurred()) |
| 100 | + Expect(res).ToNot(BeNil()) |
| 101 | + Expect(res.Metrics).To(HaveLen(3)) |
| 102 | + |
| 103 | + // Verify the metrics |
| 104 | + metricsMap := make(map[string]float64) |
| 105 | + for _, metric := range res.Metrics { |
| 106 | + metricsMap[metric.FqName] = metric.Value |
| 107 | + } |
| 108 | + |
| 109 | + // Check timestamp metrics |
| 110 | + expectedFirstPoint, _ := metricsMap[firstRecoverabilityPointMetricName] |
| 111 | + Expect(expectedFirstPoint).To(BeNumerically("~", float64(objectStore.Status.ServerRecoveryWindow[clusterName].FirstRecoverabilityPoint.Unix()), 1)) |
| 112 | + |
| 113 | + expectedLastBackup, _ := metricsMap[lastAvailableBackupTimestampMetricName] |
| 114 | + Expect(expectedLastBackup).To(BeNumerically("~", float64(objectStore.Status.ServerRecoveryWindow[clusterName].LastSuccessfulBackupTime.Unix()), 1)) |
| 115 | + }) |
| 116 | + |
| 117 | + It("should return an error if the object store is not found", func() { |
| 118 | + // Use a client without any objects |
| 119 | + m.Client = fake.NewClientBuilder().Build() |
| 120 | + _, err := m.Collect(ctx, req) |
| 121 | + Expect(err).To(HaveOccurred()) |
| 122 | + }) |
| 123 | +}) |
0 commit comments