From 8ec8c46d4e6a2afca114223b9d7fa4cecb6d36fb Mon Sep 17 00:00:00 2001 From: siyul-park Date: Fri, 27 Dec 2024 15:29:22 +0900 Subject: [PATCH] refactor: divide complex test case --- pkg/chart/chart.go | 24 ++++--- pkg/chart/chart_test.go | 16 ++--- pkg/resource/resource_test.go | 115 +++++++++++++++++++++++++-------- pkg/secret/secret_test.go | 10 +-- pkg/spec/spec_test.go | 10 +-- pkg/spec/unstructured_test.go | 117 +++++++++++++++++++++------------- 6 files changed, 198 insertions(+), 94 deletions(-) diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index f06b0427..d6b1c276 100644 --- a/pkg/chart/chart.go +++ b/pkg/chart/chart.go @@ -15,14 +15,22 @@ import ( // Chart defines the structure that combines multiple nodes into a cluster node. type Chart struct { - ID uuid.UUID `json:"id" bson:"_id" yaml:"id" map:"id" validate:"required"` - Namespace string `json:"namespace" bson:"namespace" yaml:"namespace" map:"namespace" validate:"required"` - Name string `json:"name,omitempty" bson:"name,omitempty" yaml:"name,omitempty" map:"name,omitempty"` - Annotations map[string]string `json:"annotations,omitempty" bson:"annotations,omitempty" yaml:"annotations,omitempty" map:"annotations,omitempty"` - Specs []*spec.Unstructured `json:"specs" bson:"specs" yaml:"specs" map:"specs"` - Inbounds map[string][]spec.Port `json:"inbounds,omitempty" bson:"inbounds,omitempty" yaml:"inbounds,omitempty" map:"inbounds,omitempty"` - Outbounds map[string][]spec.Port `json:"outbounds,omitempty" bson:"outbounds,omitempty" yaml:"outbounds,omitempty" map:"outbounds,omitempty"` - Env map[string][]spec.Value `json:"env,omitempty" bson:"env,omitempty" yaml:"env,omitempty" map:"env,omitempty"` + // ID is the unique identifier of the chart. + ID uuid.UUID `json:"id" bson:"_id" yaml:"id" map:"id" validate:"required"` + // Namespace groups charts logically. + Namespace string `json:"namespace" bson:"namespace" yaml:"namespace" map:"namespace" validate:"required"` + // Name is the human-readable name of the chart. + Name string `json:"name,omitempty" bson:"name,omitempty" yaml:"name,omitempty" map:"name,omitempty"` + // Annotations hold additional metadata. + Annotations map[string]string `json:"annotations,omitempty" bson:"annotations,omitempty" yaml:"annotations,omitempty" map:"annotations,omitempty"` + // Specs define the specifications of the chart. + Specs []*spec.Unstructured `json:"specs" bson:"specs" yaml:"specs" map:"specs"` + // Inbounds define the inbound ports of the chart. + Inbounds map[string][]spec.Port `json:"inbounds,omitempty" bson:"inbounds,omitempty" yaml:"inbounds,omitempty" map:"inbounds,omitempty"` + // Outbounds define the outbound ports of the chart. + Outbounds map[string][]spec.Port `json:"outbounds,omitempty" bson:"outbounds,omitempty" yaml:"outbounds,omitempty" map:"outbounds,omitempty"` + // Env holds the environment variables of the chart. + Env map[string][]spec.Value `json:"env,omitempty" bson:"env,omitempty" yaml:"env,omitempty" map:"env,omitempty"` } // Key constants for commonly used fields. diff --git a/pkg/chart/chart_test.go b/pkg/chart/chart_test.go index 076ed11f..80bc101e 100644 --- a/pkg/chart/chart_test.go +++ b/pkg/chart/chart_test.go @@ -154,35 +154,35 @@ func TestChart_Build(t *testing.T) { }) } -func TestChart_SetID(t *testing.T) { +func TestChart_ID(t *testing.T) { chrt := New() id := uuid.Must(uuid.NewV7()) chrt.SetID(id) assert.Equal(t, id, chrt.GetID()) } -func TestChart_SetNamespace(t *testing.T) { +func TestChart_Namespace(t *testing.T) { chrt := New() namespace := "test-namespace" chrt.SetNamespace(namespace) assert.Equal(t, namespace, chrt.GetNamespace()) } -func TestChart_SetName(t *testing.T) { +func TestChart_Name(t *testing.T) { chrt := New() name := "test-chart" chrt.SetName(name) assert.Equal(t, name, chrt.GetName()) } -func TestChart_SetAnnotations(t *testing.T) { +func TestChart_Annotations(t *testing.T) { chrt := New() annotations := map[string]string{"key": "value"} chrt.SetAnnotations(annotations) assert.Equal(t, annotations, chrt.GetAnnotations()) } -func TestChart_SetSpecs(t *testing.T) { +func TestChart_Specs(t *testing.T) { chrt := New() specs := []*spec.Unstructured{ { @@ -196,7 +196,7 @@ func TestChart_SetSpecs(t *testing.T) { assert.Equal(t, specs, chrt.GetSpecs()) } -func TestChart_SetInbounds(t *testing.T) { +func TestChart_Inbounds(t *testing.T) { chrt := New() ports := map[string][]spec.Port{ "http": { @@ -211,7 +211,7 @@ func TestChart_SetInbounds(t *testing.T) { assert.Equal(t, ports, chrt.GetInbounds()) } -func TestChart_SetOutbounds(t *testing.T) { +func TestChart_Outbounds(t *testing.T) { chrt := New() ports := map[string][]spec.Port{ "http": { @@ -226,7 +226,7 @@ func TestChart_SetOutbounds(t *testing.T) { assert.Equal(t, ports, chrt.GetOutbounds()) } -func TestChart_SetEnv(t *testing.T) { +func TestChart_Env(t *testing.T) { chrt := New() env := map[string][]spec.Value{ "FOO": { diff --git a/pkg/resource/resource_test.go b/pkg/resource/resource_test.go index 13c82305..dc2ed0ab 100644 --- a/pkg/resource/resource_test.go +++ b/pkg/resource/resource_test.go @@ -1,6 +1,7 @@ package resource import ( + "fmt" "testing" "github.com/go-faker/faker/v4" @@ -8,35 +9,99 @@ import ( "github.com/stretchr/testify/assert" ) -func TestMeta_GetSet(t *testing.T) { - meta := &Meta{ - ID: uuid.Must(uuid.NewV7()), - Namespace: "default", - Name: faker.UUIDHyphenated(), - Annotations: map[string]string{"key": "value"}, +func TestMatch(t *testing.T) { + id := uuid.Must(uuid.NewV7()) + + tests := []struct { + source *Meta + examples []*Meta + matches []int + }{ + { + source: &Meta{ + ID: id, + Namespace: DefaultNamespace, + Name: "node1", + }, + examples: []*Meta{ + { + ID: id, + }, + { + ID: uuid.Must(uuid.NewV7()), + }, + }, + matches: []int{0}, + }, + { + source: &Meta{ + ID: id, + Namespace: DefaultNamespace, + Name: "node1", + }, + examples: []*Meta{ + { + Namespace: DefaultNamespace, + }, + { + Namespace: "other", + }, + }, + matches: []int{0}, + }, + { + source: &Meta{ + ID: id, + Namespace: DefaultNamespace, + Name: "node1", + }, + examples: []*Meta{ + { + Name: "node1", + }, + { + Name: "node2", + }, + }, + matches: []int{0}, + }, } - assert.Equal(t, meta.ID, meta.GetID()) - assert.Equal(t, meta.Namespace, meta.GetNamespace()) - assert.Equal(t, meta.Name, meta.GetName()) - assert.Equal(t, meta.Annotations, meta.GetAnnotations()) + for _, tt := range tests { + t.Run(fmt.Sprintf("%v, %v", tt.source, tt.examples), func(t *testing.T) { + expected := make([]*Meta, 0, len(tt.matches)) + for _, i := range tt.matches { + expected = append(expected, tt.examples[i]) + } + assert.Equal(t, expected, Match(tt.source, tt.examples...)) + }) + } } -func TestMatch(t *testing.T) { - id1 := uuid.Must(uuid.NewV7()) - id2 := uuid.Must(uuid.NewV7()) - - sp := &Meta{ID: id1, Namespace: "default", Name: "node1"} - examples := []*Meta{ - {ID: id1, Namespace: "default", Name: "node1"}, - {ID: id1}, - {Namespace: "default", Name: "node1"}, - {ID: id2, Namespace: "default", Name: "node2"}, - {ID: id2}, - {Namespace: "default", Name: "node2"}, - } +func TestMeta_ID(t *testing.T) { + meta := &Meta{} + id := uuid.Must(uuid.NewV4()) + meta.SetID(id) + assert.Equal(t, id, meta.GetID()) +} + +func TestMeta_Namespace(t *testing.T) { + meta := &Meta{} + namespace := "default" + meta.SetNamespace(namespace) + assert.Equal(t, namespace, meta.GetNamespace()) +} - expeced := []*Meta{examples[0], examples[1], examples[2]} +func TestMeta_Name(t *testing.T) { + meta := &Meta{} + name := faker.UUIDHyphenated() + meta.SetName(name) + assert.Equal(t, name, meta.GetName()) +} - assert.Equal(t, expeced, Match(sp, examples...)) +func TestMeta_Annotations(t *testing.T) { + meta := &Meta{} + annotations := map[string]string{"key": "value"} + meta.SetAnnotations(annotations) + assert.Equal(t, annotations, meta.GetAnnotations()) } diff --git a/pkg/secret/secret_test.go b/pkg/secret/secret_test.go index c57532b3..3915baaa 100644 --- a/pkg/secret/secret_test.go +++ b/pkg/secret/secret_test.go @@ -8,35 +8,35 @@ import ( "github.com/stretchr/testify/assert" ) -func TestSecret_SetID(t *testing.T) { +func TestSecret_ID(t *testing.T) { scrt := New() id := uuid.Must(uuid.NewV7()) scrt.SetID(id) assert.Equal(t, id, scrt.GetID()) } -func TestSecret_SetNamespace(t *testing.T) { +func TestSecret_Namespace(t *testing.T) { scrt := New() namespace := faker.UUIDHyphenated() scrt.SetNamespace(namespace) assert.Equal(t, namespace, scrt.GetNamespace()) } -func TestSecret_SetName(t *testing.T) { +func TestSecret_Name(t *testing.T) { scrt := New() name := faker.UUIDHyphenated() scrt.SetName(name) assert.Equal(t, name, scrt.GetName()) } -func TestSecret_SetAnnotations(t *testing.T) { +func TestSecret_Annotations(t *testing.T) { scrt := New() annotation := map[string]string{"key": "value"} scrt.SetAnnotations(annotation) assert.Equal(t, annotation, scrt.GetAnnotations()) } -func TestSecret_SetData_Nil(t *testing.T) { +func TestSecret_Data(t *testing.T) { scrt := New() data := faker.UUIDHyphenated() scrt.SetData(data) diff --git a/pkg/spec/spec_test.go b/pkg/spec/spec_test.go index fd735b8a..2736c03e 100644 --- a/pkg/spec/spec_test.go +++ b/pkg/spec/spec_test.go @@ -25,35 +25,35 @@ func TestAs(t *testing.T) { assert.NoError(t, err) } -func TestMeta_SetID(t *testing.T) { +func TestMeta_ID(t *testing.T) { meta := &Meta{} id := uuid.Must(uuid.NewV7()) meta.SetID(id) assert.Equal(t, id, meta.GetID()) } -func TestMeta_SetKind(t *testing.T) { +func TestMeta_Kind(t *testing.T) { meta := &Meta{} kind := faker.UUIDHyphenated() meta.SetKind(kind) assert.Equal(t, kind, meta.GetKind()) } -func TestMeta_SetNamespace(t *testing.T) { +func TestMeta_Namespace(t *testing.T) { meta := &Meta{} namespace := faker.UUIDHyphenated() meta.SetNamespace(namespace) assert.Equal(t, namespace, meta.GetNamespace()) } -func TestMeta_SetName(t *testing.T) { +func TestMeta_Name(t *testing.T) { meta := &Meta{} name := faker.UUIDHyphenated() meta.SetName(name) assert.Equal(t, name, meta.GetName()) } -func TestMeta_SetAnnotations(t *testing.T) { +func TestMeta_Annotations(t *testing.T) { meta := &Meta{} annotations := map[string]string{"key": "value"} meta.SetAnnotations(annotations) diff --git a/pkg/spec/unstructured_test.go b/pkg/spec/unstructured_test.go index c395af52..69a1fd79 100644 --- a/pkg/spec/unstructured_test.go +++ b/pkg/spec/unstructured_test.go @@ -3,61 +3,92 @@ package spec import ( "testing" + "github.com/siyul-park/uniflow/pkg/node" + "github.com/go-faker/faker/v4" "github.com/gofrs/uuid" "github.com/stretchr/testify/assert" ) -func TestUnstructured_GetSet(t *testing.T) { - unstructured := &Unstructured{} - - id := uuid.Must(uuid.NewV7()) - unstructured.Set(KeyID, id) - val, ok := unstructured.Get(KeyID) - assert.True(t, ok) - assert.Equal(t, id, val) +func TestUnstructured_GetAndSet(t *testing.T) { + t.Run("KeyID", func(t *testing.T) { + unstructured := &Unstructured{} + id := uuid.Must(uuid.NewV4()) + unstructured.Set(KeyID, id) + val, ok := unstructured.Get(KeyID) + assert.True(t, ok) + assert.Equal(t, id, val) + }) - kind := faker.UUIDHyphenated() - unstructured.Set(KeyKind, kind) - val, ok = unstructured.Get(KeyKind) - assert.True(t, ok) - assert.Equal(t, kind, val) + t.Run("KeyKind", func(t *testing.T) { + unstructured := &Unstructured{} + kind := faker.UUIDHyphenated() + unstructured.Set(KeyKind, kind) + val, ok := unstructured.Get(KeyKind) + assert.True(t, ok) + assert.Equal(t, kind, val) + }) - unstructured.Set(KeyNamespace, "default") - val, ok = unstructured.Get(KeyNamespace) - assert.True(t, ok) - assert.Equal(t, "default", val) + t.Run("KeyNamespace", func(t *testing.T) { + unstructured := &Unstructured{} + unstructured.Set(KeyNamespace, "default") + val, ok := unstructured.Get(KeyNamespace) + assert.True(t, ok) + assert.Equal(t, "default", val) + }) - name := faker.UUIDHyphenated() - unstructured.Set(KeyName, name) - val, ok = unstructured.Get(KeyName) - assert.True(t, ok) - assert.Equal(t, name, val) + t.Run("KeyName", func(t *testing.T) { + unstructured := &Unstructured{} + name := faker.UUIDHyphenated() + unstructured.Set(KeyName, name) + val, ok := unstructured.Get(KeyName) + assert.True(t, ok) + assert.Equal(t, name, val) + }) - annotations := map[string]string{"key": "value"} - unstructured.Set(KeyAnnotations, annotations) - val, ok = unstructured.Get(KeyAnnotations) - assert.True(t, ok) - assert.Equal(t, annotations, val) + t.Run("KeyAnnotations", func(t *testing.T) { + unstructured := &Unstructured{} + annotations := map[string]string{"key": "value"} + unstructured.Set(KeyAnnotations, annotations) + val, ok := unstructured.Get(KeyAnnotations) + assert.True(t, ok) + assert.Equal(t, annotations, val) + }) - ports := map[string][]Port{"port1": {{Name: faker.UUIDHyphenated(), Port: "8080"}}} - unstructured.Set(KeyPorts, ports) - val, ok = unstructured.Get(KeyPorts) - assert.True(t, ok) - assert.Equal(t, ports, val) + t.Run("KeyPorts", func(t *testing.T) { + unstructured := &Unstructured{} + ports := map[string][]Port{ + node.PortOut: { + { + Name: faker.UUIDHyphenated(), + Port: node.PortIn, + }, + }, + } + unstructured.Set(KeyPorts, ports) + val, ok := unstructured.Get(KeyPorts) + assert.True(t, ok) + assert.Equal(t, ports, val) + }) - env := map[string][]Value{"env1": {{Name: "secret1", Data: "value1"}}} - unstructured.Set(KeyEnv, env) - val, ok = unstructured.Get(KeyEnv) - assert.True(t, ok) - assert.Equal(t, env, val) + t.Run("KeyEnv", func(t *testing.T) { + unstructured := &Unstructured{} + env := map[string][]Value{"env1": {{Name: "secret1", Data: "value1"}}} + unstructured.Set(KeyEnv, env) + val, ok := unstructured.Get(KeyEnv) + assert.True(t, ok) + assert.Equal(t, env, val) + }) - customKey := "customField" - customValue := "customValue" - unstructured.Set(customKey, customValue) - val, ok = unstructured.Get(customKey) - assert.True(t, ok) - assert.Equal(t, customValue, val) + t.Run("CustomField", func(t *testing.T) { + unstructured := &Unstructured{} + customKey := "customField" + customValue := "customValue" + unstructured.Set(customKey, customValue) + val, ok := unstructured.Get(customKey) + assert.True(t, ok) + assert.Equal(t, customValue, val) + }) } func TestUnstructured_Build(t *testing.T) {