Formatting only (gofmt -w on the 29 non-vendored, non-generated files gofmt -l reported); swag-generated apiserver/docs/docs.go left as is. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
125 lines
3.5 KiB
Go
125 lines
3.5 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestClusterInfo_NewFields(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
cluster ClusterInfo
|
|
field string
|
|
want interface{}
|
|
}{
|
|
{
|
|
name: "HeartbeatInterval field exists and serializes",
|
|
cluster: ClusterInfo{ClusterName: "test", HeartbeatInterval: 30},
|
|
field: "heartbeat_interval",
|
|
want: float64(30),
|
|
},
|
|
{
|
|
name: "Vcpus field exists and serializes",
|
|
cluster: ClusterInfo{ClusterName: "test", Vcpus: 16},
|
|
field: "vcpus",
|
|
want: float64(16),
|
|
},
|
|
{
|
|
name: "InactiveHeartbeatCount field exists and serializes",
|
|
cluster: ClusterInfo{ClusterName: "test", InactiveHeartbeatCount: 3},
|
|
field: "inactive_heartbeat_count",
|
|
want: float64(3),
|
|
},
|
|
{
|
|
name: "HeartbeatInterval zero value serializes as 0 not omitted",
|
|
cluster: ClusterInfo{ClusterName: "test", HeartbeatInterval: 0, Vcpus: 0, InactiveHeartbeatCount: 0},
|
|
field: "heartbeat_interval",
|
|
want: float64(0),
|
|
},
|
|
{
|
|
name: "Vcpus zero value serializes as 0 not omitted",
|
|
cluster: ClusterInfo{ClusterName: "test", HeartbeatInterval: 0, Vcpus: 0, InactiveHeartbeatCount: 0},
|
|
field: "vcpus",
|
|
want: float64(0),
|
|
},
|
|
{
|
|
name: "InactiveHeartbeatCount zero value serializes as 0 not omitted",
|
|
cluster: ClusterInfo{ClusterName: "test", HeartbeatInterval: 0, Vcpus: 0, InactiveHeartbeatCount: 0},
|
|
field: "inactive_heartbeat_count",
|
|
want: float64(0),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
data, err := json.Marshal(tt.cluster)
|
|
if err != nil {
|
|
t.Fatalf("json.Marshal failed: %v", err)
|
|
}
|
|
var m map[string]interface{}
|
|
if err := json.Unmarshal(data, &m); err != nil {
|
|
t.Fatalf("json.Unmarshal failed: %v", err)
|
|
}
|
|
got, ok := m[tt.field]
|
|
if !ok {
|
|
t.Errorf("field %q not found in JSON output; available keys: %v", tt.field, mapKeys(m))
|
|
} else if got != tt.want {
|
|
t.Errorf("field %q = %v, want %v", tt.field, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClusterInfo_ExistingFieldsUnchanged(t *testing.T) {
|
|
c := ClusterInfo{
|
|
ClusterName: "mycluster",
|
|
Version: "1.0",
|
|
Status: 1,
|
|
ControllerName: "ctrl1",
|
|
NumServers: 5,
|
|
Resources: `{"res1":"val1"}`,
|
|
HostTypes: "LINUX64",
|
|
HostModels: "modelA",
|
|
ClusterAdmins: "admin1,admin2",
|
|
}
|
|
|
|
data, err := json.Marshal(c)
|
|
if err != nil {
|
|
t.Fatalf("json.Marshal failed: %v", err)
|
|
}
|
|
var m map[string]interface{}
|
|
if err := json.Unmarshal(data, &m); err != nil {
|
|
t.Fatalf("json.Unmarshal failed: %v", err)
|
|
}
|
|
|
|
// Verify all existing fields still serialize correctly
|
|
existing := map[string]string{
|
|
"cluster_name": c.ClusterName,
|
|
"version": c.Version,
|
|
"controller_name": c.ControllerName,
|
|
"resources": c.Resources,
|
|
"host_types": c.HostTypes,
|
|
"host_models": c.HostModels,
|
|
"cluster_admins": c.ClusterAdmins,
|
|
}
|
|
for jsonKey, wantVal := range existing {
|
|
got, _ := m[jsonKey].(string)
|
|
if got != wantVal {
|
|
t.Errorf("existing field %q = %q, want %q", jsonKey, got, wantVal)
|
|
}
|
|
}
|
|
if got, _ := m["status"].(float64); int(got) != c.Status {
|
|
t.Errorf("existing field status = %v, want %d", got, c.Status)
|
|
}
|
|
if got, _ := m["num_servers"].(float64); int(got) != c.NumServers {
|
|
t.Errorf("existing field num_servers = %v, want %d", got, c.NumServers)
|
|
}
|
|
}
|
|
|
|
func mapKeys(m map[string]interface{}) []string {
|
|
keys := make([]string, 0, len(m))
|
|
for k := range m {
|
|
keys = append(keys, k)
|
|
}
|
|
return keys
|
|
}
|