github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/logsender/worker_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package logsender_test
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/juju/loggo"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/mgo.v2/bson"
    14  
    15  	"github.com/juju/juju/api"
    16  	"github.com/juju/juju/feature"
    17  	jujutesting "github.com/juju/juju/juju/testing"
    18  	"github.com/juju/juju/testing"
    19  	"github.com/juju/juju/testing/factory"
    20  	"github.com/juju/juju/worker/logsender"
    21  )
    22  
    23  type Suite struct {
    24  	jujutesting.JujuConnSuite
    25  	apiInfo *api.Info
    26  }
    27  
    28  var _ = gc.Suite(&Suite{})
    29  
    30  func (s *Suite) SetUpTest(c *gc.C) {
    31  	s.SetInitialFeatureFlags(feature.DbLog)
    32  	s.JujuConnSuite.SetUpTest(c)
    33  
    34  	// Create a machine for the client to log in as.
    35  	nonce := "some-nonce"
    36  	machine, password := s.Factory.MakeMachineReturningPassword(c,
    37  		&factory.MachineParams{Nonce: nonce})
    38  	s.apiInfo = s.APIInfo(c)
    39  	s.apiInfo.Tag = machine.Tag()
    40  	s.apiInfo.Password = password
    41  	s.apiInfo.Nonce = nonce
    42  }
    43  
    44  func (s *Suite) TestLogSending(c *gc.C) {
    45  	const logCount = 5
    46  	logsCh := make(chan *logsender.LogRecord, logCount)
    47  
    48  	// Start the logsender worker.
    49  	worker := logsender.New(logsCh, s.apiInfo)
    50  	defer func() {
    51  		worker.Kill()
    52  		c.Check(worker.Wait(), jc.ErrorIsNil)
    53  	}()
    54  
    55  	// Send somes logs, also building up what should appear in the
    56  	// database.
    57  	var expectedDocs []bson.M
    58  	for i := 0; i < logCount; i++ {
    59  		ts := time.Now().Truncate(time.Millisecond)
    60  		location := fmt.Sprintf("loc%d", i)
    61  		message := fmt.Sprintf("%d", i)
    62  
    63  		logsCh <- &logsender.LogRecord{
    64  			Time:     ts,
    65  			Module:   "logsender-test",
    66  			Location: location,
    67  			Level:    loggo.INFO,
    68  			Message:  message,
    69  		}
    70  
    71  		expectedDocs = append(expectedDocs, bson.M{
    72  			"t": ts,
    73  			"e": s.State.EnvironUUID(),
    74  			"n": s.apiInfo.Tag.String(),
    75  			"m": "logsender-test",
    76  			"l": location,
    77  			"v": int(loggo.INFO),
    78  			"x": message,
    79  		})
    80  	}
    81  
    82  	// Wait for the logs to appear in the database.
    83  	var docs []bson.M
    84  	logsColl := s.State.MongoSession().DB("logs").C("logs")
    85  	for a := testing.LongAttempt.Start(); a.Next(); {
    86  		err := logsColl.Find(bson.M{"m": "logsender-test"}).All(&docs)
    87  		c.Assert(err, jc.ErrorIsNil)
    88  		if len(docs) == logCount {
    89  			break
    90  		}
    91  	}
    92  
    93  	// Check that the logs are correct.
    94  	c.Assert(docs, gc.HasLen, logCount)
    95  	for i := 0; i < logCount; i++ {
    96  		doc := docs[i]
    97  		delete(doc, "_id")
    98  		c.Assert(doc, gc.DeepEquals, expectedDocs[i])
    99  	}
   100  }