~bigbes/ci-cacher

ref: e8e2b2f17cf11feb30d2cfc6fd306d9dab1775c4 ci-cacher/internal/config/config_test.go -rw-r--r-- 2.8 KiB
e8e2b2f1 — Eugene Blikh Add docs/index.html landing page; publish.yml substitutes build info 2 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package config

import (
	"os"
	"path/filepath"
	"testing"
)

func TestLoadMissingReturnsDefaults(t *testing.T) {
	cfg, err := Load(filepath.Join(t.TempDir(), "nope.toml"))
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	if got := cfg.Region; got != "us-east-1" {
		t.Errorf("default region = %q, want us-east-1", got)
	}
}

func TestSaveThenLoadRoundTrip(t *testing.T) {
	path := filepath.Join(t.TempDir(), "config.toml")
	want := Config{
		Endpoint:   "https://s3.bigb.es",
		Region:     "garage",
		Bucket:     "docker-cache",
		Prefix:     "tarantool-protobuf",
		ArchSuffix: true,
		KeyFile:    "~/.k",
		SecretFile: "~/.s",
	}
	if err := Save(path, want); err != nil {
		t.Fatalf("Save: %v", err)
	}
	got, err := Load(path)
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	if got != want {
		t.Errorf("round trip mismatch\n got: %+v\nwant: %+v", got, want)
	}
}

func TestApplyEnvOverlays(t *testing.T) {
	t.Setenv("CACHER_BUCKET", "override-bucket")
	t.Setenv("CACHER_ARCH_SUFFIX", "true")
	cfg := Config{Bucket: "from-file", Region: "garage"}
	if err := ApplyEnv(&cfg); err != nil {
		t.Fatalf("ApplyEnv: %v", err)
	}
	if cfg.Bucket != "override-bucket" {
		t.Errorf("Bucket = %q, want override-bucket", cfg.Bucket)
	}
	if !cfg.ArchSuffix {
		t.Errorf("ArchSuffix = false, want true")
	}
	if cfg.Region != "garage" {
		t.Errorf("Region clobbered to %q (unset env should not zero existing value)", cfg.Region)
	}
}

func TestCredentialsEnvBeatsFile(t *testing.T) {
	dir := t.TempDir()
	keyPath := filepath.Join(dir, "key")
	secPath := filepath.Join(dir, "sec")
	if err := os.WriteFile(keyPath, []byte("file-key\n"), 0o600); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(secPath, []byte("file-sec\n"), 0o600); err != nil {
		t.Fatal(err)
	}
	t.Setenv("CACHER_S3_KEY_ID", "env-key")
	cfg := Config{KeyFile: keyPath, SecretFile: secPath}
	k, s, err := cfg.Credentials()
	if err != nil {
		t.Fatalf("Credentials: %v", err)
	}
	if k != "env-key" {
		t.Errorf("key id = %q, want env-key", k)
	}
	if s != "file-sec" {
		t.Errorf("secret = %q, want file-sec (whitespace trimmed)", s)
	}
}

func TestCredentialsTrimsWhitespace(t *testing.T) {
	dir := t.TempDir()
	keyPath := filepath.Join(dir, "key")
	secPath := filepath.Join(dir, "sec")
	if err := os.WriteFile(keyPath, []byte("  raw-key  \r\n"), 0o600); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(secPath, []byte("\nraw-sec\n"), 0o600); err != nil {
		t.Fatal(err)
	}
	t.Setenv("CACHER_S3_KEY_ID", "")
	t.Setenv("CACHER_S3_SECRET", "")
	cfg := Config{KeyFile: keyPath, SecretFile: secPath}
	k, s, err := cfg.Credentials()
	if err != nil {
		t.Fatalf("Credentials: %v", err)
	}
	if k != "raw-key" || s != "raw-sec" {
		t.Errorf("trimming failed: key=%q secret=%q", k, s)
	}
}

func TestValidateMissingFields(t *testing.T) {
	err := Config{Region: "garage"}.Validate()
	if err == nil {
		t.Fatal("expected error for missing endpoint/bucket")
	}
}