mirror of
https://github.com/kubesphere/kubekey.git
synced 2025-12-26 01:22:51 +00:00
feat: add unit test for variable (#2502)
Signed-off-by: joyceliu <joyceliu@yunify.com> Co-authored-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
parent
86ff6371b6
commit
6cdcdd31d9
|
|
@ -50,7 +50,7 @@ var GetHostnames = func(name []string) GetFunc {
|
|||
}
|
||||
|
||||
// Add the specified host in the specified group to the hs.
|
||||
regexForIndex := regexp.MustCompile(`^(.*)\[\d\]$`)
|
||||
regexForIndex := regexp.MustCompile(`^(.*?)\[(\d+)\]$`)
|
||||
if match := regexForIndex.FindStringSubmatch(strings.TrimSpace(n)); match != nil {
|
||||
index, err := strconv.Atoi(match[2])
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -24,35 +24,131 @@ import (
|
|||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
func TestGetHostnames(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
hosts []string
|
||||
variable Variable
|
||||
except []string
|
||||
}{
|
||||
{
|
||||
name: "host value",
|
||||
hosts: []string{"n1"},
|
||||
variable: &variable{
|
||||
value: &value{
|
||||
Inventory: kkcorev1.Inventory{
|
||||
Spec: kkcorev1.InventorySpec{
|
||||
Hosts: map[string]runtime.RawExtension{
|
||||
"node1": {},
|
||||
"node2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
Hosts: map[string]host{
|
||||
"n1": {},
|
||||
"n2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
except: []string{"n1"},
|
||||
},
|
||||
{
|
||||
name: "group value",
|
||||
hosts: []string{"g1"},
|
||||
variable: &variable{
|
||||
value: &value{
|
||||
Inventory: kkcorev1.Inventory{
|
||||
Spec: kkcorev1.InventorySpec{
|
||||
Hosts: map[string]runtime.RawExtension{
|
||||
"n1": {},
|
||||
"n2": {},
|
||||
},
|
||||
Groups: map[string]kkcorev1.InventoryGroup{
|
||||
"g1": {
|
||||
Hosts: []string{"n1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Hosts: map[string]host{
|
||||
"n1": {},
|
||||
"n2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
except: []string{"n1"},
|
||||
},
|
||||
{
|
||||
name: "group index value",
|
||||
hosts: []string{"g1[0]"},
|
||||
variable: &variable{
|
||||
value: &value{
|
||||
Inventory: kkcorev1.Inventory{
|
||||
Spec: kkcorev1.InventorySpec{
|
||||
Hosts: map[string]runtime.RawExtension{
|
||||
"n1": {},
|
||||
"n2": {},
|
||||
},
|
||||
Groups: map[string]kkcorev1.InventoryGroup{
|
||||
"g1": {
|
||||
Hosts: []string{"n1", "n2"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Hosts: map[string]host{
|
||||
"n1": {},
|
||||
"n2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
except: []string{"n1"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result, err := tc.variable.Get(GetHostnames(tc.hosts))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.except, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllVariable(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
value *value
|
||||
except map[string]any
|
||||
name string
|
||||
variable Variable
|
||||
except map[string]any
|
||||
}{
|
||||
{
|
||||
name: "global override runtime variable",
|
||||
value: &value{
|
||||
Config: kkcorev1.Config{
|
||||
Spec: runtime.RawExtension{
|
||||
Raw: []byte(`{
|
||||
variable: &variable{
|
||||
value: &value{
|
||||
Config: kkcorev1.Config{
|
||||
Spec: runtime.RawExtension{
|
||||
Raw: []byte(`{
|
||||
"artifact": {
|
||||
"images": [ "abc" ]
|
||||
}
|
||||
}`)},
|
||||
},
|
||||
Inventory: kkcorev1.Inventory{
|
||||
Spec: kkcorev1.InventorySpec{
|
||||
Hosts: map[string]runtime.RawExtension{
|
||||
"localhost": {Raw: []byte(`{
|
||||
},
|
||||
Inventory: kkcorev1.Inventory{
|
||||
Spec: kkcorev1.InventorySpec{
|
||||
Hosts: map[string]runtime.RawExtension{
|
||||
"localhost": {Raw: []byte(`{
|
||||
"internal_ipv4": "127.0.0.1",
|
||||
"internal_ipv6": "::1"
|
||||
}`)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Hosts: map[string]host{
|
||||
"localhost": {},
|
||||
Hosts: map[string]host{
|
||||
"localhost": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
except: map[string]any{
|
||||
|
|
@ -81,9 +177,72 @@ func TestGetAllVariable(t *testing.T) {
|
|||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
v := variable{value: tc.value}
|
||||
|
||||
result, err := v.Get(GetAllVariable("localhost"))
|
||||
result, err := tc.variable.Get(GetAllVariable("localhost"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.except, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHostMaxLength(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
variable Variable
|
||||
except int
|
||||
}{
|
||||
{
|
||||
name: "length",
|
||||
variable: &variable{
|
||||
value: &value{
|
||||
Hosts: map[string]host{
|
||||
"n1": {},
|
||||
"n22": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
except: 3,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result, err := tc.variable.Get(GetHostMaxLength())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.except, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetWorkdir(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
variable Variable
|
||||
except string
|
||||
}{
|
||||
{
|
||||
name: "workdir",
|
||||
variable: &variable{
|
||||
value: &value{
|
||||
Config: kkcorev1.Config{
|
||||
Spec: runtime.RawExtension{
|
||||
Raw: []byte("{\"work_dir\": \"abc\"}"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
except: "abc",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result, err := tc.variable.Get(GetWorkDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,157 @@
|
|||
package variable
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/kubesphere/kubekey/v4/pkg/variable/source"
|
||||
)
|
||||
|
||||
func TestMergeRemoteVariable(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
variable *variable
|
||||
hostname string
|
||||
data map[string]any
|
||||
except value
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
variable: &variable{
|
||||
source: source.NewMemorySource(),
|
||||
value: &value{
|
||||
Hosts: map[string]host{
|
||||
"n1": {},
|
||||
"n2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
hostname: "n1",
|
||||
data: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
except: value{
|
||||
Hosts: map[string]host{
|
||||
"n1": {
|
||||
RemoteVars: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
},
|
||||
"n2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.variable.Merge(MergeRemoteVariable(tc.data, tc.hostname))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.except, *tc.variable.value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeRuntimeVariable(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
variable *variable
|
||||
hostname string
|
||||
data map[string]any
|
||||
except value
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
variable: &variable{
|
||||
source: source.NewMemorySource(),
|
||||
value: &value{
|
||||
Hosts: map[string]host{
|
||||
"n1": {},
|
||||
"n2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
hostname: "n1",
|
||||
data: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
except: value{
|
||||
Hosts: map[string]host{
|
||||
"n1": {
|
||||
RuntimeVars: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
},
|
||||
"n2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.variable.Merge(MergeRuntimeVariable(tc.data, tc.hostname))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.except, *tc.variable.value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeAllRuntimeVariable(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
variable *variable
|
||||
hostname string
|
||||
data map[string]any
|
||||
except value
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
variable: &variable{
|
||||
source: source.NewMemorySource(),
|
||||
value: &value{
|
||||
Hosts: map[string]host{
|
||||
"n1": {},
|
||||
"n2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
hostname: "n1",
|
||||
data: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
except: value{
|
||||
Hosts: map[string]host{
|
||||
"n1": {
|
||||
RuntimeVars: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
},
|
||||
"n2": {
|
||||
RuntimeVars: map[string]any{
|
||||
"k1": "k2",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.variable.Merge(MergeAllRuntimeVariable(tc.data, tc.hostname))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.except, *tc.variable.value)
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue