Skip to content

Commit

Permalink
fix golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Schmidt committed Dec 11, 2023
1 parent 6535f64 commit 707d2e3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 41 deletions.
4 changes: 2 additions & 2 deletions asconfig/conffilereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ func parseValue(k string, val interface{}) interface{} {
return value
} else if value, err := strconv.ParseBool(valStr); err == nil {
return value
} else {
return valStr
}

return valStr
}

func process(log logr.Logger, scanner *bufio.Scanner, conf Conf) (Conf, error) {
Expand Down
5 changes: 5 additions & 0 deletions asconfig/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (s *copyEffectiveRackIDStep) execute(conf Conf) error {
if rackIDStr == "" {
err := fmt.Errorf("unable to find rack id for rack %s", rack)
s.log.V(-1).Error(err, "Error copying effective rack-id to rack-id")

return err
}

Expand All @@ -288,6 +289,7 @@ func (s *copyEffectiveRackIDStep) execute(conf Conf) error {
if err != nil {
err := fmt.Errorf("unable to convert rack id %s to int", rackIDStr)
s.log.V(-1).Error(err, "Error copying effective rack-id to rack-id")

return err
}

Expand Down Expand Up @@ -601,6 +603,7 @@ func (s *transformKeyValuesStep) execute(conf Conf) error {
if val, ok := newFlatConf[newKey].([]string); !ok || len(val) <= index {
err := fmt.Errorf("shadow key %s does not have a corresponding device yet", key)
s.log.V(-1).Error(err, "Error converting shadow device to list")

return err
}

Expand Down Expand Up @@ -684,12 +687,14 @@ func (s *removeSecurityIfDisabledStep) execute(conf Conf) error {
if !ok {
err := fmt.Errorf("enable-security is not a boolean")
s.log.V(-1).Error(err, "Error removing security configs")

return err
}

cmp, err := lib.CompareVersions(build, "5.7.0")
if err != nil {
s.log.V(-1).Error(err, "Error removing security configs")

return err
}

Expand Down
8 changes: 4 additions & 4 deletions asconfig/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func normalizeFlatSchema(flatSchema map[string]interface{}) map[string]interface

// getDynamicSchema return the map of values which are dynamic
// values.
func getDynamicSchema(flatSchema map[string]interface{}) (map[string]bool, error) {
func getDynamicSchema(flatSchema map[string]interface{}) map[string]bool {
dynMap := make(map[string]bool)

for k, v := range flatSchema {
Expand All @@ -162,7 +162,7 @@ func getDynamicSchema(flatSchema map[string]interface{}) (map[string]bool, error
}
}

return dynMap, nil
return dynMap
}

// getDefaultSchema return the map of values which are dynamic
Expand Down Expand Up @@ -211,7 +211,7 @@ func getDefaultSchema(flatSchema map[string]interface{}) map[string]interface{}
// getRequiredSchema returns a slice of slices of required keys for a given context.
// Multiple slices are required because the required keys can be different
// depending con the "type" of the context.
func getRequiredSchema(flatSchema map[string]interface{}) (map[string][][]string, error) {
func getRequiredSchema(flatSchema map[string]interface{}) map[string][][]string {
keys := sortKeys(flatSchema)
reqMap := make(map[string][][]string) // We end up with 8 keys with a 6.4 schema.

Expand All @@ -233,7 +233,7 @@ func getRequiredSchema(flatSchema map[string]interface{}) (map[string][][]string
}
}

return reqMap, nil
return reqMap
}

func flattenSchema(input map[string]interface{}, sep string) map[string]interface{} {
Expand Down
2 changes: 1 addition & 1 deletion asconfig/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var sectionNameStartChar = '{'
var sectionNameEndChar = '}'

// expandConf expands map with flat keys (with sep) to Conf
func expandConf(log logr.Logger, input *Conf, sep string) Conf {
func expandConf(log logr.Logger, input *Conf, sep string) Conf { //nolint:unparam // We should think about removing the arg 'sep'
m := expandConfMap(log, input, sep)
return expandConfList(log, m)
}
Expand Down
112 changes: 79 additions & 33 deletions test/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import (
"github.com/docker/go-connections/nat"
)

var CLUSTER_NAME = "mgmt-lib-test"
var PORT_START = 10000
var ClusterName = "mgmt-lib-test"
var PortStart = 10000
var IP = "127.0.0.1"
var WORK_DIR_ABS = "test/work"
var IMAGE = "aerospike/aerospike-server-enterprise:7.0.0.2"
var CONTAINER_PREFIX = "aerospike_mgmt_lib_test_"
var WordDirAbs = "test/work"
var Image = "aerospike/aerospike-server-enterprise:7.0.0.2"
var ContainerPrefix = "aerospike_mgmt_lib_test_"
var User = "admin"
var Password = "admin"

var configTemplate = fmt.Sprintf(`
security {
Expand Down Expand Up @@ -82,50 +84,55 @@ namespace test {
data-size 1G
}
}
`, CLUSTER_NAME)
`, ClusterName)

type AerospikeContainer struct {
ip string
portBase int
configPath string
portBase int
}

type Containers_ struct {
type Containers struct {
namesToContainers map[string]*AerospikeContainer
dockerCLI *client.Client
dockerCTX context.Context
workDir string
}

var containers = &Containers_{make(map[string]*AerospikeContainer), nil, nil, ""}
var containers = &Containers{make(map[string]*AerospikeContainer), nil, nil, ""}

func Start(size int) error {
cli, _ := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
ctx := context.Background()
containers.dockerCLI = cli
containers.dockerCTX = ctx
containers.workDir, _ = filepath.Abs(WORK_DIR_ABS)
reader, err := cli.ImagePull(ctx, IMAGE, types.ImagePullOptions{})
containers.workDir, _ = filepath.Abs(WordDirAbs)
reader, err := cli.ImagePull(ctx, Image, types.ImagePullOptions{})

if err != nil {
log.Printf("Unable to pull aerospike image: %s", err)
return err
}

defer reader.Close()
io.Copy(os.Stdout, reader)
_, err = io.Copy(os.Stdout, reader)

if err != nil {
log.Printf("Unable to pull aerospike image: %s", err)
return err
}

for i := 0; i < size; i++ {
peerConnection := ""

if i > 0 {
peerConnection = fmt.Sprintf("mesh-seed-address-port %s %d", IP, PORT_START+2)
peerConnection = fmt.Sprintf("mesh-seed-address-port %s %d", IP, PortStart+2)
}

name := GetAerospikeContainerName(i)
RmAerospikeContainer(name)
RmAerospikeContainer(name) //nolint:errcheck // Removing containers just in case they were left over from a previous run

asContainer, err := RunAerospikeContainer(i, name, IP, PORT_START+(i*4), peerConnection)
asContainer, err := RunAerospikeContainer(i, name, IP, PortStart+(i*4), peerConnection)

if err != nil {
log.Printf("Unable to start testing containers")
Expand All @@ -140,8 +147,14 @@ func Start(size int) error {

func Stop() error {
log.Println("Stopping test containers")

for name := range containers.namesToContainers {
RmAerospikeContainer(name)
err := RmAerospikeContainer(name)

if err != nil {
log.Printf("Unable to remove container %s: %s", name, err)
return err
}
}

abs, _ := filepath.Abs(containers.workDir)
Expand All @@ -156,10 +169,10 @@ func Stop() error {
}

func GetAerospikeContainerName(index int) string {
return CONTAINER_PREFIX + fmt.Sprintf("%d", index)
return ContainerPrefix + fmt.Sprintf("%d", index)
}

func createConfigFile(portBase int, accessAddress string, peerConnection string) (string, error) {
func createConfigFile(portBase int, accessAddress, peerConnection string) (string, error) {
templateInput := struct {
FeaturePath string
AccessAddress string
Expand All @@ -183,7 +196,13 @@ func createConfigFile(portBase int, accessAddress string, peerConnection string)

tmpl, _ := template.New("config").Parse(configTemplate)

os.MkdirAll(containers.workDir, 0755)
err := os.MkdirAll(containers.workDir, 0o755)

if err != nil {
log.Printf("Unable to create work directory: %s", err)
return "", err
}

file, err := os.CreateTemp(containers.workDir, "aerospike_*.conf")

if err != nil {
Expand All @@ -193,7 +212,12 @@ func createConfigFile(portBase int, accessAddress string, peerConnection string)

defer file.Close()

tmpl.Execute(file, templateInput)
err = tmpl.Execute(file, templateInput)

if err != nil {
log.Printf("Unable to create config file using template: %s", err)
return "", err
}

return file.Name(), nil
}
Expand All @@ -216,7 +240,12 @@ func StopAerospikeContainer(name string) error {
return nil
}

func RunAerospikeContainer(index int, name string, ip string, portBase int, peerConnection string) (*AerospikeContainer, error) {
func RunAerospikeContainer(
index int,
name,
ip string,
portBase int,
peerConnection string) (*AerospikeContainer, error) {
ctx := containers.dockerCTX
cli := containers.dockerCLI

Expand All @@ -232,7 +261,14 @@ func RunAerospikeContainer(index int, name string, ip string, portBase int, peer
containerWorkDir := "/opt/" + containers.workDir
containerConfFile := containerWorkDir + "/" + filepath.Base(confFile)

cmd := []string{"/usr/bin/asd", "--foreground", "--config-file", containerConfFile, "--instance", fmt.Sprintf("%d", index)}
cmd := []string{
"/usr/bin/asd",
"--foreground",
"--config-file",
containerConfFile,
"--instance",
fmt.Sprintf("%d", index),
}

// Uncomment if multi-node EE tests are needed
// featKey := os.Getenv("FEATKEY")
Expand All @@ -248,7 +284,7 @@ func RunAerospikeContainer(index int, name string, ip string, portBase int, peer
}

config := container.Config{
Image: IMAGE,
Image: Image,
Hostname: ip,
Cmd: cmd,
Tty: true,
Expand Down Expand Up @@ -286,7 +322,7 @@ func RunAerospikeContainer(index int, name string, ip string, portBase int, peer
},
}

if _, err := cli.ContainerCreate(ctx, &config, hostConfig, nil, nil, name); err != nil {
if _, err = cli.ContainerCreate(ctx, &config, hostConfig, nil, nil, name); err != nil {
log.Printf("Unable to create container %s: %s", name, err)
return nil, err
}
Expand All @@ -302,11 +338,12 @@ func RunAerospikeContainer(index int, name string, ip string, portBase int, peer

log.Printf("Started container %s with IP %s", name, inspect.NetworkSettings.IPAddress)
log.Printf("Waiting for asd %s to start", name)

startTime := time.Now()
timeout := 10 * time.Second
policy := aerospike.NewClientPolicy()
policy.User = "admin"
policy.Password = "admin"
policy.User = User
policy.Password = Password

for {
asClient, err := aerospike.NewClientWithPolicy(
Expand All @@ -316,6 +353,7 @@ func RunAerospikeContainer(index int, name string, ip string, portBase int, peer
if asClient.IsConnected() {
break
}

asClient.Close()
}

Expand All @@ -328,7 +366,7 @@ func RunAerospikeContainer(index int, name string, ip string, portBase int, peer
time.Sleep(1 * time.Second)
}

return &AerospikeContainer{inspect.NetworkSettings.IPAddress, portBase, confFile}, nil
return &AerospikeContainer{inspect.NetworkSettings.IPAddress, confFile, portBase}, nil
}

func StartAerospikeContainer(name string) (string, error) {
Expand All @@ -340,9 +378,10 @@ func StartAerospikeContainer(name string) (string, error) {
return "", err
}

inspect, _ := cli.ContainerInspect(ctx, string(name))
inspect, _ := cli.ContainerInspect(ctx, name)

log.Printf("Started container %s with IP %s", name, inspect.NetworkSettings.IPAddress)

return inspect.NetworkSettings.IPAddress, nil
}

Expand All @@ -353,7 +392,7 @@ func RestartAerospikeContainer(name, confFileContents string) error {

if confFileContents != "" {
confPath := containers.namesToContainers[name].configPath
file, err := os.OpenFile(confPath, os.O_TRUNC|os.O_WRONLY, 0644)
file, err := os.OpenFile(confPath, os.O_TRUNC|os.O_WRONLY, 0o644)

if err != nil {
log.Printf("Unable to open config file %s: %s", confPath, err)
Expand All @@ -368,24 +407,31 @@ func RestartAerospikeContainer(name, confFileContents string) error {
}
}

cli.ContainerRestart(containers.dockerCTX, name, container.StopOptions{})
err := cli.ContainerRestart(containers.dockerCTX, name, container.StopOptions{})

if err != nil {
log.Printf("Unable to restart container %s: %s", name, err)
return err
}

log.Printf("Restarted container %s with IP %s", name, ip)
log.Printf("Waiting for asd %s to start", name)

startTime := time.Now()
timeout := 10 * time.Second
policy := aerospike.NewClientPolicy()
policy.User = "admin"
policy.Password = "admin"
policy.User = User
policy.Password = Password

for {
asClient, err := aerospike.NewClientWithPolicy(
policy, IP, PORT_START)
policy, IP, PortStart)

if err == nil {
if asClient.IsConnected() {
break
}

asClient.Close()
}

Expand Down
2 changes: 1 addition & 1 deletion test/generate_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (suite *GenerateE2eTestSuite) SetupTest() {

func (suite *GenerateE2eTestSuite) TestGenerate() {
asPolicy := aero.NewClientPolicy()
host := aero.NewHost(IP, PORT_START)
host := aero.NewHost(IP, PortStart)
asPolicy.User = "admin"
asPolicy.Password = "admin"

Expand Down

0 comments on commit 707d2e3

Please sign in to comment.