Skip to content

Commit 884d4f8

Browse files
authored
Merge pull request #34 from harakeishi/test/add-comprehensive-test-coverage
包括的なテストカバレッジの追加
2 parents 0eae677 + 941c5f7 commit 884d4f8

File tree

17 files changed

+5280
-64
lines changed

17 files changed

+5280
-64
lines changed

cmd/up_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestCreatePortConfig_WithReservedPorts(t *testing.T) {
1616
expectError bool
1717
}{
1818
{
19-
name: "設定ファイルの予約ポートを保持",
19+
name: "preserve reserved ports from config",
2020
portRangeStr: "",
2121
baseConfig: types.PortConfig{
2222
Range: types.PortRange{Start: 8000, End: 9000},
@@ -28,7 +28,7 @@ func TestCreatePortConfig_WithReservedPorts(t *testing.T) {
2828
expectError: false,
2929
},
3030
{
31-
name: "CLIでポート範囲を上書きしても予約ポートは保持",
31+
name: "preserve reserved ports when CLI overrides range",
3232
portRangeStr: "7000-7999",
3333
baseConfig: types.PortConfig{
3434
Range: types.PortRange{Start: 8000, End: 9000},
@@ -40,7 +40,7 @@ func TestCreatePortConfig_WithReservedPorts(t *testing.T) {
4040
expectError: false,
4141
},
4242
{
43-
name: "空の予約ポートリスト",
43+
name: "empty reserved ports list",
4444
portRangeStr: "",
4545
baseConfig: types.PortConfig{
4646
Range: types.PortRange{Start: 8000, End: 9000},
@@ -52,7 +52,7 @@ func TestCreatePortConfig_WithReservedPorts(t *testing.T) {
5252
expectError: false,
5353
},
5454
{
55-
name: "多数の予約ポート",
55+
name: "many reserved ports",
5656
portRangeStr: "",
5757
baseConfig: types.PortConfig{
5858
Range: types.PortRange{Start: 8000, End: 8100},
@@ -64,7 +64,7 @@ func TestCreatePortConfig_WithReservedPorts(t *testing.T) {
6464
expectError: false,
6565
},
6666
{
67-
name: "無効なポート範囲形式",
67+
name: "invalid port range format",
6868
portRangeStr: "invalid",
6969
baseConfig: types.PortConfig{
7070
Range: types.PortRange{Start: 8000, End: 9000},
@@ -74,7 +74,7 @@ func TestCreatePortConfig_WithReservedPorts(t *testing.T) {
7474
expectError: true,
7575
},
7676
{
77-
name: "開始ポートが終了ポートより大きい",
77+
name: "start port greater than end port",
7878
portRangeStr: "9000-8000",
7979
baseConfig: types.PortConfig{
8080
Range: types.PortRange{Start: 8000, End: 9000},
@@ -101,15 +101,15 @@ func TestCreatePortConfig_WithReservedPorts(t *testing.T) {
101101
return
102102
}
103103

104-
// ポート範囲の確認
104+
// verify port range
105105
if result.Range.Start != tt.expectedRange.Start {
106106
t.Errorf("createPortConfig() Range.Start = %d, want %d", result.Range.Start, tt.expectedRange.Start)
107107
}
108108
if result.Range.End != tt.expectedRange.End {
109109
t.Errorf("createPortConfig() Range.End = %d, want %d", result.Range.End, tt.expectedRange.End)
110110
}
111111

112-
// 予約ポートの確認
112+
// verify reserved ports
113113
if len(result.Reserved) != len(tt.expectedReserved) {
114114
t.Errorf("createPortConfig() Reserved length = %d, want %d", len(result.Reserved), len(tt.expectedReserved))
115115
return
@@ -121,7 +121,7 @@ func TestCreatePortConfig_WithReservedPorts(t *testing.T) {
121121
}
122122
}
123123

124-
// ExcludePrivilegedが保持されていることを確認
124+
// verify ExcludePrivileged is preserved
125125
if result.ExcludePrivileged != tt.baseConfig.ExcludePrivileged {
126126
t.Errorf("createPortConfig() ExcludePrivileged = %v, want %v", result.ExcludePrivileged, tt.baseConfig.ExcludePrivileged)
127127
}
@@ -137,50 +137,50 @@ func TestParsePortRange(t *testing.T) {
137137
expectError bool
138138
}{
139139
{
140-
name: "有効なポート範囲",
140+
name: "valid port range",
141141
input: "8000-9000",
142142
expected: types.PortRange{Start: 8000, End: 9000},
143143
expectError: false,
144144
},
145145
{
146-
name: "空文字列はデフォルト範囲",
146+
name: "empty string uses default range",
147147
input: "",
148148
expected: types.PortRange{Start: 8000, End: 9999},
149149
expectError: false,
150150
},
151151
{
152-
name: "スペース付きの範囲",
152+
name: "range with spaces",
153153
input: "7000 - 7999",
154154
expected: types.PortRange{Start: 7000, End: 7999},
155155
expectError: false,
156156
},
157157
{
158-
name: "無効な形式",
158+
name: "invalid format",
159159
input: "8000",
160160
expectError: true,
161161
},
162162
{
163-
name: "開始ポートが無効",
163+
name: "invalid start port",
164164
input: "abc-9000",
165165
expectError: true,
166166
},
167167
{
168-
name: "終了ポートが無効",
168+
name: "invalid end port",
169169
input: "8000-xyz",
170170
expectError: true,
171171
},
172172
{
173-
name: "範囲外のポート(開始が0以下)",
173+
name: "out of range port (start is 0 or less)",
174174
input: "0-9000",
175175
expectError: true,
176176
},
177177
{
178-
name: "範囲外のポート(終了が65535超)",
178+
name: "out of range port (end exceeds 65535)",
179179
input: "8000-70000",
180180
expectError: true,
181181
},
182182
{
183-
name: "開始が終了より大きい",
183+
name: "start greater than end",
184184
input: "9000-8000",
185185
expectError: true,
186186
},

internal/config/default_test.go

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package config
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestDefaultConfig(t *testing.T) {
10+
config := DefaultConfig()
11+
12+
if config == nil {
13+
t.Fatal("DefaultConfig() returned nil")
14+
}
15+
16+
// verify port config
17+
if config.Port.Range.Start != 8000 {
18+
t.Errorf("Port.Range.Start = %d, want 8000", config.Port.Range.Start)
19+
}
20+
if config.Port.Range.End != 9999 {
21+
t.Errorf("Port.Range.End = %d, want 9999", config.Port.Range.End)
22+
}
23+
if !config.Port.ExcludePrivileged {
24+
t.Error("Port.ExcludePrivileged = false, want true")
25+
}
26+
27+
// verify file config
28+
if config.File.ComposeFile != "compose.yml" {
29+
t.Errorf("File.ComposeFile = %s, want compose.yml", config.File.ComposeFile)
30+
}
31+
if config.File.OverrideFile != "compose.override.yml" {
32+
t.Errorf("File.OverrideFile = %s, want compose.override.yml", config.File.OverrideFile)
33+
}
34+
if !config.File.BackupEnabled {
35+
t.Error("File.BackupEnabled = false, want true")
36+
}
37+
38+
// verify watcher config
39+
if config.Watcher.Interval != 5*time.Second {
40+
t.Errorf("Watcher.Interval = %v, want 5s", config.Watcher.Interval)
41+
}
42+
if config.Watcher.MaxRetries != 3 {
43+
t.Errorf("Watcher.MaxRetries = %d, want 3", config.Watcher.MaxRetries)
44+
}
45+
46+
// verify log config
47+
if config.Log.Level != "info" {
48+
t.Errorf("Log.Level = %s, want info", config.Log.Level)
49+
}
50+
if config.Log.Format != "text" {
51+
t.Errorf("Log.Format = %s, want text", config.Log.Format)
52+
}
53+
}
54+
55+
func TestDefaultPortConfig(t *testing.T) {
56+
config := DefaultPortConfig()
57+
58+
if config.Range.Start != 8000 {
59+
t.Errorf("Range.Start = %d, want 8000", config.Range.Start)
60+
}
61+
if config.Range.End != 9999 {
62+
t.Errorf("Range.End = %d, want 9999", config.Range.End)
63+
}
64+
65+
expectedReserved := []int{8080, 8443, 9000, 9090}
66+
if !reflect.DeepEqual(config.Reserved, expectedReserved) {
67+
t.Errorf("Reserved = %v, want %v", config.Reserved, expectedReserved)
68+
}
69+
70+
if !config.ExcludePrivileged {
71+
t.Error("ExcludePrivileged = false, want true")
72+
}
73+
}
74+
75+
func TestDefaultFileConfig(t *testing.T) {
76+
config := DefaultFileConfig()
77+
78+
if config.ComposeFile != "compose.yml" {
79+
t.Errorf("ComposeFile = %s, want compose.yml", config.ComposeFile)
80+
}
81+
if config.OverrideFile != "compose.override.yml" {
82+
t.Errorf("OverrideFile = %s, want compose.override.yml", config.OverrideFile)
83+
}
84+
if !config.BackupEnabled {
85+
t.Error("BackupEnabled = false, want true")
86+
}
87+
if config.BackupDir != ".gopose/backups" {
88+
t.Errorf("BackupDir = %s, want .gopose/backups", config.BackupDir)
89+
}
90+
}
91+
92+
func TestDefaultWatcherConfig(t *testing.T) {
93+
config := DefaultWatcherConfig()
94+
95+
if config.Interval != 5*time.Second {
96+
t.Errorf("Interval = %v, want 5s", config.Interval)
97+
}
98+
if config.CleanupDelay != 30*time.Second {
99+
t.Errorf("CleanupDelay = %v, want 30s", config.CleanupDelay)
100+
}
101+
if config.MaxRetries != 3 {
102+
t.Errorf("MaxRetries = %d, want 3", config.MaxRetries)
103+
}
104+
if config.RetryInterval != 1*time.Second {
105+
t.Errorf("RetryInterval = %v, want 1s", config.RetryInterval)
106+
}
107+
}
108+
109+
func TestDefaultLogConfig(t *testing.T) {
110+
config := DefaultLogConfig()
111+
112+
if config.Level != "info" {
113+
t.Errorf("Level = %s, want info", config.Level)
114+
}
115+
if config.Format != "text" {
116+
t.Errorf("Format = %s, want text", config.Format)
117+
}
118+
if config.File != "" {
119+
t.Errorf("File = %s, want empty string", config.File)
120+
}
121+
if config.MaxSize != 100 {
122+
t.Errorf("MaxSize = %d, want 100", config.MaxSize)
123+
}
124+
if config.MaxAge != 30 {
125+
t.Errorf("MaxAge = %d, want 30", config.MaxAge)
126+
}
127+
if !config.Compress {
128+
t.Error("Compress = false, want true")
129+
}
130+
}
131+
132+
func TestDevelopmentConfig(t *testing.T) {
133+
config := DevelopmentConfig()
134+
135+
if config.Log.Level != "debug" {
136+
t.Errorf("Log.Level = %s, want debug", config.Log.Level)
137+
}
138+
if config.Log.Format != "text" {
139+
t.Errorf("Log.Format = %s, want text", config.Log.Format)
140+
}
141+
if config.Watcher.Interval != 2*time.Second {
142+
t.Errorf("Watcher.Interval = %v, want 2s", config.Watcher.Interval)
143+
}
144+
145+
// verify base config is correctly inherited
146+
if config.Port.Range.Start != 8000 {
147+
t.Errorf("Port.Range.Start = %d, want 8000", config.Port.Range.Start)
148+
}
149+
}
150+
151+
func TestProductionConfig(t *testing.T) {
152+
config := ProductionConfig()
153+
154+
if config.Log.Level != "warn" {
155+
t.Errorf("Log.Level = %s, want warn", config.Log.Level)
156+
}
157+
if config.Log.Format != "json" {
158+
t.Errorf("Log.Format = %s, want json", config.Log.Format)
159+
}
160+
if config.Log.File != "/var/log/gopose/gopose.log" {
161+
t.Errorf("Log.File = %s, want /var/log/gopose/gopose.log", config.Log.File)
162+
}
163+
if config.Watcher.Interval != 10*time.Second {
164+
t.Errorf("Watcher.Interval = %v, want 10s", config.Watcher.Interval)
165+
}
166+
if config.Watcher.CleanupDelay != 60*time.Second {
167+
t.Errorf("Watcher.CleanupDelay = %v, want 60s", config.Watcher.CleanupDelay)
168+
}
169+
170+
// verify base config is correctly inherited
171+
if config.Port.Range.Start != 8000 {
172+
t.Errorf("Port.Range.Start = %d, want 8000", config.Port.Range.Start)
173+
}
174+
}
175+
176+
func TestTestConfig(t *testing.T) {
177+
config := TestConfig()
178+
179+
if config.Log.Level != "debug" {
180+
t.Errorf("Log.Level = %s, want debug", config.Log.Level)
181+
}
182+
if config.Log.Format != "text" {
183+
t.Errorf("Log.Format = %s, want text", config.Log.Format)
184+
}
185+
if config.File.BackupEnabled {
186+
t.Error("File.BackupEnabled = true, want false (for testing)")
187+
}
188+
if config.Watcher.Interval != 100*time.Millisecond {
189+
t.Errorf("Watcher.Interval = %v, want 100ms", config.Watcher.Interval)
190+
}
191+
if config.Watcher.CleanupDelay != 1*time.Second {
192+
t.Errorf("Watcher.CleanupDelay = %v, want 1s", config.Watcher.CleanupDelay)
193+
}
194+
195+
// verify base config is correctly inherited
196+
if config.Port.Range.Start != 8000 {
197+
t.Errorf("Port.Range.Start = %d, want 8000", config.Port.Range.Start)
198+
}
199+
}
200+
201+
func TestConfigIsolation(t *testing.T) {
202+
// verify each config function returns an independent instance
203+
config1 := DefaultConfig()
204+
config2 := DefaultConfig()
205+
206+
// verify pointers are different (== on *Config compares addresses, not values)
207+
if config1 == config2 {
208+
t.Error("DefaultConfig() returns the same pointer, expected different instances")
209+
}
210+
211+
// verify modifying one does not affect the other
212+
config1.Port.Range.Start = 9999
213+
if config2.Port.Range.Start == 9999 {
214+
t.Error("Modifying config1 affected config2, expected independence")
215+
}
216+
}

0 commit comments

Comments
 (0)