Home Download Docs Code Community
     1	/*
     2	Copyright 2014 The Perkeep Authors
     3	
     4	Licensed under the Apache License, Version 2.0 (the "License");
     5	you may not use this file except in compliance with the License.
     6	You may obtain a copy of the License at
     7	
     8	     http://www.apache.org/licenses/LICENSE-2.0
     9	
    10	Unless required by applicable law or agreed to in writing, software
    11	distributed under the License is distributed on an "AS IS" BASIS,
    12	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13	See the License for the specific language governing permissions and
    14	limitations under the License.
    15	*/
    16	
    17	// Package test provides common functionality for importer tests.
    18	package test // import "perkeep.org/pkg/importer/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	// FindChildRefs returns the children of an importer.Object
    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		// Do the silly env vars dance to avoid the "non-hermetic use of host config panic".
    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	// GetRequiredChildPathObj returns the child object at path or an error if none exists.
    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	// ImporterTest sets up the environment for an importer integration test.
    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		// TODO(mpl): add a utility in integration package to provide a client that
    81		// just works with World.
    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	}
Website layout inspired by memcached.
Content by the authors.