MCPcopy Index your code
hub / github.com/kopia/kopia / StartServerContext

Function StartServerContext

internal/servertesting/servertesting.go:43–90  ·  view source on GitHub ↗

StartServerContext starts a test server with a given root context and returns APIServerInfo.

(ctx context.Context, t *testing.T, env *repotesting.Environment, tls bool)

Source from the content-addressed store, hash-verified

41
42// StartServerContext starts a test server with a given root context and returns APIServerInfo.
43func StartServerContext(ctx context.Context, t *testing.T, env *repotesting.Environment, tls bool) *repo.APIServerInfo {
44 t.Helper()
45
46 s, err := server.New(ctx, &server.Options{
47 ConfigFile: env.ConfigFile(),
48 PasswordPersist: passwordpersist.File(),
49 Authorizer: auth.LegacyAuthorizer(),
50 Authenticator: auth.CombineAuthenticators(
51 auth.AuthenticateSingleUser(TestUsername+"@"+TestHostname, TestPassword),
52 auth.AuthenticateSingleUser(TestUIUsername, TestUIPassword),
53 ),
54 RefreshInterval: 1 * time.Minute,
55 UIUser: TestUIUsername,
56 UIPreferencesFile: filepath.Join(testutil.TempDirectory(t), "ui-pref.json"),
57 })
58
59 require.NoError(t, err)
60
61 s.SetRepository(ctx, env.Repository)
62
63 // ensure we disconnect the repository before shutting down the server.
64 t.Cleanup(func() { s.SetRepository(ctx, nil) })
65
66 require.NoError(t, err)
67
68 asi := &repo.APIServerInfo{}
69
70 m := mux.NewRouter()
71 s.SetupHTMLUIAPIHandlers(m)
72 s.SetupControlAPIHandlers(m)
73 s.ServeStaticFiles(m, server.AssetFile())
74
75 hs := httptest.NewUnstartedServer(s.GRPCRouterHandler(m))
76 if tls {
77 hs.EnableHTTP2 = true
78 hs.StartTLS()
79 serverHash := sha256.Sum256(hs.Certificate().Raw)
80 asi.BaseURL = hs.URL
81 asi.TrustedServerCertificateFingerprint = hex.EncodeToString(serverHash[:])
82 } else {
83 hs.Start()
84 asi.BaseURL = hs.URL
85 }
86
87 t.Cleanup(hs.Close)
88
89 return asi
90}
91
92// ConnectAndOpenAPIServer creates temporary config file and to and opens API server for testing.
93func ConnectAndOpenAPIServer(t *testing.T, ctx context.Context, asi *repo.APIServerInfo, rco repo.ClientOptions, caching content.CachingOptions, password string, opt *repo.Options) (repo.Repository, error) {

Callers 2

TestNotificationProfileFunction · 0.92
StartServerFunction · 0.85

Calls 15

NewFunction · 0.92
FileFunction · 0.92
LegacyAuthorizerFunction · 0.92
CombineAuthenticatorsFunction · 0.92
AuthenticateSingleUserFunction · 0.92
TempDirectoryFunction · 0.92
AssetFileFunction · 0.92
HelperMethod · 0.80
ConfigFileMethod · 0.80
ServeStaticFilesMethod · 0.80

Tested by 1

TestNotificationProfileFunction · 0.74