github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/testing/testing.go (about) 1 package database 2 3 import ( 4 "crypto/aes" 5 "database/sql" 6 "fmt" 7 "log" 8 "os" 9 "strings" 10 11 "github.com/drone/drone/pkg/database" 12 "github.com/drone/drone/pkg/database/encrypt" 13 . "github.com/drone/drone/pkg/model" 14 15 _ "github.com/mattn/go-sqlite3" 16 "github.com/russross/meddler" 17 ) 18 19 const ( 20 pubkey = `sh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQClp9+xjhYj2Wz0nwLNhUiR1RkqfoVZwlJoxdubQy8GskZtY7C7YGa/PeKfdfaKOWtVgg37r/OYS3kc7bIKVup4sx/oW59FMwCZYQ2nxoaPZpPwUJs8D0Wy0b2VSP+vAnJ6jZQEIEiClrzyYafSfqN6L9T/BTkn28ktWalOHqWVKejKeD6M0uhlpyIZFsQ1K2wNt32ACwT/rbanx/r/jfczqxSkLzvIKXXs/RdKQgwRRUYnKkl4Lh6r22n9n3m2VwRor5wdsPK8sr57OsqdRpnvsFs3lxwM8w5ZiAZV3T0xTMGVs3W8Uy5HexAD6TgWBWFjSrgdXF1pE83wmUtJtVBf` 21 privkey = `-----BEGIN RSA PRIVATE KEY----- 22 MIIEogIBAAKCAQEApaffsY4WI9ls9J8CzYVIkdUZKn6FWcJSaMXbm0MvBrJGbWOw 23 u2Bmvz3in3X2ijlrVYIN+6/zmEt5HO2yClbqeLMf6FufRTMAmWENp8aGj2aT8FCb 24 PA9FstG9lUj/rwJyeo2UBCBIgpa88mGn0n6jei/U/wU5J9vJLVmpTh6llSnoyng+ 25 jNLoZaciGRbENStsDbd9gAsE/622p8f6/433M6sUpC87yCl17P0XSkIMEUVGJypJ 26 eC4eq9tp/Z95tlcEaK+cHbDyvLK+ezrKnUaZ77BbN5ccDPMOWYgGVd09MUzBlbN1 27 vFMuR3sQA+k4FgVhY0q4HVxdaRPN8JlLSbVQXwIDAQABAoIBAA3EqSPxwkdSf+rI 28 +IuqY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AoGAFFZdolsVbSieFhJt7ypzp/19dKJ8Sk6QGCk3uQpTuLJMvwcBT8X5XCTD 45 zFfYARarx87mbD2k5GZ7F0fmGYTUV14qlxJCGMythLM/xZ6EJuJWBz69puNj4yhn 46 exWM7t1BDHy2zIoPfIQLDH2h1zNTRjismMeErOCy0Uha7jrZhW8= 47 -----END RSA PRIVATE KEY-----` 48 ) 49 50 var ( 51 dbname, driver, dsn, login string 52 db *sql.DB 53 ) 54 55 func init() { 56 // create a cipher for ecnrypting and decrypting 57 // database fields 58 cipher, err := aes.NewCipher([]byte("38B241096B8DA08131563770F4CDDFAC")) 59 if err != nil { 60 log.Fatal(err) 61 } 62 63 // register function with meddler to encrypt and 64 // decrypt database fields. 65 meddler.Register("gobencrypt", &encrypt.EncryptedField{cipher}) 66 67 // Check for $DB_ENV 68 dbenv := os.Getenv("DB_ENV") 69 if dbenv == "mysql" { 70 driver = dbenv 71 dbname = "drone_test" 72 login = os.Getenv("MYSQL_LOGIN") 73 if len(login) == 0 { 74 login = "root" 75 } 76 log.Println("Using mysql database ...") 77 } else { 78 driver = "sqlite3" 79 dsn = ":memory:" 80 log.Println("Using sqlite3 database ...") 81 } 82 83 } 84 85 func Setup() { 86 // create an in-memory database 87 if driver == "mysql" { 88 idsn := fmt.Sprintf("%s@/?parseTime=true", login) 89 db, dsn = createDB(dbname, idsn) 90 } 91 database.Init(driver, dsn) 92 93 // create dummy user data 94 user1 := User{ 95 Password: "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS", 96 Name: "Brad Rydzewski", 97 Email: "brad.rydzewski@gmail.com", 98 Gravatar: "8c58a0be77ee441bb8f8595b7f1b4e87", 99 Token: "123", 100 GitlabToken: "123", 101 Admin: true} 102 user2 := User{ 103 Password: "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS", 104 Name: "Thomas Burke", 105 Email: "cavepig@gmail.com", 106 Gravatar: "c62f7126273f7fa786274274a5dec8ce", 107 Token: "456", 108 GitlabToken: "456", 109 Admin: false} 110 user3 := User{ 111 Password: "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS", 112 Name: "Carlos Morales", 113 Email: "ytsejammer@gmail.com", 114 Gravatar: "c2180a539620d90d68eaeb848364f1c2", 115 Token: "789", 116 GitlabToken: "789", 117 Admin: false} 118 119 database.SaveUser(&user1) 120 database.SaveUser(&user2) 121 database.SaveUser(&user3) 122 123 // create dummy team data 124 team1 := Team{ 125 Slug: "drone", 126 Name: "Drone", 127 Email: "support@drone.io", 128 Gravatar: "8c58a0be77ee441bb8f8595b7f1b4e87"} 129 team2 := Team{ 130 Slug: "github", 131 Name: "Github", 132 Email: "support@github.com", 133 Gravatar: "61024896f291303615bcd4f7a0dcfb74"} 134 team3 := Team{ 135 Slug: "golang", 136 Name: "Golang", 137 Email: "support@golang.org", 138 Gravatar: "991695cc770c6b8354b68cd18c280b95"} 139 140 database.SaveTeam(&team1) 141 database.SaveTeam(&team2) 142 database.SaveTeam(&team3) 143 144 // create team membership data 145 database.SaveMember(user1.ID, team1.ID, RoleOwner) 146 database.SaveMember(user2.ID, team1.ID, RoleAdmin) 147 database.SaveMember(user3.ID, team1.ID, RoleWrite) 148 database.SaveMember(user1.ID, team2.ID, RoleOwner) 149 database.SaveMember(user2.ID, team2.ID, RoleAdmin) 150 database.SaveMember(user3.ID, team2.ID, RoleWrite) 151 database.SaveMember(user1.ID, team3.ID, RoleRead) 152 153 // create dummy repo data 154 repo1 := Repo{ 155 Slug: "github.com/drone/drone", 156 Host: "github.com", 157 Owner: "drone", 158 Name: "drone", 159 Private: true, 160 Disabled: false, 161 SCM: "git", 162 URL: "git@github.com:drone/drone.git", 163 Username: "no username", 164 Password: "no password", 165 PublicKey: pubkey, 166 PrivateKey: privkey, 167 UserID: user1.ID, 168 TeamID: team1.ID, 169 } 170 repo2 := Repo{ 171 Slug: "bitbucket.org/drone/test", 172 Host: "bitbucket.org", 173 Owner: "drone", 174 Name: "test", 175 Private: false, 176 Disabled: false, 177 SCM: "hg", 178 URL: "https://bitbucket.org/drone/test", 179 Username: "no username", 180 Password: "no password", 181 PublicKey: pubkey, 182 PrivateKey: privkey, 183 UserID: user1.ID, 184 TeamID: team1.ID, 185 } 186 repo3 := Repo{ 187 Slug: "bitbucket.org/brydzewski/test", 188 Host: "bitbucket.org", 189 Owner: "brydzewski", 190 Name: "test", 191 Private: false, 192 Disabled: false, 193 SCM: "hg", 194 URL: "https://bitbucket.org/brydzewski/test", 195 Username: "no username", 196 Password: "no password", 197 PublicKey: pubkey, 198 PrivateKey: privkey, 199 UserID: user2.ID, 200 } 201 202 database.SaveRepo(&repo1) 203 database.SaveRepo(&repo2) 204 database.SaveRepo(&repo3) 205 206 commit1 := Commit{ 207 RepoID: repo1.ID, 208 Status: "Success", 209 Hash: "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", 210 Branch: "master", 211 Author: user1.Email, 212 Gravatar: user1.Gravatar, 213 Message: "commit message", 214 } 215 commit2 := Commit{ 216 RepoID: repo1.ID, 217 Status: "Failure", 218 Hash: "0eb2fa13e9f4139e803b6ad37831708d4786c74a", 219 Branch: "master", 220 Author: user1.Email, 221 Gravatar: user1.Gravatar, 222 Message: "commit message", 223 } 224 commit3 := Commit{ 225 RepoID: repo1.ID, 226 Status: "Failure", 227 Hash: "60a7fe87ccf01d0152e53242528399e05acaf047", 228 Branch: "dev", 229 Author: user1.Email, 230 Gravatar: user1.Gravatar, 231 Message: "commit message", 232 } 233 commit4 := Commit{ 234 RepoID: repo2.ID, 235 Status: "Success", 236 Hash: "a4078d1e9a0842cdd214adbf0512578799a4f2ba", 237 Branch: "master", 238 Author: user1.Email, 239 Gravatar: user1.Gravatar, 240 Message: "commit message", 241 } 242 commit5 := Commit{ 243 RepoID: repo2.ID, 244 Status: "Success", 245 Hash: "5f32ec7b08dfe3a097c1a5316de5b5069fb35ff9", 246 Branch: "develop", 247 Author: user1.Email, 248 Gravatar: user1.Gravatar, 249 Message: "commit message", 250 } 251 252 // create dummy commit data 253 database.SaveCommit(&commit1) 254 database.SaveCommit(&commit2) 255 database.SaveCommit(&commit3) 256 database.SaveCommit(&commit4) 257 database.SaveCommit(&commit5) 258 259 // create dummy build data 260 database.SaveBuild(&Build{CommitID: commit1.ID, Slug: "node_0.10", Status: "Success", Duration: 60}) 261 database.SaveBuild(&Build{CommitID: commit1.ID, Slug: "node_0.09", Status: "Success", Duration: 70}) 262 database.SaveBuild(&Build{CommitID: commit2.ID, Slug: "node_0.10", Status: "Success", Duration: 10}) 263 database.SaveBuild(&Build{CommitID: commit2.ID, Slug: "node_0.09", Status: "Failure", Duration: 65}) 264 database.SaveBuild(&Build{CommitID: commit3.ID, Slug: "node_0.10", Status: "Failure", Duration: 50}) 265 database.SaveBuild(&Build{CommitID: commit3.ID, Slug: "node_0.09", Status: "Failure", Duration: 55}) 266 } 267 268 func Teardown() { 269 database.Close() 270 if driver == "mysql" { 271 db.Exec(fmt.Sprintf("DROP DATABASE %s", dbname)) 272 } 273 } 274 275 func createDB(name, datasource string) (*sql.DB, string) { 276 db, err := sql.Open(driver, datasource) 277 if err != nil { 278 panic("Can't connect to database") 279 } 280 if _, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", name)); err != nil { 281 panic("Can't create database") 282 } 283 dsn := strings.Replace(datasource, "/", fmt.Sprintf("/%s", name), 1) 284 return db, dsn 285 }