Skip to content

Commit 2f2f840

Browse files
committed
chore(test): added first tests for usage module
1 parent 89121dd commit 2f2f840

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

Taskfile.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ includes:
1414
REPO_URL: "https://github.com/openmcp-project/usage-operator"
1515
GENERATE_DOCS_INDEX: "true"
1616
CHART_COMPONENTS: "[]"
17+
ENVTEST_REQUIRED: "true"

internal/usage/db_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package usage
2+
3+
import (
4+
"os"
5+
"slices"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
"github.com/openmcp-project/controller-utils/pkg/logging"
10+
11+
k8s "sigs.k8s.io/controller-runtime/pkg/client"
12+
)
13+
14+
var _ = Describe("Database Module", func() {
15+
BeforeEach(func() {
16+
os.Setenv("USAGE_DB_PATH", "")
17+
})
18+
Context("When getting the Database", func() {
19+
It("should return a database entity", func() {
20+
_, err := GetDB()
21+
Ω(err).Should(Succeed())
22+
})
23+
})
24+
Context("When initializing the database", func() {
25+
It("should successfully initialize the database correctly", func(ctx SpecContext) {
26+
log, err := logging.GetLogger()
27+
Ω(err).Should(Succeed())
28+
29+
err = InitDB(ctx, &log)
30+
Ω(err).Should(Succeed())
31+
})
32+
It("should have created the tables correctly", func(ctx SpecContext) {
33+
db, err := GetDB()
34+
Ω(err).Should(Succeed())
35+
36+
query := "SELECT distinct(table_name) FROM information_schema.tables"
37+
rows, err := db.Query(query)
38+
Ω(err).Should(Succeed())
39+
40+
expectedTables := []string{"hourly_usage", "mcp"}
41+
for rows.Next() {
42+
var tableName string
43+
err = rows.Scan(&tableName)
44+
Ω(err).Should(Succeed())
45+
46+
Ω(len(expectedTables)).ShouldNot(Equal(0))
47+
expectedTables = slices.DeleteFunc(expectedTables, func(expected string) bool {
48+
return expected == tableName
49+
})
50+
}
51+
52+
err = db.Close()
53+
Ω(err).Should(Succeed())
54+
})
55+
56+
})
57+
58+
var resourceEntry HourlyUsageEntry
59+
var expectedResourceName = "test-project-test-workspace-test-mcp"
60+
Context("Database Entities", Ordered, func() {
61+
BeforeAll(func() {
62+
resourceEntry = HourlyUsageEntry{
63+
Project: "test-project",
64+
Workspace: "test-workspace",
65+
Name: "test-mcp",
66+
}
67+
})
68+
It("should return the correct ResourceName", func() {
69+
Ω(resourceEntry.ResourceName()).Should(Equal(expectedResourceName))
70+
})
71+
It("should return the correct ObjectKey", func() {
72+
Ω(resourceEntry.ObjectKey()).Should(Equal(k8s.ObjectKey{
73+
Name: expectedResourceName,
74+
}))
75+
})
76+
})
77+
})

internal/usage/helper.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type usagePerDay struct {
1010
const DAY = 24 * time.Hour
1111

1212
func calculateUsage(start time.Time, end time.Time) (result []usagePerDay) {
13+
start = start.UTC()
14+
end = end.UTC()
1315
duration := start.Sub(end).Abs()
1416
return _calculateUsage(start, end, duration)
1517
}

internal/usage/helper_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package usage
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
)
10+
11+
var _ = Describe("Helper Module", func() {
12+
Context("Usage calculation", func() {
13+
It("should calculate the usage for a 1 week span", func() {
14+
firstDayDuration := 23*time.Hour + 59*time.Minute + 59*time.Second
15+
16+
end := time.Now().UTC()
17+
endDate := end.Truncate(24 * time.Hour)
18+
end = endDate.Add(firstDayDuration)
19+
20+
start := end.Add(-7 * 24 * time.Hour)
21+
start = start.Truncate(24 * time.Hour)
22+
23+
result := calculateUsage(start, end)
24+
25+
Ω(len(result)).Should(Equal(8))
26+
27+
Ω(result[0].date.Equal(endDate)).Should(Equal(true), "first date must equal end date")
28+
Ω(result[0].duration).Should(Equal(firstDayDuration), "first day must have the right duration")
29+
for _, usage := range result[1:] {
30+
Ω(usage.duration).Should(Equal(24 * time.Hour))
31+
}
32+
})
33+
34+
It("reversed: should calculate the usage for a 1 week span", func() {
35+
firstDayDuration := 23*time.Hour + 59*time.Minute + 59*time.Second
36+
37+
end := time.Now().UTC()
38+
endDate := end.Truncate(24 * time.Hour)
39+
end = endDate.Add(firstDayDuration)
40+
41+
start := end.Add(-7 * 24 * time.Hour)
42+
start = start.Truncate(24 * time.Hour)
43+
44+
result := calculateUsage(end, start)
45+
46+
Ω(len(result)).Should(Equal(8))
47+
48+
Ω(result[0].date.Equal(endDate)).Should(Equal(true), "first date must equal end date")
49+
Ω(result[0].duration).Should(Equal(firstDayDuration), "first day must have the right duration")
50+
for _, usage := range result[1:] {
51+
fmt.Println(usage.date, usage.duration)
52+
Ω(usage.duration).Should(Equal(24 * time.Hour))
53+
}
54+
})
55+
})
56+
})

internal/usage/suite_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package usage
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
10+
logf "sigs.k8s.io/controller-runtime/pkg/log"
11+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
12+
)
13+
14+
var (
15+
ctx context.Context
16+
cancel context.CancelFunc
17+
)
18+
19+
func TestUsageModule(t *testing.T) {
20+
RegisterFailHandler(Fail)
21+
22+
RunSpecs(t, "Usage Module")
23+
}
24+
25+
var _ = BeforeSuite(func() {
26+
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
27+
28+
ctx, cancel = context.WithCancel(context.TODO())
29+
30+
})
31+
32+
var _ = AfterSuite(func() {
33+
cancel()
34+
})

0 commit comments

Comments
 (0)