bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/csconfig/api_test.go (about) 1 package csconfig 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "gopkg.in/yaml.v2" 13 ) 14 15 func TestLoadLocalApiClientCfg(t *testing.T) { 16 True := true 17 tests := []struct { 18 name string 19 Input *LocalApiClientCfg 20 expectedResult *ApiCredentialsCfg 21 err string 22 }{ 23 { 24 name: "basic valid configuration", 25 Input: &LocalApiClientCfg{ 26 CredentialsFilePath: "./tests/lapi-secrets.yaml", 27 }, 28 expectedResult: &ApiCredentialsCfg{ 29 URL: "http://localhost:8080/", 30 Login: "test", 31 Password: "testpassword", 32 }, 33 }, 34 { 35 name: "invalid configuration", 36 Input: &LocalApiClientCfg{ 37 CredentialsFilePath: "./tests/bad_lapi-secrets.yaml", 38 }, 39 expectedResult: &ApiCredentialsCfg{}, 40 }, 41 { 42 name: "invalid configuration filepath", 43 Input: &LocalApiClientCfg{ 44 CredentialsFilePath: "./tests/nonexist_lapi-secrets.yaml", 45 }, 46 expectedResult: nil, 47 }, 48 { 49 name: "valid configuration with insecure skip verify", 50 Input: &LocalApiClientCfg{ 51 CredentialsFilePath: "./tests/lapi-secrets.yaml", 52 InsecureSkipVerify: &True, 53 }, 54 expectedResult: &ApiCredentialsCfg{ 55 URL: "http://localhost:8080/", 56 Login: "test", 57 Password: "testpassword", 58 }, 59 }, 60 } 61 62 for idx, test := range tests { 63 fmt.Printf("TEST '%s'\n", test.name) 64 err := test.Input.Load() 65 if err == nil && test.err != "" { 66 t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests)) 67 } else if test.err != "" { 68 if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) { 69 t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests), 70 test.err, 71 fmt.Sprintf("%s", err)) 72 } 73 } 74 75 isOk := assert.Equal(t, test.expectedResult, test.Input.Credentials) 76 if !isOk { 77 t.Fatalf("test '%s' failed", test.name) 78 } 79 80 } 81 } 82 83 func TestLoadOnlineApiClientCfg(t *testing.T) { 84 tests := []struct { 85 name string 86 Input *OnlineApiClientCfg 87 expectedResult *ApiCredentialsCfg 88 err string 89 }{ 90 { 91 name: "basic valid configuration", 92 Input: &OnlineApiClientCfg{ 93 CredentialsFilePath: "./tests/online-api-secrets.yaml", 94 }, 95 expectedResult: &ApiCredentialsCfg{ 96 URL: "http://synsec.api", 97 Login: "test", 98 Password: "testpassword", 99 }, 100 }, 101 { 102 name: "invalid configuration", 103 Input: &OnlineApiClientCfg{ 104 CredentialsFilePath: "./tests/bad_lapi-secrets.yaml", 105 }, 106 expectedResult: &ApiCredentialsCfg{}, 107 err: "failed unmarshaling api server credentials", 108 }, 109 { 110 name: "missing field configuration", 111 Input: &OnlineApiClientCfg{ 112 CredentialsFilePath: "./tests/bad_online-api-secrets.yaml", 113 }, 114 expectedResult: nil, 115 }, 116 { 117 name: "invalid configuration filepath", 118 Input: &OnlineApiClientCfg{ 119 CredentialsFilePath: "./tests/nonexist_online-api-secrets.yaml", 120 }, 121 expectedResult: &ApiCredentialsCfg{}, 122 err: "failed to read api server credentials", 123 }, 124 } 125 126 for idx, test := range tests { 127 err := test.Input.Load() 128 if err == nil && test.err != "" { 129 fmt.Printf("TEST '%s': NOK\n", test.name) 130 t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests)) 131 } else if test.err != "" { 132 if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) { 133 fmt.Printf("TEST '%s': NOK\n", test.name) 134 t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests), 135 test.err, 136 fmt.Sprintf("%s", err)) 137 } 138 } 139 140 isOk := assert.Equal(t, test.expectedResult, test.Input.Credentials) 141 if !isOk { 142 t.Fatalf("TEST '%s': NOK", test.name) 143 } else { 144 fmt.Printf("TEST '%s': OK\n", test.name) 145 } 146 147 } 148 } 149 150 func TestLoadAPIServer(t *testing.T) { 151 tmpLAPI := &LocalApiServerCfg{ 152 ProfilesPath: "./tests/profiles.yaml", 153 } 154 if err := tmpLAPI.LoadProfiles(); err != nil { 155 t.Fatalf("loading tmp profiles: %+v", err) 156 } 157 158 LogDirFullPath, err := filepath.Abs("./tests") 159 if err != nil { 160 t.Fatalf(err.Error()) 161 } 162 163 config := &Config{} 164 fcontent, err := ioutil.ReadFile("./tests/config.yaml") 165 if err != nil { 166 t.Fatalf(err.Error()) 167 } 168 configData := os.ExpandEnv(string(fcontent)) 169 err = yaml.UnmarshalStrict([]byte(configData), &config) 170 if err != nil { 171 t.Fatalf(err.Error()) 172 } 173 tests := []struct { 174 name string 175 Input *Config 176 expectedResult *LocalApiServerCfg 177 err string 178 }{ 179 { 180 name: "basic valid configuration", 181 Input: &Config{ 182 Self: []byte(configData), 183 API: &APICfg{ 184 Server: &LocalApiServerCfg{ 185 ListenURI: "http://synsec.api", 186 OnlineClient: &OnlineApiClientCfg{ 187 CredentialsFilePath: "./tests/online-api-secrets.yaml", 188 }, 189 ProfilesPath: "./tests/profiles.yaml", 190 }, 191 }, 192 DbConfig: &DatabaseCfg{ 193 Type: "sqlite", 194 DbPath: "./tests/test.db", 195 }, 196 Common: &CommonCfg{ 197 LogDir: "./tests/", 198 LogMedia: "stdout", 199 }, 200 DisableAPI: false, 201 }, 202 expectedResult: &LocalApiServerCfg{ 203 ListenURI: "http://synsec.api", 204 TLS: nil, 205 DbConfig: &DatabaseCfg{ 206 DbPath: "./tests/test.db", 207 Type: "sqlite", 208 }, 209 LogDir: LogDirFullPath, 210 LogMedia: "stdout", 211 OnlineClient: &OnlineApiClientCfg{ 212 CredentialsFilePath: "./tests/online-api-secrets.yaml", 213 Credentials: &ApiCredentialsCfg{ 214 URL: "http://synsec.api", 215 Login: "test", 216 Password: "testpassword", 217 }, 218 }, 219 Profiles: tmpLAPI.Profiles, 220 ProfilesPath: "./tests/profiles.yaml", 221 UseForwardedForHeaders: false, 222 }, 223 err: "", 224 }, 225 { 226 name: "basic valid configuration", 227 Input: &Config{ 228 Self: []byte(configData), 229 API: &APICfg{ 230 Server: &LocalApiServerCfg{}, 231 }, 232 Common: &CommonCfg{ 233 LogDir: "./tests/", 234 LogMedia: "stdout", 235 }, 236 DisableAPI: false, 237 }, 238 expectedResult: &LocalApiServerCfg{ 239 LogDir: LogDirFullPath, 240 LogMedia: "stdout", 241 }, 242 err: "while loading profiles for LAPI", 243 }, 244 } 245 246 for idx, test := range tests { 247 err := test.Input.LoadAPIServer() 248 if err == nil && test.err != "" { 249 fmt.Printf("TEST '%s': NOK\n", test.name) 250 t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests)) 251 } else if test.err != "" { 252 if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) { 253 fmt.Printf("TEST '%s': NOK\n", test.name) 254 t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests), 255 test.err, 256 fmt.Sprintf("%s", err)) 257 } 258 } 259 260 isOk := assert.Equal(t, test.expectedResult, test.Input.API.Server) 261 if !isOk { 262 t.Fatalf("TEST '%s': NOK", test.name) 263 } else { 264 fmt.Printf("TEST '%s': OK\n", test.name) 265 } 266 267 } 268 }