|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "errors" |
5 | 6 | "fmt" |
6 | 7 | "os" |
| 8 | + "path/filepath" |
7 | 9 | "testing" |
8 | 10 | "time" |
9 | 11 |
|
10 | 12 | "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
11 | 14 | ) |
12 | 15 |
|
13 | 16 | type fakeFileInfo struct { |
@@ -39,6 +42,208 @@ func saveWSLEnvs(t *testing.T) { |
39 | 42 | }) |
40 | 43 | } |
41 | 44 |
|
| 45 | +func TestGetConnectionInfo_ValidConfigFile(t *testing.T) { |
| 46 | + tmpDir := t.TempDir() |
| 47 | + configFile := filepath.Join(tmpDir, "rd-engine.json") |
| 48 | + |
| 49 | + config := ConnectionInfo{ |
| 50 | + User: "example_user", |
| 51 | + Password: "example_password", |
| 52 | + Host: "192.168.1.1", |
| 53 | + Port: 8080, |
| 54 | + } |
| 55 | + |
| 56 | + data, err := json.Marshal(config) |
| 57 | + require.NoError(t, err) |
| 58 | + err = os.WriteFile(configFile, data, 0600) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + originalConfigPath := configPath |
| 62 | + originalDefaultConfigPath := DefaultConfigPath |
| 63 | + t.Cleanup(func() { |
| 64 | + configPath = originalConfigPath |
| 65 | + DefaultConfigPath = originalDefaultConfigPath |
| 66 | + }) |
| 67 | + |
| 68 | + configPath = configFile |
| 69 | + DefaultConfigPath = configFile |
| 70 | + |
| 71 | + result, err := GetConnectionInfo(false) |
| 72 | + require.NoError(t, err) |
| 73 | + assert.Equal(t, "example_user", result.User) |
| 74 | + assert.Equal(t, "example_password", result.Password) |
| 75 | + assert.Equal(t, "192.168.1.1", result.Host) |
| 76 | + assert.Equal(t, 8080, result.Port) |
| 77 | +} |
| 78 | + |
| 79 | +func TestGetConnectionInfo_MissingConfigFile_MayBeMissing(t *testing.T) { |
| 80 | + tmpDir := t.TempDir() |
| 81 | + nonExistentFile := filepath.Join(tmpDir, "nonexistent.json") |
| 82 | + |
| 83 | + originalConfigPath := configPath |
| 84 | + originalDefaultConfigPath := DefaultConfigPath |
| 85 | + t.Cleanup(func() { |
| 86 | + configPath = originalConfigPath |
| 87 | + DefaultConfigPath = originalDefaultConfigPath |
| 88 | + }) |
| 89 | + |
| 90 | + configPath = "" |
| 91 | + DefaultConfigPath = nonExistentFile |
| 92 | + |
| 93 | + result, err := GetConnectionInfo(true) |
| 94 | + assert.Nil(t, result) |
| 95 | + assert.Nil(t, err) |
| 96 | +} |
| 97 | + |
| 98 | +func TestGetConnectionInfo_MissingConfigFile_Required(t *testing.T) { |
| 99 | + tmpDir := t.TempDir() |
| 100 | + nonExistentFile := filepath.Join(tmpDir, "nonexistent.json") |
| 101 | + |
| 102 | + originalConfigPath := configPath |
| 103 | + originalDefaultConfigPath := DefaultConfigPath |
| 104 | + t.Cleanup(func() { |
| 105 | + configPath = originalConfigPath |
| 106 | + DefaultConfigPath = originalDefaultConfigPath |
| 107 | + }) |
| 108 | + |
| 109 | + configPath = nonExistentFile |
| 110 | + DefaultConfigPath = nonExistentFile |
| 111 | + |
| 112 | + result, err := GetConnectionInfo(false) |
| 113 | + assert.Nil(t, result) |
| 114 | + assert.Error(t, err) |
| 115 | +} |
| 116 | + |
| 117 | +func TestGetConnectionInfo_InvalidJSON(t *testing.T) { |
| 118 | + tmpDir := t.TempDir() |
| 119 | + configFile := filepath.Join(tmpDir, "rd-engine.json") |
| 120 | + |
| 121 | + err := os.WriteFile(configFile, []byte("not valid json"), 0600) |
| 122 | + require.NoError(t, err) |
| 123 | + |
| 124 | + originalConfigPath := configPath |
| 125 | + originalDefaultConfigPath := DefaultConfigPath |
| 126 | + t.Cleanup(func() { |
| 127 | + configPath = originalConfigPath |
| 128 | + DefaultConfigPath = originalDefaultConfigPath |
| 129 | + }) |
| 130 | + |
| 131 | + configPath = configFile |
| 132 | + DefaultConfigPath = configFile |
| 133 | + |
| 134 | + result, err := GetConnectionInfo(false) |
| 135 | + assert.Nil(t, result) |
| 136 | + assert.Error(t, err) |
| 137 | + assert.Contains(t, err.Error(), "error parsing config file") |
| 138 | +} |
| 139 | + |
| 140 | +func TestGetConnectionInfo_CLIOverrides(t *testing.T) { |
| 141 | + tmpDir := t.TempDir() |
| 142 | + configFile := filepath.Join(tmpDir, "rd-engine.json") |
| 143 | + |
| 144 | + config := ConnectionInfo{ |
| 145 | + User: "config_user", |
| 146 | + Password: "config_password", |
| 147 | + Host: "config_host", |
| 148 | + Port: 9999, |
| 149 | + } |
| 150 | + |
| 151 | + data, err := json.Marshal(config) |
| 152 | + require.NoError(t, err) |
| 153 | + err = os.WriteFile(configFile, data, 0600) |
| 154 | + require.NoError(t, err) |
| 155 | + |
| 156 | + originalConfigPath := configPath |
| 157 | + originalDefaultConfigPath := DefaultConfigPath |
| 158 | + originalConnectionSettings := connectionSettings |
| 159 | + t.Cleanup(func() { |
| 160 | + configPath = originalConfigPath |
| 161 | + DefaultConfigPath = originalDefaultConfigPath |
| 162 | + connectionSettings = originalConnectionSettings |
| 163 | + }) |
| 164 | + |
| 165 | + configPath = configFile |
| 166 | + DefaultConfigPath = configFile |
| 167 | + connectionSettings = ConnectionInfo{ |
| 168 | + User: "override_user", |
| 169 | + Password: "override_password", |
| 170 | + Host: "override_host", |
| 171 | + Port: 1234, |
| 172 | + } |
| 173 | + |
| 174 | + result, err := GetConnectionInfo(false) |
| 175 | + require.NoError(t, err) |
| 176 | + assert.Equal(t, "override_user", result.User) |
| 177 | + assert.Equal(t, "override_password", result.Password) |
| 178 | + assert.Equal(t, "override_host", result.Host) |
| 179 | + assert.Equal(t, 1234, result.Port) |
| 180 | +} |
| 181 | + |
| 182 | +func TestGetConnectionInfo_DefaultHost(t *testing.T) { |
| 183 | + tmpDir := t.TempDir() |
| 184 | + configFile := filepath.Join(tmpDir, "rd-engine.json") |
| 185 | + |
| 186 | + config := ConnectionInfo{ |
| 187 | + User: "example_user", |
| 188 | + Password: "example_password", |
| 189 | + Port: 8080, |
| 190 | + } |
| 191 | + |
| 192 | + data, err := json.Marshal(config) |
| 193 | + require.NoError(t, err) |
| 194 | + err = os.WriteFile(configFile, data, 0600) |
| 195 | + require.NoError(t, err) |
| 196 | + |
| 197 | + originalConfigPath := configPath |
| 198 | + originalDefaultConfigPath := DefaultConfigPath |
| 199 | + originalConnectionSettings := connectionSettings |
| 200 | + t.Cleanup(func() { |
| 201 | + configPath = originalConfigPath |
| 202 | + DefaultConfigPath = originalDefaultConfigPath |
| 203 | + connectionSettings = originalConnectionSettings |
| 204 | + }) |
| 205 | + |
| 206 | + configPath = configFile |
| 207 | + DefaultConfigPath = configFile |
| 208 | + connectionSettings = ConnectionInfo{} |
| 209 | + |
| 210 | + result, err := GetConnectionInfo(false) |
| 211 | + require.NoError(t, err) |
| 212 | + assert.Equal(t, "127.0.0.1", result.Host) |
| 213 | +} |
| 214 | + |
| 215 | +func TestGetConnectionInfo_MissingRequiredFields(t *testing.T) { |
| 216 | + tmpDir := t.TempDir() |
| 217 | + configFile := filepath.Join(tmpDir, "rd-engine.json") |
| 218 | + |
| 219 | + config := ConnectionInfo{ |
| 220 | + Host: "example_host", |
| 221 | + } |
| 222 | + |
| 223 | + data, err := json.Marshal(config) |
| 224 | + require.NoError(t, err) |
| 225 | + err = os.WriteFile(configFile, data, 0600) |
| 226 | + require.NoError(t, err) |
| 227 | + |
| 228 | + originalConfigPath := configPath |
| 229 | + originalDefaultConfigPath := DefaultConfigPath |
| 230 | + originalConnectionSettings := connectionSettings |
| 231 | + t.Cleanup(func() { |
| 232 | + configPath = originalConfigPath |
| 233 | + DefaultConfigPath = originalDefaultConfigPath |
| 234 | + connectionSettings = originalConnectionSettings |
| 235 | + }) |
| 236 | + |
| 237 | + configPath = configFile |
| 238 | + DefaultConfigPath = configFile |
| 239 | + connectionSettings = ConnectionInfo{} |
| 240 | + |
| 241 | + result, err := GetConnectionInfo(false) |
| 242 | + assert.Nil(t, result) |
| 243 | + assert.Error(t, err) |
| 244 | + assert.Contains(t, err.Error(), "insufficient connection settings") |
| 245 | +} |
| 246 | + |
42 | 247 | func TestIsWSLDistro(t *testing.T) { |
43 | 248 | for _, symlinkMode := range []os.FileMode{os.ModeSymlink, 0} { |
44 | 249 | symlinkText := map[os.FileMode]string{ |
|
0 commit comments