| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package prometheus |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/plugin/framework/chartengine" |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/framework/charttpl" |
| 10 | |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | func TestBuildChartTemplate(t *testing.T) { |
| 16 | tests := map[string]struct { |
| 17 | app string |
| 18 | wantNamespace string |
| 19 | }{ |
| 20 | "no app uses the prometheus namespace": { |
| 21 | app: "", |
| 22 | wantNamespace: "prometheus", |
| 23 | }, |
| 24 | "app is folded into the namespace with the separating dot": { |
| 25 | app: "myapp", |
| 26 | wantNamespace: "prometheus.myapp", |
| 27 | }, |
| 28 | } |
| 29 | |
| 30 | for name, tc := range tests { |
| 31 | t.Run(name, func(t *testing.T) { |
| 32 | out, err := buildChartTemplate(tc.app) |
| 33 | require.NoError(t, err) |
| 34 | |
| 35 | // Parse back through charttpl's own canonical decoder (yaml.v2 UnmarshalStrict, the path |
| 36 | // chartengine uses) so the round-trip is validated against the real template contract. |
| 37 | spec, err := charttpl.DecodeYAML([]byte(out)) |
| 38 | require.NoError(t, err) |
| 39 | |
| 40 | assert.Equal(t, charttpl.VersionV1, spec.Version) |
| 41 | assert.Equal(t, tc.wantNamespace, spec.ContextNamespace, "context_namespace drives the V1-parity chart context") |
| 42 | require.NotNil(t, spec.Engine) |
| 43 | require.NotNil(t, spec.Engine.Autogen) |
| 44 | assert.True(t, spec.Engine.Autogen.Enabled, "autogen must be enabled (no static charts)") |
| 45 | assert.Equal(t, uint64(chartExpireAfterCycles), spec.Engine.Autogen.ExpireAfterSuccessCycles, |
| 46 | "autogen chart expiry must mirror V1's 10-cycle stale removal") |
| 47 | require.Len(t, spec.Groups, 1, "a stub group satisfies the non-empty groups requirement") |
| 48 | assert.Equal(t, "prometheus", spec.Groups[0].Family) |
| 49 | |
| 50 | // chartengine must accept the generated template (compiles + publishes a revision). |
| 51 | eng, err := chartengine.New() |
| 52 | require.NoError(t, err) |
| 53 | require.NoError(t, eng.LoadYAML([]byte(out), 1)) |
| 54 | }) |
| 55 | } |
| 56 | } |