Esempio n. 1
0
func (s *FromFileSuite) TestFromFile(c *C) {
	log.Println("testing FromFile")
	b, ch := test_utils.NewBlock("testingFile", "fromfile")
	go blocks.BlockRoutine(b)
	outChan := make(chan *blocks.Msg)
	ch.AddChan <- &blocks.AddChanMsg{
		Route:   "out",
		Channel: outChan,
	}

	f, err := ioutil.TempFile("", "streamtools_test_from_file.log")
	if err != nil {
		c.Errorf(err.Error())
	}

	defer syscall.Unlink(f.Name())

	var fromfilestring = string(`{"Name": "Jacqui Maher", "Location": "Brooklyn", "Dog": "Conor S. Dogberst" }
{"Name": "Nik Hanselmann", "Location": "New York", "Dog": "None:(" }
{"Name": "Mike Dewar", "Location": "The Moon", "Dog": "Percy ? Dewar" }`)

	ioutil.WriteFile(f.Name(), []byte(fromfilestring), 0644)

	ruleMsg := map[string]interface{}{"Filename": f.Name()}
	toRule := &blocks.Msg{Msg: ruleMsg, Route: "rule"}
	ch.InChan <- toRule

	time.AfterFunc(time.Duration(1)*time.Second, func() {
		ch.InChan <- &blocks.Msg{Msg: map[string]interface{}{}, Route: "poll"}
	})
	time.AfterFunc(time.Duration(1)*time.Second, func() {
		ch.InChan <- &blocks.Msg{Msg: map[string]interface{}{}, Route: "poll"}
	})
	time.AfterFunc(time.Duration(1)*time.Second, func() {
		ch.InChan <- &blocks.Msg{Msg: map[string]interface{}{}, Route: "poll"}
	})

	time.AfterFunc(time.Duration(5)*time.Second, func() {
		ch.QuitChan <- true
	})

	var expectedNames = []string{"Jacqui Maher", "Nik Hanselmann", "Mike Dewar"}
	var expectedLocations = []string{"Brooklyn", "New York", "The Moon"}
	for {
		select {
		case err := <-ch.ErrChan:
			if err != nil {
				c.Errorf(err.Error())
			} else {
				return
			}
		case messageI := <-outChan:
			message := messageI.Msg.(map[string]interface{})

			nameReceived, ok := message["Name"].(string)
			if !ok {
				log.Println("failed asserting message['Name'] to a string")
			}

			locationReceived, ok := message["Location"].(string)
			if !ok {
				log.Println("failed asserting message['Location'] to a string")
			}

			if !test_utils.StringInSlice(expectedNames, nameReceived) {
				log.Println("failed finding", nameReceived, "in expected names list")
				c.Fail()
			}

			if !test_utils.StringInSlice(expectedLocations, locationReceived) {
				log.Println("failed finding", locationReceived, "in expected locations list")
				c.Fail()
			}
		}
	}
}
Esempio n. 2
0
func (s *ParseCSVSuite) TestParseCSV(c *C) {
	log.Println("testing ParseCSV")
	b, ch := test_utils.NewBlock("testingParseCSV", "parsecsv")
	go blocks.BlockRoutine(b)
	outChan := make(chan *blocks.Msg)
	ch.AddChan <- &blocks.AddChanMsg{
		Route:   "out",
		Channel: outChan,
	}

	// where to find the xml in input
	headers := []string{"name", "email", "phone"}
	ruleMsg := map[string]interface{}{"Path": ".data", "Headers": headers}
	toRule := &blocks.Msg{Msg: ruleMsg, Route: "rule"}
	ch.InChan <- toRule

	queryOutChan := make(blocks.MsgChan)
	time.AfterFunc(time.Duration(1)*time.Second, func() {
		ch.QueryChan <- &blocks.QueryMsg{MsgChan: queryOutChan, Route: "rule"}
	})

	var csvInput = `
	Jacqui Maher, [email protected], 555-5550
	Mike Dewar, [email protected], 555-5551
	Nik Hanselmann, [email protected], 555-5552
	Jane Friedoff, [email protected], 555-5553, Extra
	`

	time.AfterFunc(time.Duration(1)*time.Second, func() {
		csvMsg := map[string]interface{}{"data": csvInput}
		postData := &blocks.Msg{Msg: csvMsg, Route: "in"}
		ch.InChan <- postData
	})

	time.AfterFunc(time.Duration(1)*time.Second, func() {
		ch.InChan <- &blocks.Msg{Msg: map[string]interface{}{}, Route: "poll"}
	})
	time.AfterFunc(time.Duration(1)*time.Second, func() {
		ch.InChan <- &blocks.Msg{Msg: map[string]interface{}{}, Route: "poll"}
	})
	time.AfterFunc(time.Duration(1)*time.Second, func() {
		ch.InChan <- &blocks.Msg{Msg: map[string]interface{}{}, Route: "poll"}
	})
	time.AfterFunc(time.Duration(1)*time.Second, func() {
		ch.InChan <- &blocks.Msg{Msg: map[string]interface{}{}, Route: "poll"}
	})

	time.AfterFunc(time.Duration(6)*time.Second, func() {
		ch.QuitChan <- true
	})

	var expectedNames = []string{"Jacqui Maher", "Nik Hanselmann", "Mike Dewar", "Jane Friedoff"}

	for {
		select {
		case err := <-ch.ErrChan:
			if err != nil {
				c.Errorf(err.Error())
			} else {
				return
			}
		case messageI := <-queryOutChan:
			if !reflect.DeepEqual(messageI, ruleMsg) {
				log.Println("Rule mismatch:", messageI, ruleMsg)
				c.Fail()
			}
		case messageI := <-outChan:
			message := messageI.Msg.(map[string]interface{})

			log.Println(message)
			nameReceived := message["name"].(string)
			if !test_utils.StringInSlice(expectedNames, nameReceived) {
				log.Println("failed finding", nameReceived, "in expected names list")
				c.Fail()
			}
		}
	}
}