Files
beelzebub/parser/configurations_parser_test.go
Mario Candela 6468b5aa6f refactor:Added Integration test and tiny refactoring (#23)
* Refactoring name convention

* Added integration test

* Added Makefile

* Bump golang.org/x/crypto from 0.0.0-20220826181053-bd7e27e6170d to 0.6.0

Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.0.0-20220826181053-bd7e27e6170d to 0.6.0.
- [Release notes](https://github.com/golang/crypto/releases)
- [Commits](https://github.com/golang/crypto/commits/v0.6.0)

---
updated-dependencies:
- dependency-name: golang.org/x/crypto
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

* Upgrade go from 1.16 to 1.20

* Added integration test: HTTP, TCP, SSH

* Added Makefile Improve README.md

* Fixed unit test CI

* Fixed go-version

* Added integration test into C.I. actions

---------

Signed-off-by: Mario Candela <m4r10.php@gmail.com>
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-02-26 18:04:05 +01:00

119 lines
4.3 KiB
Go

package parser
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
)
func mockReadfilebytesConfigurationsCore(filePath string) ([]byte, error) {
configurationsCoreBytes := []byte(`
core:
logging:
debug: false
debugReportCaller: false
logDisableTimestamp: true
logsPath: ./logs
tracing:
rabbitMQEnabled: true
rabbitMQURI: provaMock`)
return configurationsCoreBytes, nil
}
func mockReadfilebytesFormatError(filePath string) ([]byte, error) {
configurationsCoreBytes := []byte(`{{}`)
return configurationsCoreBytes, nil
}
func mockReadfilebytesError(filePath string) ([]byte, error) {
return nil, errors.New("mockErrorReadFileBytes")
}
func mockReadDirError(dirPath string) ([]string, error) {
return nil, errors.New("mockErrorReadFileBytes")
}
func mockReadDirValid(dirPath string) ([]string, error) {
return []string{""}, nil
}
func mockReadfilebytesBeelzebubServiceConfiguration(filePath string) ([]byte, error) {
beelzebubServiceConfiguration := []byte(`
apiVersion: "v1"
protocol: "http"
address: ":8080"
commands:
- regex: "wp-admin"
handler: "login"
headers:
- "Content-Type: text/html"`)
return beelzebubServiceConfiguration, nil
}
func TestReadConfigurationsCoreError(t *testing.T) {
configurationsParser := Init("mockConfigurationsCorePath", "mockConfigurationsServicesDirectory")
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesError
beelzebubCoreConfigurations, err := configurationsParser.ReadConfigurationsCore()
assert.Nil(t, beelzebubCoreConfigurations)
assert.Error(t, err)
assert.Equal(t, "in file mockConfigurationsCorePath: mockErrorReadFileBytes", err.Error())
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesFormatError
beelzebubCoreConfigurations, err = configurationsParser.ReadConfigurationsCore()
assert.Nil(t, beelzebubCoreConfigurations)
assert.Error(t, err)
assert.Equal(t, "in file mockConfigurationsCorePath: yaml: line 1: did not find expected ',' or '}'", err.Error())
}
func TestReadConfigurationsCoreValid(t *testing.T) {
configurationsParser := Init("", "")
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesConfigurationsCore
coreConfigurations, err := configurationsParser.ReadConfigurationsCore()
assert.Nil(t, err)
assert.NotNil(t, coreConfigurations.Core)
assert.NotNil(t, coreConfigurations.Core.Logging)
assert.Equal(t, coreConfigurations.Core.Logging.Debug, false)
assert.Equal(t, coreConfigurations.Core.Logging.LogDisableTimestamp, true)
assert.Equal(t, coreConfigurations.Core.Logging.DebugReportCaller, false)
assert.Equal(t, coreConfigurations.Core.Logging.LogsPath, "./logs")
assert.Equal(t, coreConfigurations.Core.Tracing.RabbitMQEnabled, true)
assert.Equal(t, coreConfigurations.Core.Tracing.RabbitMQURI, "provaMock")
}
func TestReadConfigurationsServicesFail(t *testing.T) {
configurationsParser := Init("", "")
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesError
configurationsParser.gelAllFilesNameByDirNameDependency = mockReadDirError
beelzebubServiceConfiguration, err := configurationsParser.ReadConfigurationsServices()
assert.Nil(t, beelzebubServiceConfiguration)
assert.Error(t, err)
}
func TestReadConfigurationsServicesValid(t *testing.T) {
configurationsParser := Init("", "")
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesBeelzebubServiceConfiguration
configurationsParser.gelAllFilesNameByDirNameDependency = mockReadDirValid
beelzebubServicesConfiguration, err := configurationsParser.ReadConfigurationsServices()
firstBeelzebubServiceConfiguration := beelzebubServicesConfiguration[0]
assert.Nil(t, err)
assert.Equal(t, firstBeelzebubServiceConfiguration.Protocol, "http")
assert.Equal(t, firstBeelzebubServiceConfiguration.ApiVersion, "v1")
assert.Equal(t, firstBeelzebubServiceConfiguration.Address, ":8080")
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 1)
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 1)
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Regex, "wp-admin")
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Handler, "login")
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands[0].Headers), 1)
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Headers[0], "Content-Type: text/html")
}