Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-1944

Variable in called bundle retains its value from previous call when attempting to assign null value

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Rejected
    • Priority: Low
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Promise type: methods
    • Labels:
      None
    • Story Points:
      8

      Description

      DISCLAIMER I am a novice CFEngine user, so I may be ignorant of some CFEngine behaviour that would explain what I am observing.

      Summary

      A MAIN bundle calls a SUB bundle (via @usebundle@) with a reference to an Object containing one or more attributes.
      In the SUB bundle, the value of the passed-in Object's attributes are assigned to local variables.
      If an attribute's value is null, the local variable retains the value it was assigned during the previous @usebundle@ call, unless this is the first call, in which case the variable is assigned the string "null".

      Version/Platform

      <pre>
      root@x# cf-agent -V
      CFEngine Core 3.6.5

      root@x# uname -a
      Linux x 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u1 x86_64 GNU/Linux
      </pre>

      The test program (also attached).

      <pre>
      root@x# cat ./test_usebundle_var_init.cf
      ###############################################################################
      body file control
      {
      inputs =>

      { "$(sys.libdir)/common.cf" }

      ;
      }

      ###############################################################################
      body common control
      {
      bundlesequence =>

      { "main" }

      ;
      }

      ###############################################################################
      bundle agent main
      {
      vars:
      "d1" data => parsejson('[

      { "x":null }

      ,

      { "x":"a" }

      ,

      { "x":"b" }

      ,

      { "x":null }

      ,

      { "x":"d" }

      ,

      { "x":null }

      ,

      { "x":null }

      ,

      { "x":"g" }

      ,

      { "x":"" }

      ,

      { "x":"i" }

      ,

      { "x":null }

      ]');

      "d1_keys" slist => getindices("d1");

      methods:
      "any" usebundle => sub("d1", "$(this.bundle).d1[$(d1_keys)]", "$(d1_keys)");

      }

      ###############################################################################
      bundle agent sub(container, object, key)
      {
      vars:
      "obj_toString" string => format("%S", "$(object)");
      "x_var" string => "$($(object)[x])";

      reports:
      "$(this.bundle): $(container)[$(key)]: -----------------------------------------------";
      "$(this.bundle): $(container)[$(key)]: obj_toString = '$(obj_toString)'";
      "$(this.bundle): $(container)[$(key)]: x_var = '$(x_var)'";
      "$(this.bundle): $(container)[$(key)]: x_var = '$(x_var)', x_ref = '$($(object)[x])'";
      }
      </pre>

      The results

      <pre>
      root@x# cf-agent -KI -f ./test_usebundle_var_init.cf
      R: sub: d1[0]: -----------------------------------------------
      R: sub: d1[0]: obj_toString = '

      {"x":null}

      '
      R: sub: d1[0]: x_var = 'null'
      R: sub: d1[1]: -----------------------------------------------
      R: sub: d1[1]: obj_toString = '

      {"x":"a"}

      '
      R: sub: d1[1]: x_var = 'a'
      R: sub: d1[1]: x_var = 'a', x_ref = 'a'
      R: sub: d1[2]: -----------------------------------------------
      R: sub: d1[2]: obj_toString = '

      {"x":"b"}

      '
      R: sub: d1[2]: x_var = 'b'
      R: sub: d1[2]: x_var = 'b', x_ref = 'b'
      R: sub: d1[3]: -----------------------------------------------
      R: sub: d1[3]: obj_toString = '

      {"x":null}

      '
      R: sub: d1[3]: x_var = 'b'
      R: sub: d1[4]: -----------------------------------------------
      R: sub: d1[4]: obj_toString = '

      {"x":"d"}

      '
      R: sub: d1[4]: x_var = 'd'
      R: sub: d1[4]: x_var = 'd', x_ref = 'd'
      R: sub: d1[5]: -----------------------------------------------
      R: sub: d1[5]: obj_toString = '

      {"x":null}

      '
      R: sub: d1[5]: x_var = 'd'
      R: sub: d1[6]: -----------------------------------------------
      R: sub: d1[6]: obj_toString = '

      {"x":null}

      '
      R: sub: d1[6]: x_var = 'd'
      R: sub: d1[7]: -----------------------------------------------
      R: sub: d1[7]: obj_toString = '

      {"x":"g"}

      '
      R: sub: d1[7]: x_var = 'g'
      R: sub: d1[7]: x_var = 'g', x_ref = 'g'
      R: sub: d1[8]: -----------------------------------------------
      R: sub: d1[8]: obj_toString = '

      {"x":""}

      '
      R: sub: d1[8]: x_var = ''
      R: sub: d1[8]: x_var = '', x_ref = ''
      R: sub: d1[9]: -----------------------------------------------
      R: sub: d1[9]: obj_toString = '

      {"x":"i"}

      '
      R: sub: d1[9]: x_var = 'i'
      R: sub: d1[9]: x_var = 'i', x_ref = 'i'
      R: sub: d1[10]: -----------------------------------------------
      R: sub: d1[10]: obj_toString = '

      {"x":null}

      '
      R: sub: d1[10]: x_var = 'i'
      </pre>

      Remarks

      1. For the initial call (i.e., @d[0]@), x_var is assigned the string "null" when assigned the null value.
      2. When subsequent null values are encountered (i.e., @d[3]@, @d[5]@, @d[6]@, @d[10]@), x_var retains the value it was assigned during the preceding @usebundle@ call.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              a10042 Nick Anderson
              Reporter:
              stdenisg Guy St-Denis
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: