Skip to content

Commit

Permalink
tests: add config test
Browse files Browse the repository at this point in the history
  • Loading branch information
hatamiarash7 committed Jan 19, 2025
1 parent 956128b commit b866992
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ go.work
bin
ipset
config.yml
test_config.yml
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ lint:
clean:
rm -rf $(BIN_DIR)

test: test-data
test:
$(GO) test -v ./...

$(BIN_DIR)/%: cmd/% $(SOURCES)
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ require (
github.com/prometheus/client_golang v1.20.5
github.com/samber/lo v1.47.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.10.0
github.com/vishvananda/netlink v1.3.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/vishvananda/netns v0.0.5 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
Expand Down
79 changes: 79 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package config

import (
"flag"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

// Mock for os.Open
type MockFile struct {
mock.Mock
}

func (m *MockFile) Open(name string) (*os.File, error) {
args := m.Called(name)
return args.Get(0).(*os.File), args.Error(1)
}

func TestGetEnv(t *testing.T) {
// Test when the environment variable is set
os.Setenv("CONFIG_FILE", "test_config.yml")
result := getEnv("CONFIG_FILE", "default.yml")
assert.Equal(t, "test_config.yml", result, "Expected value from environment variable")

// Test when the environment variable is not set
os.Unsetenv("CONFIG_FILE")
result = getEnv("CONFIG_FILE", "default.yml")
assert.Equal(t, "default.yml", result, "Expected default value when environment variable is not set")
}

func TestLoadConfigFile(t *testing.T) {
// Reset the flag set to avoid flag redefinition errors and handle Go test flags
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)

// Set environment variable for config file path
os.Setenv("CONFIG_FILE", "test_config.yml")

// Mocking the file open and decode process
mockFile := new(MockFile)
defer os.Unsetenv("CONFIG_FILE")

// Assume we have a test YAML file and mock the file open method
mockFile.On("Open", "test_config.yml").Return(nil, nil)

// Here we are testing a valid scenario
cfg, err := Load()

// Validate the function returns a configuration struct and no error
assert.NoError(t, err, "Expected no error loading config")
assert.NotNil(t, cfg, "Expected a non-nil configuration")
assert.Equal(t, "localhost", cfg.App.Host, "Expected default host")
assert.Equal(t, 8080, cfg.App.Port, "Expected default port")
}

func TestLoadConfigFileError(t *testing.T) {
// Reset the flag set to avoid flag redefinition errors and handle Go test flags
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)

// Test the error when the config file does not exist
_, err := Load()
assert.Error(t, err, "Expected error loading config due to non-existing file")
}

func TestLoadConfigWithInvalidYaml(t *testing.T) {
// Reset the flag set to avoid flag redefinition errors and handle Go test flags
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)

// Test loading a file with invalid YAML
invalidYaml := []byte("invalid: yaml: syntax")
mockFile := new(MockFile)
mockFile.On("Open", "test_config.yml").Return(invalidYaml, nil)

// Simulate the file being opened
_, err := Load()
assert.Error(t, err, "Expected error due to invalid YAML syntax")
}

0 comments on commit b866992

Please sign in to comment.