diff --git a/cf-agent/verify_methods.c b/cf-agent/verify_methods.c index 38031e41c5..0c752f3d2b 100644 --- a/cf-agent/verify_methods.c +++ b/cf-agent/verify_methods.c @@ -128,11 +128,11 @@ PromiseResult VerifyMethod(EvalContext *ctx, const Rval call, const Attributes * PromiseBanner(ctx, pp); - const Bundle *bp = EvalContextResolveBundleExpression(ctx, PromiseGetPolicy(pp), BufferData(method_name), "agent"); + Bundle *bp = (Bundle *) EvalContextResolveBundleExpression(ctx, PromiseGetPolicy(pp), BufferData(method_name), "agent"); if (!bp) { - bp = EvalContextResolveBundleExpression(ctx, PromiseGetPolicy(pp), BufferData(method_name), "common"); + bp = (Bundle *) EvalContextResolveBundleExpression(ctx, PromiseGetPolicy(pp), BufferData(method_name), "common"); } PromiseResult result = PROMISE_RESULT_NOOP; @@ -169,6 +169,7 @@ PromiseResult VerifyMethod(EvalContext *ctx, const Rval call, const Attributes * BundleResolve(ctx, bp); + bp->calling_bundle = (Bundle *) PromiseGetBundle(pp); result = ScheduleAgentOperations(ctx, bp); GetReturnValue(ctx, bp, pp); diff --git a/libpromises/eval_context.c b/libpromises/eval_context.c index a611e28c09..f795b1d015 100644 --- a/libpromises/eval_context.c +++ b/libpromises/eval_context.c @@ -1566,6 +1566,12 @@ void EvalContextStackPushPromiseFrame(EvalContext *ctx, const Promise *owner) EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "bundle", PromiseGetBundle(owner)->name, CF_DATA_TYPE_STRING, "source=promise"); EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "namespace", PromiseGetNamespace(owner), CF_DATA_TYPE_STRING, "source=promise"); + if (PromiseGetBundle(owner)->calling_bundle != NULL) + { + char *calling_bundle_name = StringFormat("%s:%s", PromiseGetBundle(owner)->calling_bundle->ns, PromiseGetBundle(owner)->calling_bundle->name); + EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "calling_bundle", calling_bundle_name, CF_DATA_TYPE_STRING, "source=promise"); + free(calling_bundle_name); + } // Recompute `with` for (size_t i = 0; i < SeqLength(owner->conlist); i++) diff --git a/libpromises/policy.c b/libpromises/policy.c index 7fc2dd0722..110926b48b 100644 --- a/libpromises/policy.c +++ b/libpromises/policy.c @@ -1324,6 +1324,7 @@ Bundle *PolicyAppendBundle(Policy *policy, bundle->custom_sections = SeqNew(10, BundleSectionDestroy); bundle->all_promises = SeqNew(10, NULL); bundle->evaluation_order = evaluation_order; + bundle->calling_bundle = NULL; return bundle; } diff --git a/libpromises/policy.h b/libpromises/policy.h index 24828fd569..14c7acbd83 100644 --- a/libpromises/policy.h +++ b/libpromises/policy.h @@ -71,6 +71,7 @@ typedef struct struct Bundle_ { Policy *parent_policy; + struct Bundle_ *calling_bundle; char *type; char *name; diff --git a/tests/acceptance/01_vars/01_basic/calling_bundle.cf b/tests/acceptance/01_vars/01_basic/calling_bundle.cf new file mode 100644 index 0000000000..2fde108f14 --- /dev/null +++ b/tests/acceptance/01_vars/01_basic/calling_bundle.cf @@ -0,0 +1,65 @@ +# Test $(this.calling_bundle) + +body common control +{ + inputs => { "../../default.cf.sub" }; + bundlesequence => { default("$(this.promise_filename)") }; +} + +####################################################### + +bundle agent init +{ + methods: + "a" + usebundle => new_namespace:A, + useresult => "return_var"; +} + +####################################################### + +bundle agent check +{ + vars: + "expected" + string => "default:init/new_namespace:A/B"; + + classes: + "ok" + expression => strcmp("$(init.return_var[1])", "$(expected)"); + + reports: + DEBUG:: + "$(this.promise_filename) expected dirname $(init.return_var[1]), actual $(expected)"; + + ok:: + "$(this.promise_filename) Pass"; + !ok:: + "$(this.promise_filename) FAIL"; +} + +body file control +{ + namespace => "new_namespace"; +} + + +bundle agent A +{ + methods: + "b" + usebundle => new_namespace:B, + useresult => "return_var"; + + reports: + "$(this.calling_bundle)/$(return_var[1])" + bundle_return_value_index => "1"; + +} + +bundle agent B +{ + reports: + "$(this.calling_bundle)/$(this.bundle)" + bundle_return_value_index => "1"; +}