github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/attach/results_command_test.go (about) 1 package attach_test 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/evergreen-ci/evergreen/agent/comm" 10 agentutil "github.com/evergreen-ci/evergreen/agent/testutil" 11 "github.com/evergreen-ci/evergreen/db" 12 "github.com/evergreen-ci/evergreen/model" 13 "github.com/evergreen-ci/evergreen/model/task" 14 modelutil "github.com/evergreen-ci/evergreen/model/testutil" 15 "github.com/evergreen-ci/evergreen/plugin" 16 . "github.com/evergreen-ci/evergreen/plugin/builtin/attach" 17 "github.com/evergreen-ci/evergreen/plugin/plugintest" 18 "github.com/evergreen-ci/evergreen/service" 19 "github.com/evergreen-ci/evergreen/testutil" 20 "github.com/evergreen-ci/evergreen/util" 21 "github.com/mongodb/grip/slogger" 22 . "github.com/smartystreets/goconvey/convey" 23 ) 24 25 func resetTasks(t *testing.T) { 26 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testutil.TestConfig())) 27 testutil.HandleTestingErr( 28 db.ClearCollections(task.Collection, model.TestLogCollection), t, 29 "error clearing test collections") 30 } 31 32 func TestAttachResults(t *testing.T) { 33 resetTasks(t) 34 testConfig := testutil.TestConfig() 35 cwd := testutil.GetDirectoryOfFile() 36 fmt.Println(cwd) 37 Convey("With attachResults plugin installed into plugin registry", t, func() { 38 registry := plugin.NewSimpleRegistry() 39 attachPlugin := &AttachPlugin{} 40 err := registry.Register(attachPlugin) 41 testutil.HandleTestingErr(err, t, "Couldn't register plugin: %v") 42 43 server, err := service.CreateTestServer(testConfig, nil, plugin.APIPlugins) 44 testutil.HandleTestingErr(err, t, "Couldn't set up testing server") 45 defer server.Close() 46 47 configFile := filepath.Join(cwd, "testdata", "plugin_attach_results.yml") 48 resultsLoc := filepath.Join(cwd, "testdata", "plugin_attach_results.json") 49 50 modelData, err := modelutil.SetupAPITestData(testConfig, "test", "rhel55", configFile, modelutil.NoPatch) 51 testutil.HandleTestingErr(err, t, "failed to setup test data") 52 So(err, ShouldBeNil) 53 modelData.TaskConfig.WorkDir = "." 54 55 logger := agentutil.NewTestLogger(slogger.StdOutAppender()) 56 57 httpCom := plugintest.TestAgentCommunicator(modelData, server.URL) 58 So(httpCom.TaskId, ShouldEqual, modelData.Task.Id) 59 So(httpCom.TaskSecret, ShouldEqual, modelData.Task.Secret) 60 So(httpCom.HostId, ShouldEqual, modelData.Host.Id) 61 So(httpCom.HostSecret, ShouldEqual, modelData.Host.Secret) 62 63 Convey("all commands in test project should execute successfully", func() { 64 taskConfig := modelData.TaskConfig 65 for _, projTask := range taskConfig.Project.Tasks { 66 So(len(projTask.Commands), ShouldNotEqual, 0) 67 for _, command := range projTask.Commands { 68 pluginCmds, err := registry.GetCommands(command, taskConfig.Project.Functions) 69 testutil.HandleTestingErr(err, t, "Couldn't get plugin command: %v") 70 So(pluginCmds, ShouldNotBeNil) 71 So(err, ShouldBeNil) 72 pluginCom := &comm.TaskJSONCommunicator{pluginCmds[0].Plugin(), httpCom} 73 err = pluginCmds[0].Execute(logger, pluginCom, taskConfig, make(chan bool)) 74 So(err, ShouldBeNil) 75 testTask, err := task.FindOne(task.ById(httpCom.TaskId)) 76 testutil.HandleTestingErr(err, t, "Couldn't find task") 77 So(testTask, ShouldNotBeNil) 78 // ensure test results are exactly as expected 79 // attempt to open the file 80 reportFile, err := os.Open(resultsLoc) 81 testutil.HandleTestingErr(err, t, "Couldn't open report file: '%v'", err) 82 results := &task.TestResults{} 83 err = util.ReadJSONInto(reportFile, results) 84 testutil.HandleTestingErr(err, t, "Couldn't read report file: '%v'", err) 85 testResults := *results 86 So(testTask.TestResults, ShouldResemble, testResults.Results) 87 testutil.HandleTestingErr(err, t, "Couldn't clean up test temp dir") 88 } 89 } 90 }) 91 }) 92 } 93 func TestAttachRawResults(t *testing.T) { 94 resetTasks(t) 95 testConfig := testutil.TestConfig() 96 cwd := testutil.GetDirectoryOfFile() 97 Convey("With attachResults plugin installed into plugin registry", t, func() { 98 registry := plugin.NewSimpleRegistry() 99 attachPlugin := &AttachPlugin{} 100 err := registry.Register(attachPlugin) 101 testutil.HandleTestingErr(err, t, "Couldn't register plugin: %v") 102 103 server, err := service.CreateTestServer(testConfig, nil, plugin.APIPlugins) 104 testutil.HandleTestingErr(err, t, "Couldn't set up testing server") 105 defer server.Close() 106 107 configFile := filepath.Join(cwd, "testdata", "plugin_attach_results_raw.yml") 108 resultsLoc := filepath.Join(cwd, "testdata", "plugin_attach_results_raw.json") 109 110 modelData, err := modelutil.SetupAPITestData(testConfig, "test", "rhel55", configFile, modelutil.NoPatch) 111 testutil.HandleTestingErr(err, t, "failed to setup test data") 112 113 httpCom := plugintest.TestAgentCommunicator(modelData, server.URL) 114 115 modelData.TaskConfig.WorkDir = "." 116 taskConfig := modelData.TaskConfig 117 logger := agentutil.NewTestLogger(slogger.StdOutAppender()) 118 119 Convey("when attaching a raw log ", func() { 120 for _, projTask := range taskConfig.Project.Tasks { 121 So(len(projTask.Commands), ShouldNotEqual, 0) 122 for _, command := range projTask.Commands { 123 124 pluginCmds, err := registry.GetCommands(command, taskConfig.Project.Functions) 125 testutil.HandleTestingErr(err, t, "Couldn't get plugin command: %v") 126 So(pluginCmds, ShouldNotBeNil) 127 So(err, ShouldBeNil) 128 // create a plugin communicator 129 pluginCom := &comm.TaskJSONCommunicator{pluginCmds[0].Plugin(), httpCom} 130 err = pluginCmds[0].Execute(logger, pluginCom, taskConfig, make(chan bool)) 131 So(err, ShouldBeNil) 132 Convey("when retrieving task", func() { 133 // fetch the task 134 testTask, err := task.FindOne(task.ById(httpCom.TaskId)) 135 testutil.HandleTestingErr(err, t, "Couldn't find task") 136 So(testTask, ShouldNotBeNil) 137 138 Convey("test results should match and raw log should be in appropriate collection", func() { 139 140 reportFile, err := os.Open(resultsLoc) 141 testutil.HandleTestingErr(err, t, "Couldn't open report file: '%v'", err) 142 results := &task.TestResults{} 143 err = util.ReadJSONInto(reportFile, results) 144 testutil.HandleTestingErr(err, t, "Couldn't read report file: '%v'", err) 145 146 testResults := *results 147 So(len(testResults.Results), ShouldEqual, 3) 148 So(len(testTask.TestResults), ShouldEqual, 3) 149 firstResult := testTask.TestResults[0] 150 So(firstResult.LogRaw, ShouldEqual, "") 151 So(firstResult.LogId, ShouldNotEqual, "") 152 153 testLog, err := model.FindOneTestLogById(firstResult.LogId) 154 So(err, ShouldBeNil) 155 So(testLog.Lines[0], ShouldEqual, testResults.Results[0].LogRaw) 156 157 Convey("both URL and raw log should be stored appropriately if both exist", func() { 158 urlResult := testTask.TestResults[2] 159 So(urlResult.LogRaw, ShouldEqual, "") 160 So(urlResult.URL, ShouldNotEqual, "") 161 So(urlResult.LogId, ShouldNotEqual, "") 162 163 testLog, err := model.FindOneTestLogById(urlResult.LogId) 164 So(err, ShouldBeNil) 165 So(testLog.Lines[0], ShouldEqual, testResults.Results[2].LogRaw) 166 }) 167 }) 168 }) 169 170 } 171 } 172 }) 173 }) 174 }