kubekey/pkg/modules/add_hostvars_test.go
liujian 48b7c3b34b
feat: check inventory when it's changed (#2691)
Signed-off-by: joyceliu <joyceliu@yunify.com>
2025-08-07 17:50:23 +08:00

119 lines
2.1 KiB
Go

package modules
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
)
func TestModuleAddHostvars(t *testing.T) {
type testcase struct {
name string
opt ExecOptions
expectStdout string
}
cases := []testcase{
{
name: "missing hosts",
opt: ExecOptions{
Host: "node1",
Variable: newTestVariable([]string{"node1"}, nil),
Args: runtime.RawExtension{
Raw: []byte(`
vars:
foo: bar
`),
},
},
expectStdout: StdoutFailed,
},
{
name: "missing vars",
opt: ExecOptions{
Host: "node1",
Variable: newTestVariable([]string{"node1"}, nil),
Args: runtime.RawExtension{
Raw: []byte(`
hosts: node1
`),
},
},
expectStdout: StdoutFailed,
},
{
name: "invalid hosts type",
opt: ExecOptions{
Host: "node1",
Variable: newTestVariable([]string{"node1"}, nil),
Args: runtime.RawExtension{
Raw: []byte(`
hosts:
foo: bar
vars:
a: b
`),
},
},
expectStdout: StdoutFailed,
},
{
name: "string value",
opt: ExecOptions{
Host: "node1",
Variable: newTestVariable([]string{"node1"}, nil),
Args: runtime.RawExtension{
Raw: []byte(`
hosts: node1
vars:
a: b
`),
},
},
expectStdout: StdoutSuccess,
},
{
name: "string var value",
opt: ExecOptions{
Host: "node1",
Variable: newTestVariable([]string{"node1"}, map[string]any{"a": "b"}),
Args: runtime.RawExtension{
Raw: []byte(`
hosts: node1
vars:
a: "{{ .a }}"
`),
},
},
expectStdout: StdoutSuccess,
},
{
name: "map value",
opt: ExecOptions{
Host: "node1",
Variable: newTestVariable([]string{"node1"}, nil),
Args: runtime.RawExtension{
Raw: []byte(`
hosts: node1
vars:
a:
b: c
`),
},
},
expectStdout: StdoutSuccess,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
stdout, _, _ := ModuleAddHostvars(ctx, tc.opt)
require.Equal(t, tc.expectStdout, stdout, "stdout mismatch")
})
}
}