Skip to content

Commit

Permalink
test: add basic pkg/config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isometry committed Oct 29, 2024
1 parent 4947c01 commit 32478a3
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package config

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/isometry/platform-health/pkg/provider"
"github.com/isometry/platform-health/pkg/provider/mock"
"github.com/isometry/platform-health/pkg/utils"
)

func init() {
log = utils.ContextLogger(context.TODO())
}

func TestGetInstances(t *testing.T) {
tests := []struct {
name string
config *concreteConfig
expected []provider.Instance
}{
{
name: "EmptyConfig",
config: &concreteConfig{},
expected: []provider.Instance{},
},
{
name: "PopulatedConfig",
config: &concreteConfig{
"provider1": []provider.Instance{
&mock.Mock{Name: "1"},
&mock.Mock{Name: "2"},
},
"provider2": []provider.Instance{
&mock.Mock{Name: "3"},
},
},
expected: []provider.Instance{
&mock.Mock{Name: "1"},
&mock.Mock{Name: "2"},
&mock.Mock{Name: "3"},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
instances := tt.config.GetInstances()
assert.Equal(t, tt.expected, instances)
})
}
}

func TestHarden(t *testing.T) {
tests := []struct {
name string
abstract abstractConfig
expected concreteConfig
}{
{
name: "Empty Config",
abstract: abstractConfig{},
expected: concreteConfig{},
},
{
name: "Simple Config",
abstract: abstractConfig{
"mock": []any{
map[string]any{"Name": "1"},
map[string]any{"Name": "2"},
},
},
expected: concreteConfig{
"mock": []provider.Instance{
&mock.Mock{Name: "1", Health: 1, Sleep: 1},
&mock.Mock{Name: "2", Health: 1, Sleep: 1},
},
},
},
{
name: "Invalid Config",
abstract: abstractConfig{
"mock": "invalid",
},
expected: concreteConfig{},
},
{
name: "Unknown Provider",
abstract: abstractConfig{
"unknown": []any{
map[string]any{"Name": "1"},
},
},
expected: concreteConfig{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.abstract.harden()
assert.Equal(t, &tt.expected, result)
})
}
}

0 comments on commit 32478a3

Please sign in to comment.