github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/apiv3/servicecontext/task_test.go (about)

     1  package servicecontext
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/evergreen-ci/evergreen/apiv3"
    10  	"github.com/evergreen-ci/evergreen/db"
    11  	"github.com/evergreen-ci/evergreen/model/task"
    12  	"github.com/evergreen-ci/evergreen/testutil"
    13  	. "github.com/smartystreets/goconvey/convey"
    14  )
    15  
    16  var (
    17  	testConfig = testutil.TestConfig()
    18  )
    19  
    20  func TestFindTaskById(t *testing.T) {
    21  	testutil.ConfigureIntegrationTest(t, testConfig, "TestFindTaskById")
    22  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig))
    23  
    24  	serviceContext := &DBServiceContext{}
    25  	numTasks := 10
    26  
    27  	Convey("When there are task documents in the database", t, func() {
    28  		testutil.HandleTestingErr(db.Clear(task.Collection), t, "Error clearing"+
    29  			" '%v' collection", task.Collection)
    30  		for i := 0; i < numTasks; i++ {
    31  			testTask := &task.Task{
    32  				Id:      fmt.Sprintf("task_%d", i),
    33  				BuildId: fmt.Sprintf("build_%d", i),
    34  			}
    35  			So(testTask.Insert(), ShouldBeNil)
    36  		}
    37  
    38  		Convey("then properly finding each task should succeed", func() {
    39  			for i := 0; i < numTasks; i++ {
    40  				found, err := serviceContext.FindTaskById(fmt.Sprintf("task_%d", i))
    41  				So(err, ShouldBeNil)
    42  				So(found.BuildId, ShouldEqual, fmt.Sprintf("build_%d", i))
    43  			}
    44  		})
    45  		Convey("then searching for task that doesn't exist should"+
    46  			" fail with an APIError", func() {
    47  			found, err := serviceContext.FindTaskById("fake_task")
    48  			So(err, ShouldNotBeNil)
    49  			So(found, ShouldBeNil)
    50  
    51  			So(err, ShouldHaveSameTypeAs, &apiv3.APIError{})
    52  			apiErr, ok := err.(*apiv3.APIError)
    53  			So(ok, ShouldBeTrue)
    54  			So(apiErr.StatusCode, ShouldEqual, http.StatusNotFound)
    55  
    56  		})
    57  	})
    58  }
    59  
    60  func TestFindTasksByBuildId(t *testing.T) {
    61  	testutil.ConfigureIntegrationTest(t, testConfig, "TestFindTasksByBuildId")
    62  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig))
    63  
    64  	serviceContext := &DBServiceContext{}
    65  	numBuilds := 2
    66  	numTasks := 16
    67  	taskIds := make([][]string, numBuilds)
    68  
    69  	for bix := 0; bix < numBuilds; bix++ {
    70  		tids := make([]string, numTasks)
    71  		for tix := range tids {
    72  			tids[tix] = fmt.Sprintf("task_%d_build_%d", tix, bix)
    73  		}
    74  		sort.StringSlice(tids).Sort()
    75  		taskIds[bix] = tids
    76  	}
    77  
    78  	Convey("When there are task documents in the database", t, func() {
    79  		testutil.HandleTestingErr(db.Clear(task.Collection), t, "Error clearing"+
    80  			" '%v' collection", task.Collection)
    81  		for bix := 0; bix < numBuilds; bix++ {
    82  			for tix, tid := range taskIds[bix] {
    83  				status := "pass"
    84  				if (tix % 2) == 0 {
    85  					status = "fail"
    86  				}
    87  				testTask := &task.Task{
    88  					Id:      tid,
    89  					BuildId: fmt.Sprintf("build_%d", bix),
    90  					Status:  status,
    91  				}
    92  				So(testTask.Insert(), ShouldBeNil)
    93  			}
    94  		}
    95  
    96  		Convey("then properly finding each set of tasks should succeed", func() {
    97  			for bix := 0; bix < numBuilds; bix++ {
    98  				foundTasks, err := serviceContext.FindTasksByBuildId(fmt.Sprintf("build_%d", bix),
    99  					"", "", 0, 1)
   100  				So(err, ShouldBeNil)
   101  				So(len(foundTasks), ShouldEqual, numTasks)
   102  				for tix, t := range foundTasks {
   103  					So(t.Id, ShouldEqual, taskIds[bix][tix])
   104  				}
   105  			}
   106  		})
   107  		Convey("then properly finding only tasks with status should return correct set", func() {
   108  			for _, status := range []string{"pass", "fail"} {
   109  				for bix := 0; bix < numBuilds; bix++ {
   110  					foundTasks, err := serviceContext.FindTasksByBuildId(fmt.Sprintf("build_%d", bix),
   111  						"", status, 0, 1)
   112  					So(err, ShouldBeNil)
   113  					So(len(foundTasks), ShouldEqual, numTasks/2)
   114  					for _, t := range foundTasks {
   115  						So(t.Status, ShouldEqual, status)
   116  					}
   117  				}
   118  			}
   119  		})
   120  		Convey("then properly finding only tasks from taskid should return correct set", func() {
   121  			buildId := "build_1"
   122  			tids := taskIds[1]
   123  			for _, sort := range []int{1, -1} {
   124  				for i := 0; i < numTasks; i++ {
   125  					foundTasks, err := serviceContext.FindTasksByBuildId(buildId, tids[i],
   126  						"", 0, sort)
   127  					So(err, ShouldBeNil)
   128  
   129  					startAt := 0
   130  					if sort < 0 {
   131  						startAt = len(tids) - 1
   132  					}
   133  
   134  					So(len(foundTasks), ShouldEqual, (numTasks-startAt)-i*sort)
   135  					for ix, t := range foundTasks {
   136  						index := ix
   137  						if sort > 0 {
   138  							index += i
   139  						}
   140  						So(t.Id, ShouldEqual, tids[index])
   141  					}
   142  				}
   143  			}
   144  		})
   145  		Convey("then adding a limit should return correct number and set of results",
   146  			func() {
   147  				buildId := "build_0"
   148  				limit := 2
   149  				tids := taskIds[0]
   150  				for i := 0; i < numTasks/limit; i++ {
   151  					index := i * limit
   152  					taskName := tids[index]
   153  					foundTasks, err := serviceContext.FindTasksByBuildId(buildId, taskName,
   154  						"", limit, 1)
   155  					So(err, ShouldBeNil)
   156  					So(len(foundTasks), ShouldEqual, limit)
   157  					for ix, t := range foundTasks {
   158  						So(t.Id, ShouldEqual, tids[ix+index])
   159  					}
   160  				}
   161  
   162  			})
   163  		Convey("then searching for build that doesn't exist should"+
   164  			" fail with an APIError", func() {
   165  			foundTests, err := serviceContext.FindTasksByBuildId("fake_build", "", "", 0, 1)
   166  			So(err, ShouldNotBeNil)
   167  			So(len(foundTests), ShouldEqual, 0)
   168  
   169  			So(err, ShouldHaveSameTypeAs, &apiv3.APIError{})
   170  			apiErr, ok := err.(*apiv3.APIError)
   171  			So(ok, ShouldBeTrue)
   172  			So(apiErr.StatusCode, ShouldEqual, http.StatusNotFound)
   173  		})
   174  		Convey("then searching for a project and commit with no task name should return first result",
   175  			func() {
   176  				buildId := "build_0"
   177  				foundTasks, err := serviceContext.FindTasksByBuildId(buildId, "", "", 1, 1)
   178  				So(err, ShouldBeNil)
   179  				So(len(foundTasks), ShouldEqual, 1)
   180  				task1 := foundTasks[0]
   181  				So(task1.Id, ShouldEqual, taskIds[0][0])
   182  			})
   183  		Convey("then starting at a task that doesn't exist"+
   184  			" fail with an APIError", func() {
   185  			foundTests, err := serviceContext.FindTasksByBuildId("build_0", "fake_task", "", 0, 1)
   186  			So(err, ShouldNotBeNil)
   187  			So(len(foundTests), ShouldEqual, 0)
   188  
   189  			So(err, ShouldHaveSameTypeAs, &apiv3.APIError{})
   190  			apiErr, ok := err.(*apiv3.APIError)
   191  			So(ok, ShouldBeTrue)
   192  			So(apiErr.StatusCode, ShouldEqual, http.StatusNotFound)
   193  		})
   194  	})
   195  }
   196  
   197  func TestFindTasksByProjectAndCommit(t *testing.T) {
   198  	testutil.ConfigureIntegrationTest(t, testConfig, "TestFindTasksByProjectAndCommit")
   199  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig))
   200  
   201  	serviceContext := &DBServiceContext{}
   202  	numCommits := 2
   203  	numProjects := 2
   204  	numTasks := 16
   205  	taskIds := make([][][]string, numProjects)
   206  
   207  	for pix := 0; pix < numProjects; pix++ {
   208  		taskIds[pix] = make([][]string, numCommits)
   209  		for cix := 0; cix < numCommits; cix++ {
   210  			tids := make([]string, numTasks)
   211  			for tix := range tids {
   212  				tids[tix] = fmt.Sprintf("task_%d_project%d_commit%d", tix, pix, cix)
   213  			}
   214  			sort.StringSlice(tids).Sort()
   215  			taskIds[pix][cix] = tids
   216  		}
   217  	}
   218  
   219  	Convey("When there are task documents in the database", t, func() {
   220  		testutil.HandleTestingErr(db.Clear(task.Collection), t, "Error clearing"+
   221  			" '%v' collection", task.Collection)
   222  		for cix := 0; cix < numCommits; cix++ {
   223  			for pix := 0; pix < numProjects; pix++ {
   224  				for tix, tid := range taskIds[pix][cix] {
   225  					status := "pass"
   226  					if (tix % 2) == 0 {
   227  						status = "fail"
   228  					}
   229  					testTask := &task.Task{
   230  						Id:       tid,
   231  						Revision: fmt.Sprintf("commit_%d", cix),
   232  						Project:  fmt.Sprintf("project_%d", pix),
   233  						Status:   status,
   234  					}
   235  					So(testTask.Insert(), ShouldBeNil)
   236  				}
   237  			}
   238  		}
   239  
   240  		Convey("then properly finding each set of tasks should succeed", func() {
   241  			for pix := 0; pix < numProjects; pix++ {
   242  				for cix := 0; cix < numCommits; cix++ {
   243  					foundTasks, err := serviceContext.FindTasksByProjectAndCommit(fmt.Sprintf("project_%d", pix),
   244  						fmt.Sprintf("commit_%d", cix), "", "", 0, 1)
   245  					So(err, ShouldBeNil)
   246  					So(len(foundTasks), ShouldEqual, numTasks)
   247  					for tix, t := range foundTasks {
   248  						So(t.Id, ShouldEqual, taskIds[pix][cix][tix])
   249  					}
   250  				}
   251  			}
   252  		})
   253  		Convey("then properly finding only tasks with status should return correct set", func() {
   254  			for _, status := range []string{"pass", "fail"} {
   255  				for pix := 0; pix < numProjects; pix++ {
   256  					for cix := 0; cix < numCommits; cix++ {
   257  						foundTasks, err := serviceContext.FindTasksByProjectAndCommit(fmt.Sprintf("project_%d", pix),
   258  							fmt.Sprintf("commit_%d", cix), "", status, 0, 1)
   259  						So(err, ShouldBeNil)
   260  						So(len(foundTasks), ShouldEqual, numTasks/2)
   261  						for _, t := range foundTasks {
   262  							So(t.Status, ShouldEqual, status)
   263  						}
   264  					}
   265  				}
   266  			}
   267  		})
   268  		Convey("then properly finding only tasks from taskid should return correct set", func() {
   269  			commitId := "commit_1"
   270  			projectId := "project_1"
   271  			tids := taskIds[1][1]
   272  			for _, sort := range []int{1, -1} {
   273  				for i := 0; i < numTasks; i++ {
   274  					foundTasks, err := serviceContext.FindTasksByProjectAndCommit(projectId, commitId,
   275  						tids[i], "", 0, sort)
   276  					So(err, ShouldBeNil)
   277  
   278  					startAt := 0
   279  					if sort < 0 {
   280  						startAt = len(tids) - 1
   281  					}
   282  
   283  					So(len(foundTasks), ShouldEqual, (numTasks-startAt)-i*sort)
   284  					for ix, t := range foundTasks {
   285  						index := ix
   286  						if sort > 0 {
   287  							index += i
   288  						}
   289  						So(t.Id, ShouldEqual, tids[index])
   290  					}
   291  				}
   292  			}
   293  		})
   294  		Convey("then adding a limit should return correct number and set of results",
   295  			func() {
   296  				commitId := "commit_0"
   297  				projectId := "project_0"
   298  				limit := 2
   299  				tids := taskIds[0][0]
   300  				for i := 0; i < numTasks/limit; i++ {
   301  					index := i * limit
   302  					taskName := tids[index]
   303  					foundTasks, err := serviceContext.FindTasksByProjectAndCommit(projectId, commitId,
   304  						taskName, "", limit, 1)
   305  					So(err, ShouldBeNil)
   306  					So(len(foundTasks), ShouldEqual, limit)
   307  					for ix, t := range foundTasks {
   308  						So(t.Id, ShouldEqual, tids[ix+index])
   309  					}
   310  				}
   311  
   312  			})
   313  		Convey("then searching for project that doesn't exist should"+
   314  			" fail with an APIError", func() {
   315  			foundTests, err := serviceContext.FindTasksByProjectAndCommit("fake_project", "commit_0", "", "", 0, 1)
   316  			So(err, ShouldNotBeNil)
   317  			So(len(foundTests), ShouldEqual, 0)
   318  
   319  			So(err, ShouldHaveSameTypeAs, &apiv3.APIError{})
   320  			apiErr, ok := err.(*apiv3.APIError)
   321  			So(ok, ShouldBeTrue)
   322  			So(apiErr.StatusCode, ShouldEqual, http.StatusNotFound)
   323  		})
   324  		Convey("then searching for a project and commit with no task name should return first result",
   325  			func() {
   326  				projectId := "project_0"
   327  				commitId := "commit_0"
   328  				foundTasks, err := serviceContext.FindTasksByProjectAndCommit(projectId, commitId, "", "", 1, 1)
   329  				So(err, ShouldBeNil)
   330  				So(len(foundTasks), ShouldEqual, 1)
   331  				task1 := foundTasks[0]
   332  				So(task1.Id, ShouldEqual, taskIds[0][0][0])
   333  			})
   334  		Convey("then starting at a task that doesn't exist"+
   335  			" fail with an APIError", func() {
   336  			foundTests, err := serviceContext.FindTasksByProjectAndCommit("project_0", "commit_0", "fake_task", "", 0, 1)
   337  			So(err, ShouldNotBeNil)
   338  			So(len(foundTests), ShouldEqual, 0)
   339  
   340  			So(err, ShouldHaveSameTypeAs, &apiv3.APIError{})
   341  			apiErr, ok := err.(*apiv3.APIError)
   342  			So(ok, ShouldBeTrue)
   343  			So(apiErr.StatusCode, ShouldEqual, http.StatusNotFound)
   344  		})
   345  		Convey("then searching for a commit that doesn't exist"+
   346  			" fail with an APIError", func() {
   347  			foundTests, err := serviceContext.FindTasksByProjectAndCommit("project_0", "fake_commit", "", "", 0, 1)
   348  			So(err, ShouldNotBeNil)
   349  			So(len(foundTests), ShouldEqual, 0)
   350  
   351  			So(err, ShouldHaveSameTypeAs, &apiv3.APIError{})
   352  			apiErr, ok := err.(*apiv3.APIError)
   353  			So(ok, ShouldBeTrue)
   354  			So(apiErr.StatusCode, ShouldEqual, http.StatusNotFound)
   355  		})
   356  	})
   357  }