// This function tests the default connector. func TestDefaultConnector(t *testing.T) { stream := testStream() l, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatalf("Can't listen: %v", err) } go func() { conn, err := l.Accept() if err != nil { t.Fatalf("Can't accept: %v", err) } conn.Write(stream) conn.Close() }() ch := make(chan *pb.LanzRecord) done := make(chan bool) c := lanz.New(lanz.WithAddr(l.Addr().String()), lanz.WithBackoff(1*time.Millisecond)) go func() { c.Run(ch) done <- true }() p := <-ch c.Stop() <-done if !test.DeepEqual(p, testProtoBuf) { t.Fatalf("Expected protobuf %v, but got %v", testProtoBuf, p) } }
// This function tests that basic workflow works. func TestSuccessPath(t *testing.T) { pbs := []*pb.LanzRecord{ { ConfigRecord: &pb.ConfigRecord{ Timestamp: proto.Uint64(146591697107544), LanzVersion: proto.Uint32(1), NumOfPorts: proto.Uint32(146), SegmentSize: proto.Uint32(512), MaxQueueSize: proto.Uint32(524288000), PortConfigRecord: []*pb.ConfigRecord_PortConfigRecord{ { IntfName: proto.String("Cpu"), SwitchId: proto.Uint32(2048), PortId: proto.Uint32(4096), InternalPort: proto.Bool(false), HighThreshold: proto.Uint32(50000), LowThreshold: proto.Uint32(25000), }, }, GlobalUsageReportingEnabled: proto.Bool(true), }, }, { CongestionRecord: &pb.CongestionRecord{ Timestamp: proto.Uint64(146591697107546), IntfName: proto.String("Cpu"), SwitchId: proto.Uint32(2048), PortId: proto.Uint32(4096), QueueSize: proto.Uint32(30000), }, }, { ErrorRecord: &pb.ErrorRecord{ Timestamp: proto.Uint64(146591697107549), ErrorMessage: proto.String("Error"), }, }, } conn := &testConnector{reader: bytes.NewReader(pbsToStream(pbs))} ch := make(chan *pb.LanzRecord) c, done := launchClient(ch, conn) for i, p := range pbs { r, ok := <-ch if !ok { t.Fatalf("Unexpected closed channel") } if !test.DeepEqual(p, r) { t.Fatalf("Test case %d: expected %v, but got %v", i, p, r) } } c.Stop() <-done if conn.open { t.Fatalf("Connection still open after stopping") } }
func TestGetFromMap(t *testing.T) { tests := []struct { k Key m map[Key]interface{} v interface{} found bool }{{ k: New("a"), m: map[Key]interface{}{New("a"): "b"}, v: "b", found: true, }, { k: New(uint32(35)), m: map[Key]interface{}{New(uint32(35)): "c"}, v: "c", found: true, }, { k: New(uint32(37)), m: map[Key]interface{}{New(uint32(36)): "c"}, found: false, }, { k: New(uint32(37)), m: map[Key]interface{}{}, found: false, }, { k: New(map[string]interface{}{"a": "b", "c": uint64(4)}), m: map[Key]interface{}{ New(map[string]interface{}{"a": "b", "c": uint64(4)}): "foo", }, v: "foo", found: true, }} for _, tcase := range tests { v, ok := tcase.k.GetFromMap(tcase.m) if tcase.found != ok { t.Errorf("Wrong retrieval result for case:\nk: %#v\nm: %#v\nv: %#v", tcase.k, tcase.m, tcase.v) } else if tcase.found && !ok { t.Errorf("Unable to retrieve value for case:\nk: %#v\nm: %#v\nv: %#v", tcase.k, tcase.m, tcase.v) } else if tcase.found && !test.DeepEqual(tcase.v, v) { t.Errorf("Wrong result for case:\nk: %#v\nm: %#v\nv: %#v", tcase.k, tcase.m, tcase.v) } } }
func TestDeleteFromMap(t *testing.T) { tests := []struct { k Key m map[Key]interface{} r map[Key]interface{} }{{ k: New("a"), m: map[Key]interface{}{New("a"): "b"}, r: map[Key]interface{}{}, }, { k: New("b"), m: map[Key]interface{}{New("a"): "b"}, r: map[Key]interface{}{New("a"): "b"}, }, { k: New("a"), m: map[Key]interface{}{}, r: map[Key]interface{}{}, }, { k: New(uint32(35)), m: map[Key]interface{}{New(uint32(35)): "c"}, r: map[Key]interface{}{}, }, { k: New(uint32(36)), m: map[Key]interface{}{New(uint32(35)): "c"}, r: map[Key]interface{}{New(uint32(35)): "c"}, }, { k: New(uint32(37)), m: map[Key]interface{}{}, r: map[Key]interface{}{}, }, { k: New(map[string]interface{}{"a": "b", "c": uint64(4)}), m: map[Key]interface{}{ New(map[string]interface{}{"a": "b", "c": uint64(4)}): "foo", }, r: map[Key]interface{}{}, }, { k: New(customKey{i: 42}), m: map[Key]interface{}{New(customKey{i: 42}): "c"}, r: map[Key]interface{}{}, }} for _, tcase := range tests { tcase.k.DeleteFromMap(tcase.m) if !test.DeepEqual(tcase.m, tcase.r) { t.Errorf("Wrong result for case:\nk: %#v\nm: %#v\nr: %#v", tcase.k, tcase.m, tcase.r) } } }
func TestSetToMap(t *testing.T) { tests := []struct { k Key v interface{} m map[Key]interface{} r map[Key]interface{} }{{ k: New("a"), v: "c", m: map[Key]interface{}{New("a"): "b"}, r: map[Key]interface{}{New("a"): "c"}, }, { k: New("b"), v: uint64(56), m: map[Key]interface{}{New("a"): "b"}, r: map[Key]interface{}{ New("a"): "b", New("b"): uint64(56), }, }, { k: New("a"), v: "foo", m: map[Key]interface{}{}, r: map[Key]interface{}{New("a"): "foo"}, }, { k: New(uint32(35)), v: "d", m: map[Key]interface{}{New(uint32(35)): "c"}, r: map[Key]interface{}{New(uint32(35)): "d"}, }, { k: New(uint32(36)), v: true, m: map[Key]interface{}{New(uint32(35)): "c"}, r: map[Key]interface{}{ New(uint32(35)): "c", New(uint32(36)): true, }, }, { k: New(uint32(37)), v: false, m: map[Key]interface{}{New(uint32(36)): "c"}, r: map[Key]interface{}{ New(uint32(36)): "c", New(uint32(37)): false, }, }, { k: New(uint32(37)), v: "foobar", m: map[Key]interface{}{}, r: map[Key]interface{}{New(uint32(37)): "foobar"}, }, { k: New(map[string]interface{}{"a": "b", "c": uint64(4)}), v: "foobar", m: map[Key]interface{}{ New(map[string]interface{}{"a": "b", "c": uint64(4)}): "foo", }, r: map[Key]interface{}{ New(map[string]interface{}{"a": "b", "c": uint64(4)}): "foobar", }, }, { k: New(map[string]interface{}{"a": "b", "c": uint64(7)}), v: "foobar", m: map[Key]interface{}{ New(map[string]interface{}{"a": "b", "c": uint64(4)}): "foo", }, r: map[Key]interface{}{ New(map[string]interface{}{"a": "b", "c": uint64(4)}): "foo", New(map[string]interface{}{"a": "b", "c": uint64(7)}): "foobar", }, }, { k: New(map[string]interface{}{"a": "b", "d": uint64(6)}), v: "barfoo", m: map[Key]interface{}{ New(map[string]interface{}{"a": "b", "c": uint64(4)}): "foo", }, r: map[Key]interface{}{ New(map[string]interface{}{"a": "b", "c": uint64(4)}): "foo", New(map[string]interface{}{"a": "b", "d": uint64(6)}): "barfoo", }, }} for i, tcase := range tests { tcase.k.SetToMap(tcase.m, tcase.v) if !test.DeepEqual(tcase.m, tcase.r) { t.Errorf("Wrong result for case %d:\nk: %#v\nm: %#v\nr: %#v", i, tcase.k, tcase.m, tcase.r) } } }
func TestKafkaProducer(t *testing.T) { mock := newMockAsyncProducer() toDB := make(chan proto.Message) topic := "occlient" systemID := "Foobar" toDBProducer := &producer{ notifsChan: toDB, kafkaProducer: mock, topic: topic, key: sarama.StringEncoder(systemID), encoder: openconfig.ElasticsearchMessageEncoder, done: make(chan struct{}), wg: sync.WaitGroup{}, } go toDBProducer.Run() response := &pb.SubscribeResponse{ Response: &pb.SubscribeResponse_Update{ Update: &pb.Notification{ Timestamp: 0, Prefix: newPath("/foo/bar"), Update: []*pb.Update{}, }, }, } document := map[string]interface{}{ "timestamp": int64(0), "update": map[string]interface{}{ "": map[string]interface{}{ "foo": map[string]interface{}{ "bar": map[string]interface{}{}, }, }, }, } toDB <- response kafkaMessage := <-mock.input if kafkaMessage.Topic != topic { t.Errorf("Unexpected Topic: %s, expecting %s", kafkaMessage.Topic, topic) } key, err := kafkaMessage.Key.Encode() if err != nil { t.Fatalf("Error encoding key: %s", err) } if string(key) != systemID { t.Errorf("Kafka message didn't have expected key: %s, expecting %s", string(key), systemID) } valueBytes, err := kafkaMessage.Value.Encode() if err != nil { t.Fatalf("Error encoding value: %s", err) } var result interface{} err = json.Unmarshal(valueBytes, &result) if err != nil { t.Errorf("Error decoding into JSON: %s", err) } if !test.DeepEqual(document["update"], result.(map[string]interface{})["update"]) { t.Errorf("Protobuf sent from Kafka Producer does not match original.\nOriginal: %v\nNew:%v", document, result) } toDBProducer.Stop() }