mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-22 20:00:22 +00:00
683809dc6b
Property Based Tests / api-test (push) Has been cancelled
Continuous Delivery / lint-and-build (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled
CI Pipeline / Lint Proto (push) Has been cancelled
Continuous Delivery / Build and Push Docker Images (push) Has been cancelled
CI Pipeline / lint-and-build (push) Has been cancelled
CI Pipeline / Test ${{ matrix.module }} (push) Has been cancelled
CI Pipeline / Upload Coverage (push) Has been cancelled
CI Pipeline / Detect Changes (push) Has been cancelled
Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package bootstrap_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/absmach/magistrala/bootstrap"
|
|
"github.com/absmach/magistrala/pkg/errors"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRendererStructuredOutputValidation(t *testing.T) {
|
|
renderer := bootstrap.NewRenderer()
|
|
|
|
cases := []struct {
|
|
desc string
|
|
format bootstrap.ContentFormat
|
|
template string
|
|
err error
|
|
}{
|
|
{
|
|
desc: "valid JSON output",
|
|
format: bootstrap.ContentFormatJSON,
|
|
template: `{"device_id":"{{ .Device.ID }}"}`,
|
|
},
|
|
{
|
|
desc: "invalid output for JSON format",
|
|
format: bootstrap.ContentFormatJSON,
|
|
template: `[unclosed bracket`,
|
|
err: bootstrap.ErrRenderFailed,
|
|
},
|
|
{
|
|
desc: "valid YAML output",
|
|
format: bootstrap.ContentFormatYAML,
|
|
template: "device_id: {{ .Device.ID }}",
|
|
},
|
|
{
|
|
desc: "invalid output for YAML format",
|
|
format: bootstrap.ContentFormatYAML,
|
|
template: "[unclosed bracket",
|
|
err: bootstrap.ErrRenderFailed,
|
|
},
|
|
{
|
|
desc: "valid TOML output",
|
|
format: bootstrap.ContentFormatTOML,
|
|
template: `device_id = "{{ .Device.ID }}"`,
|
|
},
|
|
{
|
|
desc: "invalid output for TOML format",
|
|
format: bootstrap.ContentFormatTOML,
|
|
template: `[unclosed bracket`,
|
|
err: bootstrap.ErrRenderFailed,
|
|
},
|
|
{
|
|
desc: "JSON template auto-converted to TOML",
|
|
format: bootstrap.ContentFormatTOML,
|
|
template: `{"device_id":"{{ .Device.ID }}"}`,
|
|
},
|
|
{
|
|
desc: "TOML template auto-converted to JSON",
|
|
format: bootstrap.ContentFormatJSON,
|
|
template: `device_id = "{{ .Device.ID }}"`,
|
|
},
|
|
{
|
|
desc: "YAML template auto-converted to TOML",
|
|
format: bootstrap.ContentFormatTOML,
|
|
template: "device_id: {{ .Device.ID }}",
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
_, err := renderer.Render(
|
|
bootstrap.Profile{
|
|
ContentFormat: tc.format,
|
|
ContentTemplate: tc.template,
|
|
},
|
|
bootstrap.Config{ID: "config-id"},
|
|
nil,
|
|
)
|
|
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.err, err))
|
|
})
|
|
}
|
|
}
|