Path: blob/main/components/ws-daemon/pkg/quota/size_test.go
2499 views
// Copyright (c) 2020 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 quota_test56import (7"encoding/json"8"fmt"9"testing"1011"github.com/gitpod-io/gitpod/ws-daemon/pkg/quota"12)1314func TestParseSize(t *testing.T) {15tests := []struct {16Input string17Expected quota.Size18Err error19}{20{"42", 42, nil},21{"42k", 42 * quota.Kilobyte, nil},22{"42m", 42 * quota.Megabyte, nil},23{"42g", 42 * quota.Gigabyte, nil},24{"42t", 42 * quota.Terabyte, nil},25{"-42m", 0, quota.ErrInvalidSize},26{"ab-42mcd", 0, quota.ErrInvalidSize},27{"abc", 0, quota.ErrInvalidSize},28{"99999999999999999999999999999999999999999999999999999999999999999999999999999999", 0, quota.ErrInvalidSize},29}3031for _, test := range tests {32t.Run(test.Input, func(t *testing.T) {33act, err := quota.ParseSize(test.Input)34if err != test.Err {35t.Errorf("unexepected error %v: expected %v", err, test.Err)36return37}38if act != test.Expected {39t.Errorf("unexpected result %v: expected %v", act, test.Expected)40}41})42}43}4445func TestStringSize(t *testing.T) {46tests := []struct {47Input quota.Size48Expected string49}{50{42, "42"},51{42 * quota.Kilobyte, "42k"},52{42 * quota.Megabyte, "42m"},53{42 * quota.Gigabyte, "42g"},54{42 * quota.Terabyte, "42t"},55{0 * quota.Terabyte, "0"},56{0, "0"},57}5859for _, test := range tests {60t.Run(test.Expected, func(t *testing.T) {61act := test.Input.String()62if act != test.Expected {63t.Errorf("unexpected result %v: expected %v", act, test.Expected)64}65})6667t.Run(fmt.Sprintf("Parse((%s).String())", test.Expected), func(t *testing.T) {68act, err := quota.ParseSize(test.Input.String())69if err != nil {70t.Errorf("unexepected error: %v", err)71return72}73if act != test.Input {74t.Errorf("unexpected result %v: expected %v", act, test.Input)75}76})77}78}7980func TestMarshalUnmarshal(t *testing.T) {81tests := []struct {82Input quota.Size `json:"input"`83Expected string `json:"expected"`84}{85{42, "\"42\""},86{42 * quota.Kilobyte, "\"42k\""},87{42 * quota.Megabyte, "\"42m\""},88{42 * quota.Gigabyte, "\"42g\""},89{42 * quota.Terabyte, "\"42t\""},90{0 * quota.Terabyte, "\"0\""},91{0, "\"0\""},92{0, "\"\""},93}94for _, test := range tests {95t.Run(fmt.Sprintf("json.marshal(%v)", test.Input), func(t *testing.T) {96if test.Expected == `""` {97return98}99out, err := json.Marshal(test.Input)100if err != nil {101t.Errorf("unexepected error: %v", err)102return103}104105act := string(out)106if act != test.Expected {107t.Errorf("unexpected result %v: expected %v", act, test.Expected)108}109})110111t.Run(fmt.Sprintf("json.unmarshal(%s)", test.Expected), func(t *testing.T) {112var out struct {113Q quota.Size `json:"q"`114}115err := json.Unmarshal([]byte(fmt.Sprintf(`{"q":%s}`, test.Expected)), &out)116if err != nil {117t.Errorf("unexepected error: %v", err)118return119}120121act := out.Q122if act != test.Input {123t.Errorf("unexpected result %v: expected %v", act, test.Input)124}125})126}127}128129130