summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/src/test_options.c
blob: 2c644046c24873a07e7ca4d1e4e13049979e974c (plain)
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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
   CTDB tests commandline options

   Copyright (C) Amitay Isaacs  2015

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"

#include <assert.h>
#include <popt.h>
#include <talloc.h>

#include "lib/util/debug.h"

#include "common/logging.h"
#include "common/path.h"

#include "tests/src/test_options.h"

static struct test_options _values;

static struct poptOption options_basic[] = {
	{
		.longName   = "socket",
		.shortName  = 's',
		.argInfo    = POPT_ARG_STRING,
		.arg        = &_values.socket,
		.descrip    = "CTDB socket path",
		.argDescrip = "filename",
	},
	{
		.longName   = "timelimit",
		.shortName  = 't',
		.argInfo    = POPT_ARG_INT,
		.arg        = &_values.timelimit,
		.descrip    = "Time limit (in seconds)",
	},
	{
		.longName   = "num-nodes",
		.shortName  = 'n',
		.argInfo    = POPT_ARG_INT,
		.arg        = &_values.num_nodes,
		.descrip    = "Number of cluster nodes",
	},
	{
		.longName   = "debug",
		.shortName  = 'd',
		.argInfo    = POPT_ARG_STRING,
		.arg        = &_values.debugstr,
		.descrip    = "Debug level",
	},
	{
		.longName   = "interactive",
		.shortName  = 'i',
		.argInfo    = POPT_ARG_NONE,
		.arg        = &_values.interactive,
		.val        = 0,
		.descrip    = "Interactive output",
	},
	POPT_TABLEEND
};

#define TEST_OPTIONS_BASIC                            \
	{                                             \
		.argInfo    = POPT_ARG_INCLUDE_TABLE, \
		.arg        = options_basic,          \
		.descrip    = "General options:",     \
	},

static struct poptOption options_database[] = {
	{
		.longName   = "database",
		.shortName  = 'D',
		.argInfo    = POPT_ARG_STRING,
		.arg        = &_values.dbname,
		.descrip    = "CTDB database name",
	},
	{
		.longName   = "key",
		.shortName  = 'k',
		.argInfo    = POPT_ARG_STRING,
		.arg        = &_values.keystr,
		.descrip    = "Name of database key",
	},
	{
		.longName   = "value",
		.shortName  = 'v',
		.argInfo    = POPT_ARG_STRING,
		.arg        = &_values.valuestr,
		.descrip    = "Value of database key",
	},
	{
		.longName   = "dbtype",
		.shortName  = 'T',
		.argInfo    = POPT_ARG_STRING,
		.arg        = &_values.dbtype,
		.descrip    = "CTDB database type",
	},
	POPT_TABLEEND
};

#define TEST_OPTIONS_DATABASE                         \
	{                                             \
		.argInfo    = POPT_ARG_INCLUDE_TABLE, \
		.arg        = options_database,       \
		.descrip    = "Database options:",    \
	},

static void set_defaults_basic(struct test_options *opts)
{
	/* Set default options */
	opts->socket = path_socket(NULL, "ctdbd"); /* leaked */
	assert(opts->socket != NULL);

	opts->timelimit = 10;
	opts->num_nodes = 1;
	opts->debugstr = "ERR";
	opts->interactive = 0;
}

static void set_defaults_database(struct test_options *opts)
{
	opts->dbname = NULL;
	opts->keystr = NULL;
	opts->valuestr = NULL;
	opts->dbtype = "volatile";
}

static bool verify_options_basic(struct test_options *opts)
{
	int log_level;
	bool status;

	status = debug_level_parse(opts->debugstr, &log_level);
	if (! status) {
		fprintf(stderr, "Error: Invalid debug string '%s'\n",
			opts->debugstr);
		return false;
	}

	debuglevel_set(log_level);

	return true;
}

static bool verify_options_database(struct test_options *opts)
{
	if (opts->dbname == NULL) {
		fprintf(stderr, "Error: Please specify database\n");
		return false;
	}
	if (opts->keystr == NULL) {
		fprintf(stderr, "Error: Please specify key name\n");
		return false;
	}

	if ((strcmp(opts->dbtype, "volatile") != 0) &&
	    (strcmp(opts->dbtype, "persistent") != 0) &&
	    (strcmp(opts->dbtype, "replicated") != 0)) {
		return false;
	}

	return true;
}

static bool process_options_common(int argc, const char **argv,
				   struct poptOption *options)
{
	poptContext pc;
	int opt;

	pc = poptGetContext(argv[0], argc, argv, options,
			    POPT_CONTEXT_KEEP_FIRST);
	while ((opt = poptGetNextOpt(pc)) != -1) {
		fprintf(stderr, "Invalid option %s: %s\n",
			poptBadOption(pc, 0), poptStrerror(opt));
		return false;
	}

	return true;
}

bool process_options_basic(int argc, const char **argv,
			   const struct test_options **opts)
{
	struct poptOption options[] = {
		POPT_AUTOHELP
		TEST_OPTIONS_BASIC
		POPT_TABLEEND
	};

	set_defaults_basic(&_values);

	if (! process_options_common(argc, argv, options)) {
		return false;
	}

	if (! verify_options_basic(&_values)) {
		return false;
	}

	*opts = &_values;
	return true;
}

bool process_options_database(int argc, const char **argv,
			      const struct test_options **opts)
{
	struct poptOption options[] = {
		POPT_AUTOHELP
		TEST_OPTIONS_BASIC
		TEST_OPTIONS_DATABASE
		POPT_TABLEEND
	};

	set_defaults_basic(&_values);
	set_defaults_database(&_values);

	if (! process_options_common(argc, argv, options)) {
		return false;
	}

	if (! verify_options_basic(&_values)) {
		return false;
	}
	if (! verify_options_database(&_values)) {
		return false;
	}

	*opts = &_values;
	return true;
}