forked from LunaNode/lobster
/
testing.go
60 lines (50 loc) · 1.46 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package lobster
import "github.com/gorilla/mux"
import "github.com/LunaNode/lobster/utils"
const TEST_BANDWIDTH = 1000
var testTables []string = []string{"users", "region_bandwidth", "vms", "plans", "charges", "sessions", "form_tokens", "antiflood"}
func TestReset() *Database {
cfg = &Config{
Default: ConfigDefault{
Debug: true,
},
Billing: ConfigBilling{
BandwidthOverageFee: 0.003,
},
Database: ConfigDatabase{
Host: "localhost",
Username: "lobstertest",
Password: "",
Name: "lobstertest",
},
Novnc: ConfigNovnc{
Listen: "127.0.0.1:6080",
Url: "TOKEN",
},
}
db := MakeDatabase()
// clear all tables
for _, table := range testTables {
db.Exec("DELETE FROM " + table)
}
return db
}
func TestLobster() *Lobster {
app := new(Lobster)
app.router = mux.NewRouter()
app.db = MakeDatabase()
return app
}
// Creates user and returns user id.
func TestUser(db *Database) int {
result := db.Exec("INSERT INTO users (username, password, credit) VALUES (?, '', 1000000)", utils.Uid(8))
userId, _ := result.LastInsertId()
return int(userId)
}
func TestVm(db *Database, userId int) int {
result := db.Exec("INSERT INTO plans (name, price, ram, cpu, storage, bandwidth) VALUES ('', 6000, 512, 1, 15, ?)", TEST_BANDWIDTH)
planId, _ := result.LastInsertId()
result = db.Exec("INSERT INTO vms (user_id, region, plan_id, name, status) VALUES (?, 'test', ?, '', 'active')", userId, planId)
vmId, _ := result.LastInsertId()
return int(vmId)
}