-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient_test.go
More file actions
170 lines (153 loc) · 5.06 KB
/
client_test.go
File metadata and controls
170 lines (153 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package stackit
import (
"fmt"
"os"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
stackitconfig "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/config"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/metadata"
)
var _ = Describe("Client", func() {
Describe("GetConfig", func() {
It("should parse valid yaml configuration", func() {
cfg, err := GetConfig(strings.NewReader(`
global:
projectId: "test-project"
iaasApi: "https://api.example.com"
region: "eu01"
apiEndpoints:
iaasApi: "https://api.example.com"
metadata:
searchOrder: "configDrive,metadataService"
requestTimeout: "5s"
blockStorage:
rescanOnResize: true`))
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Metadata).To(Equal(metadata.Opts{
SearchOrder: "configDrive,metadataService",
RequestTimeout: metadata.Duration{Duration: 5 * time.Second},
}))
Expect(cfg.BlockStorage).To(Equal(stackitconfig.BlockStorageOpts{
RescanOnResize: true,
}))
Expect(cfg.Global.ProjectID).To(Equal("test-project"))
Expect(cfg.Global.Region).To(Equal("eu01"))
Expect(cfg.Global.APIEndpoints.IaasAPI).To(Equal("https://api.example.com"))
})
It("should handle missing optional fields", func() {
cfg, err := GetConfig(strings.NewReader(`
global:
projectId: "test-project"
region: "eu01"`))
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Global.ProjectID).To(Equal("test-project"))
Expect(cfg.Global.Region).To(Equal("eu01"))
// Optional fields should be empty/zero values
Expect(cfg.Global.APIEndpoints.IaasAPI).To(BeEmpty())
})
It("should return error for invalid yaml", func() {
_, err := GetConfig(strings.NewReader(`
global:
projectId: "test-project"
region: "eu01"
invalid yaml syntax here [[[`))
Expect(err).To(HaveOccurred())
})
It("should handle empty yaml gracefully", func() {
cfg, err := GetConfig(strings.NewReader(``))
Expect(err).NotTo(HaveOccurred())
// Empty YAML should result in zero values
Expect(cfg).To(Equal(stackitconfig.CSIConfig{}))
})
})
Describe("GetConfigFromFile", func() {
var tempFile *os.File
var tempFilePath string
BeforeEach(func() {
var err error
tempFile, err = os.CreateTemp("", "test-stackitconfig-*.yaml")
Expect(err).NotTo(HaveOccurred())
tempFilePath = tempFile.Name()
})
AfterEach(func() {
if tempFile != nil {
tempFile.Close()
os.Remove(tempFilePath)
}
})
It("should read configuration from file", func() {
_, err := tempFile.WriteString(`
global:
projectId: "test-project"
iaasApi: "https://api.example.com"
region: "eu01"
metadata:
searchOrder: "configDrive,metadataService"
requestTimeout: "5s"
blockStorage:
rescanOnResize: true`)
Expect(err).NotTo(HaveOccurred())
tempFile.Close()
cfg, err := GetConfigFromFile(tempFilePath)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Global.ProjectID).To(Equal("test-project"))
})
It("should return error for non-existent file", func() {
_, err := GetConfigFromFile("non-existent-file.yaml")
Expect(err).To(HaveOccurred())
})
})
Describe("Metadata Duration Parsing", func() {
DescribeTable("should parse various duration formats",
func(durationStr string, expected time.Duration) {
yaml := fmt.Sprintf(`
global:
projectId: "test-project"
region: "eu01"
metadata:
requestTimeout: "%s"`, durationStr)
cfg, err := GetConfig(strings.NewReader(yaml))
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Metadata.RequestTimeout.Duration).To(Equal(expected))
},
Entry("seconds", "5s", 5*time.Second),
Entry("minutes", "1m", 1*time.Minute),
Entry("hours", "2h", 2*time.Hour),
Entry("milliseconds", "300ms", 300*time.Millisecond),
Entry("complex duration", "1h30m15s", 1*time.Hour+30*time.Minute+15*time.Second),
)
It("should return error for invalid duration format", func() {
yaml := `
global:
projectId: "test-project"
region: "eu01"
metadata:
requestTimeout: "invalid-duration"`
_, err := GetConfig(strings.NewReader(yaml))
Expect(err).To(HaveOccurred())
})
})
Describe("Metadata Search Order Validation", func() {
DescribeTable("should validate search order format",
func(searchOrder string, shouldError bool, errorMsg string) {
err := metadata.CheckMetadataSearchOrder(searchOrder)
if shouldError {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(errorMsg))
} else {
Expect(err).NotTo(HaveOccurred())
}
},
Entry("valid configDrive,metadataService", "configDrive,metadataService", false, ""),
Entry("valid metadataService,configDrive", "metadataService,configDrive", false, ""),
Entry("valid single configDrive", "configDrive", false, ""),
Entry("valid single metadataService", "metadataService", false, ""),
Entry("empty search order", "", true, "metadata.searchOrder"),
Entry("too many elements", "configDrive,metadataService,extra", true, "metadata.searchOrder"),
Entry("invalid element", "invalid", true, "metadata.searchOrder"),
Entry("mixed valid and invalid", "configDrive,invalid", true, "metadata.searchOrder"),
)
})
})