pxfprotocol_test.c 12.5 KB
Newer Older
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include "cmockery.h"

/* Define UNIT_TESTING so that the extension can skip declaring PG_MODULE_MAGIC */
#define UNIT_TESTING

/* include unit under test */
#include "../src/pxfprotocol.c"

/* include mock files */
#include "mock/pxfbridge_mock.c"
#include "mock/pxfuriparser_mock.c"
34
#include "mock/pxffragment_mock.c"
35

H
Heikki Linnakangas 已提交
36 37
const char *uri_no_profile = "pxf://default/tmp/dummy1?FRAGMENTER=xxx&RESOLVER=yyy&ACCESSOR=zzz";
const char *uri_param = "pxf://localhost:51200/tmp/dummy1";
38 39 40 41

void
test_pxfprotocol_validate_urls(void **state)
{
H
Heikki Linnakangas 已提交
42
	/* setup call info with no call context */
43 44
	PG_FUNCTION_ARGS = palloc0(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc0(sizeof(ExtProtocolValidatorData));
H
Heikki Linnakangas 已提交
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
	fcinfo->context->type = T_ExtProtocolValidatorData;
	Value	   *v = makeString(uri_no_profile);
	List	   *list = list_make1(v);

	((ExtProtocolValidatorData *) fcinfo->context)->url_list = list;

	/* set mock behavior for uri parsing */
	GPHDUri    *gphd_uri = palloc0(sizeof(GPHDUri));

	expect_string(parseGPHDUri, uri_str, uri_no_profile);
	will_return(parseGPHDUri, gphd_uri);

	expect_value(GPHDUri_verify_no_duplicate_options, uri, gphd_uri);
	will_be_called(GPHDUri_verify_no_duplicate_options);

	expect_value(GPHDUri_opt_exists, uri, gphd_uri);
	expect_string(GPHDUri_opt_exists, key, PXF_PROFILE);
	will_return(GPHDUri_opt_exists, false);

	expect_value(GPHDUri_verify_core_options_exist, uri, gphd_uri);
	expect_any(GPHDUri_verify_core_options_exist, coreoptions);
	will_be_called(GPHDUri_verify_core_options_exist);

	expect_value(freeGPHDUri, uri, gphd_uri);
	will_be_called(freeGPHDUri);

	Datum		d = pxfprotocol_validate_urls(fcinfo);

	assert_int_equal(DatumGetInt32(d), 0);

	/* cleanup */
	list_free_deep(list);
	pfree(fcinfo->context);
	pfree(fcinfo);
79 80 81 82 83
}

void
test_pxfprotocol_import_first_call(void **state)
{
H
Heikki Linnakangas 已提交
84
	/* setup call info with no call context */
85 86
	PG_FUNCTION_ARGS = palloc0(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc0(sizeof(ExtProtocolData));
H
Heikki Linnakangas 已提交
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
	fcinfo->context->type = T_ExtProtocolData;
	EXTPROTOCOL_GET_DATALEN(fcinfo) = 100;
	EXTPROTOCOL_GET_DATABUF(fcinfo) = palloc0(EXTPROTOCOL_GET_DATALEN(fcinfo));
	((ExtProtocolData *) fcinfo->context)->prot_last_call = false;
	((ExtProtocolData *) fcinfo->context)->prot_url = uri_param;

	Relation	relation = (Relation) palloc0(sizeof(Relation));

	((ExtProtocolData *) fcinfo->context)->prot_relation = relation;

	/* set mock behavior for uri parsing */
	GPHDUri    *gphd_uri = palloc0(sizeof(GPHDUri));

	expect_string(parseGPHDUri, uri_str, uri_param);
	will_return(parseGPHDUri, gphd_uri);

	/* set mock behavior for set fragments */
	gphd_uri->fragments = palloc0(sizeof(List));
	expect_value(get_fragments, uri, gphd_uri);
	expect_value(get_fragments, relation, relation);
	will_assign_memory(get_fragments, uri, gphd_uri, sizeof(GPHDUri));
	will_be_called(get_fragments);

	/* set mock behavior for bridge import start -- nothing here */
	expect_any(gpbridge_import_start, context);
	will_be_called(gpbridge_import_start);

	/* set mock behavior for bridge read */
	const int	EXPECTED_SIZE = 31;

	/* expected number of bytes read from the bridge */
	expect_any(gpbridge_read, context);
	expect_value(gpbridge_read, databuf, EXTPROTOCOL_GET_DATABUF(fcinfo));
	expect_value(gpbridge_read, datalen, EXTPROTOCOL_GET_DATALEN(fcinfo));
	will_return(gpbridge_read, EXPECTED_SIZE);

	Datum		d = pxfprotocol_import(fcinfo);

	/* return number of bytes read from the bridge */
	assert_int_equal(DatumGetInt32(d), EXPECTED_SIZE);
	gphadoop_context *context = (gphadoop_context *) EXTPROTOCOL_GET_USER_CTX(fcinfo);

	/* context has been created */
	assert_true(context != NULL);
	/* gphduri has been parsed */
	assert_true(context->gphd_uri != NULL);
	/* uri has been initialized */
	assert_true(context->uri.data != NULL);
	/* no write file name for import case */
	assert_int_equal(context->write_file_name.len, 0);
	assert_true(context->relation != NULL);
	/* relation pointer is copied */
	assert_int_equal(context->relation, relation);

	/* cleanup */
	pfree(relation);
	pfree(gphd_uri);
	pfree(EXTPROTOCOL_GET_USER_CTX(fcinfo));
	pfree(EXTPROTOCOL_GET_DATABUF(fcinfo));
	pfree(fcinfo->context);
	pfree(fcinfo);
148 149 150 151 152
}

void
test_pxfprotocol_import_second_call(void **state)
{
H
Heikki Linnakangas 已提交
153
	/* setup call info with call context */
154 155
	PG_FUNCTION_ARGS = palloc0(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc0(sizeof(ExtProtocolData));
H
Heikki Linnakangas 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	fcinfo->context->type = T_ExtProtocolData;
	EXTPROTOCOL_GET_DATALEN(fcinfo) = 100;
	EXTPROTOCOL_GET_DATABUF(fcinfo) = palloc0(EXTPROTOCOL_GET_DATALEN(fcinfo));
	((ExtProtocolData *) fcinfo->context)->prot_last_call = false;
	gphadoop_context *call_context = palloc0(sizeof(gphadoop_context));

	EXTPROTOCOL_SET_USER_CTX(fcinfo, call_context);

	/* set mock behavior for bridge read */
	const int	EXPECTED_SIZE = 0;

	/* expected number of bytes read from the bridge */
	expect_value(gpbridge_read, context, call_context);
	expect_value(gpbridge_read, databuf, EXTPROTOCOL_GET_DATABUF(fcinfo));
	expect_value(gpbridge_read, datalen, EXTPROTOCOL_GET_DATALEN(fcinfo));
	will_return(gpbridge_read, EXPECTED_SIZE);

	Datum		d = pxfprotocol_import(fcinfo);

	assert_int_equal(DatumGetInt32(d), EXPECTED_SIZE);
	/* return number of bytes read from the bridge */
	assert_true(EXTPROTOCOL_GET_USER_CTX(fcinfo) == call_context);
	/* context is still the same */

	/* cleanup */
	pfree(call_context);
	pfree(EXTPROTOCOL_GET_DATABUF(fcinfo));
	pfree(fcinfo->context);
	pfree(fcinfo);
185 186 187 188 189
}

void
test_pxfprotocol_import_last_call(void **state)
{
H
Heikki Linnakangas 已提交
190
	/* setup call info with a call context and last call indicator */
191 192
	PG_FUNCTION_ARGS = palloc0(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc0(sizeof(ExtProtocolData));
H
Heikki Linnakangas 已提交
193
	fcinfo->context->type = T_ExtProtocolData;
194
	gphadoop_context *call_context = palloc0(sizeof(gphadoop_context));
H
Heikki Linnakangas 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

	EXTPROTOCOL_SET_USER_CTX(fcinfo, call_context);
	EXTPROTOCOL_SET_LAST_CALL(fcinfo);

	/* init data in context that will be cleaned up */
	initStringInfo(&call_context->uri);
	initStringInfo(&call_context->write_file_name);

	/* set mock behavior for bridge cleanup */
	expect_value(gpbridge_cleanup, context, call_context);
	will_be_called(gpbridge_cleanup);

	Datum		d = pxfprotocol_import(fcinfo);

	assert_int_equal(DatumGetInt32(d), 0);
	/* 0 is returned from function */
	assert_true(EXTPROTOCOL_GET_USER_CTX(fcinfo) == NULL);
	/* call context is cleaned up */

	/* cleanup */
	pfree(fcinfo->context);
	pfree(fcinfo);
217 218
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
void
test_pxfprotocol_export_first_call(void **state)
{
	/* setup call info with no call context */
	PG_FUNCTION_ARGS = palloc0(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc0(sizeof(ExtProtocolData));
	fcinfo->context->type = T_ExtProtocolData;
	EXTPROTOCOL_GET_DATALEN(fcinfo) = 100;
	EXTPROTOCOL_GET_DATABUF(fcinfo) = palloc0(EXTPROTOCOL_GET_DATALEN(fcinfo));
	((ExtProtocolData *) fcinfo->context)->prot_last_call = false;
	((ExtProtocolData *) fcinfo->context)->prot_url = uri_param;

	Relation	relation = (Relation) palloc0(sizeof(Relation));

	((ExtProtocolData *) fcinfo->context)->prot_relation = relation;

	/* set mock behavior for uri parsing */
	GPHDUri		*gphd_uri = palloc0(sizeof(GPHDUri));

	expect_string(parseGPHDUri, uri_str, uri_param);
	will_return(parseGPHDUri, gphd_uri);

	/* set mock behavior for bridge export start -- nothing here */
	expect_any(gpbridge_export_start, context);
	will_be_called(gpbridge_export_start);

	/* set mock behavior for bridge write */
	const int	EXPECTED_SIZE = 31;

	/* expected number of bytes written to the bridge */
	expect_any(gpbridge_write, context);
	expect_value(gpbridge_write, databuf, EXTPROTOCOL_GET_DATABUF(fcinfo));
	expect_value(gpbridge_write, datalen, EXTPROTOCOL_GET_DATALEN(fcinfo));
	will_return(gpbridge_write, EXPECTED_SIZE);

	Datum		d = pxfprotocol_export(fcinfo);

	/* return number of bytes written to the bridge */
	assert_int_equal(DatumGetInt32(d), EXPECTED_SIZE);
	gphadoop_context *context = (gphadoop_context *) EXTPROTOCOL_GET_USER_CTX(fcinfo);

	/* context has been created */
	assert_true(context != NULL);
	/* gphduri has been parsed */
	assert_true(context->gphd_uri != NULL);
	/* uri has been initialized */
	assert_true(context->uri.data != NULL);
	/* write file name initialized, but empty, since it is filled by another component */
	assert_int_equal(context->write_file_name.len, 0);
	assert_true(context->relation != NULL);
	/* relation pointer is copied */
	assert_int_equal(context->relation, relation);

	/* cleanup */
	pfree(relation);
	pfree(gphd_uri);
	pfree(EXTPROTOCOL_GET_USER_CTX(fcinfo));
	pfree(EXTPROTOCOL_GET_DATABUF(fcinfo));
	pfree(fcinfo->context);
	pfree(fcinfo);
}

void
test_pxfprotocol_export_second_call(void **state)
{
	/* setup call info with call context */
	PG_FUNCTION_ARGS = palloc0(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc0(sizeof(ExtProtocolData));
	fcinfo->context->type = T_ExtProtocolData;
	EXTPROTOCOL_GET_DATALEN(fcinfo) = 100;
	EXTPROTOCOL_GET_DATABUF(fcinfo) = palloc0(EXTPROTOCOL_GET_DATALEN(fcinfo));
	((ExtProtocolData *) fcinfo->context)->prot_last_call = false;
	gphadoop_context *call_context = palloc0(sizeof(gphadoop_context));

	EXTPROTOCOL_SET_USER_CTX(fcinfo, call_context);

	/* set mock behavior for bridge write */
	const int	EXPECTED_SIZE = 0;

	/* expected number of bytes written to the bridge */
	expect_value(gpbridge_write, context, call_context);
	expect_value(gpbridge_write, databuf, EXTPROTOCOL_GET_DATABUF(fcinfo));
	expect_value(gpbridge_write, datalen, EXTPROTOCOL_GET_DATALEN(fcinfo));
	will_return(gpbridge_write, EXPECTED_SIZE);

	Datum		d = pxfprotocol_export(fcinfo);

	assert_int_equal(DatumGetInt32(d), EXPECTED_SIZE);
	/* return number of bytes written to the bridge */
	assert_true(EXTPROTOCOL_GET_USER_CTX(fcinfo) == call_context);
	/* context is still the same */

	/* cleanup */
	pfree(call_context);
	pfree(EXTPROTOCOL_GET_DATABUF(fcinfo));
	pfree(fcinfo->context);
	pfree(fcinfo);
}

void
test_pxfprotocol_export_last_call(void **state)
{
	/* setup call info with a call context and last call indicator */
	PG_FUNCTION_ARGS = palloc0(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc0(sizeof(ExtProtocolData));
	fcinfo->context->type = T_ExtProtocolData;
	gphadoop_context *call_context = palloc0(sizeof(gphadoop_context));

	EXTPROTOCOL_SET_USER_CTX(fcinfo, call_context);
	EXTPROTOCOL_SET_LAST_CALL(fcinfo);

	/* init data in context that will be cleaned up */
	initStringInfo(&call_context->uri);
	initStringInfo(&call_context->write_file_name);

	/* set mock behavior for bridge cleanup */
	expect_value(gpbridge_cleanup, context, call_context);
	will_be_called(gpbridge_cleanup);

	Datum		d = pxfprotocol_export(fcinfo);

	assert_int_equal(DatumGetInt32(d), 0);
	/* 0 is returned from function */
	assert_true(EXTPROTOCOL_GET_USER_CTX(fcinfo) == NULL);
	/* call context is cleaned up */

	/* cleanup */
	pfree(fcinfo->context);
	pfree(fcinfo);
}
349 350 351 352
/* test setup and teardown methods */
void
before_test(void)
{
H
Heikki Linnakangas 已提交
353 354
	/* set global variables */
	GpIdentity.segindex = 0;
355 356 357 358 359
}

void
after_test(void)
{
H
Heikki Linnakangas 已提交
360 361 362 363
	/*
	 * no-op, but the teardown seems to be required when the test fails,
	 * otherwise CMockery issues a mismatch error
	 */
364 365 366
}

int
H
Heikki Linnakangas 已提交
367
main(int argc, char *argv[])
368
{
H
Heikki Linnakangas 已提交
369
	cmockery_parse_arguments(argc, argv);
370

H
Heikki Linnakangas 已提交
371 372 373 374
	const		UnitTest tests[] = {
		unit_test(test_pxfprotocol_validate_urls),
		unit_test_setup_teardown(test_pxfprotocol_import_first_call, before_test, after_test),
		unit_test_setup_teardown(test_pxfprotocol_import_second_call, before_test, after_test),
375 376 377 378 379
		unit_test_setup_teardown(test_pxfprotocol_import_last_call, before_test, after_test),
		unit_test_setup_teardown(test_pxfprotocol_export_first_call, before_test, after_test),
		unit_test_setup_teardown(test_pxfprotocol_export_second_call, before_test, after_test),
		unit_test_setup_teardown(test_pxfprotocol_export_last_call, before_test, after_test)

H
Heikki Linnakangas 已提交
380
	};
381

H
Heikki Linnakangas 已提交
382
	MemoryContextInit();
383

H
Heikki Linnakangas 已提交
384
	return run_tests(tests);
385
}