Files
cozystack/hack/values-injection/text.go
T
Myasnikov Daniil fb00301f0b test(hack): check chart output for arbitrary tenant values in unit-tests
hack/values-injection renders every chart under packages/apps and
packages/extra with generated values for each string field and map
key its schema allows, including the charts its HelmReleases install,
and fails when a value changes the structure of the output rather
than only its content. Fields multiline by design are listed in
hack/testdata/values-injection/allowlist.txt.

It runs as values-injection-check, a prerequisite of unit-tests, and
the Helm Charts conventions in docs/agents/overview.md gain the rule it
enforces.

Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-12-03 10:03:22 +00:00

204 lines
4.6 KiB
Go

package main
import (
"fmt"
"math"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// This check began as a Python script, and its reports, notes and finding
// details are kept identical to that script's, so a report from either can be
// diffed against the other. The helpers below carry the Python semantics where
// they decide a verdict or a reported string: line splitting on every Unicode
// line boundary, whitespace that includes the ASCII separators
// U+001C..U+001F, and repr() quoting.
// isSpace is Python's str.isspace for one rune.
func isSpace(r rune) bool {
return unicode.IsSpace(r) || (r >= 0x1c && r <= 0x1f)
}
func strip(s string) string {
return strings.TrimFunc(s, isSpace)
}
// splitLines is Python's str.splitlines: it also ends a line at \v, \f,
// U+001C..U+001E, U+0085, U+2028 and U+2029, and takes \r\n as one break.
func splitLines(s string) []string {
var out []string
start := 0
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
switch r {
case '\n', '\v', '\f', 0x1c, 0x1d, 0x1e, 0x85, 0x2028, 0x2029:
out = append(out, s[start:i])
i += size
start = i
case '\r':
out = append(out, s[start:i])
i += size
if i < len(s) && s[i] == '\n' {
i++
}
start = i
default:
i += size
}
}
if start < len(s) {
out = append(out, s[start:])
}
return out
}
// truncate keeps the first n characters, as s[:n] does on a Python str.
func truncate(s string, n int) string {
i := 0
for j := range s {
if i == n {
return s[:j]
}
i++
}
return s
}
// lower lowercases ASCII only. Every canary is ASCII, and no other rune
// lowercases to one of its characters, so a match is the same as Python's
// str.lower() finds, while byte offsets stay valid in the original string.
func lower(s string) string {
for i := 0; i < len(s); i++ {
if 'A' <= s[i] && s[i] <= 'Z' {
b := []byte(s)
for j := i; j < len(b); j++ {
if 'A' <= b[j] && b[j] <= 'Z' {
b[j] += 'a' - 'A'
}
}
return string(b)
}
}
return s
}
func containsFold(s, c string) bool {
return strings.Contains(lower(s), c)
}
// repr is Python's repr() of a string.
func repr(s string) string {
quote := byte('\'')
if strings.IndexByte(s, '\'') >= 0 && strings.IndexByte(s, '"') < 0 {
quote = '"'
}
var b strings.Builder
b.WriteByte(quote)
for _, r := range s {
switch {
case r == rune(quote) || r == '\\':
b.WriteByte('\\')
b.WriteRune(r)
case r == '\t':
b.WriteString(`\t`)
case r == '\n':
b.WriteString(`\n`)
case r == '\r':
b.WriteString(`\r`)
case r < ' ' || r == 0x7f:
fmt.Fprintf(&b, `\x%02x`, r)
case r < 0x7f || unicode.IsPrint(r):
b.WriteRune(r)
case r <= 0xff:
fmt.Fprintf(&b, `\x%02x`, r)
case r <= 0xffff:
fmt.Fprintf(&b, `\u%04x`, r)
default:
fmt.Fprintf(&b, `\U%08x`, r)
}
}
b.WriteByte(quote)
return b.String()
}
// isInt tells whether a number decoded from a manifest was an int to the
// Python script. Manifests reached it through Go's JSON encoder, which writes
// an integral float64 below 1e21 without a fraction or an exponent.
func isInt(f float64) bool {
return f == math.Trunc(f) && math.Abs(f) < 1e21
}
// pyStr is str() of a decoded JSON value, for the few that reach a report.
func pyStr(v any) string {
if s, ok := v.(string); ok {
return s
}
return pyRepr(v)
}
// pyRepr is repr() of a decoded JSON value.
func pyRepr(v any) string {
switch x := v.(type) {
case nil:
return "None"
case string:
return repr(x)
case bool:
if x {
return "True"
}
return "False"
case int:
return strconv.Itoa(x)
case float64:
if isInt(x) {
return strconv.FormatFloat(x, 'f', -1, 64)
}
return strconv.FormatFloat(x, 'g', -1, 64)
case fmt.Stringer:
return x.String()
case []any:
parts := make([]string, len(x))
for i, e := range x {
parts[i] = pyRepr(e)
}
return "[" + strings.Join(parts, ", ") + "]"
case map[string]any:
keys := sortedKeys(x)
parts := make([]string, len(keys))
for i, k := range keys {
parts[i] = repr(k) + ": " + pyRepr(x[k])
}
return "{" + strings.Join(parts, ", ") + "}"
}
return fmt.Sprint(v)
}
// bounded counts the occurrences of word not touching an ASCII letter or
// digit on either side, as re.findall with lookarounds does.
func bounded(text, word string) int {
alnum := func(b byte) bool {
return 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z' || '0' <= b && b <= '9'
}
n := 0
for i := 0; ; {
j := strings.Index(text[i:], word)
if j < 0 {
return n
}
j += i
end := j + len(word)
if (j == 0 || !alnum(text[j-1])) && (end == len(text) || !alnum(text[end])) {
n++
i = end
} else {
i = j + 1
}
if i > len(text) {
return n
}
}
}