forked from Kong/kong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade_helpers.lua
155 lines (136 loc) · 4.18 KB
/
upgrade_helpers.lua
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
local say = require "say"
local assert = require "luassert"
local busted = require "busted"
local conf_loader = require "kong.conf_loader"
local DB = require "kong.db"
local helpers = require "spec.helpers"
local conf = conf_loader()
local function database_type()
return conf['database']
end
local function get_database()
local db = assert(DB.new(conf))
assert(db:init_connector())
return db
end
local function table_has_column(state, arguments)
local table = arguments[1]
local column_name = arguments[2]
local postgres_type = arguments[3]
local cassandra_type = arguments[4] or postgres_type
local db = get_database()
local res, err
if database_type() == 'cassandra' then
res, err = db.connector:query(string.format(
"select *"
.. " from system_schema.columns"
.. " where table_name = '%s'"
.. " and column_name = '%s'"
.. " and type = '%s'"
.. " allow filtering",
table, column_name, cassandra_type))
elseif database_type() == 'postgres' then
res, err = db.connector:query(string.format(
"select true"
.. " from information_schema.columns"
.. " where table_schema = 'public'"
.. " and table_name = '%s'"
.. " and column_name = '%s'"
.. " and data_type = '%s'",
table, column_name, postgres_type))
else
return false
end
if err then
return false
end
return not(not(res[1]))
end
say:set("assertion.table_has_column.positive", "Expected table %s to have column %s with type %s")
say:set("assertion.table_has_column.negative", "Expected table %s not to have column %s with type %s")
assert:register("assertion", "table_has_column", table_has_column, "assertion.table_has_column.positive", "assertion.table_has_column.negative")
local upstream_server_url = "http://" .. helpers.mock_upstream_host .. ":" .. helpers.mock_upstream_port .. "/"
local function create_example_service()
local admin_client = assert(helpers.admin_client())
local res = assert(admin_client:send {
method = "POST",
path = "/services/",
body = {
name = "example-service",
url = upstream_server_url
},
headers = {
["Content-Type"] = "application/json"
}
})
assert.res_status(201, res)
res = assert(admin_client:send {
method = "POST",
path = "/services/example-service/routes",
body = {
hosts = { "example.com" },
},
headers = {
["Content-Type"] = "application/json"
}
})
assert.res_status(201, res)
admin_client:close()
end
local function send_proxy_get_request()
local proxy_client = assert(helpers.proxy_client())
local res = assert(proxy_client:send {
method = "GET",
headers = {
["Host"] = "example.com",
},
path = "/",
})
local body = assert.res_status(200, res)
proxy_client:close()
return res, body
end
local function start_kong()
return helpers.start_kong {
database = database_type(),
dns_resolver = "",
proxy_listen = "0.0.0.0:9000",
admin_listen = "0.0.0.0:9001",
admin_ssl = false,
admin_gui_ssl = false,
nginx_conf = "spec/fixtures/custom_nginx.template",
}
end
local function it_when(phase, phrase, f)
return busted.it(phrase .. " #" .. phase, f)
end
local function setup(f)
return busted.it("setting up kong #setup", f)
end
local function old_after_up(phrase, f)
return it_when("old_after_up", phrase, f)
end
local function new_after_up(phrase, f)
return it_when("new_after_up", phrase, f)
end
local function new_after_finish(phrase, f)
return it_when("new_after_finish", phrase, f)
end
local function all_phases(phrase, f)
return it_when("all_phases", phrase, f)
end
return {
database_type = database_type,
get_database = get_database,
create_example_service = create_example_service,
send_proxy_get_request = send_proxy_get_request,
start_kong = start_kong,
stop_kong = helpers.stop_kong,
admin_client = helpers.admin_client,
proxy_client = helpers.proxy_client,
setup = setup,
old_after_up = old_after_up,
new_after_up = new_after_up,
new_after_finish = new_after_finish,
all_phases = all_phases
}