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 10e4c4c commit fd87cfc
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
5 changes: 4 additions & 1 deletion go-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ go 1.22.9
require (
github.com/platformsh/cli v0.0.0-20241229194532-b86546247906
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.31.0
)

require (
github.com/ccoveille/go-safecast v1.5.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-chi/chi/v5 v5.1.0 // indirect
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
)

replace github.com/platformsh/cli => ../../cli
4 changes: 2 additions & 2 deletions go-tests/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/ccoveille/go-safecast v1.5.0 h1:cT/3uVQ/i5PTiJvhvkSU81HeKNurtyQtBndXEH3hDg4=
github.com/ccoveille/go-safecast v1.5.0/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -11,8 +13,6 @@ 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/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
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 fd87cfc

Please sign in to comment.