Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: complete appconfiguartion model unit tests #484

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package generator

import (
"testing"

"github.com/stretchr/testify/assert"
"kusionstack.io/kusion/pkg/generator"
"kusionstack.io/kusion/pkg/models"
appmodel "kusionstack.io/kusion/pkg/models/appconfiguration"
"kusionstack.io/kusion/pkg/models/appconfiguration/workload"
"kusionstack.io/kusion/pkg/projectstack"
)

func TestAppsGenerator_GenerateSpec(t *testing.T) {
project, stack := buildMockProjectAndStack()
appName, app := buildMockApp()
acg := &AppsGenerator{
Apps: map[string]appmodel.AppConfiguration{
appName: *app,
},
}

spec, err := acg.GenerateSpec(&generator.Options{}, project, stack)
assert.NoError(t, err)
assert.NotNil(t, spec)
}

func TestAppConfigurationGenerator_Generate(t *testing.T) {
project, stack := buildMockProjectAndStack()
appName, app := buildMockApp()

g := &appConfigurationGenerator{
project: project,
stack: stack,
appName: appName,
app: app,
}

spec := &models.Spec{
Resources: []models.Resource{},
}

err := g.Generate(spec)
assert.NoError(t, err)
assert.NotEmpty(t, spec.Resources)
}

func TestNewAppConfigurationGeneratorFunc(t *testing.T) {
project, stack := buildMockProjectAndStack()
appName, app := buildMockApp()

t.Run("Valid app configuration generator func", func(t *testing.T) {
g, err := NewAppConfigurationGeneratorFunc(project, stack, appName, app)()
assert.NoError(t, err)
assert.NotNil(t, g)
})

t.Run("Empty app name", func(t *testing.T) {
g, err := NewAppConfigurationGeneratorFunc(project, stack, "", app)()
assert.EqualError(t, err, "app name must not be empty")
assert.Nil(t, g)
})

t.Run("Nil app", func(t *testing.T) {
g, err := NewAppConfigurationGeneratorFunc(project, stack, appName, nil)()
assert.EqualError(t, err, "can not find app configuration when generating the Spec")
assert.Nil(t, g)
})

t.Run("Empty project name", func(t *testing.T) {
project.Name = ""
g, err := NewAppConfigurationGeneratorFunc(project, stack, appName, nil)()
assert.EqualError(t, err, "project name must not be empty")
assert.Nil(t, g)
})
}

func buildMockApp() (string, *appmodel.AppConfiguration) {
return "app1", &appmodel.AppConfiguration{
Workload: &workload.Workload{
Header: workload.Header{
Type: "Service",
},
Service: &workload.Service{
Base: workload.Base{},
Type: "Deployment",
},
},
}
}

func buildMockProjectAndStack() (*projectstack.Project, *projectstack.Stack) {
project := &projectstack.Project{
ProjectConfiguration: projectstack.ProjectConfiguration{
Name: "testproject",
},
}

stack := &projectstack.Stack{
StackConfiguration: projectstack.StackConfiguration{
Name: "teststack",
},
}

return project, stack
}
171 changes: 171 additions & 0 deletions pkg/generator/appconfiguration/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package appconfiguration

import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kusionstack.io/kusion/pkg/models"
)

type mockGenerator struct {
GenerateFunc func(spec *models.Spec) error
}

func (m *mockGenerator) Generate(spec *models.Spec) error {
return m.GenerateFunc(spec)
}

func TestCallGenerators(t *testing.T) {
spec := &models.Spec{}

var (
generator1 Generator = &mockGenerator{
GenerateFunc: func(spec *models.Spec) error {
return nil
},
}
generator2 Generator = &mockGenerator{
GenerateFunc: func(spec *models.Spec) error {
return assert.AnError
},
}
gf1 = func() (Generator, error) { return generator1, nil }
gf2 = func() (Generator, error) { return generator2, nil }
)

err := CallGenerators(spec, gf1, gf2)
assert.Error(t, err)
assert.EqualError(t, err, assert.AnError.Error())
}

func TestCallGeneratorFuncs(t *testing.T) {
generatorFunc1 := func() (Generator, error) {
return &mockGenerator{}, nil
}

generatorFunc2 := func() (Generator, error) {
return nil, assert.AnError
}

generators, err := CallGeneratorFuncs(generatorFunc1)
assert.NoError(t, err)
assert.Len(t, generators, 1)
assert.IsType(t, &mockGenerator{}, generators[0])

_, err = CallGeneratorFuncs(generatorFunc2)
assert.ErrorIs(t, err, assert.AnError)
}

func TestForeachOrdered(t *testing.T) {
m := map[string]int{
"b": 2,
"a": 1,
"c": 3,
}

result := ""
err := ForeachOrdered(m, func(key string, value int) error {
result += key
return nil
})

assert.NoError(t, err)
assert.Equal(t, "abc", result)
}

func TestGenericPtr(t *testing.T) {
value := 42
ptr := GenericPtr(value)
assert.Equal(t, &value, ptr)
}

func TestMergeMaps(t *testing.T) {
map1 := map[string]string{
"a": "1",
"b": "2",
}

map2 := map[string]string{
"c": "3",
"d": "4",
}

merged := MergeMaps(map1, nil, map2)

expected := map[string]string{
"a": "1",
"b": "2",
"c": "3",
"d": "4",
}

assert.Equal(t, expected, merged)
}

func TestKubernetesResourceID(t *testing.T) {
typeMeta := v1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
}

objectMeta := v1.ObjectMeta{
Namespace: "example",
Name: "my-deployment",
}

id := KubernetesResourceID(typeMeta, objectMeta)
assert.Equal(t, "apps/v1:Deployment:example:my-deployment", id)
}

func TestAppendToSpec(t *testing.T) {
spec := &models.Spec{}
resource := &models.Resource{
ID: "v1:Namespace:fake-project",
Type: "Kubernetes",
Attributes: map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"creationTimestamp": nil,
"name": "fake-project",
},
"spec": make(map[string]interface{}),
"status": make(map[string]interface{}),
},
DependsOn: nil,
Extensions: nil,
}

err := AppendToSpec(models.Kubernetes, "resource-id", spec, resource)

assert.NoError(t, err)
assert.Len(t, spec.Resources, 1)
assert.Equal(t, "resource-id", spec.Resources[0].ID)
assert.Equal(t, models.Kubernetes, spec.Resources[0].Type)
}

func TestUniqueAppName(t *testing.T) {
projectName := "my-project"
stackName := "my-stack"
appName := "my-app"

expected := "my-project-my-stack-my-app"
result := UniqueAppName(projectName, stackName, appName)

assert.Equal(t, expected, result)
}

func TestUniqueAppLabels(t *testing.T) {
projectName := "my-project"
appName := "my-app"

expected := map[string]string{
"app.kubernetes.io/part-of": projectName,
"app.kubernetes.io/name": appName,
}

result := UniqueAppLabels(projectName, appName)

assert.Equal(t, expected, result)
}
4 changes: 2 additions & 2 deletions pkg/models/appconfiguration/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Workload struct {
*Job `yaml:",inline" json:",inline"`
}

func (w *Workload) MarshalJSON() ([]byte, error) {
SparkYuan marked this conversation as resolved.
Show resolved Hide resolved
func (w Workload) MarshalJSON() ([]byte, error) {
switch w.Header.Type {
case TypeService:
return json.Marshal(struct {
Expand Down Expand Up @@ -69,7 +69,7 @@ func (w *Workload) UnmarshalJSON(data []byte) error {
return err
}

func (w *Workload) MarshalYAML() (interface{}, error) {
func (w Workload) MarshalYAML() (interface{}, error) {
switch w.Header.Type {
case TypeService:
return struct {
Expand Down
Loading
Loading