2017-01-03 22:27:20 -08:00
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"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 (
|
2020-10-10 19:15:55 -04:00
|
|
|
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"
|
2017-12-12 11:25:46 -05:00
|
|
|
testAppDir2 = strings.Join([]string{"/home/dokku/", testAppName2}, "")
|
|
|
|
|
testEnvFile2 = strings.Join([]string{testAppDir2, "/ENV"}, "")
|
2017-12-12 11:25:16 -05:00
|
|
|
testEnvLine2 = "export testKey=TESTING"
|
2017-10-02 16:49:54 -07:00
|
|
|
)
|
|
|
|
|
|
2022-05-28 12:22:11 -04:00
|
|
|
func setupTests() (err error) {
|
2022-10-22 15:56:23 -04:00
|
|
|
if err := os.Setenv("PLUGIN_PATH", "/var/lib/dokku/plugins"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 12:22:11 -04:00
|
|
|
return os.Setenv("PLUGIN_ENABLED_PATH", "/var/lib/dokku/plugins/enabled")
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 16:49:54 -07:00
|
|
|
func setupTestApp() (err error) {
|
2024-12-03 21:45:41 -05:00
|
|
|
Expect(os.MkdirAll(testAppDir, 0766)).To(Succeed())
|
2017-10-02 16:49:54 -07:00
|
|
|
b := []byte(testEnvLine + "\n")
|
2023-12-22 01:59:22 +08:00
|
|
|
if err = os.WriteFile(testEnvFile, b, 0644); err != nil {
|
2017-10-02 16:49:54 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 11:25:16 -05:00
|
|
|
func setupTestApp2() (err error) {
|
2024-12-03 21:45:41 -05:00
|
|
|
Expect(os.MkdirAll(testAppDir2, 0766)).To(Succeed())
|
2017-12-12 11:25:16 -05:00
|
|
|
b := []byte(testEnvLine2 + "\n")
|
2023-12-22 01:59:22 +08:00
|
|
|
if err = os.WriteFile(testEnvFile2, b, 0644); err != nil {
|
2017-12-12 11:25:16 -05:00
|
|
|
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)
|
2022-05-28 12:22:11 -04:00
|
|
|
Expect(setupTests()).To(Succeed())
|
2017-01-03 22:27:20 -08:00
|
|
|
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)
|
2022-05-28 12:22:11 -04:00
|
|
|
Expect(setupTests()).To(Succeed())
|
2017-01-03 22:27:20 -08:00
|
|
|
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)
|
2022-05-28 12:22:11 -04:00
|
|
|
Expect(setupTests()).To(Succeed())
|
2017-01-03 22:27:20 -08:00
|
|
|
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)
|
2022-05-28 12:22:11 -04:00
|
|
|
Expect(setupTests()).To(Succeed())
|
|
|
|
|
Expect(VerifyAppName("1994testApp")).To(HaveOccurred())
|
2017-01-03 22:27:20 -08:00
|
|
|
}
|
|
|
|
|
|
2017-10-02 16:49:54 -07:00
|
|
|
func TestCommonVerifyAppName(t *testing.T) {
|
|
|
|
|
RegisterTestingT(t)
|
2022-05-28 12:22:11 -04:00
|
|
|
Expect(setupTests()).To(Succeed())
|
2017-10-02 16:49:54 -07:00
|
|
|
Expect(setupTestApp()).To(Succeed())
|
|
|
|
|
Expect(VerifyAppName(testAppName)).To(Succeed())
|
|
|
|
|
teardownTestApp()
|
2017-12-12 11:25:16 -05:00
|
|
|
|
|
|
|
|
RegisterTestingT(t)
|
2022-05-28 12:22:11 -04:00
|
|
|
Expect(setupTests()).To(Succeed())
|
2017-12-12 11:25:16 -05:00
|
|
|
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)
|
2022-05-28 12:22:11 -04:00
|
|
|
Expect(setupTests()).To(Succeed())
|
2017-10-02 16:49:54 -07:00
|
|
|
_, err := DokkuApps()
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCommonDokkuApps(t *testing.T) {
|
2017-01-03 22:27:20 -08:00
|
|
|
RegisterTestingT(t)
|
2022-05-28 12:22:11 -04:00
|
|
|
Expect(setupTests()).To(Succeed())
|
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 TestCommonStripInlineComments(t *testing.T) {
|
|
|
|
|
RegisterTestingT(t)
|
2022-05-28 12:22:11 -04:00
|
|
|
Expect(setupTests()).To(Succeed())
|
2017-10-02 16:49:54 -07:00
|
|
|
text := StripInlineComments(strings.Join([]string{testEnvLine, "# testing comment"}, " "))
|
|
|
|
|
Expect(text).To(Equal(testEnvLine))
|
2017-01-03 22:27:20 -08:00
|
|
|
}
|