github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/test_log.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/evergreen-ci/evergreen/db"
     7  	"github.com/evergreen-ci/evergreen/db/bsonutil"
     8  	"github.com/pkg/errors"
     9  	"gopkg.in/mgo.v2"
    10  	"gopkg.in/mgo.v2/bson"
    11  )
    12  
    13  const TestLogCollection = "test_logs"
    14  
    15  type TestLog struct {
    16  	Id            string   `bson:"_id" json:"_id"`
    17  	Name          string   `json:"name" bson:"name"`
    18  	Task          string   `json:"task" bson:"task"`
    19  	TaskExecution int      `json:"execution" bson:"execution"`
    20  	Lines         []string `json:"lines" bson:"lines"`
    21  }
    22  
    23  var (
    24  	TestLogIdKey            = bsonutil.MustHaveTag(TestLog{}, "Id")
    25  	TestLogNameKey          = bsonutil.MustHaveTag(TestLog{}, "Name")
    26  	TestLogTaskKey          = bsonutil.MustHaveTag(TestLog{}, "Task")
    27  	TestLogTaskExecutionKey = bsonutil.MustHaveTag(TestLog{}, "TaskExecution")
    28  	TestLogLinesKey         = bsonutil.MustHaveTag(TestLog{}, "Lines")
    29  )
    30  
    31  func FindOneTestLogById(id string) (*TestLog, error) {
    32  	tl := &TestLog{}
    33  	err := db.FindOne(
    34  		TestLogCollection,
    35  		bson.M{
    36  			TestLogIdKey: id,
    37  		},
    38  		db.NoProjection,
    39  		db.NoSort,
    40  		tl,
    41  	)
    42  	if err == mgo.ErrNotFound {
    43  		return nil, nil
    44  	}
    45  	return tl, errors.WithStack(err)
    46  }
    47  
    48  // FindOneTestLog returns a TestLog, given the test's name, task id,
    49  // and execution.
    50  func FindOneTestLog(name, task string, execution int) (*TestLog, error) {
    51  	tl := &TestLog{}
    52  	err := db.FindOne(
    53  		TestLogCollection,
    54  		bson.M{
    55  			TestLogNameKey:          name,
    56  			TestLogTaskKey:          task,
    57  			TestLogTaskExecutionKey: execution,
    58  		},
    59  		db.NoProjection,
    60  		db.NoSort,
    61  		tl,
    62  	)
    63  	if err == mgo.ErrNotFound {
    64  		return nil, nil
    65  	}
    66  	return tl, errors.WithStack(err)
    67  }
    68  
    69  // Insert inserts the TestLog into the database
    70  func (self *TestLog) Insert() error {
    71  	self.Id = bson.NewObjectId().Hex()
    72  	if err := self.Validate(); err != nil {
    73  		return errors.Wrap(err, "cannot insert invalid test log")
    74  	}
    75  	return errors.WithStack(db.Insert(TestLogCollection, self))
    76  }
    77  
    78  // Validate makes sure the log will accessible in the database
    79  // before the log itself is inserted. Returns an error if
    80  // something is wrong.
    81  func (self *TestLog) Validate() error {
    82  	switch {
    83  	case self.Name == "":
    84  		return errors.New("test log requires a 'Name' field")
    85  	case self.Task == "":
    86  		return errors.New("test log requires a 'Task' field")
    87  	default:
    88  		return nil
    89  	}
    90  }
    91  
    92  // URL returns the path to access the log based on its current fields.
    93  // Does not error if fields are not set.
    94  func (self *TestLog) URL() string {
    95  	return fmt.Sprintf("/test_log/%v/%v/%v",
    96  		self.Task,
    97  		self.TaskExecution,
    98  		self.Name,
    99  	)
   100  }