From b099d2c3895860e063642b17cb37763fed108dab Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Mon, 2 Oct 2017 16:49:54 -0700 Subject: [PATCH] add more tests --- plugins/common/common_test.go | 88 +++++++++++++++++++++++++++++---- plugins/config/config_test.go | 36 ++++++++++++++ plugins/network/network_test.go | 12 +++++ 3 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 plugins/config/config_test.go create mode 100644 plugins/network/network_test.go diff --git a/plugins/common/common_test.go b/plugins/common/common_test.go index 9d9a9a5b1..535ba0f01 100644 --- a/plugins/common/common_test.go +++ b/plugins/common/common_test.go @@ -1,38 +1,106 @@ package common import ( + "io/ioutil" "os" + "strings" "testing" . "github.com/onsi/gomega" ) -func TestGetEnv(t *testing.T) { +var ( + testAppName = "test-app-1" + testAppDir = strings.Join([]string{"/home/dokku/", testAppName}, "") + testEnvFile = strings.Join([]string{testAppDir, "/ENV"}, "") + testEnvLine = "export testKey=TESTING" +) + +func setupTestApp() (err error) { + Expect(os.MkdirAll(testAppDir, 0644)).To(Succeed()) + b := []byte(testEnvLine + "\n") + if err = ioutil.WriteFile(testEnvFile, b, 0644); err != nil { + return + } + return +} + +func teardownTestApp() { + os.RemoveAll(testAppDir) +} + +func TestCommonGetEnv(t *testing.T) { RegisterTestingT(t) Expect(MustGetEnv("DOKKU_ROOT")).To(Equal("/home/dokku")) } -func TestGetAppImageRepo(t *testing.T) { +func TestCommonGetAppImageRepo(t *testing.T) { RegisterTestingT(t) Expect(GetAppImageRepo("testapp")).To(Equal("dokku/testapp")) } -func TestVerifyImageInvalid(t *testing.T) { +func TestCommonVerifyImageInvalid(t *testing.T) { RegisterTestingT(t) Expect(VerifyImage("testapp")).To(Equal(false)) } -func TestVerifyAppNameInvalid(t *testing.T) { +func TestCommonVerifyAppNameInvalid(t *testing.T) { RegisterTestingT(t) err := VerifyAppName("1994testApp") Expect(err).To(HaveOccurred()) } -func TestVerifyAppName(t *testing.T) { +func TestCommonVerifyAppName(t *testing.T) { RegisterTestingT(t) - dir := "/home/dokku/testApp" - os.MkdirAll(dir, 0644) - err := VerifyAppName("testApp") - Expect(err).NotTo(HaveOccurred()) - os.RemoveAll(dir) + Expect(setupTestApp()).To(Succeed()) + Expect(VerifyAppName(testAppName)).To(Succeed()) + teardownTestApp() +} + +func TestCommonDokkuAppsError(t *testing.T) { + RegisterTestingT(t) + _, err := DokkuApps() + Expect(err).To(HaveOccurred()) +} + +func TestCommonDokkuApps(t *testing.T) { + RegisterTestingT(t) + Expect(setupTestApp()).To(Succeed()) + apps, err := DokkuApps() + Expect(err).NotTo(HaveOccurred()) + Expect(apps).To(HaveLen(1)) + Expect(apps[0]).To(Equal(testAppName)) + teardownTestApp() +} + +func TestCommonFileToSlice(t *testing.T) { + RegisterTestingT(t) + Expect(setupTestApp()).To(Succeed()) + lines, err := FileToSlice(testEnvFile) + Expect(err).NotTo(HaveOccurred()) + Expect(lines).To(Equal([]string{testEnvLine})) + teardownTestApp() +} + +func TestCommonFileExists(t *testing.T) { + RegisterTestingT(t) + Expect(setupTestApp()).To(Succeed()) + Expect(FileExists(testEnvFile)).To(BeTrue()) + teardownTestApp() +} + +func TestCommonReadFirstLine(t *testing.T) { + RegisterTestingT(t) + line := ReadFirstLine(testEnvFile) + Expect(line).To(Equal("")) + Expect(setupTestApp()).To(Succeed()) + line = ReadFirstLine(testEnvFile) + Expect(line).To(Equal(testEnvLine)) + teardownTestApp() +} + +func TestCommonStripInlineComments(t *testing.T) { + RegisterTestingT(t) + text := StripInlineComments(strings.Join([]string{testEnvLine, "# testing comment"}, " ")) + Expect(text).To(Equal(testEnvLine)) } diff --git a/plugins/config/config_test.go b/plugins/config/config_test.go new file mode 100644 index 000000000..dbe660065 --- /dev/null +++ b/plugins/config/config_test.go @@ -0,0 +1,36 @@ +package config + +import ( + "io/ioutil" + "os" + "strings" + "testing" + + . "github.com/onsi/gomega" +) + +var ( + testAppName = "test-app-1" + testAppDir = strings.Join([]string{"/home/dokku/", testAppName}, "") +) + +func setupTestApp() (err error) { + Expect(os.MkdirAll(testAppDir, 0644)).To(Succeed()) + b := []byte("export testKey=TESTING\n") + if err = ioutil.WriteFile(strings.Join([]string{testAppDir, "/ENV"}, ""), b, 0644); err != nil { + return + } + return +} + +func teardownTestApp() { + os.RemoveAll(testAppDir) +} + +func TestConfigGetWithDefault(t *testing.T) { + RegisterTestingT(t) + Expect(setupTestApp()).To(Succeed()) + Expect(GetWithDefault(testAppName, "unknownKey", "UNKNOWN")).To(Equal("UNKNOWN")) + Expect(GetWithDefault(testAppName, "testKey", "testKey")).To(Equal("TESTING")) + teardownTestApp() +} diff --git a/plugins/network/network_test.go b/plugins/network/network_test.go new file mode 100644 index 000000000..9aed75050 --- /dev/null +++ b/plugins/network/network_test.go @@ -0,0 +1,12 @@ +package network + +import ( + "testing" + + . "github.com/onsi/gomega" +) + +func TestNetworkGetDefaultValue(t *testing.T) { + RegisterTestingT(t) + Expect(GetDefaultValue("bind-all-interfaces")).To(Equal("false")) +}