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

     1  package service
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  	"testing"
     9  
    10  	"github.com/evergreen-ci/evergreen"
    11  	"github.com/evergreen-ci/evergreen/db"
    12  	"github.com/evergreen-ci/evergreen/model/event"
    13  	"github.com/evergreen-ci/evergreen/model/task"
    14  	"github.com/evergreen-ci/evergreen/plugin"
    15  	"github.com/evergreen-ci/evergreen/testutil"
    16  	"github.com/mongodb/grip"
    17  	"github.com/mongodb/grip/message"
    18  	. "github.com/smartystreets/goconvey/convey"
    19  )
    20  
    21  func TestResourceInfoEndPoints(t *testing.T) {
    22  	testConfig := testutil.TestConfig()
    23  	testApiServer, err := CreateTestServer(testConfig, nil, plugin.APIPlugins)
    24  	testutil.HandleTestingErr(err, t, "failed to create new API server")
    25  	defer testApiServer.Close()
    26  
    27  	err = db.ClearCollections(event.AllLogCollection, event.TaskLogCollection)
    28  	testutil.HandleTestingErr(err, t, "problem clearing event collection")
    29  	err = db.ClearCollections(task.Collection)
    30  	testutil.HandleTestingErr(err, t, "problem clearing task collection")
    31  
    32  	url := fmt.Sprintf("%s/api/2/task/", testApiServer.URL)
    33  
    34  	const (
    35  		taskId = "the_task_id"
    36  		hostId = "host_id"
    37  	)
    38  
    39  	_, err = insertTaskForTesting(taskId, "version", "project", task.TestResult{})
    40  	testutil.HandleTestingErr(err, t, "problem creating task")
    41  
    42  	_, err = insertHostWithRunningTask(hostId, taskId)
    43  	testutil.HandleTestingErr(err, t, "problem creating host")
    44  
    45  	Convey("For the system info endpoint", t, func() {
    46  		data := message.CollectSystemInfo().(*message.SystemInfo)
    47  		data.Base = message.Base{}
    48  		data.Errors = []string{}
    49  		Convey("the system info endpoint should return 200", func() {
    50  			payload, err := json.Marshal(data)
    51  			So(err, ShouldBeNil)
    52  
    53  			request, err := http.NewRequest("POST", url+taskId+"/system_info", bytes.NewBuffer(payload))
    54  			So(err, ShouldBeNil)
    55  			request.Header.Add(evergreen.HostHeader, hostId)
    56  
    57  			resp, err := http.DefaultClient.Do(request)
    58  			testutil.HandleTestingErr(err, t, "problem making request")
    59  			So(resp.StatusCode, ShouldEqual, 200)
    60  		})
    61  
    62  		Convey("the system data should persist in the database", func() {
    63  			events, err := event.Find(event.TaskLogCollection, event.TaskSystemInfoEvents(taskId, 0))
    64  			testutil.HandleTestingErr(err, t, "problem finding task event")
    65  			So(len(events), ShouldEqual, 1)
    66  			e := events[0]
    67  			So(e.ResourceId, ShouldEqual, taskId)
    68  			taskData, ok := e.Data.Data.(*event.TaskSystemResourceData)
    69  			So(ok, ShouldBeTrue)
    70  			grip.Info(taskData.SystemInfo)
    71  		})
    72  	})
    73  
    74  	Convey("For the process info endpoint", t, func() {
    75  		data := message.CollectProcessInfoSelfWithChildren()
    76  		Convey("the process info endpoint should return 200", func() {
    77  			payload, err := json.Marshal(data)
    78  			So(err, ShouldBeNil)
    79  
    80  			request, err := http.NewRequest("POST", url+taskId+"/process_info", bytes.NewBuffer(payload))
    81  			So(err, ShouldBeNil)
    82  			request.Header.Add(evergreen.HostHeader, hostId)
    83  			resp, err := http.DefaultClient.Do(request)
    84  			testutil.HandleTestingErr(err, t, "problem making request")
    85  			So(resp.StatusCode, ShouldEqual, 200)
    86  		})
    87  
    88  		Convey("the process data should persist in the database", func() {
    89  			events, err := event.Find(event.TaskLogCollection, event.TaskProcessInfoEvents(taskId, 0))
    90  			testutil.HandleTestingErr(err, t, "problem finding task event")
    91  			So(len(events), ShouldEqual, 1)
    92  			e := events[0]
    93  			So(e.ResourceId, ShouldEqual, taskId)
    94  			taskData, ok := e.Data.Data.(*event.TaskProcessResourceData)
    95  			So(ok, ShouldBeTrue)
    96  			grip.Info(taskData.Processes)
    97  		})
    98  	})
    99  }