github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/repotracker/github_poller_test.go (about) 1 package repotracker 2 3 import ( 4 "testing" 5 6 "github.com/evergreen-ci/evergreen/db" 7 "github.com/evergreen-ci/evergreen/model" 8 "github.com/evergreen-ci/evergreen/testutil" 9 "github.com/evergreen-ci/evergreen/thirdparty" 10 . "github.com/smartystreets/goconvey/convey" 11 ) 12 13 var ( 14 testConfig = testutil.TestConfig() 15 firstRevision = "99162ee5bc41eb314f5bb01bd12f0c43e9cb5f32" 16 lastRevision = "d0d878e81b303fd2abbf09331e54af41d6cd0c7d" 17 distantEvgRevision = "46d69e662b54a8e03267d165f2a1bc8980865d67" 18 firstRemoteConfigRef = "6dbe53d948906ed3e0a355eb25b9d54e5b011209" 19 secondRemoteConfigRef = "9b6c7d7f479da84b767995076b13c31796a5e2bf" 20 badRemoteConfigRef = "276382eb9f5ebcfce2791d1c99ce5e591023146b" 21 22 projectRef *model.ProjectRef 23 evgProjectRef *model.ProjectRef 24 ) 25 26 func resetProjectRefs() { 27 projectRef = &model.ProjectRef{ 28 Identifier: "mci-test", 29 DisplayName: "MCI Test", 30 Owner: "deafgoat", 31 Repo: "mci-test", 32 Branch: "master", 33 RepoKind: "github", 34 RemotePath: "mci", 35 Enabled: true, 36 Private: false, 37 BatchTime: 60, 38 Tracked: true, 39 } 40 evgProjectRef = &model.ProjectRef{ 41 Repo: "evergreen", 42 Owner: "evergreen-ci", 43 Branch: "master", 44 RepoKind: "github", 45 } 46 } 47 48 func init() { 49 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig)) 50 resetProjectRefs() 51 } 52 53 func dropTestDB(t *testing.T) { 54 session, _, err := db.GetGlobalSessionFactory().GetSession() 55 testutil.HandleTestingErr(err, t, "Error opening database session") 56 defer session.Close() 57 testutil.HandleTestingErr(session.DB(testConfig.Database.DB).DropDatabase(), t, 58 "Error dropping test database") 59 } 60 61 func TestGetRevisionsSinceWithPaging(t *testing.T) { 62 dropTestDB(t) 63 testutil.ConfigureIntegrationTest(t, testConfig, "TestGetRevisionsSince") 64 grp := &GithubRepositoryPoller{ 65 ProjectRef: evgProjectRef, 66 OauthToken: testConfig.Credentials[evgProjectRef.RepoKind], 67 } 68 Convey("When fetching commits from the evergreen repository", t, func() { 69 Convey("fetching > the size of a github page should succeed", func() { 70 revisions, err := grp.GetRevisionsSince(distantEvgRevision, 5000) 71 So(err, ShouldBeNil) 72 Convey("and the revision should be found", func() { 73 So(len(revisions), ShouldNotEqual, 0) 74 }) 75 }) 76 }) 77 } 78 79 func TestGetRevisionsSince(t *testing.T) { 80 dropTestDB(t) 81 var self GithubRepositoryPoller 82 83 testutil.ConfigureIntegrationTest(t, testConfig, "TestGetRevisionsSince") 84 85 Convey("When fetching github revisions (by commit) - from a repo "+ 86 "containing 3 commits - given a valid Oauth token...", t, func() { 87 self.ProjectRef = projectRef 88 self.OauthToken = testConfig.Credentials[self.ProjectRef.RepoKind] 89 90 Convey("There should be only two revisions since the first revision", 91 func() { 92 // The test repository contains only 3 revisions with revision 93 // 99162ee5bc41eb314f5bb01bd12f0c43e9cb5f32 being the first 94 // revision 95 revisions, err := self.GetRevisionsSince(firstRevision, 10) 96 testutil.HandleTestingErr(err, t, "Error fetching github revisions") 97 So(len(revisions), ShouldEqual, 2) 98 }) 99 100 Convey("There should be no revisions since the last revision", func() { 101 // The test repository contains only 3 revisions with revision 102 // d0d878e81b303fd2abbf09331e54af41d6cd0c7d being the last revision 103 revisions, err := self.GetRevisionsSince(lastRevision, 10) 104 testutil.HandleTestingErr(err, t, "Error fetching github revisions") 105 So(len(revisions), ShouldEqual, 0) 106 }) 107 108 Convey("There should be an error returned if the requested revision "+ 109 "isn't found", func() { 110 // The test repository contains only 3 revisions with revision 111 // d0d878e81b303fd2abbf09331e54af41d6cd0c7d being the last revision 112 revisions, err := self.GetRevisionsSince("lastRevision", 10) 113 So(len(revisions), ShouldEqual, 0) 114 So(err, ShouldNotBeNil) 115 }) 116 Convey("If the revision is not valid because it has less than 10 characters, should return an error", func() { 117 _, err := self.GetRevisionsSince("master", 10) 118 So(err, ShouldNotBeNil) 119 }) 120 }) 121 } 122 123 func TestGetRemoteConfig(t *testing.T) { 124 dropTestDB(t) 125 var self GithubRepositoryPoller 126 127 testutil.ConfigureIntegrationTest(t, testConfig, "TestGetRemoteConfig") 128 129 Convey("When fetching a specific github revision configuration...", 130 t, func() { 131 132 self.ProjectRef = &model.ProjectRef{ 133 Identifier: "mci-test", 134 DisplayName: "MCI Test", 135 Owner: "deafgoat", 136 Repo: "config", 137 Branch: "master", 138 RepoKind: "github", 139 RemotePath: "random.txt", 140 Enabled: true, 141 Private: false, 142 BatchTime: 60, 143 Tracked: true, 144 } 145 self.OauthToken = testConfig.Credentials[self.ProjectRef.RepoKind] 146 147 Convey("The config file at the requested revision should be "+ 148 "exactly what is returned", func() { 149 projectConfig, err := self.GetRemoteConfig(firstRemoteConfigRef) 150 testutil.HandleTestingErr(err, t, "Error fetching github "+ 151 "configuration file") 152 So(projectConfig, ShouldNotBeNil) 153 So(len(projectConfig.Tasks), ShouldEqual, 0) 154 projectConfig, err = self.GetRemoteConfig(secondRemoteConfigRef) 155 testutil.HandleTestingErr(err, t, "Error fetching github "+ 156 "configuration file") 157 So(projectConfig, ShouldNotBeNil) 158 So(len(projectConfig.Tasks), ShouldEqual, 1) 159 }) 160 Convey("an invalid revision should return an error", func() { 161 _, err := self.GetRemoteConfig("firstRemoteConfRef") 162 So(err, ShouldNotBeNil) 163 }) 164 Convey("an invalid project configuration should error out", func() { 165 _, err := self.GetRemoteConfig(badRemoteConfigRef) 166 So(err, ShouldNotBeNil) 167 }) 168 }) 169 } 170 171 func TestGetAllRevisions(t *testing.T) { 172 dropTestDB(t) 173 var self GithubRepositoryPoller 174 175 testutil.ConfigureIntegrationTest(t, testConfig, "TestGetAllRevisions") 176 177 Convey("When fetching recent github revisions (by count) - from a repo "+ 178 "containing 3 commits - given a valid Oauth token...", t, func() { 179 self.ProjectRef = projectRef 180 self.OauthToken = testConfig.Credentials[self.ProjectRef.RepoKind] 181 182 // Even though we're requesting far more revisions than exists in the 183 // remote repository, we should only get the revisions that actually 184 // exist upstream - a total of 3 185 Convey("There should be only three revisions even if you request more "+ 186 "than 3", func() { 187 revisions, err := self.GetRecentRevisions(123) 188 testutil.HandleTestingErr(err, t, "Error fetching github revisions") 189 So(len(revisions), ShouldEqual, 3) 190 }) 191 192 // Get only one recent revision and ensure it's the right revision 193 Convey("There should be only be one if you request 1 and it should be "+ 194 "the latest", func() { 195 revisions, err := self.GetRecentRevisions(1) 196 testutil.HandleTestingErr(err, t, "Error fetching github revisions") 197 So(len(revisions), ShouldEqual, 1) 198 So(revisions[0].Revision, ShouldEqual, lastRevision) 199 }) 200 201 // Get no recent revisions 202 Convey("There should be no revisions if you request 0", func() { 203 revisions, err := self.GetRecentRevisions(0) 204 testutil.HandleTestingErr(err, t, "Error fetching github revisions") 205 So(len(revisions), ShouldEqual, 0) 206 }) 207 }) 208 } 209 210 func TestGetChangedFiles(t *testing.T) { 211 dropTestDB(t) 212 var grp GithubRepositoryPoller 213 214 testutil.ConfigureIntegrationTest(t, testConfig, "TestGetAllRevisions") 215 216 Convey("When fetching changed files from evergreen-ci/evergreen ", t, func() { 217 grp.ProjectRef = evgProjectRef 218 grp.OauthToken = testConfig.Credentials[grp.ProjectRef.RepoKind] 219 220 r1 := "b11fcb25624c6a0649dd35b895f5b550d649a128" 221 Convey("the revision "+r1+" should have 8 files", func() { 222 files, err := grp.GetChangedFiles(r1) 223 So(err, ShouldBeNil) 224 So(len(files), ShouldEqual, 8) 225 So(files, ShouldContain, "cloud/providers/ec2/ec2.go") 226 So(files, ShouldContain, "public/static/dist/css/styles.css") 227 // ...probably don't need to check all 8 228 }) 229 230 r2 := "3c0133dbd4b35418c11df7b6e3a1ae31f966de42" 231 Convey("the revision "+r2+" should have 1 file", func() { 232 files, err := grp.GetChangedFiles(r2) 233 So(err, ShouldBeNil) 234 So(len(files), ShouldEqual, 1) 235 So(files, ShouldContain, "ui/rest_project.go") 236 }) 237 238 Convey("a revision that does not exist should fail", func() { 239 files, err := grp.GetChangedFiles("00000000000000000000000000") 240 So(err, ShouldNotBeNil) 241 So(err.Error(), ShouldContainSubstring, "Not Found") 242 So(files, ShouldBeNil) 243 }) 244 }) 245 } 246 247 func TestIsLastRevision(t *testing.T) { 248 Convey("When calling isLastRevision...", t, func() { 249 Convey("it should return false if the commit SHA does not match "+ 250 "the revision string passed in", func() { 251 githubCommit := &thirdparty.GithubCommit{} 252 So(isLastRevision(firstRevision, githubCommit), ShouldBeFalse) 253 }) 254 Convey("it should return true if the commit SHA matches "+ 255 "the revision string passed in", func() { 256 githubCommit := &thirdparty.GithubCommit{} 257 githubCommit.SHA = firstRevision 258 So(isLastRevision(firstRevision, githubCommit), ShouldBeTrue) 259 }) 260 }) 261 } 262 263 func TestGetCommitURL(t *testing.T) { 264 Convey("When calling getCommitURL...", t, func() { 265 Convey("the returned string should use the fields of the project "+ 266 "ref correctly", func() { 267 projectRef = &model.ProjectRef{ 268 Owner: "a", 269 Repo: "b", 270 Branch: "c", 271 } 272 expectedURL := "https://api.github.com/repos/a/b/commits?sha=c" 273 So(getCommitURL(projectRef), ShouldEqual, expectedURL) 274 }) 275 }) 276 }