1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
17
18 package test
19
20 import (
21 "context"
22 "fmt"
23 "net/http"
24 "os"
25 "strings"
26 "testing"
27
28 "perkeep.org/internal/osutil"
29 "perkeep.org/pkg/blob"
30 "perkeep.org/pkg/client"
31 "perkeep.org/pkg/importer"
32 "perkeep.org/pkg/test"
33 )
34
35
36 func FindChildRefs(parent *importer.Object) ([]blob.Ref, error) {
37 childRefs := []blob.Ref{}
38 var err error
39 parent.ForeachAttr(func(key, value string) {
40 if strings.HasPrefix(key, "camliPath:") {
41 if br, ok := blob.Parse(value); ok {
42 childRefs = append(childRefs, br)
43 return
44 }
45 if err == nil {
46 err = fmt.Errorf("invalid blobRef for %s attribute of %v: %q", key, parent, value)
47 }
48 }
49 })
50 return childRefs, err
51 }
52
53 func setupClient(w *test.World) (*client.Client, error) {
54
55 if err := os.Setenv("CAMLI_KEYID", w.ClientIdentity()); err != nil {
56 return nil, err
57 }
58 if err := os.Setenv("CAMLI_SECRET_RING", w.SecretRingFile()); err != nil {
59 return nil, err
60 }
61 osutil.AddSecretRingFlag()
62 return client.New(client.OptionServer(w.ServerBaseURL()))
63 }
64
65
66 func GetRequiredChildPathObj(parent *importer.Object, path string) (*importer.Object, error) {
67 return parent.ChildPathObjectOrFunc(path, func() (*importer.Object, error) {
68 return nil, fmt.Errorf("Unable to locate child path %s of node %v", path, parent.PermanodeRef())
69 })
70 }
71
72
73 func ImporterTest(t *testing.T, importerName string, transport http.RoundTripper, fn func(*importer.RunContext)) {
74 const importerPrefix = "/importer/"
75
76 w := test.GetWorld(t)
77 defer w.Stop()
78 baseURL := w.ServerBaseURL()
79
80
81
82 cl, err := setupClient(w)
83 if err != nil {
84 t.Fatal(err)
85 }
86 if err := cl.UploadPublicKey(context.TODO()); err != nil {
87 t.Fatal(err)
88 }
89
90 signer, err := cl.Signer()
91 if err != nil {
92 t.Fatal(err)
93 }
94 clientId := map[string]string{
95 importerName: "fakeStaticClientId",
96 }
97 clientSecret := map[string]string{
98 importerName: "fakeStaticClientSecret",
99 }
100
101 httpClient := &http.Client{
102 Transport: transport,
103 }
104
105 hc := importer.HostConfig{
106 BaseURL: baseURL,
107 Prefix: importerPrefix,
108 Target: cl,
109 BlobSource: cl,
110 Signer: signer,
111 Search: cl,
112 ClientId: clientId,
113 ClientSecret: clientSecret,
114 HTTPClient: httpClient,
115 }
116
117 host, err := importer.NewHost(hc)
118 if err != nil {
119 t.Fatal(err)
120 }
121 rc, err := importer.CreateAccount(host, importerName)
122 if err != nil {
123 t.Fatal(err)
124 }
125 fn(rc)
126
127 }