Skip to content

Commit

Permalink
separate out flags into own module, add flags tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Schmidt committed Jun 5, 2024
1 parent 4795b13 commit 260145e
Show file tree
Hide file tree
Showing 19 changed files with 1,167 additions and 412 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/docker/*
/bin/*
embed_*.go
embed_*.go
/tmp
142 changes: 142 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package cmd

import (
"context"
"fmt"
"os"
"os/exec"
"path"
"strconv"
"strings"
"testing"
"time"

"github.com/aerospike/tools-common-go/testutils"
"github.com/stretchr/testify/suite"
)

var wd, _ = os.Getwd()

type CmdTestSuite struct {
suite.Suite
app string
coverFile string
coverFileCounter int
host string
port int
}

func TestDistanceMetricFlagSuite(t *testing.T) {
suite.Run(t, new(CmdTestSuite))
}

func (suite *CmdTestSuite) SetupSuite() {
suite.app = path.Join(wd, "app.test")
suite.coverFile = path.Join(wd, "../coverage/cmd-coverage.cov")
suite.coverFileCounter = 0
suite.host = "127.0.0.1"
suite.port = 10000

err := docker_compose_up()
if err != nil {
suite.FailNowf("unable to start docker compose up", "%v", err)
}

os.Chdir("..")
goArgs := []string{"test", "-coverpkg", "./...", "-tags=integration", "-o", suite.app}

// Compile test binary
compileCmd := exec.Command("go", goArgs...)
err = compileCmd.Run()
suite.Assert().NoError(err)
}

func (suite *CmdTestSuite) TearDownSuite() {
docker_compose_down()
err := os.Remove(suite.app)
suite.Assert().NoError(err)
time.Sleep(time.Second * 5)
err = testutils.Stop()
suite.Assert().NoError(err)
}

func (suite *CmdTestSuite) runCmd(asvecCmd ...string) ([]string, error) {
strs := strings.Split(suite.coverFile, ".")
file := strs[len(strs)-2] + "-" + strconv.Itoa(suite.coverFileCounter) + "." + strs[len(strs)-1]
suite.coverFileCounter++
var args []string
args = []string{"-test.coverprofile=" + file}
args = append(args, asvecCmd...)

cmd := exec.Command(suite.app, args...)
stdout, err := cmd.Output()
// fmt.Printf("stdout: %v", string(stdout))

if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return []string{string(ee.Stderr)}, err
}
return []string{string(stdout)}, err
}

lines := strings.Split(string(stdout), "\n")

return lines, nil
}

func (suite *CmdTestSuite) TestSuccessfulCreateIndexCmd() {
testCases := []struct {
name string
cmd string
}{
{
"",
"",
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
lines, err := suite.runCmd(strings.Split(tc.cmd, " ")...)
suite.Assert().NoError(err, "error: %s, stdout/err: %s", err, lines)
})
}
}

func docker_compose_up() error {
fmt.Println("Starting docker containers")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

// docker/docker-compose.yml
cmd := exec.CommandContext(ctx, "docker", "compose", fmt.Sprintf("-f /../../docker/docker-compose.yml"), "up", "-d")
output, err := cmd.CombinedOutput()

fmt.Printf("docker compose up output: %s\n", string(output))

if err != nil {
if _, ok := err.(*exec.ExitError); ok {
return err
}
return err
}

return nil
}

func docker_compose_down() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

cmd := exec.CommandContext(ctx, "docker", "compose", fmt.Sprintf("-f /../../docker/docker-compose.yml"), "down", "-d")
_, err := cmd.Output()

if err != nil {
if _, ok := err.(*exec.ExitError); ok {
return err
}
return err
}

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

const (
logLevelFlagName = "log-level"
flagNameSeeds = "seeds"
flagNameHost = "host"
flagNameListenerName = "listener-name"
flagNameNamespace = "namespace"
flagNameSets = "sets"
flagNameIndexName = "index-name"
flagNameVectorField = "vector-field"
flagNameDimension = "dimension"
flagNameDistanceMetric = "distance-metric"
flagNameIndexMeta = "index-meta"
flagNameTimeout = "timeout"
flagNameVerbose = "verbose"
flagNameStorageNamespace = "storage-namespace"
flagNameStorageSet = "storage-set"
flagNameMaxEdges = "hnsw-max-edges"
flagNameConstructionEf = "hnsw-ef-construction"
flagNameEf = "hnsw-ef"
flagNameBatchMaxRecords = "hnsw-batch-max-records"
flagNameBatchInterval = "hnsw-batch-interval"
flagNameBatchEnabled = "hnsw-batch-enabled"
)
45 changes: 23 additions & 22 deletions cmd/createIndex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"asvec/cmd/flags"
"context"
"fmt"
"log/slog"
Expand All @@ -19,36 +20,36 @@ import (

//nolint:govet // Padding not a concern for a CLI
var createIndexFlags = &struct {
host *HostPortFlag
seeds *SeedsSliceFlag
listenerName StringOptionalFlag
host *flags.HostPortFlag
seeds *flags.SeedsSliceFlag
listenerName flags.StringOptionalFlag
namespace string
sets []string
indexName string
vectorField string
dimensions uint32
distanceMetric DistanceMetricFlag
distanceMetric flags.DistanceMetricFlag
indexMeta map[string]string
storageNamespace StringOptionalFlag
storageSet StringOptionalFlag
hnswMaxEdges Uint32OptionalFlag
hnswEf Uint32OptionalFlag
hnswConstructionEf Uint32OptionalFlag
hnswBatchMaxRecords Uint32OptionalFlag
hnswBatchInterval Uint32OptionalFlag
hnswBatchEnabled BoolOptionalFlag
storageNamespace flags.StringOptionalFlag
storageSet flags.StringOptionalFlag
hnswMaxEdges flags.Uint32OptionalFlag
hnswEf flags.Uint32OptionalFlag
hnswConstructionEf flags.Uint32OptionalFlag
hnswBatchMaxRecords flags.Uint32OptionalFlag
hnswBatchInterval flags.Uint32OptionalFlag
hnswBatchEnabled flags.BoolOptionalFlag
timeout time.Duration
}{
host: NewDefaultHostPortFlag(),
seeds: &SeedsSliceFlag{},
storageNamespace: StringOptionalFlag{},
storageSet: StringOptionalFlag{},
hnswMaxEdges: Uint32OptionalFlag{},
hnswEf: Uint32OptionalFlag{},
hnswConstructionEf: Uint32OptionalFlag{},
hnswBatchMaxRecords: Uint32OptionalFlag{},
hnswBatchInterval: Uint32OptionalFlag{},
hnswBatchEnabled: BoolOptionalFlag{},
host: flags.NewDefaultHostPortFlag(),
seeds: &flags.SeedsSliceFlag{},
storageNamespace: flags.StringOptionalFlag{},
storageSet: flags.StringOptionalFlag{},
hnswMaxEdges: flags.Uint32OptionalFlag{},
hnswEf: flags.Uint32OptionalFlag{},
hnswConstructionEf: flags.Uint32OptionalFlag{},
hnswBatchMaxRecords: flags.Uint32OptionalFlag{},
hnswBatchInterval: flags.Uint32OptionalFlag{},
hnswBatchEnabled: flags.BoolOptionalFlag{},
}

func newCreateIndexFlagSet() *pflag.FlagSet {
Expand Down
11 changes: 6 additions & 5 deletions cmd/dropIndex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"asvec/cmd/flags"
"context"
"fmt"
"log/slog"
Expand All @@ -18,16 +19,16 @@ import (

//nolint:govet // Padding not a concern for a CLI
var dropIndexFlags = &struct {
host *HostPortFlag
seeds *SeedsSliceFlag
listenerName StringOptionalFlag
host *flags.HostPortFlag
seeds *flags.SeedsSliceFlag
listenerName flags.StringOptionalFlag
namespace string
sets []string
indexName string
timeout time.Duration
}{
host: NewDefaultHostPortFlag(),
seeds: &SeedsSliceFlag{},
host: flags.NewDefaultHostPortFlag(),
seeds: &flags.SeedsSliceFlag{},
}

func newDropIndexFlagSet() *pflag.FlagSet {
Expand Down
Loading

0 comments on commit 260145e

Please sign in to comment.