Skip to content

Commit

Permalink
Add test for Valkey command
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Jan 15, 2025
1 parent fc38278 commit dd7a75d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 9 deletions.
4 changes: 2 additions & 2 deletions go-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module github.com/platformsh/legacy-cli/tests
go 1.22.9

require (
github.com/platformsh/cli v0.0.0-20241229194532-b86546247906
github.com/platformsh/cli v0.0.0-20250115143921-13620b548b78
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.31.0
)

require (
Expand All @@ -13,7 +14,6 @@ require (
github.com/kr/pretty v0.3.1 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 2 additions & 2 deletions go-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/platformsh/cli v0.0.0-20241229194532-b86546247906 h1:BvQf2yvT7USm80fAJ0C5FXfTfjjK2onrnoVMPz5CT9k=
github.com/platformsh/cli v0.0.0-20241229194532-b86546247906/go.mod h1:jMxyJGLMlkjDq7l9cVWFhsX/8xxFqEOH95rWbDFyO08=
github.com/platformsh/cli v0.0.0-20250115143921-13620b548b78 h1:DMiecWK6l+AUokNtDKXht1Mg1xSzMPaE9ogow3tV2uQ=
github.com/platformsh/cli v0.0.0-20250115143921-13620b548b78/go.mod h1:b1v98rkg8bScSoo5gK8Fc1qRta1FUU8wElRf+bGvV5Y=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
Expand Down
8 changes: 3 additions & 5 deletions go-tests/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"net/http/httptest"
"os"
"os/exec"
"strconv"
"testing"
Expand All @@ -21,10 +22,6 @@ func TestSSH(t *testing.T) {
if err != nil {
t.Fatal(err)
}
sshServer.RemoteEnv = []string{
// TODO use this
"PLATFORM_RELATIONSHIPS=e30K",
}
t.Cleanup(func() {
if err := sshServer.Stop(); err != nil {
t.Error(err)
Expand Down Expand Up @@ -71,7 +68,8 @@ func TestSSH(t *testing.T) {
}

f.Run("cc")
assert.Equal(t, sshServer.RemoteDir+"\n", f.Run("ssh", "-p", projectID, "-e", ".", "pwd"))
wd, _ := os.Getwd()
assert.Equal(t, wd+"\n", f.Run("ssh", "-p", projectID, "-e", ".", "pwd"))

_, stdErr, err := f.RunCombinedOutput("ssh", "-p", projectID, "-e", "main", "--instance", "2", "pwd")
assert.Error(t, err)
Expand Down
115 changes: 115 additions & 0 deletions go-tests/valkey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package tests

import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"

"golang.org/x/crypto/ssh"

"github.com/platformsh/cli/pkg/mockapi"
"github.com/platformsh/cli/pkg/mockssh"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestValkey(t *testing.T) {
authServer := mockapi.NewAuthServer(t)
defer authServer.Close()

myUserID := "my-user-id"

sshServer, err := mockssh.NewServer(t, authServer.URL+"/ssh/authority")
if err != nil {
t.Fatal(err)
}

relationships := map[string]any{
"cache": []map[string]any{{
"username": nil,
"host": "cache.internal",
"path": nil,
"query": url.Values{},
"password": nil,
"port": 6379,
"service": "cache",
"scheme": "valkey",
"type": "valkey:8.0",
"public": false,
}},
}
relationshipsJSON, err := json.Marshal(relationships)
require.NoError(t, err)

execHandler := mockssh.ExecHandler(t.TempDir(), []string{
"PLATFORM_RELATIONSHIPS=" + base64.StdEncoding.EncodeToString(relationshipsJSON),
})

sshServer.CommandHandler = func(conn ssh.ConnMetadata, command string, io mockssh.CommandIO) int {
if strings.HasPrefix(command, "valkey-cli") {
_, _ = fmt.Fprint(io.StdOut, "Received command: "+command)
return 0
}

return execHandler(conn, command, io)
}
t.Cleanup(func() {
if err := sshServer.Stop(); err != nil {
t.Error(err)
}
})

projectID := mockapi.ProjectID()

apiHandler := mockapi.NewHandler(t)
apiHandler.SetMyUser(&mockapi.User{ID: myUserID})
apiHandler.SetProjects([]*mockapi.Project{
{
ID: projectID,
Links: mockapi.MakeHALLinks(
"self=/projects/"+projectID,
"environments=/projects/"+projectID+"/environments",
),
DefaultBranch: "main",
},
})
mainEnv := makeEnv(projectID, "main", "production", "active", nil)
mainEnv.SetCurrentDeployment(&mockapi.Deployment{
WebApps: map[string]mockapi.App{
"app": {Name: "app", Type: "golang:1.23", Size: "M", Disk: 2048, Mounts: map[string]mockapi.Mount{}},
},
Services: map[string]mockapi.App{},
Workers: map[string]mockapi.Worker{},
Routes: mockRoutes(),
Links: mockapi.MakeHALLinks("self=/projects/" + projectID + "/environments/main/deployment/current"),
})
mainEnv.Links["pf:ssh:app:0"] = mockapi.HALLink{HREF: "ssh://app--0@ssh.cli-tests.example.com"}
apiHandler.SetEnvironments([]*mockapi.Environment{
mainEnv,
})

apiServer := httptest.NewServer(apiHandler)
defer apiServer.Close()

f := newCommandFactory(t, apiServer.URL, authServer.URL)
f.extraEnv = []string{
EnvPrefix + "SSH_OPTIONS=HostName 127.0.0.1\nPort " + strconv.Itoa(sshServer.Port()),
EnvPrefix + "SSH_HOST_KEYS=" + sshServer.HostKeyConfig(),
}

f.Run("cc")

assert.Equal(t, "Received command: valkey-cli -h cache.internal -p 6379 ping",
f.Run("valkey", "-p", projectID, "-e", ".", "ping"))

assert.Equal(t, "Received command: valkey-cli -h cache.internal -p 6379 --scan",
f.Run("valkey", "-p", projectID, "-e", ".", "--", "--scan"))

assert.Equal(t, "Received command: valkey-cli -h cache.internal -p 6379 --scan --pattern '*-11*'",
f.Run("valkey", "-p", projectID, "-e", ".", "--", "--scan --pattern '*-11*'"))
}

0 comments on commit dd7a75d

Please sign in to comment.