11local helpers = require " spec.helpers"
2+ local utils = require " kong.tools.utils"
23local declarative = require " kong.db.declarative"
34
45-- Note: include "off" strategy here as well
56for _ , strategy in helpers .all_strategies () do
67 describe (" db.dao #" .. strategy , function ()
78 local bp , db
8- local consumer , service , plugin , acl
9+ local consumer , service , service2 , plugin , plugin2 , acl
910 local group = " The A Team"
1011
1112 lazy_setup (function ()
@@ -26,7 +27,12 @@ for _, strategy in helpers.all_strategies() do
2627 name = " abc" ,
2728 url = " http://localhost" ,
2829 }
29-
30+
31+ service2 = bp .services :insert {
32+ name = " def" ,
33+ url = " http://2-localhost" ,
34+ }
35+
3036 plugin = bp .plugins :insert {
3137 enabled = true ,
3238 name = " acl" ,
@@ -35,6 +41,20 @@ for _, strategy in helpers.all_strategies() do
3541 allow = { " *" },
3642 },
3743 }
44+
45+ plugin2 = bp .plugins :insert {
46+ enabled = true ,
47+ name = " rate-limiting" ,
48+ instance_name = ' rate-limiting-instance-1' ,
49+ service = service ,
50+ config = {
51+ minute = 100 ,
52+ policy = " redis" ,
53+ redis = {
54+ host = " localhost"
55+ }
56+ },
57+ }
3858 -- Note: bp in off strategy returns service=id instead of a table
3959 plugin .service = {
4060 id = service .id
@@ -81,7 +101,7 @@ for _, strategy in helpers.all_strategies() do
81101
82102 it (" select_by_cache_key()" , function ()
83103 local cache_key = kong .db .acls :cache_key (consumer .id , group )
84-
104+
85105 local read_acl , err = kong .db .acls :select_by_cache_key (cache_key )
86106 assert .is_nil (err )
87107 assert .same (acl , read_acl )
@@ -91,6 +111,78 @@ for _, strategy in helpers.all_strategies() do
91111 local read_plugin , err = kong .db .plugins :select_by_cache_key (cache_key )
92112 assert .is_nil (err )
93113 assert .same (plugin , read_plugin )
114+
115+ cache_key = kong .db .plugins :cache_key (" rate-limiting" , nil , service .id , nil )
116+ read_plugin , err = kong .db .plugins :select_by_cache_key (cache_key )
117+ assert .is_nil (err )
118+ assert .same (plugin2 , read_plugin )
119+ end )
120+
121+ it (" page_for_route" , function ()
122+ local plugins_for_service , err = kong .db .plugins :page_for_service (service )
123+ assert .is_nil (err )
124+ assert .equal (2 , # plugins_for_service )
125+ for _ , read_plugin in ipairs (plugins_for_service ) do
126+ if read_plugin .name == ' acl' then
127+ assert .same (plugin , read_plugin )
128+ elseif read_plugin .name == ' rate-limiting' then
129+ assert .same (plugin2 , read_plugin )
130+ end
131+ end
132+ end )
133+
134+ it (" select_by_instance_name" , function ()
135+ local read_plugin , err = kong .db .plugins :select_by_instance_name (plugin2 .instance_name )
136+ assert .is_nil (err )
137+ assert .same (plugin2 , read_plugin )
138+ end )
139+
140+ it (" update_by_instance_name" , function ()
141+ local newhost = " newhost"
142+ local updated_plugin = utils .cycle_aware_deep_copy (plugin2 )
143+ updated_plugin .config .redis .host = newhost
144+ updated_plugin .config .redis_host = newhost
145+
146+ local read_plugin , err = kong .db .plugins :update_by_instance_name (plugin2 .instance_name , updated_plugin )
147+ assert .is_nil (err )
148+ assert .same (updated_plugin , read_plugin )
149+ end )
150+
151+ it (" upsert_by_instance_name" , function ()
152+ -- existing plugin upsert (update part of upsert)
153+ local newhost = " newhost"
154+ local updated_plugin = utils .cycle_aware_deep_copy (plugin2 )
155+ updated_plugin .config .redis .host = newhost
156+ updated_plugin .config .redis_host = newhost
157+
158+ local read_plugin , err = kong .db .plugins :upsert_by_instance_name (plugin2 .instance_name , updated_plugin )
159+ assert .is_nil (err )
160+ assert .same (updated_plugin , read_plugin )
161+
162+ -- new plugin upsert (insert part of upsert)
163+ local new_plugin_config = {
164+ id = utils .uuid (),
165+ enabled = true ,
166+ name = " rate-limiting" ,
167+ instance_name = ' rate-limiting-instance-2' ,
168+ service = service2 ,
169+ config = {
170+ minute = 200 ,
171+ policy = " redis" ,
172+ redis = {
173+ host = " new-host-2"
174+ }
175+ },
176+ }
177+
178+ local read_plugin , err = kong .db .plugins :upsert_by_instance_name (new_plugin_config .instance_name , new_plugin_config )
179+ assert .is_nil (err )
180+ assert .same (new_plugin_config .id , read_plugin .id )
181+ assert .same (new_plugin_config .instance_name , read_plugin .instance_name )
182+ assert .same (new_plugin_config .service .id , read_plugin .service .id )
183+ assert .same (new_plugin_config .config .minute , read_plugin .config .minute )
184+ assert .same (new_plugin_config .config .redis .host , read_plugin .config .redis .host )
185+ assert .same (new_plugin_config .config .redis .host , read_plugin .config .redis_host ) -- legacy field is included
94186 end )
95187 end )
96188end
0 commit comments