Skip to content

Commit 851c802

Browse files
author
Kai Kummerer
committed
Add tests for cache pkg
1 parent 48d0ac1 commit 851c802

File tree

4 files changed

+210
-34
lines changed

4 files changed

+210
-34
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/stackitcloud/stackit-cli
33
go 1.22
44

55
require (
6+
github.com/adrg/xdg v0.4.0
67
github.com/golang-jwt/jwt/v5 v5.2.1
78
github.com/google/go-cmp v0.6.0
89
github.com/google/uuid v1.6.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
2+
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
13
github.com/alessio/shellescape v1.4.2 h1:MHPfaU+ddJ0/bYWpgIeUnQUqKrlJ1S7BfEYPM4uEoM0=
24
github.com/alessio/shellescape v1.4.2/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
35
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
@@ -149,6 +151,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
149151
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
150152
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
151153
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
154+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
152155
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
153156
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
154157
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
@@ -184,6 +187,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
184187
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
185188
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
186189
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
190+
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
187191
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
188192
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
189193
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

internal/pkg/cache/cache.go

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,50 @@ import (
44
"errors"
55
"fmt"
66
"os"
7-
"path/filepath"
87
"regexp"
9-
)
108

11-
const (
12-
cacheFolder = ".stackit/cache"
9+
"github.com/adrg/xdg"
1310
)
1411

15-
var ErrorInvalidCacheIdentifier = fmt.Errorf("invalid cache identifier")
12+
var (
13+
cacheFolderPath = xdg.CacheHome + "/stackit"
1614

17-
var identifierRegex = regexp.MustCompile("^[a-zA-Z0-9-]+$")
15+
identifierRegex = regexp.MustCompile("^[a-zA-Z0-9-]+$")
16+
ErrorInvalidCacheIdentifier = fmt.Errorf("invalid cache identifier")
17+
)
1818

1919
func GetObject(identifier string) ([]byte, error) {
2020
if !identifierRegex.MatchString(identifier) {
2121
return nil, ErrorInvalidCacheIdentifier
2222
}
2323

24-
cacheFolderPath, err := getCacheFolderPath()
25-
if err != nil {
26-
return nil, err
27-
}
28-
29-
return os.ReadFile(cacheFolderPath + "/" + identifier + ".txt")
24+
return os.ReadFile(cacheFolderPath + "/" + identifier)
3025
}
3126

3227
func PutObject(identifier string, data []byte) error {
3328
if !identifierRegex.MatchString(identifier) {
3429
return ErrorInvalidCacheIdentifier
3530
}
3631

37-
cacheFolderPath, err := getCacheFolderPath()
38-
if err != nil {
39-
return err
40-
}
41-
42-
err = createFolderIfNotExists(cacheFolderPath)
32+
err := createFolderIfNotExists(cacheFolderPath)
4333
if err != nil {
4434
return err
4535
}
4636

47-
return os.WriteFile(cacheFolderPath+"/"+identifier+".txt", data, 0o600)
37+
return os.WriteFile(cacheFolderPath+"/"+identifier, data, 0o600)
4838
}
4939

5040
func DeleteObject(identifier string) error {
5141
if !identifierRegex.MatchString(identifier) {
5242
return ErrorInvalidCacheIdentifier
5343
}
5444

55-
cacheFolderPath, err := getCacheFolderPath()
56-
if err != nil {
57-
return err
58-
}
59-
60-
if err = os.Remove(cacheFolderPath + "/" + identifier + ".txt"); !errors.Is(err, os.ErrNotExist) {
45+
if err := os.Remove(cacheFolderPath + "/" + identifier); !errors.Is(err, os.ErrNotExist) {
6146
return err
6247
}
6348
return nil
6449
}
6550

66-
func getCacheFolderPath() (string, error) {
67-
home, err := os.UserHomeDir()
68-
if err != nil {
69-
return "", err
70-
}
71-
configFolderPath := filepath.Join(home, cacheFolder)
72-
return configFolderPath, nil
73-
}
74-
7551
func createFolderIfNotExists(folderPath string) error {
7652
_, err := os.Stat(folderPath)
7753
if os.IsNotExist(err) {

internal/pkg/cache/cache_test.go

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package cache
2+
3+
import (
4+
"errors"
5+
"os"
6+
"testing"
7+
8+
"github.com/adrg/xdg"
9+
"github.com/google/uuid"
10+
)
11+
12+
func TestGetObject(t *testing.T) {
13+
tests := []struct {
14+
description string
15+
identifier string
16+
expectFile bool
17+
expectedErr error
18+
}{
19+
{
20+
description: "identifier exists",
21+
identifier: "test-cache-get-exists",
22+
expectFile: true,
23+
expectedErr: nil,
24+
},
25+
{
26+
description: "identifier does not exist",
27+
identifier: "test-cache-get-not-exists",
28+
expectFile: false,
29+
expectedErr: os.ErrNotExist,
30+
},
31+
{
32+
description: "identifier is invalid",
33+
identifier: "in../../valid",
34+
expectFile: false,
35+
expectedErr: ErrorInvalidCacheIdentifier,
36+
},
37+
}
38+
39+
for _, tt := range tests {
40+
t.Run(tt.description, func(t *testing.T) {
41+
id := tt.identifier + "-" + uuid.NewString()
42+
43+
// setup
44+
if tt.expectFile {
45+
err := createFolderIfNotExists(cacheFolderPath)
46+
if err != nil {
47+
t.Fatalf("create cache folder: %s", err.Error())
48+
}
49+
path := cacheFolderPath + "/" + id
50+
if err := os.WriteFile(path, []byte("dummy"), 0o600); err != nil {
51+
t.Fatalf("setup: WriteFile (%s) failed", path)
52+
}
53+
}
54+
// test
55+
file, err := GetObject(id)
56+
57+
if !errors.Is(err, tt.expectedErr) {
58+
t.Fatalf("returned error (%q) does not match %q", err.Error(), tt.expectedErr.Error())
59+
}
60+
61+
if tt.expectFile {
62+
if len(file) < 1 {
63+
t.Fatalf("expected a file but byte array is empty (len %d)", len(file))
64+
}
65+
} else {
66+
if len(file) > 0 {
67+
t.Fatalf("didn't expect a file, but byte array is not empty (len %d)", len(file))
68+
}
69+
}
70+
})
71+
}
72+
}
73+
func TestPutObject(t *testing.T) {
74+
// Also test createFolderIfNotExists here
75+
tests := []struct {
76+
description string
77+
identifier string
78+
existingFile bool
79+
expectFile bool
80+
expectedErr error
81+
customPath string
82+
}{
83+
{
84+
description: "identifier already exists",
85+
identifier: "test-cache-put-exists",
86+
existingFile: true,
87+
expectFile: true,
88+
expectedErr: nil,
89+
},
90+
{
91+
description: "identifier does not exist",
92+
identifier: "test-cache-put-not-exists",
93+
expectFile: true,
94+
expectedErr: nil,
95+
},
96+
{
97+
description: "identifier is invalid",
98+
identifier: "in../../valid",
99+
expectFile: false,
100+
expectedErr: ErrorInvalidCacheIdentifier,
101+
},
102+
{
103+
description: "directory does not yet exist",
104+
identifier: "test-cache-put-folder-not-exists",
105+
expectFile: true,
106+
expectedErr: nil,
107+
customPath: "/tmp/stackit-cli-test",
108+
},
109+
}
110+
111+
for _, tt := range tests {
112+
t.Run(tt.description, func(t *testing.T) {
113+
id := tt.identifier + "-" + uuid.NewString()
114+
if tt.customPath != "" {
115+
cacheFolderPath = tt.customPath
116+
} else {
117+
cacheFolderPath = xdg.CacheHome
118+
}
119+
path := cacheFolderPath + "/" + id
120+
121+
// setup
122+
if tt.existingFile {
123+
if err := os.WriteFile(path, []byte("dummy"), 0o600); err != nil {
124+
t.Fatalf("setup: WriteFile (%s) failed", path)
125+
}
126+
}
127+
// test
128+
err := PutObject(id, []byte("dummy"))
129+
130+
if !errors.Is(err, tt.expectedErr) {
131+
t.Fatalf("returned error (%q) does not match %q", err.Error(), tt.expectedErr.Error())
132+
}
133+
134+
if tt.expectFile {
135+
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
136+
t.Fatalf("expected file (%q) to exist", path)
137+
}
138+
}
139+
})
140+
}
141+
}
142+
143+
func TestDeleteObject(t *testing.T) {
144+
tests := []struct {
145+
description string
146+
identifier string
147+
existingFile bool
148+
expectedErr error
149+
}{
150+
{
151+
description: "identifier exists",
152+
identifier: "test-cache-delete-exists",
153+
existingFile: true,
154+
expectedErr: nil,
155+
},
156+
{
157+
description: "identifier does not exist",
158+
identifier: "test-cache-delete-not-exists",
159+
existingFile: false,
160+
expectedErr: nil,
161+
},
162+
{
163+
description: "identifier is invalid",
164+
identifier: "in../../valid",
165+
existingFile: false,
166+
expectedErr: ErrorInvalidCacheIdentifier,
167+
},
168+
}
169+
170+
for _, tt := range tests {
171+
t.Run(tt.description, func(t *testing.T) {
172+
id := tt.identifier + "-" + uuid.NewString()
173+
path := cacheFolderPath + "/" + id
174+
175+
// setup
176+
if tt.existingFile {
177+
if err := os.WriteFile(path, []byte("dummy"), 0o600); err != nil {
178+
t.Fatalf("setup: WriteFile (%s) failed", path)
179+
}
180+
}
181+
// test
182+
err := DeleteObject(id)
183+
184+
if !errors.Is(err, tt.expectedErr) {
185+
t.Fatalf("returned error (%q) does not match %q", err.Error(), tt.expectedErr.Error())
186+
}
187+
188+
if tt.existingFile {
189+
if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {
190+
t.Fatalf("expected file (%q) to not exist", path)
191+
}
192+
}
193+
})
194+
}
195+
}

0 commit comments

Comments
 (0)