Skip to content

Commit

Permalink
pass net.ip object to adapter for cases where expression evaluates to…
Browse files Browse the repository at this point in the history
… []byte (istio#3091)

Automatic merge from submit-queue.

pass net.ip object to adapter for cases where expression evaluates to []byte

Fixes issue istio#3075
  • Loading branch information
guptasu authored and istio-merge-robot committed Feb 2, 2018
1 parent 3bc6e49 commit 7a277fa
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 86 deletions.
12 changes: 6 additions & 6 deletions mixer/pkg/il/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package interpreter

import (
"bytes"
"errors"
"fmt"
"net"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1694,7 +1694,7 @@ func TestInterpreter_Eval(t *testing.T) {
ret
end
`,
expected: []byte{0x1, 0x2, 0x4, 0x6},
expected: net.ParseIP("1.2.4.6"),
externs: map[string]Extern{
"ext": ExternFromFn("ext", func() []byte {
return []byte{0x1, 0x2, 0x4, 0x6}
Expand Down Expand Up @@ -1851,7 +1851,7 @@ func TestInterpreter_Eval(t *testing.T) {
ret
end
`,
expected: []byte{0x1, 0x2, 0x4, 0x6},
expected: net.ParseIP("1.2.4.6"),
input: map[string]interface{}{
"a": []byte{0x1, 0x2, 0x4, 0x6},
},
Expand Down Expand Up @@ -2450,13 +2450,13 @@ func runTestProgram(t *testing.T, p *il.Program, test test) {
}

func areEqual(a1 interface{}, a2 interface{}) bool {
b1, b1Ok := a1.([]byte)
b2, b2Ok := a2.([]byte)
b1, b1Ok := a1.(net.IP)
b2, b2Ok := a2.(net.IP)
if b1Ok != b2Ok {
return false
}
if b1Ok {
return bytes.Equal(b1, b2)
return b1.Equal(b2)
}
return a1 == a2
}
9 changes: 9 additions & 0 deletions mixer/pkg/il/interpreter/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package interpreter
import (
"fmt"
"math"
"net"
"time"

"istio.io/istio/mixer/pkg/il"
Expand Down Expand Up @@ -108,6 +109,14 @@ func (r Result) AsInterface() interface{} {
case il.Void:
return nil
case il.Interface:
// TODO: currently byte[] can only be net.IP address type, so we can
// safely do this. Ideally we want to have a better fix.
if r.vi != nil {
switch r.vi.(type) {
case []byte:
return net.IP(r.vi.([]uint8))
}
}
return r.vi
default:
log.Warnf("interpreter.Result: Unknown type encountered. Returning nil. type: '%v'", r.t)
Expand Down
22 changes: 11 additions & 11 deletions mixer/pkg/il/testing/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ end`,
E: `target.ip| ip("10.1.12.3")`,
Type: descriptor.IP_ADDRESS,
I: map[string]interface{}{},
R: []uint8(net.ParseIP("10.1.12.3")),
R: net.ParseIP("10.1.12.3"),
Referenced: []string{"target.ip"},
conf: exprEvalAttrs,
},
Expand Down Expand Up @@ -1757,15 +1757,15 @@ end`,
I: map[string]interface{}{
"aip": []byte{0x1, 0x2, 0x3, 0x4},
},
R: []byte{0x1, 0x2, 0x3, 0x4},
R: net.ParseIP("1.2.3.4"),
},
{
E: `aip | bip`,
Type: descriptor.IP_ADDRESS,
I: map[string]interface{}{
"bip": []byte{0x4, 0x5, 0x6, 0x7},
},
R: []byte{0x4, 0x5, 0x6, 0x7},
R: net.ParseIP("4.5.6.7"),
},
{
E: `aip | bip`,
Expand All @@ -1774,12 +1774,12 @@ end`,
"aip": []byte{0x1, 0x2, 0x3, 0x4},
"bip": []byte{0x4, 0x5, 0x6, 0x7},
},
R: []byte{0x1, 0x2, 0x3, 0x4},
R: net.ParseIP("1.2.3.4"),
},
{
E: `ip("0.0.0.0")`,
Type: descriptor.IP_ADDRESS,
R: []byte(net.IPv4zero),
R: net.IPv4zero,
IL: `fn eval() interface
apush_s "0.0.0.0"
call ip
Expand Down Expand Up @@ -1910,7 +1910,7 @@ fn eval() interface
ret
end
`,
R: []uint8(net.ParseIP("1.2.3.4")),
R: net.ParseIP("1.2.3.4"),
},

{
Expand All @@ -1926,7 +1926,7 @@ fn eval() interface
ret
end
`,
R: []uint8(net.ParseIP("1.2.3.4")),
R: net.ParseIP("1.2.3.4"),
},

{
Expand All @@ -1942,7 +1942,7 @@ L0:
ret
end
`,
R: []uint8(net.ParseIP("1.2.3.4")),
R: net.ParseIP("1.2.3.4"),
},

{
Expand All @@ -1958,7 +1958,7 @@ L0:
ret
end
`,
R: []uint8(net.ParseIP("5.6.7.8")),
R: net.ParseIP("5.6.7.8"),
},

{
Expand All @@ -1967,7 +1967,7 @@ end
I: map[string]interface{}{
"bs": "1.2.3.4",
},
R: []uint8(net.ParseIP("1.2.3.4")),
R: net.ParseIP("1.2.3.4"),
},

{
Expand All @@ -1990,7 +1990,7 @@ end
I: map[string]interface{}{
"ar": map[string]string{"foo": "1.2.3.4"},
},
R: []uint8(net.ParseIP("1.2.3.4")),
R: net.ParseIP("1.2.3.4"),
},

{
Expand Down
8 changes: 4 additions & 4 deletions mixer/pkg/il/testing/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
package ilt

import (
"bytes"
"net"
)

// AreEqual checks for equality of given values. It handles comparison of []byte as a special case.
func AreEqual(e interface{}, a interface{}) bool {
if eb, ok := e.([]byte); ok {
if ab, ok := a.([]byte); ok {
return bytes.Equal(ab, eb)
if eb, ok := e.(net.IP); ok {
if ab, ok := a.(net.IP); ok {
return ab.Equal(eb)
}

return false
Expand Down
11 changes: 8 additions & 3 deletions mixer/template/sample/createinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

"github.com/davecgh/go-spew/spew"
"github.com/gogo/protobuf/proto"

pb "istio.io/api/mixer/v1/config/descriptor"
Expand Down Expand Up @@ -123,6 +124,9 @@ var defaultAttributeInfos = map[string]*istio_mixer_v1_config.AttributeManifest_
"adr": {
ValueType: pb.DURATION,
},
"ap1": {
ValueType: pb.IP_ADDRESS,
},
"generated.ai": {
ValueType: pb.INT64,
},
Expand Down Expand Up @@ -201,7 +205,7 @@ func TestCreateInstanceBuilder(t *testing.T) {
}

if !reflect.DeepEqual(actual, tst.expect) {
tt.Fatalf("Instance mismatch,\ngot =%+v\nwant=%+v", actual, tst.expect)
tt.Fatalf("Instance mismatch,\ngot =%+v\nwant=%+v", spew.Sdump(actual), spew.Sdump(tst.expect))
}
})
}
Expand Down Expand Up @@ -556,13 +560,14 @@ func generateCheckTests() []createInstanceTest {
var defaultReportAttributes = map[string]interface{}{
"ats": time.Date(2017, time.January, 01, 0, 0, 0, 0, time.UTC),
"adr": 10 * time.Second,
"ap1": []byte(net.ParseIP("2.3.4.5")),
}

// a default Report Instance Parameter. All of the fields can be calculated, given the values in the
// default bag.
var defaultReportInstanceParam = sample_report.InstanceParam{
Value: "1",
Dimensions: map[string]string{"s": "2"},
Dimensions: map[string]string{"s": "2", "p": "ap1"},
BoolPrimitive: "true",
DoublePrimitive: "1.2",
Int64Primitive: "54362",
Expand Down Expand Up @@ -612,7 +617,7 @@ var defaultReportInstanceParam = sample_report.InstanceParam{
var defaultReportInstance = &sample_report.Instance{
Name: "instance1",
Value: int64(1),
Dimensions: map[string]interface{}{"s": int64(2)},
Dimensions: map[string]interface{}{"s": int64(2), "p": net.ParseIP("2.3.4.5")},
BoolPrimitive: true,
DoublePrimitive: 1.2,
Int64Primitive: 54362,
Expand Down
7 changes: 7 additions & 0 deletions mixer/template/sample/dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package sample

import (
"bytes"
"context"
"errors"
"net"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -129,6 +131,7 @@ func TestDispatchGenAttrs_Success(t *testing.T) {
apaOutput: sample_apa.Output{
StringPrimitive: "This is an output",
Int64Primitive: defaultApaAttributes["ai"].(int64),
OutIp: net.ParseIP("2.3.4.5"),
},
}

Expand All @@ -155,6 +158,10 @@ func TestDispatchGenAttrs_Success(t *testing.T) {
if ai, ok := outBag.Get("generated.ai"); !ok || ai != defaultApaAttributes["ai"] {
t.Fatalf("Expected attribute not found or different than expected: %v != %v", ai, defaultApaAttributes["ai"])
}

if ai, ok := outBag.Get("generated.ip"); !ok || !bytes.Equal(ai.([]byte), []byte{0x2, 0x3, 0x4, 0x5}) {
t.Fatalf("Expected attribute not found or different than expected: %v != %v", ai, []byte{0x2, 0x3, 0x4, 0x5})
}
}

func TestDispatchGenAttrs_Failure(t *testing.T) {
Expand Down
Loading

0 comments on commit 7a277fa

Please sign in to comment.