mirror of
https://github.com/restic/restic.git
synced 2025-10-27 17:40:58 +00:00
Vendor dependencies for GCS
This commit is contained in:
99
vendor/cloud.google.com/go/internal/testutil/cmp.go
generated
vendored
Normal file
99
vendor/cloud.google.com/go/internal/testutil/cmp.go
generated
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
var (
|
||||
alwaysEqual = cmp.Comparer(func(_, _ interface{}) bool { return true })
|
||||
|
||||
defaultCmpOptions = []cmp.Option{
|
||||
// Use proto.Equal for protobufs
|
||||
cmp.Comparer(proto.Equal),
|
||||
// NaNs compare equal
|
||||
cmp.FilterValues(func(x, y float64) bool {
|
||||
return math.IsNaN(x) && math.IsNaN(y)
|
||||
}, alwaysEqual),
|
||||
cmp.FilterValues(func(x, y float32) bool {
|
||||
return math.IsNaN(float64(x)) && math.IsNaN(float64(y))
|
||||
}, alwaysEqual),
|
||||
}
|
||||
)
|
||||
|
||||
// Equal tests two values for equality.
|
||||
func Equal(x, y interface{}, opts ...cmp.Option) bool {
|
||||
// Put default options at the end. Order doesn't matter.
|
||||
opts = append(opts[:len(opts):len(opts)], defaultCmpOptions...)
|
||||
return cmp.Equal(x, y, opts...)
|
||||
}
|
||||
|
||||
// Diff reports the differences between two values.
|
||||
// Diff(x, y) == "" iff Equal(x, y).
|
||||
func Diff(x, y interface{}, opts ...cmp.Option) string {
|
||||
// Put default options at the end. Order doesn't matter.
|
||||
opts = append(opts[:len(opts):len(opts)], defaultCmpOptions...)
|
||||
return cmp.Diff(x, y, opts...)
|
||||
}
|
||||
|
||||
// TODO(jba): remove the code below when cmpopts becomes available.
|
||||
|
||||
// IgnoreUnexported returns an Option that only ignores the immediate unexported
|
||||
// fields of a struct, including anonymous fields of unexported types.
|
||||
// In particular, unexported fields within the struct's exported fields
|
||||
// of struct types, including anonymous fields, will not be ignored unless the
|
||||
// type of the field itself is also passed to IgnoreUnexported.
|
||||
func IgnoreUnexported(typs ...interface{}) cmp.Option {
|
||||
ux := newUnexportedFilter(typs...)
|
||||
return cmp.FilterPath(ux.filter, cmp.Ignore())
|
||||
}
|
||||
|
||||
type unexportedFilter struct{ m map[reflect.Type]bool }
|
||||
|
||||
func newUnexportedFilter(typs ...interface{}) unexportedFilter {
|
||||
ux := unexportedFilter{m: make(map[reflect.Type]bool)}
|
||||
for _, typ := range typs {
|
||||
t := reflect.TypeOf(typ)
|
||||
if t == nil || t.Kind() != reflect.Struct {
|
||||
panic(fmt.Sprintf("invalid struct type: %T", typ))
|
||||
}
|
||||
ux.m[t] = true
|
||||
}
|
||||
return ux
|
||||
}
|
||||
func (xf unexportedFilter) filter(p cmp.Path) bool {
|
||||
if len(p) < 2 {
|
||||
return false
|
||||
}
|
||||
sf, ok := p[len(p)-1].(cmp.StructField)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return xf.m[p[len(p)-2].Type()] && !isExported(sf.Name())
|
||||
}
|
||||
|
||||
// isExported reports whether the identifier is exported.
|
||||
func isExported(id string) bool {
|
||||
r, _ := utf8.DecodeRuneInString(id)
|
||||
return unicode.IsUpper(r)
|
||||
}
|
||||
67
vendor/cloud.google.com/go/internal/testutil/context.go
generated
vendored
Normal file
67
vendor/cloud.google.com/go/internal/testutil/context.go
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package testutil contains helper functions for writing tests.
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
)
|
||||
|
||||
const (
|
||||
envProjID = "GCLOUD_TESTS_GOLANG_PROJECT_ID"
|
||||
envPrivateKey = "GCLOUD_TESTS_GOLANG_KEY"
|
||||
)
|
||||
|
||||
// ProjID returns the project ID to use in integration tests, or the empty
|
||||
// string if none is configured.
|
||||
func ProjID() string {
|
||||
projID := os.Getenv(envProjID)
|
||||
if projID == "" {
|
||||
return ""
|
||||
}
|
||||
return projID
|
||||
}
|
||||
|
||||
// TokenSource returns the OAuth2 token source to use in integration tests,
|
||||
// or nil if none is configured. If the environment variable is unset,
|
||||
// TokenSource will try to find 'Application Default Credentials'. Else,
|
||||
// TokenSource will return nil.
|
||||
// TokenSource will log.Fatal if the token source is specified but missing or invalid.
|
||||
func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource {
|
||||
key := os.Getenv(envPrivateKey)
|
||||
if key == "" { // Try for application default credentials.
|
||||
ts, err := google.DefaultTokenSource(ctx, scopes...)
|
||||
if err != nil {
|
||||
log.Println("No 'Application Default Credentials' found.")
|
||||
return nil
|
||||
}
|
||||
return ts
|
||||
}
|
||||
jsonKey, err := ioutil.ReadFile(key)
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot read the JSON key file, err: %v", err)
|
||||
}
|
||||
conf, err := google.JWTConfigFromJSON(jsonKey, scopes...)
|
||||
if err != nil {
|
||||
log.Fatalf("google.JWTConfigFromJSON: %v", err)
|
||||
}
|
||||
return conf.TokenSource(ctx)
|
||||
}
|
||||
73
vendor/cloud.google.com/go/internal/testutil/server.go
generated
vendored
Normal file
73
vendor/cloud.google.com/go/internal/testutil/server.go
generated
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
Copyright 2016 Google Inc. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// A Server is an in-process gRPC server, listening on a system-chosen port on
|
||||
// the local loopback interface. Servers are for testing only and are not
|
||||
// intended to be used in production code.
|
||||
//
|
||||
// To create a server, make a new Server, register your handlers, then call
|
||||
// Start:
|
||||
//
|
||||
// srv, err := NewServer()
|
||||
// ...
|
||||
// mypb.RegisterMyServiceServer(srv.Gsrv, &myHandler)
|
||||
// ....
|
||||
// srv.Start()
|
||||
//
|
||||
// Clients should connect to the server with no security:
|
||||
//
|
||||
// conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
|
||||
// ...
|
||||
type Server struct {
|
||||
Addr string
|
||||
l net.Listener
|
||||
Gsrv *grpc.Server
|
||||
}
|
||||
|
||||
// NewServer creates a new Server. The Server will be listening for gRPC connections
|
||||
// at the address named by the Addr field, without TLS.
|
||||
func NewServer(opts ...grpc.ServerOption) (*Server, error) {
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := &Server{
|
||||
Addr: l.Addr().String(),
|
||||
l: l,
|
||||
Gsrv: grpc.NewServer(opts...),
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Start causes the server to start accepting incoming connections.
|
||||
// Call Start after registering handlers.
|
||||
func (s *Server) Start() {
|
||||
go s.Gsrv.Serve(s.l)
|
||||
}
|
||||
|
||||
// Close shuts down the server.
|
||||
func (s *Server) Close() {
|
||||
s.Gsrv.Stop()
|
||||
s.l.Close()
|
||||
}
|
||||
35
vendor/cloud.google.com/go/internal/testutil/server_test.go
generated
vendored
Normal file
35
vendor/cloud.google.com/go/internal/testutil/server_test.go
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func TestNewServer(t *testing.T) {
|
||||
srv, err := NewServer()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv.Start()
|
||||
conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conn.Close()
|
||||
srv.Close()
|
||||
}
|
||||
93
vendor/cloud.google.com/go/internal/testutil/unique.go
generated
vendored
Normal file
93
vendor/cloud.google.com/go/internal/testutil/unique.go
generated
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// This file supports generating unique IDs so that multiple test executions
|
||||
// don't interfere with each other, and cleaning up old entities that may
|
||||
// remain if tests exit early.
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var startTime = time.Now().UTC()
|
||||
|
||||
// A UIDSpace manages a set of unique IDs distinguished by a prefix.
|
||||
type UIDSpace struct {
|
||||
Prefix string
|
||||
re *regexp.Regexp
|
||||
mu sync.Mutex
|
||||
count int
|
||||
}
|
||||
|
||||
func NewUIDSpace(prefix string) *UIDSpace {
|
||||
return &UIDSpace{
|
||||
Prefix: prefix,
|
||||
re: regexp.MustCompile("^" + regexp.QuoteMeta(prefix) + `-(\d{4})(\d{2})(\d{2})-(\d+)-\d+$`),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// New generates a new unique ID . The ID consists of the UIDSpace's prefix, a
|
||||
// timestamp, and a counter value. All unique IDs generated in the same test
|
||||
// execution will have the same timestamp.
|
||||
//
|
||||
// Aside from the characters in the prefix, IDs contain only letters, numbers
|
||||
// and hyphens.
|
||||
func (s *UIDSpace) New() string { return s.newID(startTime) }
|
||||
|
||||
func (s *UIDSpace) newID(t time.Time) string {
|
||||
s.mu.Lock()
|
||||
c := s.count
|
||||
s.count++
|
||||
s.mu.Unlock()
|
||||
// Write the time as a date followed by nanoseconds from midnight of that date.
|
||||
// That makes it easier to see the approximate time of the ID when it is displayed.
|
||||
y, m, d := t.Date()
|
||||
ns := t.Sub(time.Date(y, m, d, 0, 0, 0, 0, time.UTC))
|
||||
// Zero-pad the counter for lexical sort order for IDs with the same timestamp.
|
||||
return fmt.Sprintf("%s-%04d%02d%02d-%d-%04d", s.Prefix, y, m, d, ns, c)
|
||||
}
|
||||
|
||||
// Timestamp extracts the timestamp of uid, which must have been generated by
|
||||
// s. The second return value is true on success, false if there was a problem.
|
||||
func (s *UIDSpace) Timestamp(uid string) (time.Time, bool) {
|
||||
subs := s.re.FindStringSubmatch(uid)
|
||||
if subs == nil {
|
||||
return time.Time{}, false
|
||||
}
|
||||
y, err1 := strconv.Atoi(subs[1])
|
||||
m, err2 := strconv.Atoi(subs[2])
|
||||
d, err3 := strconv.Atoi(subs[3])
|
||||
ns, err4 := strconv.Atoi(subs[4])
|
||||
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
|
||||
return time.Time{}, false
|
||||
}
|
||||
return time.Date(y, time.Month(m), d, 0, 0, 0, ns, time.UTC), true
|
||||
}
|
||||
|
||||
// Older reports whether uid was created by m and has a timestamp older than
|
||||
// the current time by at least d.
|
||||
func (s *UIDSpace) Older(uid string, d time.Duration) bool {
|
||||
ts, ok := s.Timestamp(uid)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return time.Since(ts) > d
|
||||
}
|
||||
62
vendor/cloud.google.com/go/internal/testutil/unique_test.go
generated
vendored
Normal file
62
vendor/cloud.google.com/go/internal/testutil/unique_test.go
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
s := NewUIDSpace("prefix")
|
||||
tm := time.Date(2017, 1, 6, 0, 0, 0, 21, time.UTC)
|
||||
got := s.newID(tm)
|
||||
want := "prefix-20170106-21-0000"
|
||||
if got != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimestamp(t *testing.T) {
|
||||
s := NewUIDSpace("unique-ID")
|
||||
uid := s.New()
|
||||
got, ok := s.Timestamp(uid)
|
||||
if !ok {
|
||||
t.Fatal("got ok = false, want true")
|
||||
}
|
||||
if !startTime.Equal(got) {
|
||||
t.Errorf("got %s, want %s", got, startTime)
|
||||
}
|
||||
|
||||
got, ok = s.Timestamp("unique-ID-20160308-123-8")
|
||||
if !ok {
|
||||
t.Fatal("got false, want true")
|
||||
}
|
||||
if want := time.Date(2016, 3, 8, 0, 0, 0, 123, time.UTC); !want.Equal(got) {
|
||||
t.Errorf("got %s, want %s", got, want)
|
||||
}
|
||||
if _, ok = s.Timestamp("invalid-time-1234"); ok {
|
||||
t.Error("got true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOlder(t *testing.T) {
|
||||
s := NewUIDSpace("uid")
|
||||
// A non-matching ID returns false.
|
||||
id2 := NewUIDSpace("different-prefix").New()
|
||||
if got, want := s.Older(id2, time.Second), false; got != want {
|
||||
t.Errorf("got %t, want %t", got, want)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user