|
|
@@ -1,460 +1,39 @@
|
|
|
package kubemodel
|
|
|
|
|
|
import (
|
|
|
- "errors"
|
|
|
"testing"
|
|
|
"time"
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
)
|
|
|
|
|
|
-func TestKubeModelMarshalBinary(t *testing.T) {
|
|
|
- s := time.Now().UTC().Truncate(time.Hour)
|
|
|
- e := s.Add(time.Hour)
|
|
|
+func TestKubeModelSetCodecRoundTrip(t *testing.T) {
|
|
|
+ start := time.Now().UTC().Truncate(time.Hour)
|
|
|
+ end := start.Add(time.Hour)
|
|
|
|
|
|
- // Test empty KubeModelSet
|
|
|
+ t.Run("empty KubeModelSet", func(t *testing.T) {
|
|
|
+ kms := NewKubeModelSet(start, end)
|
|
|
|
|
|
- kms := NewKubeModelSet(s, e)
|
|
|
+ b, err := kms.MarshalBinary()
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
- b, err := kms.MarshalBinary()
|
|
|
- require.NoError(t, err)
|
|
|
+ act := new(KubeModelSet)
|
|
|
+ err = act.UnmarshalBinary(b)
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
- var act = new(KubeModelSet)
|
|
|
- err = act.UnmarshalBinary(b)
|
|
|
- require.NoError(t, err)
|
|
|
+ KubeModelSetEquals(t, kms, act)
|
|
|
+ })
|
|
|
|
|
|
- require.Equal(t, kms.Metadata, act.Metadata)
|
|
|
- require.Equal(t, kms.Window, act.Window)
|
|
|
- require.Equal(t, kms.Cluster, act.Cluster)
|
|
|
- require.Equal(t, kms.Namespaces, act.Namespaces)
|
|
|
- require.Equal(t, kms.ResourceQuotas, act.ResourceQuotas)
|
|
|
+ t.Run("full KubeModelSet", func(t *testing.T) {
|
|
|
+ kms := NewMockKubeModelSet(start, end)
|
|
|
|
|
|
- // Test non-empty KubeModelSet
|
|
|
+ b, err := kms.MarshalBinary()
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
- kms = NewKubeModelSet(s, e)
|
|
|
+ act := new(KubeModelSet)
|
|
|
+ err = act.UnmarshalBinary(b)
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
- kms.Metadata.CreatedAt = time.Now().UTC()
|
|
|
-
|
|
|
- kms.RegisterCluster(&Cluster{UID: "cluster", Start: s, End: e})
|
|
|
-
|
|
|
- kms.RegisterNamespace(&Namespace{UID: "ns1", Name: "ns1", Start: s, End: e})
|
|
|
- kms.Namespaces["ns1"].Labels = map[string]string{"label1": "label1"}
|
|
|
- kms.Namespaces["ns1"].Annotations = map[string]string{"anno1": "anno1"}
|
|
|
-
|
|
|
- kms.RegisterNamespace(&Namespace{UID: "ns2", Name: "ns2", Start: s, End: e})
|
|
|
- kms.Namespaces["ns2"].Labels = map[string]string{"label2": "label2"}
|
|
|
- kms.Namespaces["ns2"].Annotations = map[string]string{"anno2": "anno2"}
|
|
|
-
|
|
|
- kms.RegisterResourceQuota(&ResourceQuota{UID: "rq1", Name: "rq1", NamespaceUID: "ns1", Start: s, End: e})
|
|
|
- kms.ResourceQuotas["rq1"].Spec = &ResourceQuotaSpec{
|
|
|
- Hard: &ResourceQuotaSpecHard{
|
|
|
- Requests: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- Limits: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
- kms.ResourceQuotas["rq1"].Status = &ResourceQuotaStatus{
|
|
|
- Used: &ResourceQuotaStatusUsed{
|
|
|
- Requests: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- Limits: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- kms.RegisterResourceQuota(&ResourceQuota{UID: "rq2", Name: "rq2", NamespaceUID: "ns1", Start: s, End: e})
|
|
|
- kms.ResourceQuotas["rq2"].Spec = &ResourceQuotaSpec{
|
|
|
- Hard: &ResourceQuotaSpecHard{
|
|
|
- Requests: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- Limits: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
- kms.ResourceQuotas["rq2"].Status = &ResourceQuotaStatus{
|
|
|
- Used: &ResourceQuotaStatusUsed{
|
|
|
- Requests: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- Limits: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- kms.RegisterResourceQuota(&ResourceQuota{UID: "rq3", Name: "rq3", NamespaceUID: "ns2", Start: s, End: e})
|
|
|
- kms.ResourceQuotas["rq3"].Spec = &ResourceQuotaSpec{
|
|
|
- Hard: &ResourceQuotaSpecHard{
|
|
|
- Requests: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- Limits: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
- kms.ResourceQuotas["rq3"].Status = &ResourceQuotaStatus{
|
|
|
- Used: &ResourceQuotaStatusUsed{
|
|
|
- Requests: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- Limits: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- kms.RegisterResourceQuota(&ResourceQuota{UID: "rq4", Name: "rq4", NamespaceUID: "ns2", Start: s, End: e})
|
|
|
- kms.ResourceQuotas["rq4"].Spec = &ResourceQuotaSpec{
|
|
|
- Hard: &ResourceQuotaSpecHard{
|
|
|
- Requests: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- Limits: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
- kms.ResourceQuotas["rq4"].Status = &ResourceQuotaStatus{
|
|
|
- Used: &ResourceQuotaStatusUsed{
|
|
|
- Requests: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- Limits: ResourceQuantities{
|
|
|
- ResourceCPU: ResourceQuantity{
|
|
|
- Resource: ResourceCPU,
|
|
|
- Unit: UnitMillicore,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- ResourceMemory: ResourceQuantity{
|
|
|
- Resource: ResourceMemory,
|
|
|
- Unit: UnitByte,
|
|
|
- Values: Stats{
|
|
|
- StatAvg: 1,
|
|
|
- StatMax: 1,
|
|
|
- StatP85: 1,
|
|
|
- StatP95: 1,
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- kms.Error(errors.New("test error"))
|
|
|
- kms.Warn("test warning")
|
|
|
- kms.Info("test info")
|
|
|
- kms.Debug("test debug")
|
|
|
- kms.Trace("test trace")
|
|
|
-
|
|
|
- kms.Metadata.CompletedAt = time.Now().UTC()
|
|
|
-
|
|
|
- b, err = kms.MarshalBinary()
|
|
|
- require.NoError(t, err)
|
|
|
-
|
|
|
- act = new(KubeModelSet)
|
|
|
- err = act.UnmarshalBinary(b)
|
|
|
- require.NoError(t, err)
|
|
|
-
|
|
|
- require.Equal(t, kms.Metadata, act.Metadata)
|
|
|
- require.Equal(t, kms.Window, act.Window)
|
|
|
- require.Equal(t, kms.Cluster, act.Cluster)
|
|
|
- require.Equal(t, kms.Namespaces, act.Namespaces)
|
|
|
- require.Equal(t, kms.ResourceQuotas, act.ResourceQuotas)
|
|
|
+ KubeModelSetEquals(t, kms, act)
|
|
|
+ })
|
|
|
}
|