提交 650170f0 编写于 作者: W wujing

add container list unit test

Signed-off-by: Nwujing <wujing50@huawei.com>
上级 46ff577a
......@@ -21,6 +21,10 @@
#include "host_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef COMMANDER_MAX_OPTIONS
#define COMMANDER_MAX_OPTIONS 64
#endif
......@@ -112,5 +116,9 @@ int check_default_ulimit_type(const char *type);
void free_default_ulimit(host_config_ulimits_element **default_ulimit);
#ifdef __cplusplus
}
#endif
#endif /* COMMANDER_H */
......@@ -26,6 +26,10 @@
#include "json_common.h"
#include "isula_connect.h"
#ifdef __cplusplus
extern "C" {
#endif
/* max arguments can be specify in client */
#define MAX_CLIENT_ARGS 1000
......@@ -322,15 +326,15 @@ struct client_arguments {
{ CMD_OPT_TYPE_STRING_DUP, false, "host", 'H', &(cmdargs).socket, \
"Daemon socket(s) to connect to", command_valid_socket }, \
{ CMD_OPT_TYPE_BOOL, false, "tls", 0, &(cmdargs).tls, \
"Use TLS; implied by --tlsverify", NULL}, \
"Use TLS; implied by --tlsverify", NULL}, \
{ CMD_OPT_TYPE_BOOL, false, "tlsverify", 0, &(cmdargs).tls_verify, \
"Use TLS and verify the remote", NULL}, \
"Use TLS and verify the remote", NULL}, \
{ CMD_OPT_TYPE_STRING_DUP, false, "tlscacert", 0, &(cmdargs).ca_file, \
"Trust certs signed only by this CA (default \"/root/.iSulad/ca.pem\")", NULL }, \
"Trust certs signed only by this CA (default \"/root/.iSulad/ca.pem\")", NULL }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "tlscert", 0, &(cmdargs).cert_file, \
"Path to TLS certificate file (default \"/root/.iSulad/cert.pem\")", NULL }, \
"Path to TLS certificate file (default \"/root/.iSulad/cert.pem\")", NULL }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "tlskey", 0, &(cmdargs).key_file, \
"Path to TLS key file (default \"/root/.iSulad/key.pem\")", NULL }, \
"Path to TLS key file (default \"/root/.iSulad/key.pem\")", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "help", 0, NULL, "Print usage", NULL }
#define VERSION_OPTIONS(cmdargs) \
......@@ -350,5 +354,9 @@ extern void client_print_error(uint32_t cc, uint32_t server_errono, const char *
extern client_connect_config_t get_connect_config(const struct client_arguments *args);
#ifdef __cplusplus
}
#endif
#endif /* __ISULA_ARGUMENTS_H */
......@@ -17,137 +17,141 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define CREATE_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_CALLBACK, false, "accel", 0, &(cmdargs).custom_conf.accel, \
"Accelerator bindings (format: [<name>=]<runtime>[@<driver>[,<options>]])", \
command_append_array }, \
{ CMD_OPT_TYPE_BOOL, false, "read-only", 0, &(cmdargs).custom_conf.readonly, \
"Make container rootfs readonly", NULL }, \
"Make container rootfs readonly", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "cap-add", 0, &(cmdargs).custom_conf.cap_adds, \
"Add Linux capabilities ('ALL' to add all capabilities)", command_append_array }, \
"Add Linux capabilities ('ALL' to add all capabilities)", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "cap-drop", 0, &(cmdargs).custom_conf.cap_drops, \
"Drop Linux capabilities ('ALL' to drop all capabilities)", command_append_array }, \
"Drop Linux capabilities ('ALL' to drop all capabilities)", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "cpu-shares", 0, &(cmdargs).cr.cpu_shares, \
"CPU shares (relative weight)", command_convert_llong }, \
"CPU shares (relative weight)", command_convert_llong }, \
{ CMD_OPT_TYPE_CALLBACK, false, "cpu-period", 0, &(cmdargs).cr.cpu_period, \
"Limit CPU CFS (Completely Fair Scheduler) period", command_convert_llong }, \
"Limit CPU CFS (Completely Fair Scheduler) period", command_convert_llong }, \
{ CMD_OPT_TYPE_CALLBACK, false, "cpu-quota", 0, &(cmdargs).cr.cpu_quota, \
"Limit CPU CFS (Completely Fair Scheduler) quota", command_convert_llong }, \
"Limit CPU CFS (Completely Fair Scheduler) quota", command_convert_llong }, \
{ CMD_OPT_TYPE_STRING, false, "cpuset-cpus", 0, &(cmdargs).cr.cpuset_cpus, \
"CPUs in which to allow execution (e.g. 0-3, 0,1)", NULL }, \
"CPUs in which to allow execution (e.g. 0-3, 0,1)", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "cpuset-mems", 0, &(cmdargs).cr.cpuset_mems, \
"MEMs in which to allow execution (0-3, 0,1)", NULL }, \
"MEMs in which to allow execution (0-3, 0,1)", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "device-read-bps", 0, &(cmdargs).custom_conf.blkio_throttle_read_bps_device, \
"Limit read rate (bytes per second) from a device (default [])", command_append_array }, \
"Limit read rate (bytes per second) from a device (default [])", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "device-write-bps", 0, &(cmdargs).custom_conf.blkio_throttle_write_bps_device, \
"Limit write rate (bytes per second) to a device (default [])", command_append_array }, \
"Limit write rate (bytes per second) to a device (default [])", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "oom-score-adj", 0, &(cmdargs).cr.oom_score_adj, \
"Tune host's OOM preferences (-1000 to 1000)", command_convert_llong }, \
"Tune host's OOM preferences (-1000 to 1000)", command_convert_llong }, \
{ CMD_OPT_TYPE_CALLBACK, false, "device", 0, &(cmdargs).custom_conf.devices, \
"Add a host device to the container", command_append_array }, \
"Add a host device to the container", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "env", 'e', &(cmdargs).custom_conf.env, \
"Set environment variables", command_append_array }, \
"Set environment variables", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "env-file", 0, &(cmdargs).custom_conf.env_file, \
"Read in a file of environment variables", command_append_array }, \
"Read in a file of environment variables", command_append_array }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "entrypoint", 0, &(cmdargs).custom_conf.entrypoint, \
"Entrypoint to run when starting the container", NULL }, \
"Entrypoint to run when starting the container", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "external-rootfs", 0, &(cmdargs).external_rootfs, \
"Specify the custom rootfs that is not managed by isulad for the container, directory or block device", NULL }, \
"Specify the custom rootfs that is not managed by isulad for the container, directory or block device", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "files-limit", 0, &(cmdargs).custom_conf.files_limit, \
"Tune container files limit (set -1 for unlimited)", command_convert_llong }, \
"Tune container files limit (set -1 for unlimited)", command_convert_llong }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "hook-spec", 0, &(cmdargs).custom_conf.hook_spec, \
"File containing hook definition(prestart, poststart, poststop)", NULL }, \
"File containing hook definition(prestart, poststart, poststop)", NULL }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "hostname", 'h', &(cmdargs).custom_conf.hostname, \
"Container host name", NULL }, \
"Container host name", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "add-host", 0, &(cmdargs).custom_conf.extra_hosts, \
"Add a custom host-to-IP mapping (host:ip)", command_append_array }, \
"Add a custom host-to-IP mapping (host:ip)", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "dns", 0, &(cmdargs).custom_conf.dns, \
"Set custom DNS servers", command_append_array }, \
"Set custom DNS servers", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "dns-opt", 0, &(cmdargs).custom_conf.dns_options, \
"Set DNS options", command_append_array }, \
"Set DNS options", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "dns-search", 0, &(cmdargs).custom_conf.dns_search, \
"Set custom DNS search domains", command_append_array }, \
"Set custom DNS search domains", command_append_array }, \
{ CMD_OPT_TYPE_STRING, false, "user-remap", 0, &(cmdargs).custom_conf.user_remap, \
"Set user remap for container", NULL }, \
"Set user remap for container", NULL }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "ipc", 0, &(cmdargs).custom_conf.share_ns[NAMESPACE_IPC], \
"IPC namespace to use", NULL }, \
"IPC namespace to use", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "shm-size", 0, &(cmdargs).custom_conf.shm_size, \
"Size of /dev/shm, default value is 64MB", command_convert_membytes }, \
"Size of /dev/shm, default value is 64MB", command_convert_membytes }, \
{ CMD_OPT_TYPE_CALLBACK, false, "kernel-memory", 0, &(cmdargs).cr.kernel_memory_limit, \
"Kernel memory limit", command_convert_membytes }, \
"Kernel memory limit", command_convert_membytes }, \
{ CMD_OPT_TYPE_CALLBACK, false, "hugetlb-limit", 0, &(cmdargs).custom_conf.hugepage_limits, \
"Huge page limit (format: [size:]<limit>, e.g. --hugetlb-limit 2MB:32MB)", command_append_array }, \
"Huge page limit (format: [size:]<limit>, e.g. --hugetlb-limit 2MB:32MB)", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "log-opt", 0, &(cmdargs), \
"Container log options, value formate: key=value", callback_log_opt }, \
"Container log options, value formate: key=value", callback_log_opt }, \
{ CMD_OPT_TYPE_CALLBACK, false, "memory", 'm', &(cmdargs).cr.memory_limit, \
"Memory limit", command_convert_membytes }, \
"Memory limit", command_convert_membytes }, \
{ CMD_OPT_TYPE_CALLBACK, false, "memory-reservation", 0, &(cmdargs).cr.memory_reservation, \
"Memory soft limit", command_convert_membytes }, \
"Memory soft limit", command_convert_membytes }, \
{ CMD_OPT_TYPE_CALLBACK, false, "memory-swap", 0, &(cmdargs).cr.memory_swap, \
"Swap limit equal to memory plus swap: '-1' to enable unlimited swap", command_convert_memswapbytes }, \
"Swap limit equal to memory plus swap: '-1' to enable unlimited swap", command_convert_memswapbytes }, \
{ CMD_OPT_TYPE_CALLBACK, false, "mount", 0, &(cmdargs).custom_conf.mounts, \
"Attach a filesystem mount to the service", command_append_array }, \
"Attach a filesystem mount to the service", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "group-add", 0, &(cmdargs).custom_conf.group_add, \
"Add additional groups to join", command_append_array }, \
"Add additional groups to join", command_append_array }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "name", 'n', &(cmdargs).name, "Name of the container", NULL }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "net", 0, &(cmdargs).custom_conf.share_ns[NAMESPACE_NET], \
"Connect a container to a network", NULL }, \
"Connect a container to a network", NULL }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "pid", 0, &(cmdargs).custom_conf.share_ns[NAMESPACE_PID], \
"PID namespace to use", NULL }, \
"PID namespace to use", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "pids-limit", 0, &(cmdargs).custom_conf.pids_limit, \
"Tune container pids limit (set -1 for unlimited)", command_convert_llong }, \
"Tune container pids limit (set -1 for unlimited)", command_convert_llong }, \
{ CMD_OPT_TYPE_BOOL, false, "privileged", 0, &(cmdargs).custom_conf.privileged, \
"Give extended privileges to this container", NULL }, \
"Give extended privileges to this container", NULL }, \
{ CMD_OPT_TYPE_BOOL, false, "tty", 't', &(cmdargs).custom_conf.tty, "Allocate a pseudo-TTY", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "restart", 0, &(cmdargs).restart, \
"Restart policy to apply when a container exits(no, always, on-reboot, on-failure[:max-retries])", NULL }, \
"Restart policy to apply when a container exits(no, always, on-reboot, on-failure[:max-retries])", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "host-channel", 0, &(cmdargs).host_channel, \
"Create share memory between host and container", NULL }, \
"Create share memory between host and container", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "runtime", 'R', &(cmdargs).runtime, \
"Runtime to use for containers(default: lcr)", NULL }, \
"Runtime to use for containers(default: lcr)", NULL }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "user", 'u', &(cmdargs).custom_conf.user, \
"Username or UID (format: <name|uid>[:<group|gid>])", NULL }, \
"Username or UID (format: <name|uid>[:<group|gid>])", NULL }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "uts", 0, &(cmdargs).custom_conf.share_ns[NAMESPACE_UTS], \
"UTS namespace to use", NULL }, \
"UTS namespace to use", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "volume", 'v', &(cmdargs).custom_conf.volumes, \
"Bind mount a volume", command_append_array }, \
"Bind mount a volume", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "annotation", 0, &(cmdargs), \
"Set annotations on a container", callback_annotation }, \
"Set annotations on a container", callback_annotation }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "workdir", 0, &(cmdargs).custom_conf.workdir, \
"Working directory inside the container", NULL }, \
"Working directory inside the container", NULL }, \
{ CMD_OPT_TYPE_BOOL, false, "system-container", 0, &(cmdargs).custom_conf.system_container, \
"Extend some features only needed by running system container", NULL }, \
"Extend some features only needed by running system container", NULL }, \
{ CMD_OPT_TYPE_BOOL, false, "oom-kill-disable", 0, &(cmdargs).custom_conf.oom_kill_disable, \
"Disable OOM Killer", NULL }, \
"Disable OOM Killer", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "security-opt", 0, &(cmdargs).custom_conf.security, \
"Security Options (default [])", command_append_array }, \
"Security Options (default [])", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "storage-opt", 0, &(cmdargs).custom_conf.storage_opts, \
"Storage driver options for the container", command_append_array }, \
"Storage driver options for the container", command_append_array }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "health-cmd", 0, &(cmdargs).custom_conf.health_cmd, \
"Command to run to check health", NULL }, \
"Command to run to check health", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "sysctl", 0, &(cmdargs).custom_conf.sysctls, \
"Sysctl options", command_append_array }, \
"Sysctl options", command_append_array }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "env-target-file", 0, &(cmdargs).custom_conf.env_target_file, \
"Export env to target file path in rootfs", NULL }, \
"Export env to target file path in rootfs", NULL }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "cgroup-parent", 0, &(cmdargs).custom_conf.cgroup_parent, \
"Optional parent cgroup for the container", NULL }, \
"Optional parent cgroup for the container", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "health-interval", 0, &(cmdargs).custom_conf.health_interval, \
"Time between running the check (ms|s|m|h) (default 30s)", command_convert_nanoseconds }, \
"Time between running the check (ms|s|m|h) (default 30s)", command_convert_nanoseconds }, \
{ CMD_OPT_TYPE_CALLBACK, false, "health-retries", 0, &(cmdargs).custom_conf.health_retries, \
"Consecutive failures needed to report unhealthy (default 3)", command_convert_int }, \
"Consecutive failures needed to report unhealthy (default 3)", command_convert_int }, \
{ CMD_OPT_TYPE_CALLBACK, false, "health-timeout", 0, &(cmdargs).custom_conf.health_timeout, \
"Maximum time to allow one check to run (ms|s|m|h) (default 30s)", command_convert_nanoseconds }, \
"Maximum time to allow one check to run (ms|s|m|h) (default 30s)", command_convert_nanoseconds }, \
{ CMD_OPT_TYPE_CALLBACK, false, "health-start-period", 0, &(cmdargs).custom_conf.health_start_period, \
"Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) " \
"(default 0s)", command_convert_nanoseconds }, \
"Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) " \
"(default 0s)", command_convert_nanoseconds }, \
{ CMD_OPT_TYPE_BOOL, false, "no-healthcheck", 0, &(cmdargs).custom_conf.no_healthcheck, \
"Disable any container-specified HEALTHCHECK", NULL }, \
"Disable any container-specified HEALTHCHECK", NULL }, \
{ CMD_OPT_TYPE_BOOL, false, "health-exit-on-unhealthy", 0, &(cmdargs).custom_conf.exit_on_unhealthy, \
"Kill the container when it is detected to be unhealthy", NULL }, \
"Kill the container when it is detected to be unhealthy", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "ns-change-opt", 0, &(cmdargs).custom_conf.ns_change_opt, \
"Namespaced kernel param options for system container (default [])", NULL }, \
"Namespaced kernel param options for system container (default [])", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "ulimit", 0, &(cmdargs).custom_conf.ulimits, \
"Ulimit options (default [])", command_append_array }
"Ulimit options (default [])", command_append_array }
#define CREATE_EXTEND_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "interactive", 'i', &(cmdargs).custom_conf.open_stdin, \
......@@ -169,5 +173,9 @@ int callback_annotation(command_option_t *option, const char *value);
int cmd_create_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_CREATE_H */
......@@ -18,6 +18,10 @@
#include "arguments.h"
#include "wait.h"
#ifdef __cplusplus
extern "C" {
#endif
#define KILL_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "signal", 's', &(cmdargs).signal, \
"Signal to send to the container (default \"SIGKILL\")", NULL }
......@@ -26,5 +30,10 @@ extern const char g_cmd_kill_desc[];
extern const char g_cmd_kill_usage[];
extern struct client_arguments g_cmd_kill_args;
int cmd_kill_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_rename_desc[];
extern const char g_cmd_rename_usage[];
extern struct client_arguments g_cmd_rename_args;
int cmd_rename_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -15,6 +15,10 @@
#ifndef __CMD_RESTART_H
#define __CMD_RESTART_H
#ifdef __cplusplus
extern "C" {
#endif
#define RESTART_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_CALLBACK, false, "time", 't', &(cmdargs).time, \
"Seconds to wait for stop before killing it (default 10)", command_convert_int }
......@@ -24,5 +28,9 @@ extern const char g_cmd_restart_usage[];
extern struct client_arguments g_cmd_restart_args;
int cmd_restart_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_RESTART_H */
......@@ -17,16 +17,24 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define DELETE_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "force", 'f', &(cmdargs).force, \
"Force the removal of a running container (uses SIGKILL)", NULL }, \
{ CMD_OPT_TYPE_BOOL, false, "volumes", 'v', &(cmdargs).volume, \
"Remove the volumes associated with the container", NULL }
"Remove the volumes associated with the container", NULL }
extern const char g_cmd_delete_desc[];
extern const char g_cmd_delete_usage[];
extern struct client_arguments g_cmd_delete_args;
int cmd_delete_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_DELETE_H */
......@@ -19,16 +19,24 @@
#include "start.h"
#include "wait.h"
#ifdef __cplusplus
extern "C" {
#endif
#define RUN_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "detach", 'd', &(cmdargs).detach, \
"Run container in background and print container ID", NULL }, \
{ CMD_OPT_TYPE_BOOL, false, "rm", 0, &(cmdargs).custom_conf.auto_remove, \
"Automatically remove the container when it exits", NULL }
"Automatically remove the container when it exits", NULL }
extern const char g_cmd_run_desc[];
extern const char g_cmd_run_usage[];
extern struct client_arguments g_cmd_run_args;
int cmd_run_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_RUN_H */
......@@ -19,6 +19,10 @@
#include "commands.h"
#include <termios.h>
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_start_desc[];
extern struct client_arguments g_cmd_start_args;
......@@ -28,5 +32,10 @@ void client_restore_console(bool reset_tty, const struct termios *oldtios, struc
int client_start(const struct client_arguments *args, bool *reset_tty, struct termios *oldtios,
struct command_fifo_config **console_fifos);
int cmd_start_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_START_H */
......@@ -17,10 +17,14 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define STOP_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "force", 'f', &(cmdargs).force, "Stop by force killing", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "time", 't', &(cmdargs).time, \
"Seconds to wait for stop before killing it (default 10)", command_convert_int }
"Seconds to wait for stop before killing it (default 10)", command_convert_int }
extern const char g_cmd_stop_desc[];
extern const char g_cmd_stop_usage[];
......@@ -28,5 +32,9 @@ extern struct client_arguments g_cmd_stop_args;
int cmd_stop_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_STOP_H */
......@@ -18,6 +18,10 @@
#include "arguments.h"
#include <semaphore.h>
#ifdef __cplusplus
extern "C" {
#endif
#define CLIENT_RUNDIR "/var/run/isula"
// A command is described by:
......@@ -69,5 +73,10 @@ int commmand_default_help(const char * const program_name,
const char **argv);
int run_command(struct command *commands, int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __COMMAND_H */
......@@ -17,18 +17,26 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define EVENTS_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "name", 'n', &(cmdargs).name, \
"Name of the container", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "since", 'S', &(cmdargs).since, \
"Show all events created since this timestamp", NULL }, \
"Show all events created since this timestamp", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "until", 'U', &(cmdargs).until, \
"Show all events created until this timestamp", NULL }
"Show all events created until this timestamp", NULL }
extern const char g_cmd_events_desc[];
extern const char g_cmd_events_usage[];
extern struct client_arguments g_cmd_events_args;
int cmd_events_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_EVENT_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define EXPORT_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "output", 'o', &(cmdargs).file, "Write to a file", NULL }
......@@ -25,5 +29,9 @@ extern const char g_cmd_export_usage[];
extern struct client_arguments g_cmd_export_args;
int cmd_export_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_pause_desc[];
extern const char g_cmd_pause_usage[];
extern struct client_arguments g_cmd_pause_args;
int cmd_pause_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_resume_desc[];
extern const char g_cmd_resume_usage[];
extern struct client_arguments g_cmd_resume_args;
int cmd_resume_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,16 +17,24 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define STATUS_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "all", 'a', &(cmdargs).showall, \
"Show all containers (default shows just running)", NULL }, \
{ CMD_OPT_TYPE_BOOL, false, "no-stream", 0, &(cmdargs).nostream, \
"Disable streaming stats and only pull the first result", NULL }
"Disable streaming stats and only pull the first result", NULL }
extern const char g_cmd_stats_desc[];
extern const char g_cmd_stats_usage[];
extern struct client_arguments g_cmd_stats_args;
int cmd_stats_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_STATS_H */
......@@ -17,27 +17,31 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define UPDATE_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_CALLBACK, false, "cpu-shares", 0, &(cmdargs).cr.cpu_shares, \
"CPU shares (relative weight)", command_convert_llong }, \
{ CMD_OPT_TYPE_CALLBACK, false, "cpu-period", 0, &(cmdargs).cr.cpu_period, \
"Limit CPU CFS (Completely Fair Scheduler) period", command_convert_llong }, \
"Limit CPU CFS (Completely Fair Scheduler) period", command_convert_llong }, \
{ CMD_OPT_TYPE_CALLBACK, false, "cpu-quota", 0, &(cmdargs).cr.cpu_quota, \
"Limit CPU CFS (Completely Fair Scheduler) quota", command_convert_llong }, \
"Limit CPU CFS (Completely Fair Scheduler) quota", command_convert_llong }, \
{ CMD_OPT_TYPE_STRING, false, "cpuset-cpus", 0, &(cmdargs).cr.cpuset_cpus, \
"CPUs in which to allow execution (0-3, 0,1)", NULL }, \
"CPUs in which to allow execution (0-3, 0,1)", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "cpuset-mems", 0, &(cmdargs).cr.cpuset_mems, \
"MEMs in which to allow execution (0-3, 0,1)", NULL }, \
"MEMs in which to allow execution (0-3, 0,1)", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "kernel-memory", 0, &(cmdargs).cr.kernel_memory_limit, \
"Kernel memory limit", command_convert_membytes }, \
"Kernel memory limit", command_convert_membytes }, \
{ CMD_OPT_TYPE_CALLBACK, false, "memory", 'm', &(cmdargs).cr.memory_limit, \
"Memory limit", command_convert_membytes }, \
"Memory limit", command_convert_membytes }, \
{ CMD_OPT_TYPE_CALLBACK, false, "memory-reservation", 0, &(cmdargs).cr.memory_reservation, \
"Memory soft limit", command_convert_membytes }, \
"Memory soft limit", command_convert_membytes }, \
{ CMD_OPT_TYPE_CALLBACK, false, "memory-swap", 0, &(cmdargs).cr.memory_swap, \
"Swap limit equal to memory plus swap: '-1' to enable unlimited swap", command_convert_memswapbytes }, \
"Swap limit equal to memory plus swap: '-1' to enable unlimited swap", command_convert_memswapbytes }, \
{ CMD_OPT_TYPE_STRING, false, "restart", 0, &(cmdargs).restart, \
"Restart policy to apply when a container exits", NULL }
"Restart policy to apply when a container exits", NULL }
extern const char g_cmd_update_desc[];
extern const char g_cmd_update_usage[];
......@@ -45,5 +49,9 @@ extern struct client_arguments g_cmd_update_args;
int cmd_update_main(int argc, const char **argv);
int update_checker(const struct client_arguments *args);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_UPDATE_H */
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_images_desc[];
extern const char g_cmd_images_usage[];
extern struct client_arguments g_cmd_images_args;
int cmd_images_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_IMAGES_LIST_H */
......@@ -17,10 +17,14 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LOAD_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "input", 'i', &(cmdargs).file, "Read from a manifest or an archive", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "tag", 0, &(cmdargs).tag, \
"Name and optionally a tag in the 'name:tag' format, valid if type is docker", NULL }
"Name and optionally a tag in the 'name:tag' format, valid if type is docker", NULL }
#define EMBEDDED_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "type", 't', &(cmdargs).type, "Image type, embedded or docker(default)", NULL }
......@@ -29,5 +33,9 @@ extern const char g_cmd_load_desc[];
extern struct client_arguments g_cmd_load_args;
int cmd_load_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LOAD_H */
......@@ -16,11 +16,15 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LOGIN_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "username", 'u', &(cmdargs).username, "Username", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "password", 'p', &(cmdargs).password, "Password", NULL }, \
{ CMD_OPT_TYPE_BOOL, false, "password-stdin", 0, &(cmdargs).password_stdin, \
"Take the password from stdin", NULL }, \
"Take the password from stdin", NULL }, \
extern const char g_cmd_login_desc[];
......@@ -28,5 +32,9 @@ extern const char g_cmd_login_usage[];
extern struct client_arguments g_cmd_login_args;
int cmd_login_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LOGIN_H */
......@@ -16,10 +16,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_logout_desc[];
extern const char g_cmd_logout_usage[];
extern struct client_arguments g_cmd_logout_args;
int cmd_logout_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LOGOUT_H */
......@@ -18,6 +18,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_pull_desc[];
extern const char g_cmd_pull_usage[];
extern struct client_arguments g_cmd_pull_args;
......@@ -25,5 +29,9 @@ int client_pull(const struct client_arguments *args);
int cmd_pull_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_PULL_IMAGE_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define RMI_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "force", 'f', &(cmdargs).force, "Force removal of the image", NULL }
......@@ -25,5 +29,9 @@ extern const char g_cmd_rmi_usage[];
extern struct client_arguments g_cmd_rmi_args;
int cmd_rmi_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_REMOVE_IMAGE_H */
......@@ -17,6 +17,10 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define HEALTH_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "service", 'S', &(cmdargs).service, "GRPC service name", NULL }
......@@ -25,5 +29,9 @@ extern const char g_cmd_health_check_usage[];
extern struct client_arguments g_cmd_health_check_args;
int cmd_health_check_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_info_desc[];
extern const char g_cmd_info_usage[];
extern struct client_arguments g_cmd_info_args;
int cmd_info_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_INFO_H */
......@@ -17,16 +17,24 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define INSPECT_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_STRING, false, "format", 'f', &(cmdargs).format, \
"Format the output using the given go template", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "time", 't', &(cmdargs).time, \
"Seconds to wait for inspect timeout (default 120)", command_convert_int }
"Seconds to wait for inspect timeout (default 120)", command_convert_int }
extern const char g_cmd_inspect_desc[];
extern const char g_cmd_inspect_usage[];
extern struct client_arguments g_cmd_inspect_args;
int cmd_inspect_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_INSPECT_H */
......@@ -17,10 +17,14 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LOGS_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "follow", 'f', &(cmdargs).follow, "Follow log output", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "tail", 0, &(cmdargs).tail, \
"Number of lines to show from the end of the logs", callback_tail }
"Number of lines to show from the end of the logs", callback_tail }
extern const char g_cmd_logs_desc[];
extern const char g_cmd_logs_usage[];
......@@ -28,5 +32,10 @@ extern struct client_arguments g_cmd_logs_args;
int callback_tail(command_option_t *option, const char *arg);
int cmd_logs_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LOGS_H */
......@@ -17,21 +17,29 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LIST_OPTIONS(cmdargs) \
{ CMD_OPT_TYPE_BOOL, false, "all", 'a', &(cmdargs).list_all, \
"Display all containers (default shows just running)", NULL }, \
{ CMD_OPT_TYPE_BOOL, false, "quiet", 'q', &(cmdargs).dispname, "Only display numeric IDs", NULL }, \
{ CMD_OPT_TYPE_CALLBACK, false, "filter", 'f', &(cmdargs).filters, \
"Filter output based on conditions provided", command_append_array }, \
"Filter output based on conditions provided", command_append_array }, \
{ CMD_OPT_TYPE_BOOL, false, "no-trunc", 0, &(cmdargs).no_trunc, \
"Don't truncate output", NULL }, \
"Don't truncate output", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "format", 0, &(cmdargs).format, \
"Format the output using the given go template", NULL }
"Format the output using the given go template", NULL }
extern const char g_cmd_list_desc[];
extern const char g_cmd_list_usage[];
extern struct client_arguments g_cmd_list_args;
int cmd_list_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_LIST_H */
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_top_desc[];
extern const char g_cmd_top_usage[];
extern struct client_arguments g_cmd_top_args;
int cmd_top_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_TOP_H */
......@@ -17,10 +17,18 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_version_desc[];
extern const char g_cmd_version_usage[];
extern struct client_arguments g_cmd_version_args;
int cmd_version_main(int argc, const char **argv);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_VERSION_H */
......@@ -17,11 +17,19 @@
#include "arguments.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char g_cmd_wait_desc[];
extern const char g_cmd_wait_usage[];
extern struct client_arguments g_cmd_wait_args;
int cmd_wait_main(int argc, const char **argv);
int client_wait(const struct client_arguments *args, unsigned int *exit_code);
#ifdef __cplusplus
}
#endif
#endif /* __CMD_WAIT_H */
project(iSulad_LLT)
function(gmock_find_library _name)
find_library(${_name}
NAMES ${ARGN}
HINTS
$ENV{GMOCK_ROOT}
${GMOCK_ROOT}
)
mark_as_advanced(${_name})
endfunction()
find_path(GMOCK_INCLUDE_DIR gmock/gmock.h
HINTS
$ENV{GMOCK_ROOT}/include
${GMOCK_ROOT}/include
)
mark_as_advanced(GMOCK_INCLUDE_DIR)
gmock_find_library(GMOCK_LIBRARY gmock)
gmock_find_library(GMOCK_LIBRARY_DEBUG gmockd)
gmock_find_library(GMOCK_MAIN_LIBRARY gmock_main)
gmock_find_library(GMOCK_MAIN_LIBRARY_DEBUG gmock_maind)
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GMock DEFAULT_MSG GMOCK_LIBRARY GMOCK_INCLUDE_DIR GMOCK_MAIN_LIBRARY)
if(GMOCK_FOUND)
set(GMOCK_INCLUDE_DIRS ${GMOCK_INCLUDE_DIR})
set(GMOCK_BOTH_LIBRARIES ${GMOCK_LIBRARIES} ${GMOCK_MAIN_LIBRARIES})
endif()
# setup testing
find_package(Threads REQUIRED)
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
include_directories(${GMOCK_INCLUDE_DIRS})
add_subdirectory(cutils)
add_subdirectory(image)
add_subdirectory(path)
add_subdirectory(cmd)
project(iSulad_LLT)
add_subdirectory(isula)
project(iSulad_LLT)
add_subdirectory(infomation)
project(iSulad_LLT)
add_subdirectory(ps)
project(iSulad_LLT)
SET(EXE ps_llt)
add_executable(${EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/log.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_string.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_array.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_convert.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_verify.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_regex.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256/sha256.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/path.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/commander.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/console/console.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/arguments.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/libisulad.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/libisula.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/types_def.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/mainloop.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/container_def.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/error.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client/isula_connect.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json/schema/src/read_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/information/ps.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/grpc_client_mock.cc
${CMAKE_BINARY_DIR}/json/json_common.c
${CMAKE_BINARY_DIR}/json/host_config.c
${CMAKE_BINARY_DIR}/json/container_path_stat.c
${CMAKE_BINARY_DIR}/json/timestamp.c
ps_llt.cc)
target_include_directories(${EXE} PUBLIC
${GTEST_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../include
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/http
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/console
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/information
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client/grpc
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json/schema/src
${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks
${CMAKE_BINARY_DIR}/json
${CMAKE_BINARY_DIR}/conf
)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lsecurec -lz)
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
* Description: ps llt
* Author: wujing
* Create: 2019-12-19
*/
#include "ps.h"
#include <ctime>
#include <cmath>
#include <random>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "grpc_client_mock.h"
#include "utils.h"
using ::testing::Args;
using ::testing::ByRef;
using ::testing::SetArgPointee;
using ::testing::DoAll;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::NotNull;
using ::testing::AtLeast;
using ::testing::Invoke;
using ::testing::_;
using namespace std;
class ContainerListUnitTest : public testing::Test {
public:
void SetUp() override
{
GrpcClient_SetMock(&m_grpcClient);
::testing::Mock::AllowLeak(&m_grpcClient);
}
void TearDown() override
{
GrpcClient_SetMock(nullptr);
}
NiceMock<MockGrpcClient> m_grpcClient;
};
namespace {
unsigned generate_random_pid()
{
constexpr int pid_start = 10000;
constexpr int pid_end = 10000;
static default_random_engine e(time(0));
static uniform_int_distribution<unsigned> u(pid_start, pid_end);
return u(e);
}
long long generate_random_created()
{
// unix nanos: 2019-01-01T00:00:00.000000000+08:00
constexpr int64_t start = 1546272000000000000ll;
// unix nanos: 2019-12-31T00:00:00.000000000+08:00
constexpr int64_t end = 1577721600000000000ll;
static default_random_engine e(time(0));
static uniform_int_distribution<long long> u(start, end);
return u(e);
}
string generate_random_string(int length)
{
static string chset = "abcdefghijklmnopqrstuvwxyz1234567890";
string result;
result.resize(length);
srand(time(NULL));
for (int i = 0; i < length; i++) {
static default_random_engine e(time(0));
static uniform_int_distribution<unsigned> u(0, chset.size() - 1);
result[i] = chset[u(e) % chset.length()];
}
return result;
}
int set_container_summary(struct isula_list_response *response, int index)
{
constexpr int id_len = 64;
constexpr int name_len = 8;
response->container_summary[index] = (struct isula_container_summary_info *)util_common_calloc_s(
sizeof(struct isula_container_summary_info));
if (response->container_summary[index] == nullptr) {
return -1;
}
response->container_summary[index]->id = util_strdup_s(generate_random_string(id_len).c_str());
response->container_summary[index]->name = util_strdup_s(generate_random_string(name_len).c_str());
response->container_summary[index]->runtime = util_strdup_s("lcr");
response->container_summary[index]->pid = generate_random_pid();
response->container_summary[index]->status = (Container_Status)CONTAINER_STATUS_RUNNING;
response->container_summary[index]->image = util_strdup_s("busybox:latest");
response->container_summary[index]->command = util_strdup_s("/bin/sh");
response->container_summary[index]->startat = util_strdup_s("2019-12-31T23:55:50.867369507+08:00");
response->container_summary[index]->finishat = util_strdup_s("2020-01-01T23:55:50.867369507+08:00");
response->container_summary[index]->exit_code = 0;
response->container_summary[index]->restart_count = 0;
response->container_summary[index]->created = generate_random_created();
response->container_summary[index]->health_state = util_strdup_s("(healthy)");
response->container_num++;
return 0;
}
} // namespace
int ContainerList(const struct isula_list_request *request,
struct isula_list_response *response, void *arg)
{
(void)request;
(void)arg;
constexpr int container_cnt = 5;
response->cc = 0;
response->server_errono = 0;
response->errmsg = nullptr;
response->container_summary = (struct isula_container_summary_info **)util_common_calloc_s(
sizeof(struct isula_container_summary_info *) * container_cnt);
if (response->container_summary == nullptr) {
return -1;
}
for (size_t i {}; i < container_cnt; ++i) {
if (set_container_summary(response, i)) {
return -1;
}
}
return 0;
}
int invokeGrpcOpsInit(isula_connect_ops *ops)
{
if (ops == nullptr) {
return -1;
}
ops->container.list = &ContainerList;
return 0;
}
TEST_F(ContainerListUnitTest, test_cmd_list_main_all)
{
const char *argv[] = {"isula", "ps", "-a"};
const char *argv_failure[] = {"isula", "ps", "-k"};
isula_connect_ops ops;
ops.container.list = &ContainerList;
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_))
.WillOnce(Return(-1))
.WillOnce(DoAll(SetArgPointee<0>(ByRef(ops)), Return(0)));
ASSERT_EQ(connect_client_ops_init(), -1);
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
EXPECT_EXIT(cmd_list_main(sizeof(argv_failure) / sizeof(argv_failure[0]), const_cast<const char **>(argv_failure)),
testing::ExitedWithCode(125), "Unkown flag found");
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
}
TEST_F(ContainerListUnitTest, test_cmd_list_main_format)
{
const char *argv[] = {
"isula", "ps", "-a", "--format", "\"table XXX{{.ID}}AAA{{.Image}}"
" {{.Status}} {{.Pid}} {{.Command}} {{.Created}} {{.Ports}} {{.ExitCode}} "
"{{.RestartCount}} {{.StartAt}} {{.FinishAt}} {{.Runtime}} \t{{.Names}} \n{{.State}}\""
};
const char *argv_failure[] = {"isula", "ps", "--format", "\"{{.ID}} {{.XXX}}"};
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
EXPECT_EXIT(cmd_list_main(sizeof(argv_failure) / sizeof(argv_failure[0]), const_cast<const char **>(argv_failure)),
testing::ExitedWithCode(1), "not support the field");
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
}
TEST_F(ContainerListUnitTest, test_cmd_list_main_notrunc)
{
const char *argv[] = {"isula", "ps", "-q", "--no-trunc"};
testing::internal::CaptureStdout();
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
std::string output = testing::internal::GetCapturedStdout();
if (output.find("CONTAINER ID") != std::string::npos) {
ADD_FAILURE() << "the output of command('isula ps -q --no-trunc') should not include table headers";
}
std::cout << "Gtest Captured Stdout:" << std::endl << output;
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
}
TEST_F(ContainerListUnitTest, test_cmd_list_main_debug)
{
const char *argv[] = {"isula", "ps", "-a", "-D"};
testing::internal::CaptureStdout();
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_list_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
std::string output = testing::internal::GetCapturedStdout();
std::vector<std::string> tableItems {"CONTAINER ID", "IMAGE", "COMMAND", "CREATED", "STATUS", "PORTS", "NAMES"};
for (const auto &elem : tableItems) {
if (output.find(elem) == std::string::npos) {
ADD_FAILURE() << "container list info should include " << elem;
}
}
if (output.find("healthy") == std::string::npos) {
FAIL() << "container list info should include healthy";
}
std::cout << "Gtest Captured Stdout:" << std::endl << output;
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
SUCCEED() << "test isula ps --debug success";
}
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* Description: grpc client mock
* Author: wujing
* Create: 2019-12-19
*/
#include "grpc_client_mock.h"
namespace {
MockGrpcClient *g_grpc_client_mock = NULL;
}
void GrpcClient_SetMock(MockGrpcClient* mock)
{
g_grpc_client_mock = mock;
}
int grpc_ops_init(isula_connect_ops *ops)
{
if (g_grpc_client_mock != nullptr) {
return g_grpc_client_mock->GrpcOpsInit(ops);
}
return 0;
}
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* Description: grpc client mock
* Author: wujing
* Create: 2019-12-19
*/
#ifndef GRPC_CLIENT_MOCK_H_
#define GRPC_CLIENT_MOCK_H_
#include <gmock/gmock.h>
#include "grpc_client.h"
class MockGrpcClient {
public:
virtual ~MockGrpcClient() = default;
MOCK_METHOD1(GrpcOpsInit, int(isula_connect_ops *ops));
};
void GrpcClient_SetMock(MockGrpcClient* mock);
#endif // GRPC_CLIENT_MOCK_H_
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册