add more tests

This commit is contained in:
Michael Hobbs committed 2017-10-02 16:49:54 -07:00
1 parent c9ea884e5d
commit b099d2c389
3 files changed
+125 -9

No files matched your search

+77 -9
View File
@@ -1,38 +1,106 @@
package common package common
import ( import (
"io/ioutil"
"os" "os"
"strings"
"testing" "testing"
. "github.com/onsi/gomega" . "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) RegisterTestingT(t)
Expect(MustGetEnv("DOKKU_ROOT")).To(Equal("/home/dokku")) Expect(MustGetEnv("DOKKU_ROOT")).To(Equal("/home/dokku"))
} }
func TestGetAppImageRepo(t *testing.T) { func TestCommonGetAppImageRepo(t *testing.T) {
RegisterTestingT(t) RegisterTestingT(t)
Expect(GetAppImageRepo("testapp")).To(Equal("dokku/testapp")) Expect(GetAppImageRepo("testapp")).To(Equal("dokku/testapp"))
} }
func TestVerifyImageInvalid(t *testing.T) { func TestCommonVerifyImageInvalid(t *testing.T) {
RegisterTestingT(t) RegisterTestingT(t)
Expect(VerifyImage("testapp")).To(Equal(false)) Expect(VerifyImage("testapp")).To(Equal(false))
} }
func TestVerifyAppNameInvalid(t *testing.T) { func TestCommonVerifyAppNameInvalid(t *testing.T) {
RegisterTestingT(t) RegisterTestingT(t)
err := VerifyAppName("1994testApp") err := VerifyAppName("1994testApp")
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
} }
func TestVerifyAppName(t *testing.T) { func TestCommonVerifyAppName(t *testing.T) {
RegisterTestingT(t) RegisterTestingT(t)
dir := "/home/dokku/testApp" Expect(setupTestApp()).To(Succeed())
os.MkdirAll(dir, 0644) Expect(VerifyAppName(testAppName)).To(Succeed())
err := VerifyAppName("testApp") 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(err).NotTo(HaveOccurred())
os.RemoveAll(dir) 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))
} }
+36
View File
@@ -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()
}
+12
View File
@@ -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"))
}