From 65faae67828fb3eb3eac05b582aae9f9d1dea51c Mon Sep 17 00:00:00 2001 From: Steve Milner Date: Mon, 18 Sep 2017 16:23:12 -0400 Subject: [PATCH] test: Add libkpod config tests - config_test.go for testing libkpod/config.go - testdata/config.toml as a fixture for config_test.go Signed-off-by: Steve Milner --- libkpod/config_test.go | 54 ++++++++++++++++++++++++++++++++++++ libkpod/testdata/config.toml | 28 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 libkpod/config_test.go create mode 100644 libkpod/testdata/config.toml diff --git a/libkpod/config_test.go b/libkpod/config_test.go new file mode 100644 index 00000000..e6820d3c --- /dev/null +++ b/libkpod/config_test.go @@ -0,0 +1,54 @@ +package libkpod + +import ( + "io/ioutil" + "os" + "testing" +) + +// TestConfigToFile ensures Config.ToFile(..) encodes and writes out +// a Config instance toa a file on disk. +func TestConfigToFile(t *testing.T) { + // Test with a default configuration + c := DefaultConfig() + tmpfile, err := ioutil.TempFile("", "config") + if err != nil { + t.Fatalf("Unable to create temporary file: %+v", err) + } + // Clean up temporary file + defer os.Remove(tmpfile.Name()) + + // Make the ToFile calls + err = c.ToFile(tmpfile.Name()) + // Make sure no errors occurred while populating the file + if err != nil { + t.Fatalf("Unable to write to temporary file: %+v", err) + } + + // Make sure the file is on disk + if _, err := os.Stat(tmpfile.Name()); os.IsNotExist(err) { + t.Fatalf("The config file was not written to disk: %+v", err) + } +} + +// TestConfigUpdateFromFile ensures Config.UpdateFromFile(..) properly +// updates an already create Config instancec with new data. +func TestConfigUpdateFromFile(t *testing.T) { + // Test with a default configuration + c := DefaultConfig() + // Make the ToFile calls + err := c.UpdateFromFile("testdata/config.toml") + // Make sure no errors occurred while populating from the file + if err != nil { + t.Fatalf("Unable update config from file: %+v", err) + } + + // Check fields that should have changed after UpdateFromFile + if c.Storage != "overlay2" { + t.Fatalf("Update failed. Storage did not change to overlay2") + } + + if c.RuntimeConfig.PidsLimit != 2048 { + t.Fatalf("Update failed. RuntimeConfig.PidsLimit did not change to 2048") + } +} diff --git a/libkpod/testdata/config.toml b/libkpod/testdata/config.toml new file mode 100644 index 00000000..31827367 --- /dev/null +++ b/libkpod/testdata/config.toml @@ -0,0 +1,28 @@ +[crio] + root = "/var/lib/containers/storage" + runroot = "/var/run/containers/storage" + storage_driver = "overlay2" + log_dir = "/var/log/crio/pods" + file_locking = true + [crio.runtime] + runtime = "/usr/bin/runc" + runtime_untrusted_workload = "" + default_workload_trust = "trusted" + conmon = "/usr/local/libexec/crio/conmon" + conmon_env = ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"] + selinux = true + seccomp_profile = "/etc/crio/seccomp.json" + apparmor_profile = "crio-default" + cgroup_manager = "cgroupfs" + hooks_dir_path = "/usr/share/containers/oci/hooks.d" + pids_limit = 2048 + container_exits_dir = "/var/run/kpod/exits" + [crio.image] + default_transport = "docker://" + pause_image = "kubernetes/pause" + pause_command = "/pause" + signature_policy = "" + image_volumes = "mkdir" + [crio.network] + network_dir = "/etc/cni/net.d/" + plugin_dir = "/opt/cni/bin/"