264 lines
6.4 KiB
Go
264 lines
6.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"golang.org/x/net/html"
|
||
|
)
|
||
|
|
||
|
func TestIntegrationReloadRules(t *testing.T) {
|
||
|
if testing.Short() {
|
||
|
t.Skip("skipping reload rules integration test")
|
||
|
}
|
||
|
|
||
|
c := &Config{}
|
||
|
|
||
|
tmpDir := t.TempDir()
|
||
|
|
||
|
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
|
||
|
tempConfigDir := fmt.Sprintf("%s/tmp/config", tmpDir)
|
||
|
|
||
|
confFile := fmt.Sprintf("%s/config.json", tempConfigDir)
|
||
|
rulesFile := fmt.Sprintf("%s/rules.json", tempConfigDir)
|
||
|
lFp := fmt.Sprintf("%s/app.log", tempLogsDir)
|
||
|
|
||
|
mkDirForTest(t, tempConfigDir)
|
||
|
mkDirForTest(t, tempLogsDir)
|
||
|
|
||
|
rulesJsonFp := "testData/rules.json"
|
||
|
cpFileForTest(t, rulesJsonFp, rulesFile)
|
||
|
|
||
|
p := ParsedConf{
|
||
|
RulesFp: rulesFile,
|
||
|
LogFp: lFp,
|
||
|
Port: "9050",
|
||
|
Compression: true,
|
||
|
SizeToRotate: "5MB",
|
||
|
}
|
||
|
jsonString, _ := json.Marshal(p)
|
||
|
writeForTest(t, confFile, jsonString)
|
||
|
|
||
|
_, err := c.LoadMainConfigFile(confFile)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
l, err := newFileLogger(p.LogFp, p.SizeToRotate, p.Compression)
|
||
|
assert.NotEmpty(t, l)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
isEmpty := isFileEmpty(t, lFp)
|
||
|
assert.Equal(t, isEmpty, true)
|
||
|
|
||
|
app := newTestApp(t, c, l)
|
||
|
app.Routes()
|
||
|
ts := newTestServer(t, app.Mux)
|
||
|
defer ts.Close()
|
||
|
|
||
|
code, _, body := ts.get(t, "/reloadRules")
|
||
|
assert.Equal(t, code, http.StatusOK)
|
||
|
assert.Equal(t, string(body), "ok")
|
||
|
|
||
|
isEmpty = isFileEmpty(t, lFp)
|
||
|
assert.Equal(t, isEmpty, true)
|
||
|
|
||
|
oldRules := app.Config.MappingRules.Mappings
|
||
|
assert.NotEmpty(t, oldRules)
|
||
|
|
||
|
nRulesJsonFp := "testData/rules2.json"
|
||
|
cpFileForTest(t, nRulesJsonFp, rulesFile)
|
||
|
|
||
|
code, _, body = ts.get(t, "/reloadRules")
|
||
|
assert.Equal(t, code, http.StatusOK)
|
||
|
assert.Equal(t, string(body), "ok")
|
||
|
|
||
|
isEmpty = isFileEmpty(t, lFp)
|
||
|
assert.Equal(t, isEmpty, true)
|
||
|
|
||
|
newRules := app.Config.MappingRules.Mappings
|
||
|
assert.NotEmpty(t, newRules)
|
||
|
|
||
|
assert.NotEqualValues(t, oldRules, newRules)
|
||
|
}
|
||
|
|
||
|
func TestIntegrationServeRules(t *testing.T) {
|
||
|
if testing.Short() {
|
||
|
t.Skip("skipping serve rules integration test")
|
||
|
}
|
||
|
|
||
|
t.Run("return default html if go-get is missing", func(t *testing.T) {
|
||
|
c := &Config{}
|
||
|
|
||
|
tmpDir := t.TempDir()
|
||
|
|
||
|
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
|
||
|
tempConfigDir := fmt.Sprintf("%s/tmp/config", tmpDir)
|
||
|
|
||
|
confFile := fmt.Sprintf("%s/config.json", tempConfigDir)
|
||
|
rulesFile := fmt.Sprintf("%s/rules.json", tempConfigDir)
|
||
|
lFp := fmt.Sprintf("%s/app.log", tempLogsDir)
|
||
|
|
||
|
mkDirForTest(t, tempConfigDir)
|
||
|
mkDirForTest(t, tempLogsDir)
|
||
|
|
||
|
rulesJsonFp := "testData/rules2.json"
|
||
|
cpFileForTest(t, rulesJsonFp, rulesFile)
|
||
|
|
||
|
p := ParsedConf{
|
||
|
RulesFp: rulesFile,
|
||
|
LogFp: lFp,
|
||
|
Port: "9050",
|
||
|
Compression: true,
|
||
|
SizeToRotate: "5MB",
|
||
|
}
|
||
|
jsonString, _ := json.Marshal(p)
|
||
|
writeForTest(t, confFile, jsonString)
|
||
|
|
||
|
_, err := c.LoadMainConfigFile(confFile)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
err = c.LoadMappingFile(p.RulesFp)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
l, err := newFileLogger(p.LogFp, p.SizeToRotate, p.Compression)
|
||
|
assert.NotEmpty(t, l)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
isEmpty := isFileEmpty(t, lFp)
|
||
|
assert.Equal(t, isEmpty, true)
|
||
|
|
||
|
app := newTestApp(t, c, l)
|
||
|
app.Routes()
|
||
|
|
||
|
ts := newTestServer(t, app.Mux)
|
||
|
defer ts.Close()
|
||
|
|
||
|
code, _, body := ts.get(t, "/touche")
|
||
|
assert.Equal(t, code, http.StatusOK)
|
||
|
expected, _ := getDefaultHtml()
|
||
|
assert.Equal(t, string(body), string(expected))
|
||
|
|
||
|
isEmpty = isFileEmpty(t, lFp)
|
||
|
assert.Equal(t, isEmpty, false)
|
||
|
|
||
|
})
|
||
|
|
||
|
t.Run("return normal html if go-get is included and the package exists", func(t *testing.T) {
|
||
|
c := &Config{}
|
||
|
|
||
|
tmpDir := t.TempDir()
|
||
|
|
||
|
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
|
||
|
tempConfigDir := fmt.Sprintf("%s/tmp/config", tmpDir)
|
||
|
|
||
|
confFile := fmt.Sprintf("%s/config.json", tempConfigDir)
|
||
|
rulesFile := fmt.Sprintf("%s/rules.json", tempConfigDir)
|
||
|
lFp := fmt.Sprintf("%s/app.log", tempLogsDir)
|
||
|
|
||
|
mkDirForTest(t, tempConfigDir)
|
||
|
mkDirForTest(t, tempLogsDir)
|
||
|
|
||
|
rulesJsonFp := "testData/rules3.json"
|
||
|
cpFileForTest(t, rulesJsonFp, rulesFile)
|
||
|
|
||
|
p := ParsedConf{
|
||
|
RulesFp: rulesFile,
|
||
|
LogFp: lFp,
|
||
|
Port: "9050",
|
||
|
Compression: true,
|
||
|
SizeToRotate: "5MB",
|
||
|
}
|
||
|
jsonString, _ := json.Marshal(p)
|
||
|
writeForTest(t, confFile, jsonString)
|
||
|
|
||
|
_, err := c.LoadMainConfigFile(confFile)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
err = c.LoadMappingFile(p.RulesFp)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
l, err := newFileLogger(p.LogFp, p.SizeToRotate, p.Compression)
|
||
|
assert.NotEmpty(t, l)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
isEmpty := isFileEmpty(t, lFp)
|
||
|
assert.Equal(t, isEmpty, true)
|
||
|
|
||
|
app := newTestApp(t, c, l)
|
||
|
app.Routes()
|
||
|
ts := newTestServer(t, app.Mux)
|
||
|
defer ts.Close()
|
||
|
|
||
|
code, _, body := ts.get(t, "/x/touche?go-get=1")
|
||
|
assert.Equal(t, code, http.StatusOK)
|
||
|
assert.NotEmpty(t, string(body))
|
||
|
|
||
|
_, err = html.Parse(strings.NewReader(string(body)))
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
t.Logf("Printing returned html => %s", string(body))
|
||
|
isEmpty = isFileEmpty(t, lFp)
|
||
|
assert.Equal(t, isEmpty, false)
|
||
|
})
|
||
|
|
||
|
t.Run("return default html if go-get is included and the package does not exists", func(t *testing.T) {
|
||
|
c := &Config{}
|
||
|
|
||
|
tmpDir := t.TempDir()
|
||
|
|
||
|
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
|
||
|
tempConfigDir := fmt.Sprintf("%s/tmp/config", tmpDir)
|
||
|
|
||
|
confFile := fmt.Sprintf("%s/config.json", tempConfigDir)
|
||
|
rulesFile := fmt.Sprintf("%s/rules.json", tempConfigDir)
|
||
|
lFp := fmt.Sprintf("%s/app.log", tempLogsDir)
|
||
|
|
||
|
mkDirForTest(t, tempConfigDir)
|
||
|
mkDirForTest(t, tempLogsDir)
|
||
|
|
||
|
rulesJsonFp := "testData/rules3.json"
|
||
|
cpFileForTest(t, rulesJsonFp, rulesFile)
|
||
|
|
||
|
p := ParsedConf{
|
||
|
RulesFp: rulesFile,
|
||
|
LogFp: lFp,
|
||
|
Port: "9050",
|
||
|
Compression: true,
|
||
|
SizeToRotate: "5MB",
|
||
|
}
|
||
|
jsonString, _ := json.Marshal(p)
|
||
|
writeForTest(t, confFile, jsonString)
|
||
|
|
||
|
_, err := c.LoadMainConfigFile(confFile)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
err = c.LoadMappingFile(p.RulesFp)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
l, err := newFileLogger(p.LogFp, p.SizeToRotate, p.Compression)
|
||
|
assert.NotEmpty(t, l)
|
||
|
assert.Equal(t, err, nil)
|
||
|
|
||
|
isEmpty := isFileEmpty(t, lFp)
|
||
|
assert.Equal(t, isEmpty, true)
|
||
|
|
||
|
app := newTestApp(t, c, l)
|
||
|
app.Routes()
|
||
|
ts := newTestServer(t, app.Mux)
|
||
|
defer ts.Close()
|
||
|
|
||
|
code, _, body := ts.get(t, "/x/fuckoff?go-get=1")
|
||
|
assert.Equal(t, code, http.StatusOK)
|
||
|
expected, _ := getDefaultHtml()
|
||
|
assert.Equal(t, string(body), string(expected))
|
||
|
|
||
|
isEmpty = isFileEmpty(t, lFp)
|
||
|
assert.Equal(t, isEmpty, false)
|
||
|
|
||
|
t.Logf("Printing returned html => %s", string(body))
|
||
|
})
|
||
|
}
|