kubekey/feature/pkg/variable/internal_test.go
github-actions[bot] fe55978234
Some checks failed
CI / Build (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
Sync feature branch to master/feature directory (#2400)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2024-09-11 09:48:38 +08:00

96 lines
2.2 KiB
Go

/*
Copyright 2023 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package variable
import (
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
kkcorev1 "github.com/kubesphere/kubekey/v4/pkg/apis/core/v1"
)
func TestGetAllVariable(t *testing.T) {
testcases := []struct {
name string
value *value
except map[string]any
}{
{
name: "global override runtime 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(`{
"internal_ipv4": "127.0.0.1",
"internal_ipv6": "::1"
}`)},
},
},
},
Hosts: map[string]host{
"localhost": {},
},
},
except: map[string]any{
"internal_ipv4": "127.0.0.1",
"internal_ipv6": "::1",
"artifact": map[string]any{
"images": []any{"abc"},
},
"groups": map[string]any{"all": []string{"localhost"}},
"inventory_hosts": map[string]any{
"localhost": map[string]any{
"internal_ipv4": "127.0.0.1",
"internal_ipv6": "::1",
"artifact": map[string]any{
"images": []any{"abc"},
},
"inventory_name": "localhost",
"hostname": "localhost",
},
},
"inventory_name": "localhost",
"hostname": "localhost",
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
v := variable{value: tc.value}
result, err := v.Get(GetAllVariable("localhost"))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tc.except, result)
})
}
}