script_test.go

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