2017-01-03 22:27:20 -08:00
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
import (
|
2017-10-02 16:49:54 -07:00
|
|
|
"io/ioutil"
|
2017-01-03 22:27:20 -08:00
|
|
|
"os"
|
2017-10-02 16:49:54 -07:00
|
|
|
"strings"
|
2017-01-03 22:27:20 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
)
|
|
|
|
|
|
2017-10-02 16:49:54 -07:00
|
|
|
var (
|
|
|
|
|
testAppName = "test-app-1"
|
|
|
|
|
testAppDir = strings.Join([]string{"/home/dokku/", testAppName}, "")
|
|
|
|
|
testEnvFile = strings.Join([]string{testAppDir, "/ENV"}, "")
|
|
|
|
|
testEnvLine = "export testKey=TESTING"
|
2017-12-12 11:25:16 -05:00
|
|
|
testAppName2 = "01-test-app-1"
|
|
|
|
|
testAppDir2 = strings.Join([]string{"/home/dokku/", testAppName}, "")
|
|
|
|
|
testEnvFile2 = strings.Join([]string{testAppDir, "/ENV"}, "")
|
|
|
|
|
testEnvLine2 = "export testKey=TESTING"
|
2017-10-02 16:49:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 11:25:16 -05:00
|
|
|
func setupTestApp2() (err error) {
|
|
|
|
|
Expect(os.MkdirAll(testAppDir2, 0644)).To(Succeed())
|
|
|
|
|
b := []byte(testEnvLine2 + "\n")
|
|
|
|
|
if err = ioutil.WriteFile(testEnvFile2, b, 0644); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 16:49:54 -07:00
|
|
|
func teardownTestApp() {
|
|
|
|
|
os.RemoveAll(testAppDir)
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 11:25:16 -05:00
|
|
|
func teardownTestApp2() {
|
|
|
|
|
os.RemoveAll(testAppDir2)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 16:49:54 -07:00
|
|
|
func TestCommonGetEnv(t *testing.T) {
|
2017-01-03 22:27:20 -08:00
|
|
|
RegisterTestingT(t)
|
|
|
|
|
Expect(MustGetEnv("DOKKU_ROOT")).To(Equal("/home/dokku"))
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 16:49:54 -07:00
|
|
|
func TestCommonGetAppImageRepo(t *testing.T) {
|
2017-01-03 22:27:20 -08:00
|
|
|
RegisterTestingT(t)
|
|
|
|
|
Expect(GetAppImageRepo("testapp")).To(Equal("dokku/testapp"))
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 16:49:54 -07:00
|
|
|
func TestCommonVerifyImageInvalid(t *testing.T) {
|
2017-01-03 22:27:20 -08:00
|
|
|
RegisterTestingT(t)
|
|
|
|
|
Expect(VerifyImage("testapp")).To(Equal(false))
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 16:49:54 -07:00
|
|
|
func TestCommonVerifyAppNameInvalid(t *testing.T) {
|
2017-01-03 22:27:20 -08:00
|
|
|
RegisterTestingT(t)
|
|
|
|
|
err := VerifyAppName("1994testApp")
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 16:49:54 -07:00
|
|
|
func TestCommonVerifyAppName(t *testing.T) {
|
|
|
|
|
RegisterTestingT(t)
|
|
|
|
|
Expect(setupTestApp()).To(Succeed())
|
|
|
|
|
Expect(VerifyAppName(testAppName)).To(Succeed())
|
|
|
|
|
teardownTestApp()
|
2017-12-12 11:25:16 -05:00
|
|
|
|
|
|
|
|
RegisterTestingT(t)
|
|
|
|
|
Expect(setupTestApp2()).To(Succeed())
|
|
|
|
|
Expect(VerifyAppName(testAppName2)).To(Succeed())
|
|
|
|
|
teardownTestApp2()
|
2017-10-02 16:49:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCommonDokkuAppsError(t *testing.T) {
|
|
|
|
|
RegisterTestingT(t)
|
|
|
|
|
_, err := DokkuApps()
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCommonDokkuApps(t *testing.T) {
|
2017-01-03 22:27:20 -08:00
|
|
|
RegisterTestingT(t)
|
2017-10-02 16:49:54 -07:00
|
|
|
Expect(setupTestApp()).To(Succeed())
|
|
|
|
|
apps, err := DokkuApps()
|
2017-01-03 22:27:20 -08:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2017-10-02 16:49:54 -07:00
|
|
|
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))
|
2017-01-03 22:27:20 -08:00
|
|
|
}
|