garethr / kubetest

Write unit tests for your Kubernetes configurations
Other
309 stars 41 forks source link

ideas for test structure / DRYness? #12

Open jsleeio opened 6 years ago

jsleeio commented 6 years ago

So I'm running a test like the below, because I want to ensure anything deployed to our production clusters has resources specs in it. Unfortunately if I write it like this and a dev runs the testsuite on a definition lacking any resources at all, it'll give them this:

FATA key "resources" not in dict

... and then stop. I would like to have all the test failures show up the first time they run the tests, but without having to to break it up into many separate functions (and then add even more if I decide to require CPU resources as well, which is likely)

any suggestions? I'm open to hacking on kubetest itself if that's the best option

def test_resources():
    containers = []
    if spec["kind"] == "Deployment":
        containers = spec["spec"]["template"]["spec"]["containers"]
    if spec["kind"] == "Pod":
        containers = spec["spec"]["containers"]
    if len(containers) > 0:
      for container in containers:
          assert_contains(container, "resources", "must include a resources section")
          assert_contains(container["resources"], "requests", "must include a resources requests section")
          assert_contains(container["resources"], "limits", "must include a resources requests section")
          assert_contains(container["resources"]["requests"], "memory", "must include a memory value in resources requests section")
          assert_contains(container["resources"]["limits"], "memory", "must include a memory value in resources requests section")
          assert_not_equal(container["resources"]["requests"]["memory"], container["resources"]["limits"]["memory"], "memory resource requests+limits must not be identical")
jsleeio commented 6 years ago

For now I've worked around some of the issues here by splitting up all the separate tests (lots of repetition) and conditionally executing tests to avoid fatal errors. This is an improvement... not great though