Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate all the duplicated testdata, allow running from any directory #545

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions clientconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ import (
"time"

"google.golang.org/grpc/credentials"
"google.golang.org/grpc/test"
)

const tlsDir = "testdata/"

func TestDialTimeout(t *testing.T) {
conn, err := Dial("Non-Existent.Server:80", WithTimeout(time.Millisecond), WithBlock(), WithInsecure())
if err == nil {
Expand All @@ -53,7 +52,7 @@ func TestDialTimeout(t *testing.T) {
}

func TestTLSDialTimeout(t *testing.T) {
creds, err := credentials.NewClientTLSFromFile(tlsDir+"ca.pem", "x.test.youtube.com")
creds, err := credentials.NewClientTLSFromFile(test.Abs("testdata/ca.pem"), "x.test.youtube.com")
if err != nil {
t.Fatalf("Failed to create credentials %v", err)
}
Expand All @@ -67,7 +66,7 @@ func TestTLSDialTimeout(t *testing.T) {
}

func TestCredentialsMisuse(t *testing.T) {
creds, err := credentials.NewClientTLSFromFile(tlsDir+"ca.pem", "x.test.youtube.com")
creds, err := credentials.NewClientTLSFromFile(test.Abs("testdata/ca.pem"), "x.test.youtube.com")
if err != nil {
t.Fatalf("Failed to create credentials %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions examples/route_guide/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ import (
"google.golang.org/grpc/credentials"
pb "google.golang.org/grpc/examples/route_guide/routeguide"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/test"
)

var (
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
caFile = flag.String("ca_file", "testdata/ca.pem", "The file containning the CA root cert file")
caFile = flag.String("ca_file", "testdata/ca.pem", "The file containning the CA root cert file. Relative paths are interpreted from the google.golang.org/grpc directory.")
serverAddr = flag.String("server_addr", "127.0.0.1:10000", "The server address in the format of host:port")
serverHostOverride = flag.String("server_host_override", "x.test.youtube.com", "The server name use to verify the hostname returned by TLS handshake")
)
Expand Down Expand Up @@ -167,7 +168,7 @@ func main() {
var creds credentials.TransportAuthenticator
if *caFile != "" {
var err error
creds, err = credentials.NewClientTLSFromFile(*caFile, sn)
creds, err = credentials.NewClientTLSFromFile(test.Abs(*caFile), sn)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
}
Expand Down
15 changes: 7 additions & 8 deletions examples/route_guide/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,21 @@ import (
"net"
"time"

"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"

"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"

"github.com/golang/protobuf/proto"
"google.golang.org/grpc/test"

pb "google.golang.org/grpc/examples/route_guide/routeguide"
)

var (
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
certFile = flag.String("cert_file", "testdata/server1.pem", "The TLS cert file")
keyFile = flag.String("key_file", "testdata/server1.key", "The TLS key file")
jsonDBFile = flag.String("json_db_file", "testdata/route_guide_db.json", "A json file containing a list of features")
certFile = flag.String("cert_file", "testdata/server1.pem", "If -tls is true, the TLS cert file. Relative paths are interpreted from the google.golang.org/grpc directory.")
keyFile = flag.String("key_file", "testdata/server1.key", "If -tls is true, the TLS key file. Relative paths are interpreted from the google.golang.org/grpc directory.")
jsonDBFile = flag.String("json_db_file", "examples/route_guide/testdata/route_guide_db.json", "A JSON file containing a list of features. Relative paths are interpreted from the google.golang.org/grpc directory.")
port = flag.Int("port", 10000, "The server port")
)

Expand Down Expand Up @@ -214,7 +213,7 @@ func serialize(point *pb.Point) string {

func newServer() *routeGuideServer {
s := new(routeGuideServer)
s.loadFeatures(*jsonDBFile)
s.loadFeatures(test.Abs(*jsonDBFile))
s.routeNotes = make(map[string][]*pb.RouteNote)
return s
}
Expand All @@ -227,7 +226,7 @@ func main() {
}
var opts []grpc.ServerOption
if *tls {
creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
creds, err := credentials.NewServerTLSFromFile(test.Abs(*certFile), test.Abs(*keyFile))
if err != nil {
grpclog.Fatalf("Failed to generate credentials %v", err)
}
Expand Down
15 changes: 0 additions & 15 deletions examples/route_guide/testdata/ca.pem

This file was deleted.

16 changes: 0 additions & 16 deletions examples/route_guide/testdata/server1.key

This file was deleted.

16 changes: 0 additions & 16 deletions examples/route_guide/testdata/server1.pem

This file was deleted.

3 changes: 2 additions & 1 deletion interop/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/interop"
testpb "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/test"
)

var (
Expand Down Expand Up @@ -88,7 +89,7 @@ func main() {
var creds credentials.TransportAuthenticator
if *testCA {
var err error
creds, err = credentials.NewClientTLSFromFile(testCAFile, sn)
creds, err = credentials.NewClientTLSFromFile(test.Abs(testCAFile), sn)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
}
Expand Down
15 changes: 0 additions & 15 deletions interop/client/testdata/ca.pem

This file was deleted.

16 changes: 0 additions & 16 deletions interop/client/testdata/server1.key

This file was deleted.

16 changes: 0 additions & 16 deletions interop/client/testdata/server1.pem

This file was deleted.

15 changes: 0 additions & 15 deletions interop/server/testdata/ca.pem

This file was deleted.

16 changes: 0 additions & 16 deletions interop/server/testdata/server1.key

This file was deleted.

16 changes: 0 additions & 16 deletions interop/server/testdata/server1.pem

This file was deleted.

8 changes: 3 additions & 5 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
*/

package grpc_test
package test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're here, would you mind moving this file up to the repo root and leaving it in package grpc_test? The current makeup of package test is confusing, where's it's half test utilities and half real tests.


import (
"flag"
Expand Down Expand Up @@ -267,8 +267,6 @@ func (s *testServer) HalfDuplexCall(stream testpb.TestService_HalfDuplexCallServ
return nil
}

const tlsDir = "testdata/"

func TestReconnectTimeout(t *testing.T) {
defer leakCheck(t)()
lis, err := net.Listen("tcp", ":0")
Expand Down Expand Up @@ -378,7 +376,7 @@ func serverSetUp(t *testing.T, servON bool, hs *health.HealthServer, maxStream u
t.Fatalf("Failed to listen: %v", err)
}
if e.security == "tls" {
creds, err := credentials.NewServerTLSFromFile(tlsDir+"server1.pem", tlsDir+"server1.key")
creds, err := credentials.NewServerTLSFromFile(Abs("testdata/server1.pem"), Abs("testdata/server1.key"))
if err != nil {
t.Fatalf("Failed to generate credentials %v", err)
}
Expand Down Expand Up @@ -408,7 +406,7 @@ func serverSetUp(t *testing.T, servON bool, hs *health.HealthServer, maxStream u
func clientSetUp(t *testing.T, addr string, cp grpc.Compressor, dc grpc.Decompressor, ua string, e env) (cc *grpc.ClientConn) {
var derr error
if e.security == "tls" {
creds, err := credentials.NewClientTLSFromFile(tlsDir+"ca.pem", "x.test.youtube.com")
creds, err := credentials.NewClientTLSFromFile(Abs("testdata/ca.pem"), "x.test.youtube.com")
if err != nil {
t.Fatalf("Failed to create credentials %v", err)
}
Expand Down
78 changes: 78 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

// Package test contains shared code internal to gRPC's unit tests.
//
// Nothing in this package is a stable API. It should not be used.
package test

import (
"log"
"os"
"path/filepath"
)

// Abs returns the absolute path the given relative file or directory path,
// relative to the google.golang.org/grpc directory in the user's GOPATH.
// If rel is already absolute, it is returned unmodified.
func Abs(rel string) string {
if filepath.IsAbs(rel) {
return rel
}
v, err := goPackagePath("google.golang.org/grpc")
if err != nil {
log.Fatalf("Error finding google.golang.org/grpc/testdata directory: %v", err)
}
return filepath.Join(v, rel)
}

func goPackagePath(pkg string) (path string, err error) {
gp := os.Getenv("GOPATH")
if gp == "" {
return path, os.ErrNotExist
}
for _, p := range filepath.SplitList(gp) {
dir := filepath.Join(p, "src", filepath.FromSlash(pkg))
fi, err := os.Stat(dir)
if os.IsNotExist(err) {
continue
}
if err != nil {
return "", err
}
if !fi.IsDir() {
continue
}
return dir, nil
}
return path, os.ErrNotExist
}
15 changes: 0 additions & 15 deletions test/testdata/ca.pem

This file was deleted.

Loading