Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/usage/pkg/apiv1/pricer_test.go
2499 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package apiv1
6
7
import (
8
"testing"
9
"time"
10
11
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
12
"github.com/google/go-cmp/cmp"
13
"github.com/google/go-cmp/cmp/cmpopts"
14
"github.com/stretchr/testify/require"
15
)
16
17
func TestWorkspacePricer_Default(t *testing.T) {
18
const (
19
expectedCreditsPerMinute = float64(1) / 6
20
expectedCreditsPerSecond = expectedCreditsPerMinute / 60
21
)
22
23
testCases := []struct {
24
Name string
25
Seconds int64
26
ExpectedCredits float64
27
}{
28
{
29
Name: "0 seconds",
30
Seconds: 0,
31
ExpectedCredits: 0,
32
},
33
{
34
Name: "1 second",
35
Seconds: 1,
36
ExpectedCredits: 1 * expectedCreditsPerSecond,
37
},
38
{
39
Name: "60 seconds",
40
Seconds: 60,
41
ExpectedCredits: 1 * expectedCreditsPerMinute,
42
},
43
{
44
Name: "90 seconds",
45
Seconds: 90,
46
ExpectedCredits: 1.5 * expectedCreditsPerMinute,
47
},
48
{
49
Name: "6 minutes",
50
Seconds: 360,
51
ExpectedCredits: 6 * expectedCreditsPerMinute,
52
},
53
{
54
Name: "6 minutes and 1 second",
55
Seconds: 361,
56
ExpectedCredits: 6*expectedCreditsPerMinute + 1*expectedCreditsPerSecond,
57
},
58
{
59
Name: "1 hour",
60
Seconds: 3600,
61
ExpectedCredits: 60 * expectedCreditsPerMinute,
62
},
63
}
64
65
for _, tc := range testCases {
66
t.Run(tc.Name, func(t *testing.T) {
67
actualCredits := DefaultWorkspacePricer.Credits("default", tc.Seconds)
68
69
require.True(t, cmp.Equal(tc.ExpectedCredits, actualCredits, cmpopts.EquateApprox(0, 0.0000001)))
70
})
71
}
72
}
73
74
func TestWorkspaceInstanceForUsage_WorkspaceRuntimeSeconds(t *testing.T) {
75
type Scenario struct {
76
Name string
77
Instance *db.WorkspaceInstanceForUsage
78
StopTimeIfStillRunning time.Time
79
ExpectedCredits float64
80
}
81
82
for _, s := range []Scenario{
83
{
84
Name: "does not use stop time if still running if the instance stopped",
85
Instance: &db.WorkspaceInstanceForUsage{
86
WorkspaceClass: db.WorkspaceClass_Default,
87
StartedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),
88
StoppingTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 6, 0, 0, time.UTC)),
89
},
90
// Override is before actual stop time
91
StopTimeIfStillRunning: time.Date(2022, 9, 8, 11, 21, 29, 00, time.UTC),
92
ExpectedCredits: 1,
93
},
94
{
95
Name: "uses stop time when instance is not stopped",
96
Instance: &db.WorkspaceInstanceForUsage{
97
WorkspaceClass: db.WorkspaceClass_Default,
98
StartedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),
99
},
100
StopTimeIfStillRunning: time.Date(2022, 9, 8, 12, 12, 0, 00, time.UTC),
101
ExpectedCredits: 2,
102
},
103
{
104
Name: "uses creation time when stop time if still running is less than started time",
105
Instance: &db.WorkspaceInstanceForUsage{
106
WorkspaceClass: db.WorkspaceClass_Default,
107
StartedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),
108
},
109
StopTimeIfStillRunning: time.Date(2022, 9, 8, 11, 0, 0, 00, time.UTC),
110
ExpectedCredits: 0,
111
},
112
{
113
Name: "an errored instance that has no stopping time.",
114
Instance: &db.WorkspaceInstanceForUsage{
115
WorkspaceClass: db.WorkspaceClass_Default,
116
StartedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),
117
StoppedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),
118
},
119
StopTimeIfStillRunning: time.Date(2022, 9, 8, 11, 0, 0, 00, time.UTC),
120
ExpectedCredits: 0,
121
},
122
} {
123
t.Run(s.Name, func(t *testing.T) {
124
credits := DefaultWorkspacePricer.CreditsUsedByInstance(s.Instance, s.StopTimeIfStillRunning)
125
require.Equal(t, s.ExpectedCredits, credits)
126
})
127
}
128
}
129
130