script_test.go

 1package testscript
 2
 3import (
 4	"context"
 5	"fmt"
 6	"path/filepath"
 7	"testing"
 8
 9	"github.com/charmbracelet/soft-serve/server"
10	"github.com/charmbracelet/soft-serve/server/config"
11	"github.com/charmbracelet/soft-serve/server/test"
12	"github.com/rogpeppe/go-internal/testscript"
13)
14
15func TestScript(t *testing.T) {
16	key, err := filepath.Abs("./testdata/admin1")
17	if err != nil {
18		t.Fatal(err)
19	}
20
21	testscript.Run(t, testscript.Params{
22		Dir:           "testdata/script",
23		UpdateScripts: true,
24		Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
25			"soft": func(ts *testscript.TestScript, _ bool, args []string) {
26				args = append([]string{
27					"-F", "/dev/null",
28					"-o", "StrictHostKeyChecking=no",
29					"-o", "UserKnownHostsFile=/dev/null",
30					"-o", "IdentityAgent=none",
31					"-o", "IdentitiesOnly=yes",
32					"-i", key,
33					"-p", ts.Getenv("SSH_PORT"),
34					"localhost",
35					"--",
36				}, args...)
37				ts.Check(ts.Exec("ssh", args...))
38			},
39		},
40		Setup: func(e *testscript.Env) error {
41			cfg := config.DefaultConfig()
42			cfg.InitialAdminKeys = []string{
43				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJI/1tawpdPmzuJcTGTJ+QReqB6cRUdKj4iQIdJUFdrl",
44			}
45			cfg.DataPath = t.TempDir()
46
47			sshPort := test.RandomPort()
48			e.Setenv("SSH_PORT", fmt.Sprintf("%d", sshPort))
49			cfg.SSH.ListenAddr = fmt.Sprintf("localhost:%d", sshPort)
50			cfg.HTTP.ListenAddr = fmt.Sprintf("localhost:%d", test.RandomPort())
51			cfg.Git.ListenAddr = fmt.Sprintf("localhost:%d", test.RandomPort())
52			cfg.Stats.ListenAddr = fmt.Sprintf("localhost:%d", test.RandomPort())
53			ctx := config.WithContext(context.Background(), cfg)
54			srv, err := server.NewServer(ctx)
55			if err != nil {
56				return err
57			}
58			go func() {
59				if err := srv.Start(); err != nil {
60					e.T().Fatal(err)
61				}
62			}()
63			e.Defer(func() {
64				if err := srv.Shutdown(context.Background()); err != nil {
65					e.T().Fatal(err)
66				}
67			})
68			return nil
69		},
70	})
71}