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

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/evergreen-ci/evergreen"
     9  	"github.com/evergreen-ci/evergreen/db"
    10  	"github.com/evergreen-ci/evergreen/model/task"
    11  	"github.com/evergreen-ci/evergreen/model/version"
    12  	"github.com/evergreen-ci/evergreen/testutil"
    13  	. "github.com/smartystreets/goconvey/convey"
    14  )
    15  
    16  func dropTestDB(t *testing.T) {
    17  	session, _, err := db.GetGlobalSessionFactory().GetSession()
    18  	testutil.HandleTestingErr(err, t, "Error opening database session")
    19  	defer session.Close()
    20  	testutil.HandleTestingErr(session.DB(testConfig.Database.DB).DropDatabase(), t, "Error dropping test database")
    21  }
    22  
    23  func createVersion(order int, project string, buildVariants []string) error {
    24  	v := &version.Version{}
    25  	testActivationTime := time.Now().Add(time.Duration(4) * time.Hour)
    26  
    27  	for _, variant := range buildVariants {
    28  		v.BuildVariants = append(v.BuildVariants, version.BuildStatus{
    29  			BuildVariant: variant,
    30  			Activated:    false,
    31  			ActivateAt:   testActivationTime,
    32  		})
    33  	}
    34  	v.RevisionOrderNumber = order
    35  	v.Identifier = project
    36  	v.Id = fmt.Sprintf("version_%v_%v", order, project)
    37  	v.Requester = evergreen.RepotrackerVersionRequester
    38  	return v.Insert()
    39  }
    40  
    41  func createTask(id string, order int, project string, buildVariant string, gitspec string) error {
    42  	task := &task.Task{}
    43  	task.BuildVariant = buildVariant
    44  	task.RevisionOrderNumber = order
    45  	task.Project = project
    46  	task.Revision = gitspec
    47  	task.DisplayName = id
    48  	task.Id = id
    49  	task.Requester = evergreen.RepotrackerVersionRequester
    50  	return task.Insert()
    51  }
    52  
    53  func TestBuildVariantHistoryIterator(t *testing.T) {
    54  	dropTestDB(t)
    55  
    56  	Convey("Should return the correct tasks and versions", t, func() {
    57  		So(createVersion(1, "project1", []string{"bv1", "bv2"}), ShouldBeNil)
    58  		So(createVersion(1, "project2", []string{"bv1", "bv2"}), ShouldBeNil)
    59  		So(createVersion(2, "project1", []string{"bv1", "bv2"}), ShouldBeNil)
    60  		So(createVersion(3, "project1", []string{"bv2"}), ShouldBeNil)
    61  
    62  		So(createTask("task1", 1, "project1", "bv1", "gitspec0"), ShouldBeNil)
    63  		So(createTask("task2", 1, "project1", "bv2", "gitspec0"), ShouldBeNil)
    64  		So(createTask("task3", 1, "project2", "bv1", "gitspec1"), ShouldBeNil)
    65  		So(createTask("task4", 1, "project2", "bv2", "gitspec1"), ShouldBeNil)
    66  		So(createTask("task5", 2, "project1", "bv1", "gitspec2"), ShouldBeNil)
    67  		So(createTask("task6", 2, "project1", "bv2", "gitspec2"), ShouldBeNil)
    68  		So(createTask("task7", 3, "project1", "bv2", "gitspec3"), ShouldBeNil)
    69  
    70  		Convey("Should respect project and build variant rules", func() {
    71  			iter := NewBuildVariantHistoryIterator("bv1", "bv1", "project1")
    72  
    73  			tasks, versions, err := iter.GetItems(nil, 5)
    74  			So(err, ShouldBeNil)
    75  			So(len(versions), ShouldEqual, 2)
    76  			// Versions on project1 that have `bv1` in their build variants list
    77  			So(versions[0].Id, ShouldEqual, "version_2_project1")
    78  			So(versions[1].Id, ShouldEqual, "version_1_project1")
    79  
    80  			// Tasks with order >= 1 s.t. project == `project1` and build_variant == `bv1`
    81  			So(len(tasks), ShouldEqual, 2)
    82  			So(tasks[0]["_id"], ShouldEqual, "gitspec2")
    83  			So(tasks[1]["_id"], ShouldEqual, "gitspec0")
    84  		})
    85  	})
    86  }