Skip to content

Commit

Permalink
chore: add server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Apr 7, 2022
1 parent 21b4d4e commit aa1c744
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ cmd/soft/soft
.ssh
.repos
dist
testdata
1 change: 1 addition & 0 deletions server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func softServeMiddleware(ac *appCfg.Config) wish.Middleware {
for _, rp := range ac.Source.AllRepos() {
if rp.Name() == repo {
repoExists = true
break
}
}
if !repoExists {
Expand Down
28 changes: 28 additions & 0 deletions server/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package server

import (
"testing"

"github.com/charmbracelet/soft-serve/internal/config"
"github.com/charmbracelet/wish/testsession"
"github.com/gliderlabs/ssh"
"github.com/matryer/is"
)

var ()

func TestMiddleware(t *testing.T) {
is := is.New(t)
appCfg, err := config.NewConfig(cfg)
is.NoErr(err)
_ = testsession.New(t, &ssh.Server{
Handler: softServeMiddleware(appCfg)(func(s ssh.Session) {
t.Run("TestCatConfig", func(t *testing.T) {
_, err := s.Write([]byte("config/config.json"))
if err == nil {
t.Errorf("Expected error, got nil")
}
})
}),
}, nil)
}
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,8 @@ func (srv *Server) Start() error {
func (srv *Server) Shutdown(ctx context.Context) error {
return srv.SSHServer.Shutdown(ctx)
}

// Close closes the SSH server.
func (srv *Server) Close() error {
return srv.SSHServer.Close()
}
115 changes: 115 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package server

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/charmbracelet/keygen"
"github.com/charmbracelet/soft-serve/config"
"github.com/gliderlabs/ssh"
"github.com/go-git/go-git/v5"
gconfig "github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
gssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/matryer/is"
cssh "golang.org/x/crypto/ssh"
)

var (
testdata = "testdata"
cfg = &config.Config{
BindAddr: "",
Host: "localhost",
Port: 22222,
RepoPath: fmt.Sprintf("%s/repos", testdata),
KeyPath: fmt.Sprintf("%s/key", testdata),
}
pkPath = ""
)

func TestServer(t *testing.T) {
t.Cleanup(func() {
os.RemoveAll(testdata)
})
is := is.New(t)
_, pkPath = createKeyPair(t)
s := setupServer(t)
err := s.Reload()
is.NoErr(err)
t.Run("TestPushRepo", testPushRepo)
t.Run("TestCloneRepo", testCloneRepo)
}

func testPushRepo(t *testing.T) {
is := is.New(t)
rp := t.TempDir()
r, err := git.PlainInit(rp, false)
is.NoErr(err)
wt, err := r.Worktree()
is.NoErr(err)
_, err = wt.Filesystem.Create("testfile")
is.NoErr(err)
_, err = wt.Add("testfile")
is.NoErr(err)
author := &object.Signature{
Name: "test",
Email: "",
}
_, err = wt.Commit("test commit", &git.CommitOptions{
All: true,
Author: author,
Committer: author,
})
is.NoErr(err)
_, err = r.CreateRemote(&gconfig.RemoteConfig{
Name: "origin",
URLs: []string{fmt.Sprintf("ssh://%s:%d/%s", cfg.Host, cfg.Port, "testrepo")},
})
auth, err := gssh.NewPublicKeysFromFile("git", pkPath, "")
is.NoErr(err)
auth.HostKeyCallbackHelper = gssh.HostKeyCallbackHelper{
HostKeyCallback: cssh.InsecureIgnoreHostKey(),
}
err = r.Push(&git.PushOptions{
RemoteName: "origin",
Auth: auth,
})
is.NoErr(err)
}

func testCloneRepo(t *testing.T) {
is := is.New(t)
auth, err := gssh.NewPublicKeysFromFile("git", pkPath, "")
is.NoErr(err)
auth.HostKeyCallbackHelper = gssh.HostKeyCallbackHelper{
HostKeyCallback: cssh.InsecureIgnoreHostKey(),
}
dst := t.TempDir()
_, err = git.PlainClone(dst, false, &git.CloneOptions{
URL: fmt.Sprintf("ssh://%s:%d/config", cfg.Host, cfg.Port),
Auth: auth,
})
is.NoErr(err)
}

func setupServer(t *testing.T) *Server {
s := NewServer(cfg)
go func() {
s.Start()
}()
defer s.Close()
return s
}

func createKeyPair(t *testing.T) (ssh.PublicKey, string) {
is := is.New(t)
t.Helper()
keyDir := t.TempDir()
kp, err := keygen.NewWithWrite(keyDir, "id", nil, keygen.Ed25519)
is.NoErr(err)
pubkey, _, _, _, err := ssh.ParseAuthorizedKey(kp.PublicKey)
is.NoErr(err)
return pubkey, filepath.Join(keyDir, "id_ed25519")
}

0 comments on commit aa1c744

Please sign in to comment.