1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
package singleprocess
import (
"bytes"
"context"
"io/ioutil"
"os"
"path/filepath"
"github.com/hashicorp/go-hclog"
"github.com/imdario/mergo"
"github.com/mitchellh/go-testing-interface"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/require"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant/internal/server"
pb "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
"github.com/hashicorp/vagrant/internal/server/singleprocess/state"
"github.com/hashicorp/vagrant/internal/serverclient"
)
// TestServer starts a singleprocess server and returns the connected client.
// We use t.Cleanup to ensure resources are automatically cleaned up.
func TestServer(t testing.T, opts ...Option) *serverclient.VagrantClient {
return server.TestServer(t, TestImpl(t, opts...))
}
// TestImpl returns the vagrant server implementation. This can be used
// with server.TestServer. It is easier to just use TestServer directly.
func TestImpl(t testing.T, opts ...Option) pb.VagrantServer {
var buf bytes.Buffer
l := hclog.New(&hclog.LoggerOptions{
Name: "test",
Level: hclog.Trace,
Output: &buf,
IncludeLocation: true,
})
impl, err := New(append(
[]Option{WithDB(state.TestDB(t)), WithLogger(l)},
opts...,
)...)
t.Cleanup(func() {
t.Log(buf.String())
})
require.NoError(t, err)
return impl
}
// // TestWithURLService is an Option for testing only that creates an
// // in-memory URL service server. This requires access to an external
// // postgres server.
// //
// // If out is non-nil, it will be written to with the DevSetup info.
// func TestWithURLService(t testing.T, out *hzntest.DevSetup) Option {
// // Create the test server. On test end we close the channel which quits
// // the Horizon test server.
// setupCh := make(chan *hzntest.DevSetup, 1)
// closeCh := make(chan struct{})
// t.Cleanup(func() { close(closeCh) })
// go hzntest.Dev(t, func(setup *hzntest.DevSetup) {
// hubclient, err := hznhub.NewHub(hclog.L(), setup.ControlClient, setup.HubToken)
// require.NoError(t, err)
// go hubclient.Run(context.Background(), setup.ClientListener)
// setupCh <- setup
// <-closeCh
// })
// setup := <-setupCh
// // Make our test registration API
// wphzndata := wphzn.TestServer(t,
// wphzn.WithNamespace("/"),
// wphzn.WithHznControl(setup.MgmtClient),
// )
// // Get our account token.
// wpaccountResp, err := wphzndata.Client.RegisterGuestAccount(
// context.Background(),
// &wphznpb.RegisterGuestAccountRequest{
// ServerId: "A",
// },
// )
// require.NoError(t, err)
// // We need to get the account since that is what we need to query with
// tokenInfo, err := setup.MgmtClient.GetTokenPublicKey(context.Background(), &hznpb.Noop{})
// require.NoError(t, err)
// token, err := hzntoken.CheckTokenED25519(wpaccountResp.Token, tokenInfo.PublicKey)
// require.NoError(t, err)
// setup.Account = token.Account()
// // Copy our setup config
// if out != nil {
// *out = *setup
// }
// return func(s *service, cfg *config) error {
// if cfg.serverConfig == nil {
// cfg.serverConfig = &serverconfig.Config{}
// }
// cfg.serverConfig.URL = &serverconfig.URL{
// Enabled: true,
// APIAddress: wphzndata.Addr,
// APIInsecure: true,
// APIToken: wpaccountResp.Token,
// ControlAddress: fmt.Sprintf("dev://%s", setup.HubAddr),
// AutomaticAppHostname: true,
// }
// return nil
// }
// }
// TestRunner registers a runner and returns the ID and a function to
// deregister the runner. This uses t.Cleanup so that the runner will always
// be deregistered on test completion.
func TestRunner(t testing.T, client pb.VagrantClient, r *pb.Runner) (string, func()) {
require := require.New(t)
ctx := context.Background()
// Get the runner
if r == nil {
r = &pb.Runner{}
}
id, err := server.Id()
require.NoError(err)
require.NoError(mergo.Merge(r, &pb.Runner{Id: id}))
// Open the config stream
stream, err := client.RunnerConfig(ctx)
require.NoError(err)
t.Cleanup(func() { stream.CloseSend() })
// Register
require.NoError(err)
require.NoError(stream.Send(&pb.RunnerConfigRequest{
Event: &pb.RunnerConfigRequest_Open_{
Open: &pb.RunnerConfigRequest_Open{
Runner: r,
},
},
}))
// Wait for first message to confirm we're registered
_, err = stream.Recv()
require.NoError(err)
return id, func() { stream.CloseSend() }
}
// TestBasis creates the basis in the DB.
func TestBasis(t testing.T, client pb.VagrantClient, ref *pb.Basis) *pb.Basis {
if ref == nil {
ref = &pb.Basis{}
}
td := testTempDir(t)
defaultBasis := &pb.Basis{
Name: filepath.Base(td),
Path: td,
}
require.NoError(t, mergo.Merge(ref, defaultBasis))
resp, err := client.UpsertBasis(context.Background(), &pb.UpsertBasisRequest{
Basis: ref,
})
require.NoError(t, err)
return resp.Basis
}
func TestJob(t testing.T, client pb.VagrantClient, ref *pb.Job) *pb.Job {
j := testJobProto(t, client, ref)
resp, err := client.QueueJob(context.Background(), &pb.QueueJobRequest{Job: j})
require.Nil(t, err)
j.Id = resp.JobId
return j
}
func testJobProto(t testing.T, client pb.VagrantClient, ref *pb.Job) *pb.Job {
var job pb.Job
if ref == nil {
ref = &pb.Job{}
}
err := mapstructure.Decode(ref, &job)
require.NoError(t, err)
if job.Scope == nil {
basis := TestBasis(t, client, nil)
job.Scope = &pb.Job_Basis{
Basis: &vagrant_plugin_sdk.Ref_Basis{
ResourceId: basis.ResourceId,
},
}
}
return state.TestJobProto(t, &job)
}
func testTempDir(t testing.T) string {
dir, err := ioutil.TempDir("", "vagrant-test")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(dir) })
return dir
}
|