Skip to content

Commit

Permalink
Adds conformance runner (#2)
Browse files Browse the repository at this point in the history
* Adds conformance runner

* adds vendor
  • Loading branch information
bg451 authored Dec 12, 2018
1 parent 5230ea2 commit c76fc5a
Show file tree
Hide file tree
Showing 610 changed files with 399,555 additions and 0 deletions.
151 changes: 151 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/lightstep/lightstep-tracer-go"
version = "0.15.6"

[[constraint]]
name = "github.com/opentracing/opentracing-go"
version = "1.0.2"

[prune]
go-tests = true
unused-packages = true
108 changes: 108 additions & 0 deletions runner/body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"bytes"
"encoding/base64"
"fmt"

lightstep "github.com/lightstep/lightstep-tracer-go"
opentracing "github.com/opentracing/opentracing-go"
)

type Carriers struct {
TextMap map[string]string `json:"text_map"`

// Binary is the a base64 representation of the encoded byte array.
Binary string `json:"binary"`
}

// NewBody creates a new request body and injects a span context into it.
func NewBodyFromContext(tracer opentracing.Tracer, ctx opentracing.SpanContext) (*Carriers, error) {
body := &Carriers{
TextMap: map[string]string{},
Binary: "",
}

err := tracer.Inject(ctx, opentracing.TextMap, opentracing.TextMapCarrier(body.TextMap))
if err != nil {
panic(err)
}

buffer := bytes.NewBuffer(nil)
if err := tracer.Inject(ctx, opentracing.Binary, buffer); err != nil {
panic(err)
}
body.Binary = base64.StdEncoding.EncodeToString(buffer.Bytes())

err = body.Equals(tracer, ctx)
return body, err
}

func (b *Carriers) Equals(tracer opentracing.Tracer, ctx opentracing.SpanContext) error {
original, ok := ctx.(lightstep.SpanContext)
if !ok {
panic("not lightstep context")
}

if err := b.checkTextMap(tracer, original); err != nil {
return fmt.Errorf("text map: %v", err)
}
if err := b.checkBinary(tracer, original); err != nil {
return fmt.Errorf("binary: %v", err)
}
return nil
}

func (b *Carriers) checkBinary(tracer opentracing.Tracer, original lightstep.SpanContext) error {
bs, err := base64.StdEncoding.DecodeString(b.Binary)
if err != nil {
return err
}

buf := bytes.NewBuffer(bs)
ctx, err := tracer.Extract(opentracing.Binary, buf)
if err != nil {
return err
}
return contextsAreEqual(original, ctx)
}

func (b *Carriers) checkTextMap(tracer opentracing.Tracer, original lightstep.SpanContext) error {
ctx, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(b.TextMap))
if err != nil {
return err
}
return contextsAreEqual(original, ctx)
}

func contextsAreEqual(a lightstep.SpanContext, otcontext opentracing.SpanContext) error {
if otcontext == nil {
return fmt.Errorf("extracted context was nil")
}

b, ok := otcontext.(lightstep.SpanContext)
if !ok {
panic("not ok")
}

if a.TraceID != b.TraceID {
return fmt.Errorf("expected %+v, got %+v", a, b)
}
if a.SpanID != b.SpanID {
return fmt.Errorf("expected %+v, got %+v", a, b)
}
if len(a.Baggage) != len(b.Baggage) {
return fmt.Errorf("expected %+v, got %+v", a, b)
}

for key, value := range a.Baggage {
v, ok := b.Baggage[key]
if !ok {
return fmt.Errorf("extracted context does not have baggage for %v", key)
}
if v != value {
return fmt.Errorf("expected value %s, got %s", value, v)
}
}
return nil
}
66 changes: 66 additions & 0 deletions runner/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"

lightstep "github.com/lightstep/lightstep-tracer-go"
)

func main() {
argv := os.Args[1:]

if len(argv) == 0 {
panic(fmt.Sprint("no command provided", argv))
}

tracer := lightstep.NewTracer(lightstep.Options{
AccessToken: "invalid",
})

stdinReader, stdinWriter := io.Pipe()
stdoutReader, stdoutWriter := io.Pipe()

cmd := exec.Command(argv[0], argv[1:]...)
cmd.Stdin = stdinReader
cmd.Stdout = stdoutWriter
cmd.Stderr = os.Stderr
go func() {
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
log.Println("program has exited")
}()

ctx := tracer.StartSpan("fake").Context()

body, err := NewBodyFromContext(tracer, ctx)
if err != nil {
log.Fatal("couldnt create body ", err)
}
if err := json.NewEncoder(stdinWriter).Encode(body); err != nil {
log.Fatal("could not marshall body: ", err)
}

if err := stdinWriter.Close(); err != nil {
log.Println("failed to close writer")
}

var result Carriers
if err := json.NewDecoder(stdoutReader).Decode(&result); err != nil {
log.Fatal("could not decode ", err)
}

if err := result.Equals(tracer, ctx); err != nil {
log.Println(body, result)
log.Fatal(err)
}

log.Println("SUCCESS: span contexts are equal")
cmd.Process.Kill()
os.Exit(0)
}
3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c76fc5a

Please sign in to comment.