Path: blob/main/components/usage/pkg/apiv1/pricer_test.go
2499 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package apiv156import (7"testing"8"time"910db "github.com/gitpod-io/gitpod/components/gitpod-db/go"11"github.com/google/go-cmp/cmp"12"github.com/google/go-cmp/cmp/cmpopts"13"github.com/stretchr/testify/require"14)1516func TestWorkspacePricer_Default(t *testing.T) {17const (18expectedCreditsPerMinute = float64(1) / 619expectedCreditsPerSecond = expectedCreditsPerMinute / 6020)2122testCases := []struct {23Name string24Seconds int6425ExpectedCredits float6426}{27{28Name: "0 seconds",29Seconds: 0,30ExpectedCredits: 0,31},32{33Name: "1 second",34Seconds: 1,35ExpectedCredits: 1 * expectedCreditsPerSecond,36},37{38Name: "60 seconds",39Seconds: 60,40ExpectedCredits: 1 * expectedCreditsPerMinute,41},42{43Name: "90 seconds",44Seconds: 90,45ExpectedCredits: 1.5 * expectedCreditsPerMinute,46},47{48Name: "6 minutes",49Seconds: 360,50ExpectedCredits: 6 * expectedCreditsPerMinute,51},52{53Name: "6 minutes and 1 second",54Seconds: 361,55ExpectedCredits: 6*expectedCreditsPerMinute + 1*expectedCreditsPerSecond,56},57{58Name: "1 hour",59Seconds: 3600,60ExpectedCredits: 60 * expectedCreditsPerMinute,61},62}6364for _, tc := range testCases {65t.Run(tc.Name, func(t *testing.T) {66actualCredits := DefaultWorkspacePricer.Credits("default", tc.Seconds)6768require.True(t, cmp.Equal(tc.ExpectedCredits, actualCredits, cmpopts.EquateApprox(0, 0.0000001)))69})70}71}7273func TestWorkspaceInstanceForUsage_WorkspaceRuntimeSeconds(t *testing.T) {74type Scenario struct {75Name string76Instance *db.WorkspaceInstanceForUsage77StopTimeIfStillRunning time.Time78ExpectedCredits float6479}8081for _, s := range []Scenario{82{83Name: "does not use stop time if still running if the instance stopped",84Instance: &db.WorkspaceInstanceForUsage{85WorkspaceClass: db.WorkspaceClass_Default,86StartedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),87StoppingTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 6, 0, 0, time.UTC)),88},89// Override is before actual stop time90StopTimeIfStillRunning: time.Date(2022, 9, 8, 11, 21, 29, 00, time.UTC),91ExpectedCredits: 1,92},93{94Name: "uses stop time when instance is not stopped",95Instance: &db.WorkspaceInstanceForUsage{96WorkspaceClass: db.WorkspaceClass_Default,97StartedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),98},99StopTimeIfStillRunning: time.Date(2022, 9, 8, 12, 12, 0, 00, time.UTC),100ExpectedCredits: 2,101},102{103Name: "uses creation time when stop time if still running is less than started time",104Instance: &db.WorkspaceInstanceForUsage{105WorkspaceClass: db.WorkspaceClass_Default,106StartedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),107},108StopTimeIfStillRunning: time.Date(2022, 9, 8, 11, 0, 0, 00, time.UTC),109ExpectedCredits: 0,110},111{112Name: "an errored instance that has no stopping time.",113Instance: &db.WorkspaceInstanceForUsage{114WorkspaceClass: db.WorkspaceClass_Default,115StartedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),116StoppedTime: db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)),117},118StopTimeIfStillRunning: time.Date(2022, 9, 8, 11, 0, 0, 00, time.UTC),119ExpectedCredits: 0,120},121} {122t.Run(s.Name, func(t *testing.T) {123credits := DefaultWorkspacePricer.CreditsUsedByInstance(s.Instance, s.StopTimeIfStillRunning)124require.Equal(t, s.ExpectedCredits, credits)125})126}127}128129130