1package testscript
2
3import (
4 "context"
5 "flag"
6 "fmt"
7 "os"
8 "path/filepath"
9 "testing"
10 "time"
11
12 "github.com/charmbracelet/soft-serve/server"
13 "github.com/charmbracelet/soft-serve/server/config"
14 "github.com/charmbracelet/soft-serve/server/test"
15 "github.com/rogpeppe/go-internal/testscript"
16)
17
18var update = flag.Bool("update", false, "update script files")
19
20func TestScript(t *testing.T) {
21 flag.Parse()
22 key, err := filepath.Abs("./testdata/admin1")
23 if err != nil {
24 t.Fatal(err)
25 }
26
27 for _, f := range []string{
28 "admin1",
29 "admin2",
30 "user1",
31 "user2",
32 } {
33 if err := os.Chmod(filepath.Join("./testdata/", f), 0o600); err != nil {
34 t.Fatal(err)
35 }
36 }
37
38 testscript.Run(t, testscript.Params{
39 Dir: "testdata/script",
40 UpdateScripts: *update,
41 Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
42 "soft": func(ts *testscript.TestScript, _ bool, args []string) {
43 args = append([]string{
44 "-F", "/dev/null",
45 "-o", "StrictHostKeyChecking=no",
46 "-o", "UserKnownHostsFile=/dev/null",
47 "-o", "IdentityAgent=none",
48 "-o", "IdentitiesOnly=yes",
49 "-i", key,
50 "-p", ts.Getenv("SSH_PORT"),
51 "localhost",
52 "--",
53 }, args...)
54 ts.Check(ts.Exec("ssh", args...))
55 },
56 },
57 Setup: func(e *testscript.Env) error {
58 sshPort := test.RandomPort()
59 e.Setenv("SSH_PORT", fmt.Sprintf("%d", sshPort))
60 data := t.TempDir()
61 cfg := config.Config{
62 Name: "Test Soft Serve",
63 DataPath: data,
64 InitialAdminKeys: []string{
65 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJI/1tawpdPmzuJcTGTJ+QReqB6cRUdKj4iQIdJUFdrl",
66 },
67 SSH: config.SSHConfig{
68 ListenAddr: fmt.Sprintf("localhost:%d", sshPort),
69 PublicURL: fmt.Sprintf("ssh://localhost:%d", sshPort),
70 KeyPath: filepath.Join(data, "ssh", "soft_serve_host_ed25519"),
71 ClientKeyPath: filepath.Join(data, "ssh", "soft_serve_client_ed25519"),
72 },
73 Git: config.GitConfig{
74 ListenAddr: fmt.Sprintf("localhost:%d", test.RandomPort()),
75 IdleTimeout: 3,
76 MaxConnections: 32,
77 },
78 HTTP: config.HTTPConfig{
79 ListenAddr: fmt.Sprintf("localhost:%d", test.RandomPort()),
80 PublicURL: fmt.Sprintf("http://localhost:%d", test.RandomPort()),
81 },
82 Stats: config.StatsConfig{
83 ListenAddr: fmt.Sprintf("localhost:%d", test.RandomPort()),
84 },
85 Log: config.LogConfig{
86 Format: "text",
87 TimeFormat: time.DateTime,
88 },
89 }
90 ctx := config.WithContext(context.Background(), &cfg)
91 srv, err := server.NewServer(ctx)
92 if err != nil {
93 return err
94 }
95 go func() {
96 if err := srv.Start(); err != nil {
97 e.T().Fatal(err)
98 }
99 }()
100 e.Defer(func() {
101 if err := srv.Shutdown(context.Background()); err != nil {
102 e.T().Fatal(err)
103 }
104 })
105 return nil
106 },
107 })
108}