-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm_test.go
96 lines (84 loc) · 2.16 KB
/
helm_test.go
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
// SPDX-FileCopyrightText: 2024 Karl Theil @karlderkaefer
// SPDX-FileContributor: Karl Theil @karlderkaefer
//
// SPDX-License-Identifier: MIT-Modern-Variant
package main
import (
"fmt"
"os/exec"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type TestCase struct {
name string
chartPath string
helmDepsSkipRefresh bool
expectedNumObjects int
}
func TestUpdateDependencies(t *testing.T) {
testCases := []TestCase{
{
name: "With Refresh",
chartPath: "charts/benchmark-subchart-level-1",
helmDepsSkipRefresh: false,
expectedNumObjects: 56,
},
{
name: "Without Refresh",
chartPath: "charts/benchmark-subchart-level-1",
helmDepsSkipRefresh: true,
expectedNumObjects: 56,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
config := &HelmUpdateConfig{}
if tc.helmDepsSkipRefresh {
config.SkipDepdencyRefresh = true
}
updater := HelmUpdater{
registryHelper: &RegistryHelper{
config: config,
},
config: config,
}
if tc.helmDepsSkipRefresh {
err := updater.registryHelper.UpdateRegistryInfo()
assert.NoError(t, err)
}
startTime := time.Now()
err := updater.UpdateChart(tc.chartPath)
duration := time.Since(startTime)
assert.NoError(t, err)
t.Logf("UpdateChart took %s", duration)
numberObject, err := countObjects(tc.chartPath)
assert.NoError(t, err)
assert.Equal(t, tc.expectedNumObjects, numberObject, "Number of objects in umbrella chart")
})
}
}
// render helm template of chart and count yaml objects
func countObjects(chartPath string) (int, error) {
output, err := runHelmTemplate(chartPath)
if err != nil {
return 0, err
}
yamlDocs := strings.Split(output, "\n---\n")
count := 0
for _, doc := range yamlDocs {
if strings.TrimSpace(doc) != "" {
count++
}
}
return count, nil
}
func runHelmTemplate(chartPath string) (string, error) {
cmd := exec.Command("helm", "template", chartPath)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to run helm template: %w", err)
}
return string(output), nil
}