testing new env and config
This commit is contained in:
+100
-57
@@ -2,31 +2,59 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeConfigFile(t *testing.T, content string) string {
|
func writeConfigFiles(t *testing.T, cfgMap map[string]string, envPerm os.FileMode) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
path := filepath.Join(dir, "config.toml")
|
tomlPath := filepath.Join(dir, "config.toml")
|
||||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
envPath := filepath.Join(dir, "pkgstash.env")
|
||||||
|
|
||||||
|
// set env_file path
|
||||||
|
cfgMap["env_file"] = fmt.Sprintf("%q", envPath)
|
||||||
|
|
||||||
|
// build the content strings
|
||||||
|
var tb strings.Builder
|
||||||
|
var eb strings.Builder
|
||||||
|
for k, v := range cfgMap {
|
||||||
|
if k == "PKGSTASH_TOKEN" {
|
||||||
|
fmt.Fprintf(&eb, "%s=%s\n", k, v)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(&tb, "%s = %s\n", k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//write config.toml
|
||||||
|
if err := os.WriteFile(tomlPath, []byte(tb.String()), 0644); err != nil {
|
||||||
t.Fatalf("failed write test config: %v", err)
|
t.Fatalf("failed write test config: %v", err)
|
||||||
}
|
}
|
||||||
return path
|
|
||||||
|
//write env
|
||||||
|
if err := os.WriteFile(envPath, []byte(eb.String()), envPerm); err != nil {
|
||||||
|
t.Fatalf("failed write test env: %v", err)
|
||||||
|
}
|
||||||
|
return tomlPath
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultCfgMap() map[string]string {
|
||||||
|
return map[string]string{
|
||||||
|
"env_file": `""`,
|
||||||
|
"cache_root": `"srv/cache"`,
|
||||||
|
"mirror_urls": `["https://mirror.example.com"]`,
|
||||||
|
"mirrored_repos": `["core", "extra"]`,
|
||||||
|
"port": `"8090"`,
|
||||||
|
"PKGSTASH_TOKEN": "testtoken",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadConfig(t *testing.T) {
|
func TestReadConfig(t *testing.T) {
|
||||||
path := writeConfigFile(t, `
|
cfgMap := defaultCfgMap()
|
||||||
cache_root = "srv/cache"
|
path := writeConfigFiles(t, cfgMap, 0600)
|
||||||
mirror_urls = ["https://mirror.example.com"]
|
|
||||||
mirrored_repos = ["core", "extra"]
|
|
||||||
port = "8090"
|
|
||||||
|
|
||||||
[auth]
|
|
||||||
token = "testtoken"
|
|
||||||
`)
|
|
||||||
|
|
||||||
cfg, err := ReadConfig(path)
|
cfg, err := ReadConfig(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -38,15 +66,9 @@ token = "testtoken"
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMissingCacheRoot(t *testing.T) {
|
func TestMissingCacheRoot(t *testing.T) {
|
||||||
path := writeConfigFile(t, `
|
cfgMap := defaultCfgMap()
|
||||||
mirror_urls = ["https://mirror.example.com"]
|
cfgMap["cache_root"] = ""
|
||||||
mirrored_repos = ["core", "extra"]
|
path := writeConfigFiles(t, cfgMap, 0600)
|
||||||
port = "8090"
|
|
||||||
|
|
||||||
[auth]
|
|
||||||
token = "testtoken"
|
|
||||||
`)
|
|
||||||
|
|
||||||
_, err := ReadConfig(path)
|
_, err := ReadConfig(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected err got nil")
|
t.Fatal("expected err got nil")
|
||||||
@@ -54,14 +76,9 @@ token = "testtoken"
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMissingMirrorUrls(t *testing.T) {
|
func TestMissingMirrorUrls(t *testing.T) {
|
||||||
path := writeConfigFile(t, `
|
cfgMap := defaultCfgMap()
|
||||||
cache_root = "srv/cache"
|
delete(cfgMap, "mirror_urls")
|
||||||
mirrored_repos = ["core", "extra"]
|
path := writeConfigFiles(t, cfgMap, 0600)
|
||||||
port = "8090"
|
|
||||||
|
|
||||||
[auth]
|
|
||||||
token = "testtoken"
|
|
||||||
`)
|
|
||||||
|
|
||||||
_, err := ReadConfig(path)
|
_, err := ReadConfig(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -70,14 +87,9 @@ token = "testtoken"
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMissingMirroredRepos(t *testing.T) {
|
func TestMissingMirroredRepos(t *testing.T) {
|
||||||
path := writeConfigFile(t, `
|
cfgMap := defaultCfgMap()
|
||||||
cache_root = "srv/cache"
|
delete(cfgMap, "mirrored_repos")
|
||||||
mirror_urls = ["https://mirror.example.com"]
|
path := writeConfigFiles(t, cfgMap, 0600)
|
||||||
port = "8090"
|
|
||||||
|
|
||||||
[auth]
|
|
||||||
token = "testtoken"
|
|
||||||
`)
|
|
||||||
|
|
||||||
_, err := ReadConfig(path)
|
_, err := ReadConfig(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -86,14 +98,9 @@ token = "testtoken"
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMissingPort(t *testing.T) {
|
func TestMissingPort(t *testing.T) {
|
||||||
path := writeConfigFile(t, `
|
cfgMap := defaultCfgMap()
|
||||||
cache_root = "srv/cache"
|
cfgMap["port"] = ""
|
||||||
mirror_urls = ["https://mirror.example.com"]
|
path := writeConfigFiles(t, cfgMap, 0600)
|
||||||
mirrored_repos = ["core", "extra"]
|
|
||||||
|
|
||||||
[auth]
|
|
||||||
token = "testtoken"
|
|
||||||
`)
|
|
||||||
|
|
||||||
_, err := ReadConfig(path)
|
_, err := ReadConfig(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -102,14 +109,9 @@ token = "testtoken"
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMissingAuthToken(t *testing.T) {
|
func TestMissingAuthToken(t *testing.T) {
|
||||||
path := writeConfigFile(t, `
|
cfgMap := defaultCfgMap()
|
||||||
cache_root = "srv/cache"
|
cfgMap["PKGSTASH_TOKEN"] = ""
|
||||||
mirror_urls = ["https://mirror.example.com"]
|
path := writeConfigFiles(t, cfgMap, 0600)
|
||||||
mirrored_repos = ["core", "extra"]
|
|
||||||
port = "8090"
|
|
||||||
|
|
||||||
[auth]
|
|
||||||
`)
|
|
||||||
|
|
||||||
_, err := ReadConfig(path)
|
_, err := ReadConfig(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -127,12 +129,53 @@ func TestMissingFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidToml(t *testing.T) {
|
func TestInvalidToml(t *testing.T) {
|
||||||
path := writeConfigFile(t, `
|
path := filepath.Join(t.TempDir(), "bad.toml")
|
||||||
cache_root = [srv/cache]
|
if err := os.WriteFile(path, []byte("cache_root = [srv/cache]"), 0644); err != nil {
|
||||||
`)
|
t.Fatalf("failed write test config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
_, err := ReadConfig(path)
|
_, err := ReadConfig(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected err got nil")
|
t.Fatal("expected err got nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWrongPermissionsEnv(t *testing.T) {
|
||||||
|
cfgMap := defaultCfgMap()
|
||||||
|
path := writeConfigFiles(t, cfgMap, 0644)
|
||||||
|
_, err := ReadConfig(path)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected err got nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmptyEnv(t *testing.T) {
|
||||||
|
cfgMap := defaultCfgMap()
|
||||||
|
delete(cfgMap, "PKGSTASH_TOKEN")
|
||||||
|
path := writeConfigFiles(t, cfgMap, 0600)
|
||||||
|
_, err := ReadConfig(path)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected err got nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmptyKeyEnv(t *testing.T) {
|
||||||
|
cfgMap := defaultCfgMap()
|
||||||
|
cfgMap["PKGSTASH_TOKEN"] = ""
|
||||||
|
path := writeConfigFiles(t, cfgMap, 0600)
|
||||||
|
_, err := ReadConfig(path)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected err got nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultToken(t *testing.T) {
|
||||||
|
cfgMap := defaultCfgMap()
|
||||||
|
cfgMap["PKGSTASH_TOKEN"] = "changeme"
|
||||||
|
path := writeConfigFiles(t, cfgMap, 0600)
|
||||||
|
_, err := ReadConfig(path)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected err got nil")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func newTestServer(t *testing.T, mirrorHandler http.HandlerFunc) (*httptest.Serv
|
|||||||
}
|
}
|
||||||
cfg := &Config{
|
cfg := &Config{
|
||||||
Port: "0",
|
Port: "0",
|
||||||
Auth: AuthConfig{Token: "testtoken"},
|
Token: "testtoken",
|
||||||
}
|
}
|
||||||
logLevel := new(slog.LevelVar)
|
logLevel := new(slog.LevelVar)
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
|
|||||||
Reference in New Issue
Block a user