Wednesday 30 June 2021

Remove random parts of an object (Chaos Monkey Style)

I have a JavaScript object e.g.:

const testFixture = {
  a: [
    {b:1},
    {b:2},
    {b:3},
  ],
  b: {c: {d: 44, e: "foo", f: [1,2,3]}}
  c: 3,
  d: false,
  f: "Blah",
}

I'd like to have a function I could pass this object to that would mutate it to remove random properties from it, so that I can test whether the thing that uses this object displays an error state, rather than silently erroring.


Edit:

To be clear, I mean any deeply nested property. e.g. it might remove a.b.c.d.e.f[1] or a[2].b


Edit 2:

Here's a buggy solution I'm working on based on ideas from Eureka and mkaatman's answers.

It seems to be changing key names to "undefined" which I wasn't expecting. It's also changing numbers to {} which I wasn't expecting. Not sure why.

var testFixture2 = {
  a: [{
      b: 1, c: 2
    },
    {
      b: 2, c: 2
    },
    {
      b: 3, c: 2, d: "bar"
    },
  ],
  b: {
    c: {
      d: 44,
      e: "foo",
      f: [1, 2, 3]
    }
  },
  c: 3,
  d: false,
  f: "Blah"
};


function getRandomIndex(max) {
  return Math.floor(Math.random() * max);
}

function chaosMonkey(thing) {
  if (typeof thing === "object") {
    console.log("object", Object.keys(thing).length, thing);
    const newlyDeformedObject = { ...thing};
    // Make a list of all the keys
    const keys = Object.keys(thing);
    // Choose one at random
    const iKey = getRandomIndex(keys.length);
    let target = newlyDeformedObject[keys[iKey]];
  
    const shouldDelete = getRandomIndex(3) === 0;
    if (shouldDelete) {
      delete target;
      console.log("Object deleted", keys[iKey]);
    } else {
     console.log("+++ Going deeper", thing);
      newlyDeformedObject[keys[iKey]] = chaosMonkey({ ...newlyDeformedObject[keys[iKey]] });
    }
    return newlyDeformedObject;
  } else if (typeof thing === "array") {
    console.log(array);
    const iKey = getRandomIndex(thing.length);
    const shouldDelete = getRandomIndex(3) === 0;
    if (shouldDelete) {
      delete array[iKey];
      console.log("Array deleted", iKey);
    } else {
      array[iKey] = chaosMonkey(array[iKey]);
      return array;
    }
  } else {
    //@todo do something bad based on type e.g. number -> NaN, string -> '', but these are less likely to break something
    delete thing;
    return;
  }
}

console.log(JSON.stringify(chaosMonkey(testFixture2), null, 2));

NB: the chances of any object key or array item being recursed into are equal, in order to make modifications equally likely anywhere in the object.


Edit 3:

Additional Requirement:

  • It MUST always remove at least one thing.

Bonus points for:

  1. ways to control the number of things that get deleted

  2. any way to limit which properties get deleted or recursed into. i.e. allow/deny lists, where:

    • allowRemovalList = properties that it's ok to remove
    • denyRemovalList = properties that it's not ok to remove

(It could be that you have some properties that it's ok to remove entirely, but they should not be recursed into and inner parts of them removed.)

NB: Originally I asked for whitelist/blacklist but this caused confusion (and I wouldn't want anyone copying this code to be surprised when they use it) and some answers have implemented it so that blacklist = properties to always remove. I won't penalise any answer for that (and it's trivial to change anyway).



from Remove random parts of an object (Chaos Monkey Style)

No comments:

Post a Comment