Skip to content

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Schmidt committed Dec 6, 2023
1 parent 78954ce commit ac06c31
Show file tree
Hide file tree
Showing 6 changed files with 595 additions and 37 deletions.
83 changes: 51 additions & 32 deletions asconfig/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"github.com/go-logr/logr"
)

const (
expandConfKey = "expanded_config"
flatConfKey = "flat_config"
metadataKey = "metadata"
buildKey = "asd_build"
)

// namespaceRe is a regular expression used to match and extract namespace configurations from the config file.
var namespaceRe = regexp.MustCompile(fmt.Sprintf(`(^namespaces\%s)(.+?)(\%s.+)`, sep, sep))
var indexedRe = regexp.MustCompile(`(.+)\[(\d+)\](.*)`)
Expand All @@ -24,28 +31,23 @@ type ConfGetter interface {
GetAsInfo(cmdList ...string) (Conf, error)
}

// isSupportedGenerateVersion checks if the provided version is supported for generating the config.
func isSupportedGenerateVersion(version string) (bool, error) {
s, err := IsSupportedVersion(version)

if err != nil {
return false, err
}
type GenConf struct {
conf Conf
version string
}

if !s {
return false, nil
func newGenConf(conf Conf, version string) *GenConf {
return &GenConf{
conf: conf,
version: version,
}

cmp, err := lib.CompareVersions(version, "5.0.0")

return cmp >= 0, err
}

// GenerateConf generates the config based on the provided log and ConfGetter.
// If removeDefaults is true, it will remove default values from the config.
// Without removeDefaults, the config that is generate will not be valid. Many
// default values are out of the acceptable range required by the server.
func GenerateConf(log logr.Logger, confGetter ConfGetter, removeDefaults bool) (Conf, error) {
func GenerateConf(log logr.Logger, confGetter ConfGetter, removeDefaults bool) (*GenConf, error) {
log.V(1).Info("Generating config")

validConfig := Conf{}
Expand Down Expand Up @@ -74,7 +76,24 @@ func GenerateConf(log logr.Logger, confGetter ConfGetter, removeDefaults bool) (
return nil, err
}

return validConfig["expanded_config"].(Conf), nil
return newGenConf(validConfig[expandConfKey].(Conf), validConfig[metadataKey].(Conf)[buildKey].(string)), nil
}

// isSupportedGenerateVersion checks if the provided version is supported for generating the config.
func isSupportedGenerateVersion(version string) (bool, error) {
s, err := IsSupportedVersion(version)

if err != nil {
return false, err
}

if !s {
return false, nil
}

cmp, err := lib.CompareVersions(version, "5.0.0")

return cmp >= 0, err
}

// pipelineStep is an interface that defines a step in the pipeline for generating the config.
Expand Down Expand Up @@ -144,13 +163,13 @@ func (s *GetConfigStep) execute(conf Conf) error {
delete(configs, "racks")
}

metadata, err := s.confGetter.GetAsInfo("metadata")
metadata, err := s.confGetter.GetAsInfo(metadataKey)
if err != nil {
s.log.V(-1).Error(err, "Error getting metadata from node")
return err
}

conf["metadata"] = metadata["metadata"]
conf[metadataKey] = metadata[metadataKey]

return nil
}
Expand All @@ -173,7 +192,7 @@ func newServerVersionCheckStep(log logr.Logger, checkFunc func(string) (bool, er
func (s *ServerVersionCheckStep) execute(conf Conf) error {
s.log.V(1).Info("Checking server version")

build := conf["metadata"].(Conf)["asd_build"].(string)
build := conf[metadataKey].(Conf)[buildKey].(string)
isSupported, err := s.checkFunc(build)

if err != nil {
Expand Down Expand Up @@ -213,9 +232,9 @@ func (s *copyEffectiveRackIDStep) execute(conf Conf) error {
return nil
}

flatConfig := conf["flat_config"].(Conf)
flatConfig := conf[flatConfKey].(Conf)
effectiveRacks := conf["racks"].([]Conf)
nodeID := conf["metadata"].(Conf)["node_id"].(string)
nodeID := conf[metadataKey].(Conf)["node_id"].(string)

for _, rackInfo := range effectiveRacks {
ns := rackInfo["ns"].(string)
Expand Down Expand Up @@ -339,7 +358,7 @@ func convertDictToList(config Conf) []Conf {
for _, key1 := range keys1 {
config2, ok := config[key1].(Conf)

if !ok {
if !ok || config2 == nil {
continue
}

Expand Down Expand Up @@ -393,7 +412,7 @@ func (s *flattenConfStep) execute(conf Conf) error {

convertDictRespToConf(origConfig)

conf["flat_config"] = flattenConf(s.log, conf["config"].(Conf), sep)
conf[flatConfKey] = flattenConf(s.log, conf["config"].(Conf), sep)

return nil
}
Expand Down Expand Up @@ -512,7 +531,7 @@ func parseIndexField(key string) (newKey string, index int, err error) {
func (s *transformKeyValuesStep) execute(conf Conf) error {
s.log.V(1).Info("Transforming key values")

origFlatConf := conf["flat_config"].(Conf)
origFlatConf := conf[flatConfKey].(Conf)
newFlatConf := make(Conf, len(origFlatConf)) // we will overwrite flat_config
sortedKeys := sortedKeys(origFlatConf)
scNamspaces := []string{}
Expand Down Expand Up @@ -584,7 +603,7 @@ func (s *transformKeyValuesStep) execute(conf Conf) error {
}
}

build := conf["metadata"].(Conf)["asd_build"].(string)
build := conf[metadataKey].(Conf)[buildKey].(string)
flatSchema, err := getFlatNormalizedSchema(build)
normalizedKey := namedRe.ReplaceAllString(key, "_")

Expand All @@ -611,7 +630,7 @@ func (s *transformKeyValuesStep) execute(conf Conf) error {
}
}

conf["flat_config"] = newFlatConf
conf[flatConfKey] = newFlatConf

return nil
}
Expand All @@ -632,8 +651,8 @@ func newRemoveSecurityIfDisabledStep(log logr.Logger) *removeSecurityIfDisabledS
func (s *removeSecurityIfDisabledStep) execute(conf Conf) error {
s.log.V(1).Info("Removing security configs if security is disabled")

flatConf := conf["flat_config"].(Conf)
build := conf["metadata"].(Conf)["asd_build"].(string)
flatConf := conf[flatConfKey].(Conf)
build := conf[metadataKey].(Conf)[buildKey].(string)

if val, ok := flatConf["security.enable-security"]; ok {
securityEnabled, ok := val.(bool)
Expand Down Expand Up @@ -745,8 +764,8 @@ func defaultSlice(m map[string][]string, key string) []string {
func (s *removeDefaultsStep) execute(conf Conf) error {
s.log.V(1).Info("Removing default values")

flatConf := conf["flat_config"].(Conf)
build := conf["metadata"].(Conf)["asd_build"].(string)
flatConf := conf[flatConfKey].(Conf)
build := conf[metadataKey].(Conf)[buildKey].(string)

flatSchema, err := getFlatNormalizedSchema(build)
if err != nil {
Expand Down Expand Up @@ -840,7 +859,7 @@ func (s *removeDefaultsStep) execute(conf Conf) error {
}

if securityFound {
build := conf["metadata"].(Conf)["asd_build"].(string)
build := conf[metadataKey].(Conf)[buildKey].(string)
cmp, err := lib.CompareVersions(build, "5.7.0")

if err != nil {
Expand Down Expand Up @@ -874,10 +893,10 @@ func newExpandConfStep(log logr.Logger) *expandConfStep {
func (s *expandConfStep) execute(conf Conf) error {
s.log.V(1).Info("Expanding config")

flatConf := conf["flat_config"].(Conf)
flatConf := conf[flatConfKey].(Conf)
expandedConf := expandConf(s.log, &flatConf, sep)

conf["expanded_config"] = expandedConf
conf[expandConfKey] = expandedConf

return nil
}
84 changes: 84 additions & 0 deletions asconfig/generate_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package asconfig

import (
"testing"

aero "github.com/aerospike/aerospike-client-go/v6"
"github.com/aerospike/aerospike-management-lib/info"
"github.com/aerospike/aerospike-management-lib/test"
"github.com/go-logr/logr"
"github.com/stretchr/testify/suite"
)

type GenerateE2eTestSuite struct {
suite.Suite
}

func (suite *GenerateE2eTestSuite) SetupSuite() {
err := test.Start(1)

if err != nil {
suite.T().Fatal(err)
}
}

// Uncomment this function to check server logs after failure
func (suite *GenerateE2eTestSuite) TearDownSuite() {
err := test.Stop()

if err != nil {
suite.T().Fatal(err)
}
}

func (suite *GenerateE2eTestSuite) SetupTest() {
}

func (suite *GenerateE2eTestSuite) TestGenerate() {
Init(logr.Discard(), "/Users/jesseschmidt/Developer/aerospike-admin/lib/live_cluster/client/config-schemas")
asPolicy := aero.NewClientPolicy()
host := aero.NewHost(test.IP, test.PORT_START)
asPolicy.User = "admin"
asPolicy.Password = "admin"
asinfo := info.NewAsInfo(logr.Discard(), host, asPolicy)
genConf, err := GenerateConf(logr.Discard(), asinfo, true)

suite.Assert().Nil(err)

genConfWithDefaults, err := GenerateConf(logr.Discard(), asinfo, false)

suite.Assert().Nil(err)

asconf, err := NewMapAsConfig(logr.Discard(), genConf.version, genConf.conf)

suite.Assert().Nil(err)

asconfWithDefaults, err := NewMapAsConfig(logr.Discard(), genConfWithDefaults.version, genConfWithDefaults.conf)

suite.Assert().Nil(err)

test.RestartAerospikeContainer(test.GetAerospikeContainerName(0), asconf.ToConfFile())

asinfo2 := info.NewAsInfo(logr.Discard(), host, asPolicy)
genConf2, err := GenerateConf(logr.Discard(), asinfo2, true)

suite.Assert().Nil(err)

genConfWithDefaults2, err := GenerateConf(logr.Discard(), asinfo2, false)

suite.Assert().Nil(err)

asconf2, err := NewMapAsConfig(logr.Discard(), genConf2.version, genConf2.conf)

suite.Assert().Nil(err)

asconfWithDefaults2, err := NewMapAsConfig(logr.Discard(), genConfWithDefaults2.version, genConfWithDefaults2.conf)

suite.Assert().Nil(err)
suite.Assert().Equal(asconf, asconf2)
suite.Assert().Equal(asconfWithDefaults, asconfWithDefaults2)
}

func TestGenerateE2ETestSuiteSuite(t *testing.T) {
suite.Run(t, new(GenerateE2eTestSuite))
}
11 changes: 6 additions & 5 deletions asconfig/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"github.com/stretchr/testify/suite"
)

type GenerateTestSuite struct {
type GenerateUnitTestSuite struct {
suite.Suite
mockGetter *MockConfGetter
// asinfo *AsInfo
ctrl *gomock.Controller
}

func (suite *GenerateTestSuite) SetupTest() {
func (suite *GenerateUnitTestSuite) SetupTest() {
suite.ctrl = gomock.NewController(suite.T())
suite.mockGetter = NewMockConfGetter(suite.ctrl)
}
Expand Down Expand Up @@ -44,7 +44,7 @@ func convertIntToInt64(conf Conf) Conf {
return conf
}

func (suite *GenerateTestSuite) TestGenerate() {
func (suite *GenerateUnitTestSuite) TestGenerate() {
testCases := []GenerateTC{
loggingTC,
namespaceTC,
Expand All @@ -71,17 +71,18 @@ func (suite *GenerateTestSuite) TestGenerate() {
suite.mockGetter.EXPECT().AllConfigs().Return(convertIntToInt64(tc.allConfigs), nil)
suite.mockGetter.EXPECT().GetAsInfo("metadata").Return(tc.metadata, nil)
logger := logr.Discard()
expected := newGenConf(convertIntToInt64(tc.expected), tc.metadata["metadata"].(Conf)["asd_build"].(string))

actual, err := GenerateConf(logger, suite.mockGetter, tc.removeDefaults)

suite.Assert().Nil(err)
suite.Assert().Equal(convertIntToInt64(tc.expected), actual)
suite.Assert().Equal(expected, actual)
})
}
}

func TestGenerateTestSuiteSuite(t *testing.T) {
suite.Run(t, new(GenerateTestSuite))
suite.Run(t, new(GenerateUnitTestSuite))
}

var loggingTC = GenerateTC{
Expand Down
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ require (
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/tools v0.7.0 // indirect
)

// Pinned this dependcy to fix vulnerbaility in `golang.org/x/net` pkg
Expand All @@ -26,6 +37,7 @@ replace golang.org/x/net => golang.org/x/net v0.17.0
replace google.golang.org/grpc => google.golang.org/grpc v1.56.3

require (
github.com/docker/docker v24.0.7+incompatible
github.com/golang/protobuf v1.5.3 // indirect
github.com/stretchr/testify v1.8.4
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
Expand Down
Loading

0 comments on commit ac06c31

Please sign in to comment.