github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/git/patch_test.go (about) 1 package git 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/evergreen-ci/evergreen" 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/patch" 12 "github.com/evergreen-ci/evergreen/model/task" 13 modelutil "github.com/evergreen-ci/evergreen/model/testutil" 14 "github.com/evergreen-ci/evergreen/model/version" 15 "github.com/evergreen-ci/evergreen/plugin" 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 TestPatchPluginAPI(t *testing.T) { 24 testConfig := testutil.TestConfig() 25 cwd := testutil.GetDirectoryOfFile() 26 Convey("With a running api server and installed plugin", t, func() { 27 registry := plugin.NewSimpleRegistry() 28 gitPlugin := &GitPlugin{} 29 err := registry.Register(gitPlugin) 30 testutil.HandleTestingErr(err, t, "Couldn't register patch plugin") 31 server, err := service.CreateTestServer(testConfig, nil, plugin.APIPlugins) 32 testutil.HandleTestingErr(err, t, "Couldn't set up testing server") 33 configPath := filepath.Join(cwd, "testdata", "plugin_patch.yml") 34 patchFile := filepath.Join(cwd, "testdata", "test.patch") 35 36 testCommand := GitGetProjectCommand{Directory: "dir"} 37 modelData, err := modelutil.SetupAPITestData(testConfig, "testTask", "testvar", configPath, modelutil.NoPatch) 38 testutil.HandleTestingErr(err, t, "Couldn't set up test documents") 39 err = plugintest.SetupPatchData(modelData, patchFile, t) 40 testutil.HandleTestingErr(err, t, "Couldn't set up test documents") 41 42 logger := agentutil.NewTestLogger(slogger.StdOutAppender()) 43 44 Convey("calls to existing tasks with patches should succeed", func() { 45 httpCom := plugintest.TestAgentCommunicator(modelData, server.URL) 46 pluginCom := &comm.TaskJSONCommunicator{gitPlugin.Name(), httpCom} 47 patch, err := testCommand.GetPatch(pluginCom, logger) 48 So(err, ShouldBeNil) 49 So(patch, ShouldNotBeNil) 50 testutil.HandleTestingErr(db.Clear(version.Collection), t, 51 "unable to clear versions collection") 52 }) 53 Convey("calls to non-existing tasks should fail", func() { 54 v := version.Version{Id: ""} 55 testutil.HandleTestingErr(v.Insert(), t, "Couldn't insert dummy version") 56 modelData.Task = &task.Task{ 57 Id: "BAD_TASK_ID", 58 } 59 httpCom := plugintest.TestAgentCommunicator(modelData, server.URL) 60 pluginCom := &comm.TaskJSONCommunicator{gitPlugin.Name(), httpCom} 61 patch, err := testCommand.GetPatch(pluginCom, logger) 62 So(err.Error(), ShouldContainSubstring, "not found") 63 So(err, ShouldNotBeNil) 64 So(patch, ShouldBeNil) 65 testutil.HandleTestingErr(db.Clear(version.Collection), t, 66 "unable to clear versions collection") 67 }) 68 Convey("calls to existing tasks without patches should fail", func() { 69 noPatchTask := task.Task{Id: "noPatchTask", BuildId: "a"} 70 testutil.HandleTestingErr(noPatchTask.Insert(), t, "Couldn't insert patch task") 71 noPatchVersion := version.Version{Id: "noPatchVersion", BuildIds: []string{"a"}} 72 testutil.HandleTestingErr(noPatchVersion.Insert(), t, "Couldn't insert patch version") 73 v := version.Version{Id: ""} 74 testutil.HandleTestingErr(v.Insert(), t, "Couldn't insert dummy version") 75 modelData.Task = &noPatchTask 76 httpCom := plugintest.TestAgentCommunicator(modelData, server.URL) 77 pluginCom := &comm.TaskJSONCommunicator{gitPlugin.Name(), httpCom} 78 patch, err := testCommand.GetPatch(pluginCom, logger) 79 So(err, ShouldNotBeNil) 80 So(err.Error(), ShouldContainSubstring, "no patch found for task") 81 So(patch, ShouldBeNil) 82 testutil.HandleTestingErr(db.Clear(version.Collection), t, 83 "unable to clear versions collection") 84 }) 85 86 }) 87 } 88 89 func TestPatchPlugin(t *testing.T) { 90 cwd := testutil.GetDirectoryOfFile() 91 testConfig := testutil.TestConfig() 92 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig)) 93 Convey("With patch plugin installed into plugin registry", t, func() { 94 registry := plugin.NewSimpleRegistry() 95 gitPlugin := &GitPlugin{} 96 err := registry.Register(gitPlugin) 97 testutil.HandleTestingErr(err, t, "Couldn't register plugin %v") 98 testutil.HandleTestingErr(db.Clear(version.Collection), t, 99 "unable to clear versions collection") 100 version := &version.Version{ 101 Id: "", 102 } 103 So(version.Insert(), ShouldBeNil) 104 server, err := service.CreateTestServer(testConfig, nil, plugin.APIPlugins) 105 testutil.HandleTestingErr(err, t, "Couldn't set up testing server") 106 defer server.Close() 107 108 patchFile := filepath.Join(cwd, "testdata", "testmodule.patch") 109 configPath := filepath.Join(testutil.GetDirectoryOfFile(), "testdata", "plugin_patch.yml") 110 modelData, err := modelutil.SetupAPITestData(testConfig, "testTask", "testvar", configPath, modelutil.InlinePatch) 111 testutil.HandleTestingErr(err, t, "Couldn't set up test documents") 112 113 err = plugintest.SetupPatchData(modelData, patchFile, t) 114 testutil.HandleTestingErr(err, t, "Couldn't set up patch documents") 115 116 taskConfig := modelData.TaskConfig 117 httpCom := plugintest.TestAgentCommunicator(modelData, server.URL) 118 119 logger := agentutil.NewTestLogger(slogger.StdOutAppender()) 120 121 Convey("all commands in test project should execute successfully", func() { 122 taskConfig.Task.Requester = evergreen.PatchVersionRequester 123 124 for _, task := range taskConfig.Project.Tasks { 125 So(len(task.Commands), ShouldNotEqual, 0) 126 for _, command := range task.Commands { 127 pluginCmds, err := registry.GetCommands(command, taskConfig.Project.Functions) 128 testutil.HandleTestingErr(err, t, "Couldn't get plugin command: %v") 129 So(pluginCmds, ShouldNotBeNil) 130 So(err, ShouldBeNil) 131 pluginCom := &comm.TaskJSONCommunicator{pluginCmds[0].Plugin(), httpCom} 132 err = pluginCmds[0].Execute(logger, pluginCom, taskConfig, make(chan bool)) 133 So(err, ShouldBeNil) 134 } 135 } 136 }) 137 }) 138 } 139 140 func TestGetPatchCommands(t *testing.T) { 141 Convey("With a patch that has modules", t, func() { 142 testPatch := patch.Patch{ 143 Patches: []patch.ModulePatch{ 144 patch.ModulePatch{ 145 ModuleName: "", 146 PatchSet: patch.PatchSet{ 147 Patch: "", 148 }, 149 }, 150 patch.ModulePatch{ 151 ModuleName: "anotherOne", 152 PatchSet: patch.PatchSet{ 153 Patch: "these are words", 154 }, 155 }, 156 }, 157 } 158 159 Convey("on an empty patch module, a set of commands that does not apply the patch should be returned", func() { 160 commands := GetPatchCommands(testPatch.Patches[0], "", "") 161 So(len(commands), ShouldEqual, 5) 162 }) 163 Convey("on a patch with content, the set of commands should apply the patch", func() { 164 commands := GetPatchCommands(testPatch.Patches[1], "", "") 165 So(len(commands), ShouldEqual, 8) 166 }) 167 }) 168 }