Skip to content

Commit

Permalink
refactor: divide complex test case
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 27, 2024
1 parent dcf2653 commit 8ec8c46
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 94 deletions.
24 changes: 16 additions & 8 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions pkg/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand Down
115 changes: 90 additions & 25 deletions pkg/resource/resource_test.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,107 @@
package resource

import (
"fmt"
"testing"

"github.com/go-faker/faker/v4"
"github.com/gofrs/uuid"
"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())
}
10 changes: 5 additions & 5 deletions pkg/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/spec/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 8ec8c46

Please sign in to comment.