Skip to content

Commit 9c7d919

Browse files
committed
add tests
1 parent 20151ee commit 9c7d919

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package cells
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/mock"
9+
"github.com/stretchr/testify/require"
10+
cells_config "github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/cells/config"
11+
cells_storage "github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/cells/storage"
12+
storage_mocks "github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/cells/storage/mocks"
13+
"github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/common"
14+
"github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/types"
15+
metrics_mocks "github.com/ydb-platform/nbs/cloud/tasks/metrics/mocks"
16+
)
17+
18+
func TestCollectCellsMetricsTaskCollectZoneMetrics(t *testing.T) {
19+
ctx := context.Background()
20+
storage := storage_mocks.NewStorageMock()
21+
registry := metrics_mocks.NewRegistryMock()
22+
config := &cells_config.CellsConfig{
23+
Cells: map[string]*cells_config.ZoneCells{
24+
"zone-a": {Cells: []string{"zone-a-cell1", "zone-a"}},
25+
},
26+
}
27+
28+
task := collectCellsMetricsTask{
29+
storage: storage,
30+
registry: registry,
31+
config: config,
32+
}
33+
34+
collectedDiskKinds := []types.DiskKind{
35+
types.DiskKind_DISK_KIND_SSD,
36+
}
37+
38+
freeBytes := uint64(1024)
39+
totalBytes := uint64(2048)
40+
41+
createdAt := time.Now().Add(-time.Hour)
42+
43+
storage.On(
44+
"GetRecentClusterCapacities",
45+
ctx,
46+
"zone-a",
47+
types.DiskKind_DISK_KIND_SSD,
48+
).Return(
49+
[]cells_storage.ClusterCapacity{
50+
{
51+
ZoneID: "zone-a",
52+
CellID: "zone-a",
53+
FreeBytes: freeBytes,
54+
TotalBytes: totalBytes,
55+
CreatedAt: createdAt,
56+
},
57+
{
58+
ZoneID: "zone-a",
59+
CellID: "zone-a-cell1",
60+
FreeBytes: freeBytes * 2,
61+
TotalBytes: totalBytes * 2,
62+
CreatedAt: createdAt,
63+
},
64+
},
65+
nil,
66+
)
67+
68+
firstCellTags := map[string]string{
69+
"kind": common.DiskKindToString(types.DiskKind_DISK_KIND_SSD),
70+
"zone": "zone-a",
71+
"cell": "zone-a",
72+
}
73+
74+
secondCellTags := map[string]string{
75+
"kind": common.DiskKindToString(types.DiskKind_DISK_KIND_SSD),
76+
"zone": "zone-a",
77+
"cell": "zone-a-cell1",
78+
}
79+
80+
moreThanAnHour := mock.MatchedBy(func(value float64) bool {
81+
return value >= time.Hour.Seconds()
82+
})
83+
84+
registry.GetGauge(
85+
"free_bytes",
86+
firstCellTags,
87+
).On("Set", float64(freeBytes))
88+
registry.GetGauge(
89+
"total_bytes",
90+
firstCellTags,
91+
).On("Set", float64(totalBytes))
92+
registry.GetGauge(
93+
"time_since_last_update",
94+
firstCellTags,
95+
).On("Set", moreThanAnHour)
96+
97+
registry.GetGauge(
98+
"free_bytes",
99+
secondCellTags,
100+
).On("Set", float64(freeBytes*2))
101+
registry.GetGauge(
102+
"total_bytes",
103+
secondCellTags,
104+
).On("Set", float64(totalBytes*2))
105+
registry.GetGauge(
106+
"time_since_last_update",
107+
secondCellTags,
108+
).On("Set", moreThanAnHour)
109+
110+
err := task.collectZoneMetrics(ctx, "zone-a", collectedDiskKinds)
111+
require.NoError(t, err)
112+
113+
mock.AssertExpectationsForObjects(
114+
t,
115+
storage,
116+
)
117+
118+
registry.AssertAllExpectations(t)
119+
}

cloud/disk_manager/internal/pkg/cells/storage/storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (c *clusterCapacityState) toClusterCapacity() (ClusterCapacity, error) {
4747
Kind: kind,
4848
TotalBytes: c.TotalBytes,
4949
FreeBytes: c.FreeBytes,
50+
CreatedAt: c.CreatedAt,
5051
}, nil
5152
}
5253

cloud/disk_manager/internal/pkg/cells/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SRCS(
1010

1111
GO_TEST_SRCS(
1212
cells_test.go
13+
collect_cells_metrics_task_test.go
1314
collect_cluster_capacity_task_test.go
1415
)
1516

0 commit comments

Comments
 (0)