Skip to content

Commit

Permalink
Merge branch 'main' into CLC-245
Browse files Browse the repository at this point in the history
  • Loading branch information
kutluhanmetin committed Aug 24, 2023
2 parents 01b7617 + ef475e6 commit 211d9ae
Show file tree
Hide file tree
Showing 53 changed files with 656 additions and 246 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/nightly_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,4 @@ jobs:
- name: "Run All Tests"
run: |
make test
- name: "Run Coverage"
run: |
make test-cover
make view-cover
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GIT_COMMIT = $(shell git rev-parse HEAD 2> /dev/null || echo unknown)
CLC_VERSION ?= v0.0.0-CUSTOMBUILD
MAIN_CMD_HELP ?= Hazelcast CLC
LDFLAGS = -s -w -X 'github.com/hazelcast/hazelcast-commandline-client/clc/cmd.MainCommandShortHelp=$(MAIN_CMD_HELP)' -X 'github.com/hazelcast/hazelcast-commandline-client/internal.GitCommit=$(GIT_COMMIT)' -X 'github.com/hazelcast/hazelcast-commandline-client/internal.Version=$(CLC_VERSION)' -X 'github.com/hazelcast/hazelcast-go-client/internal.ClientType=CLC' -X 'github.com/hazelcast/hazelcast-go-client/internal.ClientVersion=$(CLC_VERSION)'
TEST_FLAGS ?= -v -count 1 -timeout 30m -race
TEST_FLAGS ?= -count 1 -timeout 30m -race
COVERAGE_OUT = coverage.out
PACKAGES = $(shell go list ./... | grep -v internal/it | tr '\n' ',')
BINARY_NAME ?= clc
Expand Down
6 changes: 6 additions & 0 deletions base/commands/config/config_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func (cm AddCmd) Exec(_ context.Context, ec plug.ExecContext) error {
if ec.Interactive() || ec.Props().GetBool(clc.PropertyVerbose) {
I2(fmt.Fprintf(ec.Stdout(), "Created configuration at: %s\n", filepath.Join(dir, cfgPath)))
}
mopt := config.ConvertKeyValuesToMap(opts)
// ignoring the JSON path for now
_, _, err = config.CreateJSON(target, mopt)
if err != nil {
ec.Logger().Warn("Failed creating the JSON configuration: %s", err.Error())
}
return nil
}

Expand Down
12 changes: 4 additions & 8 deletions base/commands/config/config_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@ func (cm ImportCmd) Init(cc plug.InitContext) error {
short := "Imports configuration from an arbitrary source"
long := `Imports configuration from an arbitrary source
Currently importing only Viridian connection configuration is supported.
Currently importing Viridian connection configuration is supported only.
1. On Viridian console, visit:
Dashboard -> Connect Client -> Quick connection guide -> Python
Dashboard -> Connect Client -> CLI
2. Copy the text in box 1 and pass it as the second parameter.
2. Copy the URL in box 2 and pass it as the second parameter.
Make sure the text is quoted before running:
clc config import my-config "curl https://api.viridian.hazelcast.com ... default.zip"
Alternatively, you can use an already downloaded Python client sample:
clc config import my-config /home/me/Downloads/hazelcast-cloud-python-sample....zip
clc config import my-config "https://api.viridian.hazelcast.com/client_samples/download/..."
`
cc.SetCommandHelp(long, short)
Expand Down
2 changes: 1 addition & 1 deletion base/commands/config/config_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestConfig(t *testing.T) {

func importTest(t *testing.T) {
tcx := it.TestContext{T: t}
const configURL = "https://rcd-download.s3.us-east-2.amazonaws.com/hazelcast-cloud-python-sample-client-pr-FOR_TESTING-default.zip"
const configURL = "https://rcd-download.s3.us-east-2.amazonaws.com/hazelcast-cloud-clc-sample-client-pr-FOR_TESTING-default.zip"
tcx.Tester(func(tcx it.TestContext) {
name := it.NewUniqueObjectName("cfg")
ctx := context.Background()
Expand Down
1 change: 1 addition & 0 deletions base/commands/demo/demo_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestGenerateData(t *testing.T) {
}

func generateData_WikipediaTest(t *testing.T) {
it.MarkFlaky(t, "https://github.com/hazelcast/hazelcast-commandline-client/issues/350")
it.MapTester(t, func(tcx it.TestContext, m *hz.Map) {
t := tcx.T
ctx := context.Background()
Expand Down
31 changes: 14 additions & 17 deletions base/commands/project/project_create_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,22 @@ import (
)

func TestCreateCommand(t *testing.T) {
// TODO: create a temp home and copy the template into it
testDir := filepath.Join(check.MustValue(filepath.Abs("testdata")))
home := filepath.Join(testDir, "home")
tcx := it.TestContext{T: t}
tcx.Tester(func(tcx it.TestContext) {
it.WithEnv(paths.EnvCLCHome, home, func() {
tempDir := check.MustValue(os.MkdirTemp("", "clc-"))
outDir := filepath.Join(tempDir, "my-project")
fixture := filepath.Join(testDir, "fixture", "simple")
defer func() {
// ignoring the error here
_ = os.RemoveAll(outDir)
}()
ctx := context.Background()
// logging to stderr in order to avoid creating the logs directory
cmd := []string{"project", "create", "simple", "-o", outDir, "--log.path", "stderr", "another_key=foo", "key1=bar"}
check.Must(tcx.CLC().Execute(ctx, cmd...))
check.Must(compareDirectories(fixture, outDir))
})
testHomeDir := "testdata/home"
check.Must(paths.CopyDir(testHomeDir, tcx.HomePath()))
tempDir := check.MustValue(os.MkdirTemp("", "clc-"))
outDir := filepath.Join(tempDir, "my-project")
fixtureDir := "testdata/fixture/simple"
defer func() {
// ignoring the error here
_ = os.RemoveAll(outDir)
}()
ctx := context.Background()
// logging to stderr in order to avoid creating the logs directory
cmd := []string{"project", "create", "simple", "-o", outDir, "--log.path", "stderr", "another_key=foo", "key1=bar"}
check.Must(tcx.CLC().Execute(ctx, cmd...))
check.Must(compareDirectories(fixtureDir, outDir))
})
}

Expand Down
8 changes: 4 additions & 4 deletions base/commands/project/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"

"github.com/hazelcast/hazelcast-commandline-client/clc/paths"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
Expand Down Expand Up @@ -103,11 +103,11 @@ func parseYAML(prefix string, yamlFile []byte, result map[string]string) error {
case string:
(result)[fullKey] = val
default:
if _, isMap := val.(map[any]any); !isMap {
if _, isMap := val.(map[string]any); !isMap {
(result)[fullKey] = fmt.Sprintf("%v", val)
}
}
if subMap, isMap := v.(map[any]any); isMap {
if subMap, isMap := v.(map[string]any); isMap {
err = parseYAML(fullKey, marshalYAML(subMap), result)
if err != nil {
return err
Expand All @@ -124,7 +124,7 @@ func joinKeys(prefix, key string) string {
return prefix + "." + key
}

func marshalYAML(m map[any]any) []byte {
func marshalYAML(m map[string]any) []byte {
d, _ := yaml.Marshal(m)
return d
}
Expand Down
34 changes: 29 additions & 5 deletions base/commands/viridian/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,20 @@ func waitClusterState(ctx context.Context, ec plug.ExecContext, api *viridian.AP
func tryImportConfig(ctx context.Context, ec plug.ExecContext, api *viridian.API, clusterID, cfgName string) (configPath string, err error) {
cpv, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
sp.SetText("Importing configuration")
zipPath, stop, err := api.DownloadConfig(ctx, clusterID, "python")
cfgPath, ok, err := importCLCConfig(ctx, ec, api, clusterID, cfgName)
if err != nil {
return nil, err
ec.Logger().Error(err)
} else if ok {
return cfgPath, err
}
defer stop()
cfgPath, err := config.CreateFromZip(ctx, ec, cfgName, zipPath)
ec.Logger().Debugf("could not download CLC configuration, trying the Python configuration.")
cfgPath, ok, err = importPythonConfig(ctx, ec, api, clusterID, cfgName)
if err != nil {
return nil, err
}
cfgDir, _ := filepath.Split(cfgPath)
// import the Java/.Net certificates
zipPath, stop, err = api.DownloadConfig(ctx, clusterID, "java")
zipPath, stop, err := api.DownloadConfig(ctx, clusterID, "java")
if err != nil {
return nil, err
}
Expand All @@ -173,6 +175,28 @@ func tryImportConfig(ctx context.Context, ec plug.ExecContext, api *viridian.API
return cp, nil
}

func importCLCConfig(ctx context.Context, ec plug.ExecContext, api *viridian.API, clusterID, cfgName string) (configPath string, ok bool, err error) {
return importConfig(ctx, ec, api, clusterID, cfgName, "clc", config.CreateFromZip)
}

func importPythonConfig(ctx context.Context, ec plug.ExecContext, api *viridian.API, clusterID, cfgName string) (configPath string, ok bool, err error) {
return importConfig(ctx, ec, api, clusterID, cfgName, "python", config.CreateFromZipLegacy)
}

func importConfig(ctx context.Context, ec plug.ExecContext, api *viridian.API, clusterID, cfgName, language string, f func(ctx context.Context, ec plug.ExecContext, target, path string) (string, bool, error)) (configPath string, ok bool, err error) {
zipPath, stop, err := api.DownloadConfig(ctx, clusterID, language)
if err != nil {
return "", false, err
}
defer stop()
cfgPath, ok, err := f(ctx, ec, cfgName, zipPath)
if err != nil {
return "", false, err
}
return cfgPath, ok, nil

}

// importFileFromZip extracts files matching selectPaths to targetDir
// Note that this function assumes a Viridian sample zip file.
func importFileFromZip(ctx context.Context, ec plug.ExecContext, selectPaths *types.Set[string], zipPath, targetDir string) (imported *types.Set[string], err error) {
Expand Down
41 changes: 0 additions & 41 deletions base/commands/viridian/download_logs_it_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,3 @@
//go:build std || viridian

package viridian_test

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/hazelcast/hazelcast-commandline-client/clc/paths"
"github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/it"
)

func downloadLogs_NonInteractiveTest(t *testing.T) {
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
dir := check.MustValue(os.MkdirTemp("", "log"))
defer func() { check.Must(os.RemoveAll(dir)) }()
c := createOrGetClusterWithState(ctx, tcx, "RUNNING")
tcx.WithReset(func() {
tcx.CLCExecute(ctx, "viridian", "download-logs", c.ID, "--output-dir", dir)
tcx.AssertStderrContains("OK")
require.FileExists(t, paths.Join(dir, "node-1.log"))
})
})
}

func downloadLogs_InteractiveTest(t *testing.T) {
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
dir := check.MustValue(os.MkdirTemp("", "log"))
defer func() { check.Must(os.RemoveAll(dir)) }()
t.Logf("Downloading to directory: %s", dir)
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
c := createOrGetClusterWithState(ctx, tcx, "RUNNING")
tcx.WriteStdinf("\\viridian download-logs %s -o %s\n", c.Name, dir)
tcx.AssertStderrContains("OK")
require.FileExists(t, paths.Join(dir, "node-1.log"))
})
})
})
}
49 changes: 43 additions & 6 deletions base/commands/viridian/viridian_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package viridian_test
import (
"context"
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -59,7 +60,7 @@ func TestViridian(t *testing.T) {
{"resumeCluster_NonInteractive", resumeCluster_NonInteractiveTest},
{"stopCluster_Interactive", stopCluster_InteractiveTest},
{"stopCluster_NonInteractive", stopCluster_NonInteractiveTest},
{"streamLogs_nonInteractive", streamLogs_nonInteractiveTest},
{"streamLogs_NonInteractive", streamLogs_NonInteractiveTest},
}
for _, tc := range testCases {
t.Run(tc.name, tc.f)
Expand All @@ -73,12 +74,13 @@ func loginWithParams_NonInteractiveTest(t *testing.T) {
}
tcx.Tester(func(tcx it.TestContext) {
ctx := context.Background()
tcx.CLCExecute(ctx, "viridian", "login", "--api-key", it.ViridianAPIKey(), "--api-secret", it.ViridianAPISecret())
tcx.CLCExecute(ctx, "viridian", "login", "--api-base", "dev2", "--api-key", it.ViridianAPIKey(), "--api-secret", it.ViridianAPISecret())
tcx.AssertStdoutContains("Viridian token was fetched and saved.")
})
}

func loginWithParams_InteractiveTest(t *testing.T) {
t.Skipf("Skipping interactive Viridian tests")
tcx := it.TestContext{
T: t,
UseViridian: true,
Expand All @@ -87,7 +89,7 @@ func loginWithParams_InteractiveTest(t *testing.T) {
ctx := context.Background()
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
tcx.WriteStdinf("\\viridian login --api-key %s --api-secret %s\n", it.ViridianAPIKey(), it.ViridianAPISecret())
tcx.WriteStdinf("\\viridian login --api-base dev2 --api-key %s --api-secret %s\n", it.ViridianAPIKey(), it.ViridianAPISecret())
tcx.AssertStdoutContains("Viridian token was fetched and saved.")
})
})
Expand Down Expand Up @@ -122,6 +124,7 @@ func listClusters_NonInteractiveTest(t *testing.T) {
}

func listClusters_InteractiveTest(t *testing.T) {
t.Skipf("Skipping interactive Viridian tests")
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
Expand All @@ -146,12 +149,12 @@ func createCluster_NonInteractiveTest(t *testing.T) {
cs := check.MustValue(tcx.Viridian.ListClusters(ctx))
cid := cs[0].ID
tcx.AssertStdoutDollar(fmt.Sprintf("$%s$%s$", cid, clusterName))
check.Must(waitState(ctx, tcx, cid, "RUNNING"))
require.True(t, paths.Exists(paths.ResolveConfigDir(clusterName)))
})
}

func createCluster_InteractiveTest(t *testing.T) {
t.Skipf("Skipping interactive Viridian tests")
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
tcx.WithShell(ctx, func(tcx it.TestContext) {
ensureNoClusterRunning(ctx, tcx)
Expand Down Expand Up @@ -180,6 +183,7 @@ func stopCluster_NonInteractiveTest(t *testing.T) {
}

func stopCluster_InteractiveTest(t *testing.T) {
t.Skipf("Skipping interactive Viridian tests")
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
Expand All @@ -202,6 +206,7 @@ func resumeCluster_NonInteractiveTest(t *testing.T) {
}

func resumeCluster_InteractiveTest(t *testing.T) {
t.Skipf("Skipping interactive Viridian tests")
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
Expand All @@ -226,6 +231,7 @@ func getCluster_NonInteractiveTest(t *testing.T) {
}

func getCluster_InteractiveTest(t *testing.T) {
t.Skipf("Skipping interactive Viridian tests")
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
Expand All @@ -252,7 +258,7 @@ func deleteCluster_NonInteractiveTest(t *testing.T) {
}

func deleteCluster_InteractiveTest(t *testing.T) {
t.Skip()
t.Skipf("Skipping interactive Viridian tests")
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
Expand All @@ -270,14 +276,45 @@ func deleteCluster_InteractiveTest(t *testing.T) {
})
}

func downloadLogs_NonInteractiveTest(t *testing.T) {
t.Skipf("skipping this test until the reason of failure is determined")
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
dir := check.MustValue(os.MkdirTemp("", "log"))
defer func() { check.Must(os.RemoveAll(dir)) }()
c := createOrGetClusterWithState(ctx, tcx, "RUNNING")
tcx.WithReset(func() {
tcx.CLCExecute(ctx, "viridian", "download-logs", c.ID, "--output-dir", dir)
tcx.AssertStderrContains("OK")
require.FileExists(t, paths.Join(dir, "node-1.log"))
})
})
}

func downloadLogs_InteractiveTest(t *testing.T) {
t.Skipf("Skipping interactive Viridian tests")
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
dir := check.MustValue(os.MkdirTemp("", "log"))
defer func() { check.Must(os.RemoveAll(dir)) }()
t.Logf("Downloading to directory: %s", dir)
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
c := createOrGetClusterWithState(ctx, tcx, "RUNNING")
tcx.WriteStdinf("\\viridian download-logs %s -o %s\n", c.Name, dir)
tcx.AssertStderrContains("OK")
require.FileExists(t, paths.Join(dir, "node-1.log"))
})
})
})
}

func viridianTester(t *testing.T, f func(ctx context.Context, tcx it.TestContext)) {
tcx := it.TestContext{
T: t,
UseViridian: true,
}
tcx.Tester(func(tcx it.TestContext) {
ctx := context.Background()
tcx.CLCExecute(ctx, "viridian", "login", "--api-key", it.ViridianAPIKey(), "--api-secret", it.ViridianAPISecret())
tcx.CLCExecute(ctx, "viridian", "--api-base", "dev2", "login", "--api-key", it.ViridianAPIKey(), "--api-secret", it.ViridianAPISecret())
tcx.AssertStdoutContains("Viridian token was fetched and saved.")
tcx.WithReset(func() {
f(ctx, tcx)
Expand Down
3 changes: 2 additions & 1 deletion base/commands/viridian/viridian_it_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
"github.com/hazelcast/hazelcast-commandline-client/internal/it"
)

func streamLogs_nonInteractiveTest(t *testing.T) {
func streamLogs_NonInteractiveTest(t *testing.T) {
t.Skipf("skipping this test until the reason of failure is determined")
viridianTester(t, func(ctx context.Context, tcx it.TestContext) {
c := createOrGetClusterWithState(ctx, tcx, "RUNNING")
go func() {
Expand Down
Loading

0 comments on commit 211d9ae

Please sign in to comment.