Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-daemon/pkg/quota/size_test.go
2499 views
1
// Copyright (c) 2020 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 quota_test
6
7
import (
8
"encoding/json"
9
"fmt"
10
"testing"
11
12
"github.com/gitpod-io/gitpod/ws-daemon/pkg/quota"
13
)
14
15
func TestParseSize(t *testing.T) {
16
tests := []struct {
17
Input string
18
Expected quota.Size
19
Err error
20
}{
21
{"42", 42, nil},
22
{"42k", 42 * quota.Kilobyte, nil},
23
{"42m", 42 * quota.Megabyte, nil},
24
{"42g", 42 * quota.Gigabyte, nil},
25
{"42t", 42 * quota.Terabyte, nil},
26
{"-42m", 0, quota.ErrInvalidSize},
27
{"ab-42mcd", 0, quota.ErrInvalidSize},
28
{"abc", 0, quota.ErrInvalidSize},
29
{"99999999999999999999999999999999999999999999999999999999999999999999999999999999", 0, quota.ErrInvalidSize},
30
}
31
32
for _, test := range tests {
33
t.Run(test.Input, func(t *testing.T) {
34
act, err := quota.ParseSize(test.Input)
35
if err != test.Err {
36
t.Errorf("unexepected error %v: expected %v", err, test.Err)
37
return
38
}
39
if act != test.Expected {
40
t.Errorf("unexpected result %v: expected %v", act, test.Expected)
41
}
42
})
43
}
44
}
45
46
func TestStringSize(t *testing.T) {
47
tests := []struct {
48
Input quota.Size
49
Expected string
50
}{
51
{42, "42"},
52
{42 * quota.Kilobyte, "42k"},
53
{42 * quota.Megabyte, "42m"},
54
{42 * quota.Gigabyte, "42g"},
55
{42 * quota.Terabyte, "42t"},
56
{0 * quota.Terabyte, "0"},
57
{0, "0"},
58
}
59
60
for _, test := range tests {
61
t.Run(test.Expected, func(t *testing.T) {
62
act := test.Input.String()
63
if act != test.Expected {
64
t.Errorf("unexpected result %v: expected %v", act, test.Expected)
65
}
66
})
67
68
t.Run(fmt.Sprintf("Parse((%s).String())", test.Expected), func(t *testing.T) {
69
act, err := quota.ParseSize(test.Input.String())
70
if err != nil {
71
t.Errorf("unexepected error: %v", err)
72
return
73
}
74
if act != test.Input {
75
t.Errorf("unexpected result %v: expected %v", act, test.Input)
76
}
77
})
78
}
79
}
80
81
func TestMarshalUnmarshal(t *testing.T) {
82
tests := []struct {
83
Input quota.Size `json:"input"`
84
Expected string `json:"expected"`
85
}{
86
{42, "\"42\""},
87
{42 * quota.Kilobyte, "\"42k\""},
88
{42 * quota.Megabyte, "\"42m\""},
89
{42 * quota.Gigabyte, "\"42g\""},
90
{42 * quota.Terabyte, "\"42t\""},
91
{0 * quota.Terabyte, "\"0\""},
92
{0, "\"0\""},
93
{0, "\"\""},
94
}
95
for _, test := range tests {
96
t.Run(fmt.Sprintf("json.marshal(%v)", test.Input), func(t *testing.T) {
97
if test.Expected == `""` {
98
return
99
}
100
out, err := json.Marshal(test.Input)
101
if err != nil {
102
t.Errorf("unexepected error: %v", err)
103
return
104
}
105
106
act := string(out)
107
if act != test.Expected {
108
t.Errorf("unexpected result %v: expected %v", act, test.Expected)
109
}
110
})
111
112
t.Run(fmt.Sprintf("json.unmarshal(%s)", test.Expected), func(t *testing.T) {
113
var out struct {
114
Q quota.Size `json:"q"`
115
}
116
err := json.Unmarshal([]byte(fmt.Sprintf(`{"q":%s}`, test.Expected)), &out)
117
if err != nil {
118
t.Errorf("unexepected error: %v", err)
119
return
120
}
121
122
act := out.Q
123
if act != test.Input {
124
t.Errorf("unexpected result %v: expected %v", act, test.Input)
125
}
126
})
127
}
128
}
129
130