Skip to content

Commit af8309d

Browse files
author
Kai Kummerer
committed
Add tests for cache pkg
1 parent d840cf9 commit af8309d

File tree

4 files changed

+222
-34
lines changed

4 files changed

+222
-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=
@@ -138,6 +140,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
138140
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
139141
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
140142
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
143+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
141144
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
142145
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
143146
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
@@ -173,6 +176,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
173176
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
174177
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
175178
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
179+
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
176180
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
177181
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
178182
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=

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: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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 tt.expectedErr == os.ErrNotExist {
58+
if !os.IsNotExist(err) {
59+
t.Fatalf("expected err did not match ErrNotExist")
60+
}
61+
} else if !errors.Is(err, tt.expectedErr) {
62+
t.Fatalf("returned error (%q) does not match %q", err.Error(), tt.expectedErr.Error())
63+
}
64+
65+
if tt.expectFile {
66+
if len(file) < 1 {
67+
t.Fatalf("expected a file but byte array is empty (len %d)", len(file))
68+
}
69+
} else {
70+
if len(file) > 0 {
71+
t.Fatalf("didn't expect a file, but byte array is not empty (len %d)", len(file))
72+
}
73+
}
74+
})
75+
}
76+
}
77+
func TestPutObject(t *testing.T) {
78+
// Also test createFolderIfNotExists here
79+
tests := []struct {
80+
description string
81+
identifier string
82+
existingFile bool
83+
expectFile bool
84+
expectedErr error
85+
customPath string
86+
}{
87+
{
88+
description: "identifier already exists",
89+
identifier: "test-cache-put-exists",
90+
existingFile: true,
91+
expectFile: true,
92+
expectedErr: nil,
93+
},
94+
{
95+
description: "identifier does not exist",
96+
identifier: "test-cache-put-not-exists",
97+
expectFile: true,
98+
expectedErr: nil,
99+
},
100+
{
101+
description: "identifier is invalid",
102+
identifier: "in../../valid",
103+
expectFile: false,
104+
expectedErr: ErrorInvalidCacheIdentifier,
105+
},
106+
{
107+
description: "directory does not yet exist",
108+
identifier: "test-cache-put-folder-not-exists",
109+
expectFile: true,
110+
expectedErr: nil,
111+
customPath: "/tmp/stackit-cli-test",
112+
},
113+
}
114+
115+
for _, tt := range tests {
116+
t.Run(tt.description, func(t *testing.T) {
117+
id := tt.identifier + "-" + uuid.NewString()
118+
if tt.customPath != "" {
119+
cacheFolderPath = tt.customPath
120+
} else {
121+
cacheFolderPath = xdg.CacheHome
122+
}
123+
path := cacheFolderPath + "/" + id
124+
125+
// setup
126+
if tt.existingFile {
127+
if err := os.WriteFile(path, []byte("dummy"), 0o600); err != nil {
128+
t.Fatalf("setup: WriteFile (%s) failed", path)
129+
}
130+
}
131+
// test
132+
err := PutObject(id, []byte("dummy"))
133+
134+
if tt.expectedErr == os.ErrNotExist {
135+
if !os.IsNotExist(err) {
136+
t.Fatalf("expected err did not match ErrNotExist")
137+
}
138+
} else if !errors.Is(err, tt.expectedErr) {
139+
t.Fatalf("returned error (%q) does not match %q", err.Error(), tt.expectedErr.Error())
140+
}
141+
142+
if tt.expectFile {
143+
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
144+
t.Fatalf("expected file (%q) to exist", path)
145+
}
146+
}
147+
})
148+
}
149+
}
150+
151+
func TestDeleteObject(t *testing.T) {
152+
tests := []struct {
153+
description string
154+
identifier string
155+
existingFile bool
156+
expectedErr error
157+
}{
158+
{
159+
description: "identifier exists",
160+
identifier: "test-cache-delete-exists",
161+
existingFile: true,
162+
expectedErr: nil,
163+
},
164+
{
165+
description: "identifier does not exist",
166+
identifier: "test-cache-delete-not-exists",
167+
existingFile: false,
168+
expectedErr: nil,
169+
},
170+
{
171+
description: "identifier is invalid",
172+
identifier: "in../../valid",
173+
existingFile: false,
174+
expectedErr: ErrorInvalidCacheIdentifier,
175+
},
176+
}
177+
178+
for _, tt := range tests {
179+
t.Run(tt.description, func(t *testing.T) {
180+
id := tt.identifier + "-" + uuid.NewString()
181+
path := cacheFolderPath + "/" + id
182+
183+
// setup
184+
if tt.existingFile {
185+
if err := os.WriteFile(path, []byte("dummy"), 0o600); err != nil {
186+
t.Fatalf("setup: WriteFile (%s) failed", path)
187+
}
188+
}
189+
// test
190+
err := DeleteObject(id)
191+
192+
if tt.expectedErr == os.ErrNotExist {
193+
if !os.IsNotExist(err) {
194+
t.Fatalf("expected err did not match ErrNotExist")
195+
}
196+
} else if !errors.Is(err, tt.expectedErr) {
197+
t.Fatalf("returned error (%q) does not match %q", err.Error(), tt.expectedErr.Error())
198+
}
199+
200+
if tt.existingFile {
201+
if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {
202+
t.Fatalf("expected file (%q) to not exist", path)
203+
}
204+
}
205+
})
206+
}
207+
}

0 commit comments

Comments
 (0)