script_test.go

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