Skip to content

Commit a77bd0e

Browse files
authored
test: lua folder implementation (#538)
* test lua folder implementation * add error when lua script is empty * add error when lua script is empty
1 parent 02a2af7 commit a77bd0e

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

pkg/manifests/template/helm.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (h *helm) luaValues(svc *console.ServiceDeploymentForAgent) (map[string]int
200200
return nil, valuesFiles, fmt.Errorf("no service found")
201201
}
202202

203-
if svc.Helm == nil || (svc.Helm.LuaScript == nil && svc.Helm.LuaFile == nil) {
203+
if svc.Helm == nil || (svc.Helm.LuaScript == nil && svc.Helm.LuaFile == nil && svc.Helm.LuaFolder == nil) {
204204
return newValues, valuesFiles, nil
205205
}
206206

@@ -228,16 +228,18 @@ func (h *helm) luaValues(svc *console.ServiceDeploymentForAgent) (map[string]int
228228
return nil, valuesFiles, fmt.Errorf("failed to read lua file %s: %w", *svc.Helm.LuaFile, err)
229229
}
230230
luaString = string(luaContents)
231-
default:
232-
return nil, valuesFiles, fmt.Errorf("no lua script or file provided")
233231
}
234232

235233
if svc.Helm.LuaFolder != nil && len(*svc.Helm.LuaFolder) > 0 {
236234
luaFolder, err := h.luaFolder(svc, *svc.Helm.LuaFolder)
237235
if err != nil {
238236
return nil, valuesFiles, err
239237
}
240-
luaString = luaFolder + "\n\n" + luaString
238+
luaString = luaFolder + luaString
239+
}
240+
241+
if luaString == "" {
242+
return nil, valuesFiles, fmt.Errorf("no lua script, file, or folder provided")
241243
}
242244

243245
// Execute the Lua script
@@ -323,7 +325,7 @@ func (h *helm) luaFolder(svc *console.ServiceDeploymentForAgent, folder string)
323325

324326
luaFileContents := make([]string, 0)
325327
for _, file := range luaFiles {
326-
luaContents, err := os.ReadFile(file)
328+
luaContents, err := os.ReadFile(filepath.Join(h.dir, file))
327329
if err != nil {
328330
return "", fmt.Errorf("failed to read lua file %s: %w", file, err)
329331
}

pkg/manifests/template/helm_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,28 @@ var _ = Describe("Helm template", Ordered, func() {
158158
Expect(ns.GetFinalizers()).To(HaveExactElements("a", "b", "c"))
159159

160160
})
161+
162+
It("should successfully read lua folder", func() {
163+
// check lua script when throw error
164+
dir := filepath.Join("..", "..", "..", "test", "helm", "lua-merge-empty")
165+
166+
svc.Helm = &console.ServiceDeploymentForAgent_Helm{
167+
IgnoreHooks: lo.ToPtr(false),
168+
}
169+
170+
svc.Helm.LuaFolder = lo.ToPtr("lua")
171+
172+
resp, err := NewHelm(dir).Render(svc, mapper)
173+
Expect(err).NotTo(HaveOccurred())
174+
Expect(len(resp)).To(Equal(3))
175+
var ns unstructured.Unstructured
176+
for _, i := range resp {
177+
if i.GetName() == "result" {
178+
ns = i
179+
}
180+
}
181+
Expect(ns.GetFinalizers()).To(HaveExactElements("a", "b", "c"))
182+
183+
})
161184
})
162185
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
values = {}
2+
3+
local baseStr = fs.read("templates/base.yaml")
4+
local patchStr = fs.read("templates/patch.yaml")
5+
local base = encoding.yamlDecode(baseStr)
6+
local patch = encoding.yamlDecode(patchStr)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
local result, err = utils.merge(base, patch)
2+
values["items"] = result["metadata"]["finalizers"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
values["items"] = result["metadata"]["finalizers"]

0 commit comments

Comments
 (0)