Pārlūkot izejas kodu

Add emqx_plugins module test case (#2801)

JianBo He 6 gadi atpakaļ
vecāks
revīzija
acf54509f5

+ 35 - 2
test/emqx_plugins_SUITE.erl

@@ -19,8 +19,41 @@
 -compile(export_all).
 -compile(nowarn_export_all).
 
--include_lib("proper/include/proper.hrl").
 -include_lib("eunit/include/eunit.hrl").
 
-
 all() -> emqx_ct:all(?MODULE).
+
+init_per_suite(Config) ->
+
+    %% Compile extra plugin code
+
+    DataPath = proplists:get_value(data_dir, Config),
+    AppPath = filename:join([DataPath, "emqx_mini_plugin"]),
+    Cmd = lists:flatten(io_lib:format("cd ~s && make", [AppPath])),
+
+    ct:pal("Executing ~s~n", [Cmd]),
+    ct:pal("~n ~s~n", [os:cmd(Cmd)]),
+
+    code:add_path(filename:join([AppPath, "_build", "default", "lib", "emqx_mini_plugin", "ebin"])),
+
+    put(loaded_file, filename:join([DataPath, "loaded_plugins"])),
+    emqx_ct_helpers:start_apps([], fun set_sepecial_cfg/1),
+
+    Config.
+
+set_sepecial_cfg(_) ->
+    ExpandPath = filename:dirname(code:lib_dir(emqx_mini_plugin)),
+
+    application:set_env(emqx, plugins_loaded_file, get(loaded_file)),
+    application:set_env(emqx, expand_plugins_dir, ExpandPath),
+    ok.
+
+end_per_suite(_Config) ->
+    emqx_ct_helpers:stop_apps([]).
+
+t_load(_) ->
+    {error, load_app_fail} = emqx_plugins:load_expand_plugin("./not_existed_path/"),
+
+    {error, not_started} = emqx_plugins:unload(emqx_mini_plugin),
+    {ok, _} = emqx_plugins:load(emqx_mini_plugin),
+    ok = emqx_plugins:unload(emqx_mini_plugin).

+ 35 - 0
test/emqx_plugins_SUITE_data/emqx_mini_plugin/Makefile

@@ -0,0 +1,35 @@
+## shallow clone for speed
+
+REBAR_GIT_CLONE_OPTIONS += --depth 1
+export REBAR_GIT_CLONE_OPTIONS
+
+REBAR = rebar3
+all: compile
+
+compile:
+	$(REBAR) compile
+
+clean: distclean
+
+ct: compile
+	$(REBAR) as test ct -v
+
+eunit: compile
+	$(REBAR) as test eunit
+
+xref:
+	$(REBAR) xref
+
+distclean:
+	@rm -rf _build
+	@rm -f data/app.*.config data/vm.*.args rebar.lock
+
+CUTTLEFISH_SCRIPT = _build/default/lib/cuttlefish/cuttlefish
+
+$(CUTTLEFISH_SCRIPT):
+	@${REBAR} get-deps
+	@if [ ! -f cuttlefish ]; then make -C _build/default/lib/cuttlefish; fi
+
+app.config: $(CUTTLEFISH_SCRIPT) etc/emqx_mini_plugin.conf
+	$(verbose) $(CUTTLEFISH_SCRIPT) -l info -e etc/ -c etc/emqx_mini_plugin.conf -i priv/emqx_mini_plugin.schema -d data
+

+ 1 - 0
test/emqx_plugins_SUITE_data/emqx_mini_plugin/etc/emqx_mini_plugin.conf

@@ -0,0 +1 @@
+mini.name = test

+ 5 - 0
test/emqx_plugins_SUITE_data/emqx_mini_plugin/priv/emqx_mini_plugin.schema

@@ -0,0 +1,5 @@
+%%-*- mode: erlang -*-
+
+{mapping, "mini.name", "emqx_mini_plugin.name", [
+  {datatype, string}
+]}.

+ 25 - 0
test/emqx_plugins_SUITE_data/emqx_mini_plugin/rebar.config

@@ -0,0 +1,25 @@
+{deps,
+    []}.
+
+{edoc_opts, [{preprocess, true}]}.
+{erl_opts, [warn_unused_vars,
+            warn_shadow_vars,
+            warn_unused_import,
+            warn_obsolete_guard,
+            debug_info,
+            {parse_transform}]}.
+
+{xref_checks, [undefined_function_calls, undefined_functions,
+               locals_not_used, deprecated_function_calls,
+               warnings_as_errors, deprecated_functions]}.
+{cover_enabled, true}.
+{cover_opts, [verbose]}.
+{cover_export_enabled, true}.
+
+{profiles,
+    [{test, [
+        {deps, [ {emqx_ct_helper, {git, "https://github.com/emqx/emqx-ct-helpers", {tag, "v1.1.4"}}}
+               , {cuttlefish, {git, "https://github.com/emqx/cuttlefish", {tag, "v3.0.0"}}}
+               ]}
+    ]}
+]}.

+ 14 - 0
test/emqx_plugins_SUITE_data/emqx_mini_plugin/src/emqx_mini_plugin.app.src

@@ -0,0 +1,14 @@
+{application, emqx_mini_plugin,
+ [{description, "An EMQ X plugin for testcase"},
+  {vsn, "git"},
+  {modules, []},
+  {registered, []},
+  {mod, {emqx_mini_plugin_app, []}},
+  {applications,
+   [kernel,
+    stdlib
+   ]},
+  {env,[]},
+  {licenses, ["Apache 2.0"]},
+  {links, []}
+ ]}.

+ 42 - 0
test/emqx_plugins_SUITE_data/emqx_mini_plugin/src/emqx_mini_plugin_app.erl

@@ -0,0 +1,42 @@
+%%%-------------------------------------------------------------------
+%% @doc emqx_mini_plugin public API
+%% @end
+%%%-------------------------------------------------------------------
+
+-module(emqx_mini_plugin_app).
+
+-behaviour(application).
+-behaviour(supervisor).
+
+-emqx_plugin(?MODULE).
+
+%% Application APIs
+-export([ start/2
+        , stop/1
+        ]).
+
+%% Supervisor callback
+-export([init/1]).
+
+
+%% -- Application
+
+start(_StartType, _StartArgs) ->
+    {ok, Sup} = start_link(),
+    {ok, Sup}.
+
+stop(_State) ->
+    ok.
+
+%% --- Supervisor
+
+start_link() ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+init([]) ->
+    SupFlags = #{strategy => one_for_all,
+                 intensity => 0,
+                 period => 1},
+    ChildSpecs = [],
+    {ok, {SupFlags, ChildSpecs}}.
+