pxfprotocol_test.c 8.2 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 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
	/* setup call info with no call context */
	PG_FUNCTION_ARGS = palloc(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc(sizeof(ExtProtocolValidatorData));
	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, FRAGMENTER);
	will_return(GPHDUri_opt_exists, true);

	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);
83 84 85 86 87
}

void
test_pxfprotocol_export(void **state)
{
H
Heikki Linnakangas 已提交
88 89 90
	Datum		d = pxfprotocol_export(NULL);

	assert_int_equal(DatumGetInt32(d), 0);
91 92 93 94 95
}

void
test_pxfprotocol_import_first_call(void **state)
{
H
Heikki Linnakangas 已提交
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 156 157 158 159
	/* setup call info with no call context */
	PG_FUNCTION_ARGS = palloc(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc(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 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);
160 161 162 163 164
}

void
test_pxfprotocol_import_second_call(void **state)
{
H
Heikki Linnakangas 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	/* setup call info with call context */
	PG_FUNCTION_ARGS = palloc(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc(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 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);
197 198 199 200 201
}

void
test_pxfprotocol_import_last_call(void **state)
{
H
Heikki Linnakangas 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	/* setup call info with a call context and last call indicator */
	PG_FUNCTION_ARGS = palloc(sizeof(FunctionCallInfoData));
	fcinfo->context = palloc(sizeof(ExtProtocolData));
	fcinfo->context->type = T_ExtProtocolData;
	gphadoop_context *call_context = palloc(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_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);
229 230 231 232 233 234
}

/* test setup and teardown methods */
void
before_test(void)
{
H
Heikki Linnakangas 已提交
235 236
	/* set global variables */
	GpIdentity.segindex = 0;
237 238 239 240 241
}

void
after_test(void)
{
H
Heikki Linnakangas 已提交
242 243 244 245
	/*
	 * no-op, but the teardown seems to be required when the test fails,
	 * otherwise CMockery issues a mismatch error
	 */
246 247 248
}

int
H
Heikki Linnakangas 已提交
249
main(int argc, char *argv[])
250
{
H
Heikki Linnakangas 已提交
251
	cmockery_parse_arguments(argc, argv);
252

H
Heikki Linnakangas 已提交
253 254 255 256 257 258 259
	const		UnitTest tests[] = {
		unit_test(test_pxfprotocol_validate_urls),
		unit_test(test_pxfprotocol_export),
		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),
		unit_test_setup_teardown(test_pxfprotocol_import_last_call, before_test, after_test)
	};
260

H
Heikki Linnakangas 已提交
261
	MemoryContextInit();
262

H
Heikki Linnakangas 已提交
263
	return run_tests(tests);
264
}