github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/gotest/gotest_plugin_test.go (about)

     1  package gotest_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/evergreen-ci/evergreen/agent/comm"
     9  	agentutil "github.com/evergreen-ci/evergreen/agent/testutil"
    10  	"github.com/evergreen-ci/evergreen/db"
    11  	"github.com/evergreen-ci/evergreen/model"
    12  	"github.com/evergreen-ci/evergreen/model/task"
    13  	modelutil "github.com/evergreen-ci/evergreen/model/testutil"
    14  	"github.com/evergreen-ci/evergreen/plugin"
    15  	. "github.com/evergreen-ci/evergreen/plugin/builtin/gotest"
    16  	"github.com/evergreen-ci/evergreen/plugin/plugintest"
    17  	"github.com/evergreen-ci/evergreen/service"
    18  	"github.com/evergreen-ci/evergreen/testutil"
    19  	"github.com/mongodb/grip/slogger"
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func reset(t *testing.T) {
    24  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testutil.TestConfig()))
    25  	testutil.HandleTestingErr(
    26  		db.ClearCollections(task.Collection, model.TestLogCollection), t,
    27  		"error clearing test collections")
    28  }
    29  
    30  func TestGotestPluginOnFailingTests(t *testing.T) {
    31  	currentDirectory := testutil.GetDirectoryOfFile()
    32  	testConfig := testutil.TestConfig()
    33  	SkipConvey("With gotest plugin installed into plugin registry", t, func() {
    34  		reset(t)
    35  
    36  		registry := plugin.NewSimpleRegistry()
    37  		testPlugin := &GotestPlugin{}
    38  		err := registry.Register(testPlugin)
    39  		testutil.HandleTestingErr(err, t, "Couldn't register plugin %v")
    40  
    41  		server, err := service.CreateTestServer(testConfig, nil, plugin.APIPlugins)
    42  		testutil.HandleTestingErr(err, t, "Couldn't set up testing server")
    43  		defer server.Close()
    44  		configPath := filepath.Join(currentDirectory, "testdata", "bad.yml")
    45  		modelData, err := modelutil.SetupAPITestData(testConfig, "test", "rhel55", configPath, modelutil.NoPatch)
    46  		testutil.HandleTestingErr(err, t, "failed to setup test data")
    47  		httpCom := plugintest.TestAgentCommunicator(modelData, server.URL)
    48  		taskConfig := modelData.TaskConfig
    49  		logger := agentutil.NewTestLogger(slogger.StdOutAppender())
    50  
    51  		Convey("all commands in test project should execute successfully", func() {
    52  			curWD, err := os.Getwd()
    53  			testutil.HandleTestingErr(err, t, "Couldn't get working directory: %v")
    54  			taskConfig.WorkDir = curWD
    55  
    56  			for _, testTask := range taskConfig.Project.Tasks {
    57  				So(len(testTask.Commands), ShouldNotEqual, 0)
    58  				for _, command := range testTask.Commands {
    59  					pluginCmds, err := registry.GetCommands(command, taskConfig.Project.Functions)
    60  					testutil.HandleTestingErr(err, t, "Couldn't get plugin command: %v")
    61  					So(pluginCmds, ShouldNotBeNil)
    62  					So(err, ShouldBeNil)
    63  					pluginCom := &comm.TaskJSONCommunicator{pluginCmds[0].Plugin(), httpCom}
    64  					err = pluginCmds[0].Execute(logger, pluginCom, taskConfig, make(chan bool))
    65  					So(err, ShouldNotBeNil)
    66  					So(err.Error(), ShouldEqual, "test failures")
    67  				}
    68  			}
    69  
    70  			Convey("and the tests in the task should be updated", func() {
    71  				updatedTask, err := task.FindOne(task.ById(modelData.Task.Id))
    72  				So(err, ShouldBeNil)
    73  				So(updatedTask, ShouldNotBeNil)
    74  				So(len(updatedTask.TestResults), ShouldEqual, 5)
    75  				So(updatedTask.TestResults[0].Status, ShouldEqual, "fail")
    76  				So(updatedTask.TestResults[1].Status, ShouldEqual, "fail")
    77  				So(updatedTask.TestResults[2].Status, ShouldEqual, "skip")
    78  				So(updatedTask.TestResults[3].Status, ShouldEqual, "pass")
    79  				So(updatedTask.TestResults[4].Status, ShouldEqual, "fail")
    80  
    81  				Convey("with relevant logs present in the DB as well", func() {
    82  					log, err := model.FindOneTestLog("0_badpkg", "testTaskId", 0)
    83  					So(log, ShouldNotBeNil)
    84  					So(err, ShouldBeNil)
    85  					So(log.Lines[0], ShouldContainSubstring, "TestFail01")
    86  				})
    87  			})
    88  
    89  		})
    90  	})
    91  }
    92  
    93  func TestGotestPluginOnPassingTests(t *testing.T) {
    94  	currentDirectory := testutil.GetDirectoryOfFile()
    95  	SkipConvey("With gotest plugin installed into plugin registry", t, func() {
    96  		reset(t)
    97  		testConfig := testutil.TestConfig()
    98  		testutil.ConfigureIntegrationTest(t, testConfig, "TestGotestPluginOnPassingTests")
    99  		registry := plugin.NewSimpleRegistry()
   100  		testPlugin := &GotestPlugin{}
   101  		err := registry.Register(testPlugin)
   102  		testutil.HandleTestingErr(err, t, "Couldn't register plugin %v")
   103  
   104  		server, err := service.CreateTestServer(testutil.TestConfig(), nil, plugin.APIPlugins)
   105  		testutil.HandleTestingErr(err, t, "Couldn't set up testing server")
   106  		defer server.Close()
   107  
   108  		configPath := filepath.Join(currentDirectory, "testdata", "bad.yml")
   109  
   110  		modelData, err := modelutil.SetupAPITestData(testConfig, "test", "rhel55", configPath, modelutil.NoPatch)
   111  		testutil.HandleTestingErr(err, t, "failed to setup test data")
   112  		httpCom := plugintest.TestAgentCommunicator(modelData, server.URL)
   113  		taskConfig := modelData.TaskConfig
   114  		logger := agentutil.NewTestLogger(slogger.StdOutAppender())
   115  
   116  		Convey("all commands in test project should execute successfully", func() {
   117  			curWD, err := os.Getwd()
   118  			testutil.HandleTestingErr(err, t, "Couldn't get working directory: %v")
   119  			taskConfig.WorkDir = curWD
   120  
   121  			for _, testTask := range taskConfig.Project.Tasks {
   122  				So(len(testTask.Commands), ShouldNotEqual, 0)
   123  				for _, command := range testTask.Commands {
   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  					pluginCom := &comm.TaskJSONCommunicator{pluginCmds[0].Plugin(), httpCom}
   129  					err = pluginCmds[0].Execute(logger, pluginCom, taskConfig, make(chan bool))
   130  
   131  					So(err, ShouldBeNil)
   132  				}
   133  			}
   134  
   135  			Convey("and the tests in the task should be updated", func() {
   136  				updatedTask, err := task.FindOne(task.ById(modelData.Task.Id))
   137  				So(err, ShouldBeNil)
   138  				So(updatedTask, ShouldNotBeNil)
   139  				So(len(updatedTask.TestResults), ShouldEqual, 2)
   140  				So(updatedTask.TestResults[0].Status, ShouldEqual, "pass")
   141  				So(updatedTask.TestResults[1].Status, ShouldEqual, "pass")
   142  				So(updatedTask.TestResults[0].TestFile, ShouldEqual, "TestPass01")
   143  				So(updatedTask.TestResults[1].TestFile, ShouldEqual, "TestPass02")
   144  				So(updatedTask.TestResults[0].StartTime, ShouldBeLessThan,
   145  					updatedTask.TestResults[0].EndTime)
   146  				So(updatedTask.TestResults[1].StartTime, ShouldBeLessThan,
   147  					updatedTask.TestResults[1].EndTime)
   148  
   149  				Convey("with relevant logs present in the DB as well", func() {
   150  					log, err := model.FindOneTestLog("0_goodpkg", "testTaskId", 0)
   151  					So(log, ShouldNotBeNil)
   152  					So(err, ShouldBeNil)
   153  					So(log.Lines[0], ShouldContainSubstring, "TestPass01")
   154  				})
   155  
   156  			})
   157  		})
   158  	})
   159  }