Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test SSH using a mock server #1519

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/platformsh/legacy-cli/tests
go 1.22.9

require (
github.com/platformsh/cli v0.0.0-20241227091635-fea73d95f802
github.com/platformsh/cli v0.0.0-20241229114059-6abd256f63d0
github.com/stretchr/testify v1.9.0
)

Expand All @@ -13,7 +13,7 @@ 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.24.0 // indirect
golang.org/x/sys v0.21.0 // 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
)
16 changes: 8 additions & 8 deletions go-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ 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-20241227091635-fea73d95f802 h1:gkfWMyJ5lVWD3RUY878iiOaltaCUgPwUstLqVo4hmUc=
github.com/platformsh/cli v0.0.0-20241227091635-fea73d95f802/go.mod h1:92H+miZV5F6iXLr/2698VLSZI8X7h+l/xd+i+1JaBAY=
github.com/platformsh/cli v0.0.0-20241229114059-6abd256f63d0 h1:Im0GlY2U42e464pX3V0baCcRGoWihpv1SrHFEPp5U7I=
github.com/platformsh/cli v0.0.0-20241229114059-6abd256f63d0/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=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
84 changes: 84 additions & 0 deletions go-tests/ssh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package tests

import (
"net/http/httptest"
"os/exec"
"strconv"
"testing"

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

func TestSSH(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)
}
sshServer.RemoteEnv = []string{
// TODO use this
"PLATFORM_RELATIONSHIPS=e30K",
}
t.Cleanup(func() {
if err := sshServer.Stop(); err != nil {
t.Error(err)
}
})

projectID := "aiyaikii1uere"

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://[email protected]"}
mainEnv.Links["pf:ssh:app:1"] = mockapi.HALLink{HREF: "ssh://[email protected]"}
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, sshServer.RemoteDir+"\n", f.Run("ssh", "-p", projectID, "-e", ".", "pwd"))

_, stdErr, err := f.RunCombinedOutput("ssh", "-p", projectID, "-e", "main", "--instance", "2", "pwd")
assert.Error(t, err)
assert.Contains(t, stdErr, "Available instances: 0, 1")

_, _, err = f.RunCombinedOutput("ssh", "-p", projectID, "-e", "main", "--instance", "1", "exit 2")
var exitErr *exec.ExitError
assert.ErrorAs(t, err, &exitErr)
assert.Equal(t, 2, exitErr.ExitCode())
}
2 changes: 1 addition & 1 deletion src/Service/SshConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function configureHostKeys()
if (!is_array($additionalKeys)) {
$additionalKeys = explode("\n", $additionalKeys);
}
$hostKeys = rtrim($hostKeys, "\n") . "\n" . $additionalKeys;
$hostKeys = rtrim($hostKeys, "\n") . "\n" . implode("\n", $additionalKeys);
}
if (empty($hostKeys)) {
return null;
Expand Down
Loading