How to use Timeouts method of targets Package

Best Syzkaller code snippet using targets.Timeouts

managercontext_integration_test.go

Source:managercontext_integration_test.go Github

copy

Full Screen

...78 Enabled: &DefaultServerCompressEnabled,79 Level: DefaultServerCompressLevel,80 Types: DefaultServerCompressTypes,81 }82 svrTimeoutsCfg := &ServerTimeoutsConfig{83 ReadHeaderTimeout: DefaultServerTimeoutsReadHeaderTimeout,84 }85 falseValue := false86 secret1Filename := path.Join(os.TempDir(), "secret1")87 secretWithNewLineFilename := path.Join(os.TempDir(), "secret-with-new-line")88 tests := []struct {89 name string90 configs map[string]string91 envVariables map[string]string92 secretFiles map[string]string93 expectedResult *Config94 wantErr bool95 }{96 {97 name: "Configuration not found",98 configs: map[string]string{99 "config": "",100 },101 wantErr: true,102 },103 {104 name: "Not a yaml",105 configs: map[string]string{106 "config.yaml": "notayaml",107 },108 wantErr: true,109 },110 {111 name: "Empty configuration file",112 configs: map[string]string{113 "config.yaml": "",114 },115 expectedResult: &Config{116 Log: &LogConfig{117 Level: "info",118 Format: "json",119 },120 Server: &ServerConfig{121 Port: 8080,122 Compress: svrCompressCfg,123 Timeouts: svrTimeoutsCfg,124 },125 InternalServer: &ServerConfig{126 Port: 9090,127 Compress: svrCompressCfg,128 Timeouts: svrTimeoutsCfg,129 },130 Templates: defaultTemplateCfg,131 Tracing: &TracingConfig{Enabled: false},132 ListTargets: &ListTargetsConfig{133 Enabled: false,134 },135 Targets: nil,136 },137 },138 {139 name: "no configuration file",140 configs: map[string]string{},141 expectedResult: &Config{142 Log: &LogConfig{143 Level: "info",144 Format: "json",145 },146 Server: &ServerConfig{147 Port: 8080,148 Compress: svrCompressCfg,149 Timeouts: svrTimeoutsCfg,150 },151 InternalServer: &ServerConfig{152 Port: 9090,153 Compress: svrCompressCfg,154 Timeouts: svrTimeoutsCfg,155 },156 Templates: defaultTemplateCfg,157 Tracing: &TracingConfig{Enabled: false},158 ListTargets: &ListTargetsConfig{159 Enabled: false,160 },161 Targets: nil,162 },163 },164 {165 name: "Test all default values with minimal config",166 configs: map[string]string{167 "config.yaml": `168targets:169 test:170 mount:171 path: /test/172 bucket:173 name: bucket1174 region: us-east-1175`,176 },177 wantErr: false,178 expectedResult: &Config{179 Log: &LogConfig{180 Level: "info",181 Format: "json",182 },183 Server: &ServerConfig{184 Port: 8080,185 Compress: svrCompressCfg,186 Timeouts: svrTimeoutsCfg,187 },188 InternalServer: &ServerConfig{189 Port: 9090,190 Compress: svrCompressCfg,191 Timeouts: svrTimeoutsCfg,192 },193 Templates: defaultTemplateCfg,194 Tracing: &TracingConfig{Enabled: false},195 ListTargets: &ListTargetsConfig{196 Enabled: false,197 },198 Targets: map[string]*TargetConfig{199 "test": {200 Name: "test",201 Mount: &MountConfig{202 Path: []string{"/test/"},203 },204 Bucket: &BucketConfig{205 Name: "bucket1",206 Region: "us-east-1",207 S3ListMaxKeys: 1000,208 },209 Actions: &ActionsConfig{210 GET: &GetActionConfig{Enabled: true},211 },212 Templates: &TargetTemplateConfig{},213 },214 },215 },216 },217 {218 name: "Should merge target across multiple files",219 configs: map[string]string{220 "config.yaml": `221targets:222 test:223 mount:224 path: /test/225`,226 "config2.yaml": `227targets:228 test:229 bucket:230 name: bucket1231`,232 "config3.yaml": `233targets:234 test:235 bucket:236 region: us-east-1237`,238 },239 wantErr: false,240 expectedResult: &Config{241 Log: &LogConfig{242 Level: "info",243 Format: "json",244 },245 Server: &ServerConfig{246 Port: 8080,247 Compress: svrCompressCfg,248 Timeouts: svrTimeoutsCfg,249 },250 InternalServer: &ServerConfig{251 Port: 9090,252 Compress: svrCompressCfg,253 Timeouts: svrTimeoutsCfg,254 },255 Templates: defaultTemplateCfg,256 Tracing: &TracingConfig{Enabled: false},257 ListTargets: &ListTargetsConfig{258 Enabled: false,259 },260 Targets: map[string]*TargetConfig{261 "test": {262 Name: "test",263 Mount: &MountConfig{264 Path: []string{"/test/"},265 },266 Bucket: &BucketConfig{267 Name: "bucket1",268 Region: "us-east-1",269 S3ListMaxKeys: 1000,270 },271 Actions: &ActionsConfig{272 GET: &GetActionConfig{Enabled: true},273 },274 Templates: &TargetTemplateConfig{},275 },276 },277 },278 },279 {280 name: "Should merge multiple targets across multiple files",281 configs: map[string]string{282 "config.yaml": `283targets:284 test:285 mount:286 path: /test/287 test2:288 mount:289 path: /test2/290`,291 "config2.yaml": `292targets:293 test:294 bucket:295 name: bucket1296 test2:297 bucket:298 name: bucket2299 region: us-east-1300`,301 "config3.yaml": `302targets:303 test:304 bucket:305 region: us-east-1306`,307 },308 wantErr: false,309 expectedResult: &Config{310 Log: &LogConfig{311 Level: "info",312 Format: "json",313 },314 Server: &ServerConfig{315 Port: 8080,316 Compress: svrCompressCfg,317 Timeouts: svrTimeoutsCfg,318 },319 InternalServer: &ServerConfig{320 Port: 9090,321 Compress: svrCompressCfg,322 Timeouts: svrTimeoutsCfg,323 },324 Templates: defaultTemplateCfg,325 Tracing: &TracingConfig{Enabled: false},326 ListTargets: &ListTargetsConfig{327 Enabled: false,328 },329 Targets: map[string]*TargetConfig{330 "test": {331 Name: "test",332 Mount: &MountConfig{333 Path: []string{"/test/"},334 },335 Bucket: &BucketConfig{336 Name: "bucket1",337 Region: "us-east-1",338 S3ListMaxKeys: 1000,339 },340 Actions: &ActionsConfig{341 GET: &GetActionConfig{Enabled: true},342 },343 Templates: &TargetTemplateConfig{},344 },345 "test2": {346 Name: "test2",347 Mount: &MountConfig{348 Path: []string{"/test2/"},349 },350 Bucket: &BucketConfig{351 Name: "bucket2",352 Region: "us-east-1",353 S3ListMaxKeys: 1000,354 },355 Actions: &ActionsConfig{356 GET: &GetActionConfig{Enabled: true},357 },358 Templates: &TargetTemplateConfig{},359 },360 },361 },362 },363 {364 name: "Test disable server compress",365 configs: map[string]string{366 "config.yaml": `367server:368 compress:369 enabled: false370targets:371 test:372 mount:373 path: /test/374 bucket:375 name: bucket1376 region: us-east-1377`,378 },379 wantErr: false,380 expectedResult: &Config{381 Log: &LogConfig{382 Level: "info",383 Format: "json",384 },385 Server: &ServerConfig{386 Port: 8080,387 Compress: &ServerCompressConfig{388 Enabled: &falseValue,389 Level: DefaultServerCompressLevel,390 Types: DefaultServerCompressTypes,391 },392 Timeouts: svrTimeoutsCfg,393 },394 InternalServer: &ServerConfig{395 Port: 9090,396 Compress: svrCompressCfg,397 Timeouts: svrTimeoutsCfg,398 },399 Templates: defaultTemplateCfg,400 Tracing: &TracingConfig{Enabled: false},401 ListTargets: &ListTargetsConfig{402 Enabled: false,403 },404 Targets: map[string]*TargetConfig{405 "test": {406 Name: "test",407 Mount: &MountConfig{408 Path: []string{"/test/"},409 },410 Bucket: &BucketConfig{411 Name: "bucket1",412 Region: "us-east-1",413 S3ListMaxKeys: 1000,414 },415 Actions: &ActionsConfig{416 GET: &GetActionConfig{Enabled: true},417 },418 Templates: &TargetTemplateConfig{},419 },420 },421 },422 },423 {424 name: "override template should have default headers templates when not override",425 configs: map[string]string{426 "config.yaml": `427targets:428 test:429 mount:430 path: /test/431 templates:432 folderList:433 path: fake1.tpl434 headers:435 override: value436 notFoundError:437 path: fake2.tpl438 internalServerError:439 path: fake2.tpl440 forbiddenError:441 path: fake2.tpl442 unauthorizedError:443 path: fake2.tpl444 badRequestError:445 path: fake2.tpl446 put:447 path: fake2.tpl448 delete:449 path: fake2.tpl450 bucket:451 name: bucket1452 region: us-east-1453`,454 },455 wantErr: false,456 expectedResult: &Config{457 Log: &LogConfig{458 Level: "info",459 Format: "json",460 },461 Server: &ServerConfig{462 Port: 8080,463 Compress: svrCompressCfg,464 Timeouts: svrTimeoutsCfg,465 },466 InternalServer: &ServerConfig{467 Port: 9090,468 Compress: svrCompressCfg,469 Timeouts: svrTimeoutsCfg,470 },471 Templates: defaultTemplateCfg,472 Tracing: &TracingConfig{Enabled: false},473 ListTargets: &ListTargetsConfig{474 Enabled: false,475 },476 Targets: map[string]*TargetConfig{477 "test": {478 Name: "test",479 Mount: &MountConfig{480 Path: []string{"/test/"},481 },482 Bucket: &BucketConfig{483 Name: "bucket1",484 Region: "us-east-1",485 S3ListMaxKeys: 1000,486 },487 Actions: &ActionsConfig{488 GET: &GetActionConfig{Enabled: true},489 },490 Templates: &TargetTemplateConfig{491 FolderList: &TargetTemplateConfigItem{492 Path: "fake1.tpl",493 Headers: map[string]string{494 "override": "value",495 },496 },497 NotFoundError: &TargetTemplateConfigItem{498 Path: "fake2.tpl",499 Headers: map[string]string{500 "Content-Type": "{{ template \"main.headers.contentType\" . }}",501 },502 },503 InternalServerError: &TargetTemplateConfigItem{504 Path: "fake2.tpl",505 Headers: map[string]string{506 "Content-Type": "{{ template \"main.headers.contentType\" . }}",507 },508 },509 UnauthorizedError: &TargetTemplateConfigItem{510 Path: "fake2.tpl",511 Headers: map[string]string{512 "Content-Type": "{{ template \"main.headers.contentType\" . }}",513 },514 },515 ForbiddenError: &TargetTemplateConfigItem{516 Path: "fake2.tpl",517 Headers: map[string]string{518 "Content-Type": "{{ template \"main.headers.contentType\" . }}",519 },520 },521 BadRequestError: &TargetTemplateConfigItem{522 Path: "fake2.tpl",523 Headers: map[string]string{524 "Content-Type": "{{ template \"main.headers.contentType\" . }}",525 },526 },527 Put: &TargetTemplateConfigItem{528 Path: "fake2.tpl",529 Headers: map[string]string{},530 },531 Delete: &TargetTemplateConfigItem{532 Path: "fake2.tpl",533 Headers: map[string]string{},534 },535 },536 },537 },538 },539 },540 {541 name: "override template status code should work in general and target override",542 configs: map[string]string{543 "config.yaml": `544templates:545 folderList:546 status: "400"547targets:548 test:549 mount:550 path: /test/551 templates:552 folderList:553 path: fake1.tpl554 headers:555 override: value556 status: "499"557 notFoundError:558 path: fake2.tpl559 internalServerError:560 path: fake2.tpl561 forbiddenError:562 path: fake2.tpl563 unauthorizedError:564 path: fake2.tpl565 badRequestError:566 path: fake2.tpl567 bucket:568 name: bucket1569 region: us-east-1570`,571 },572 wantErr: false,573 expectedResult: &Config{574 Log: &LogConfig{575 Level: "info",576 Format: "json",577 },578 Server: &ServerConfig{579 Port: 8080,580 Compress: svrCompressCfg,581 Timeouts: svrTimeoutsCfg,582 },583 InternalServer: &ServerConfig{584 Port: 9090,585 Compress: svrCompressCfg,586 Timeouts: svrTimeoutsCfg,587 },588 Templates: &TemplateConfig{589 Helpers: []string{"templates/_helpers.tpl"},590 FolderList: &TemplateConfigItem{591 Path: "templates/folder-list.tpl",592 Headers: map[string]string{593 "Content-Type": "{{ template \"main.headers.contentType\" . }}",594 },595 Status: "400",596 },597 TargetList: &TemplateConfigItem{598 Path: "templates/target-list.tpl",599 Headers: map[string]string{600 "Content-Type": "{{ template \"main.headers.contentType\" . }}",601 },602 Status: "200",603 },604 NotFoundError: &TemplateConfigItem{605 Path: "templates/not-found-error.tpl",606 Headers: map[string]string{607 "Content-Type": "{{ template \"main.headers.contentType\" . }}",608 },609 Status: "404",610 },611 InternalServerError: &TemplateConfigItem{612 Path: "templates/internal-server-error.tpl",613 Headers: map[string]string{614 "Content-Type": "{{ template \"main.headers.contentType\" . }}",615 },616 Status: "500",617 },618 UnauthorizedError: &TemplateConfigItem{619 Path: "templates/unauthorized-error.tpl",620 Headers: map[string]string{621 "Content-Type": "{{ template \"main.headers.contentType\" . }}",622 },623 Status: "401",624 },625 ForbiddenError: &TemplateConfigItem{626 Path: "templates/forbidden-error.tpl",627 Headers: map[string]string{628 "Content-Type": "{{ template \"main.headers.contentType\" . }}",629 },630 Status: "403",631 },632 BadRequestError: &TemplateConfigItem{633 Path: "templates/bad-request-error.tpl",634 Headers: map[string]string{635 "Content-Type": "{{ template \"main.headers.contentType\" . }}",636 },637 Status: "400",638 },639 Put: &TemplateConfigItem{640 Path: "templates/put.tpl",641 Headers: map[string]string{},642 Status: "204",643 },644 Delete: &TemplateConfigItem{645 Path: "templates/delete.tpl",646 Headers: map[string]string{},647 Status: "204",648 },649 },650 Tracing: &TracingConfig{Enabled: false},651 ListTargets: &ListTargetsConfig{652 Enabled: false,653 },654 Targets: map[string]*TargetConfig{655 "test": {656 Name: "test",657 Mount: &MountConfig{658 Path: []string{"/test/"},659 },660 Bucket: &BucketConfig{661 Name: "bucket1",662 Region: "us-east-1",663 S3ListMaxKeys: 1000,664 },665 Actions: &ActionsConfig{666 GET: &GetActionConfig{Enabled: true},667 },668 Templates: &TargetTemplateConfig{669 FolderList: &TargetTemplateConfigItem{670 Path: "fake1.tpl",671 Headers: map[string]string{672 "override": "value",673 },674 Status: "499",675 },676 NotFoundError: &TargetTemplateConfigItem{677 Path: "fake2.tpl",678 Headers: map[string]string{679 "Content-Type": "{{ template \"main.headers.contentType\" . }}",680 },681 },682 InternalServerError: &TargetTemplateConfigItem{683 Path: "fake2.tpl",684 Headers: map[string]string{685 "Content-Type": "{{ template \"main.headers.contentType\" . }}",686 },687 },688 UnauthorizedError: &TargetTemplateConfigItem{689 Path: "fake2.tpl",690 Headers: map[string]string{691 "Content-Type": "{{ template \"main.headers.contentType\" . }}",692 },693 },694 ForbiddenError: &TargetTemplateConfigItem{695 Path: "fake2.tpl",696 Headers: map[string]string{697 "Content-Type": "{{ template \"main.headers.contentType\" . }}",698 },699 },700 BadRequestError: &TargetTemplateConfigItem{701 Path: "fake2.tpl",702 Headers: map[string]string{703 "Content-Type": "{{ template \"main.headers.contentType\" . }}",704 },705 },706 },707 },708 },709 },710 },711 {712 name: "Test server compress configurations error (level)",713 configs: map[string]string{714 "config.yaml": `715server:716 compress:717 enabled: true718 level: 0719targets:720 test:721 mount:722 path: /test/723 bucket:724 name: bucket1725 region: us-east-1726`,727 },728 wantErr: true,729 },730 {731 name: "Test server compress configurations error (types)",732 configs: map[string]string{733 "config.yaml": `734server:735 compress:736 enabled: true737 types: []738targets:739 test:740 mount:741 path: /test/742 bucket:743 name: bucket1744 region: us-east-1745`,746 },747 wantErr: true,748 },749 {750 name: "Test secrets from environment variable",751 configs: map[string]string{752 "config.yaml": `753targets:754 test:755 mount:756 path: /test/757 bucket:758 name: bucket1759 region: us-east-1760 credentials:761 accessKey:762 env: ENV1763 secretKey:764 env: ENV2`,765 },766 envVariables: map[string]string{767 "ENV1": "VALUE1",768 "ENV2": "VALUE2",769 },770 wantErr: false,771 expectedResult: &Config{772 Log: &LogConfig{773 Level: "info",774 Format: "json",775 },776 Server: &ServerConfig{777 Port: 8080,778 Compress: svrCompressCfg,779 Timeouts: svrTimeoutsCfg,780 },781 InternalServer: &ServerConfig{782 Port: 9090,783 Compress: svrCompressCfg,784 Timeouts: svrTimeoutsCfg,785 },786 Templates: defaultTemplateCfg,787 Tracing: &TracingConfig{Enabled: false},788 ListTargets: &ListTargetsConfig{789 Enabled: false,790 },791 Targets: map[string]*TargetConfig{792 "test": {793 Name: "test",794 Mount: &MountConfig{795 Path: []string{"/test/"},796 },797 Bucket: &BucketConfig{798 Name: "bucket1",799 Region: "us-east-1",800 S3ListMaxKeys: 1000,801 Credentials: &BucketCredentialConfig{802 AccessKey: &CredentialConfig{803 Env: "ENV1",804 Value: "VALUE1",805 },806 SecretKey: &CredentialConfig{807 Env: "ENV2",808 Value: "VALUE2",809 },810 },811 },812 Actions: &ActionsConfig{813 GET: &GetActionConfig{Enabled: true},814 },815 Templates: &TargetTemplateConfig{},816 },817 },818 },819 },820 {821 name: "Test secrets from environment variable with empty environment variable",822 configs: map[string]string{823 "config.yaml": `824targets:825 test:826 mount:827 path: /test/828 bucket:829 name: bucket1830 region: us-east-1831 credentials:832 accessKey:833 env: ENV1834 secretKey:835 env: ENV2`,836 },837 envVariables: map[string]string{838 "ENV1": "VALUE1",839 },840 wantErr: true,841 },842 {843 name: "Test secrets from a not found file",844 configs: map[string]string{845 "config.yaml": `846targets:847 test:848 mount:849 path: /test/850 bucket:851 name: bucket1852 region: us-east-1853 credentials:854 accessKey:855 path: ` + secret1Filename + `856 secretKey:857 value: VALUE2`,858 },859 wantErr: true,860 },861 {862 name: "Test secrets from a file and direct value",863 configs: map[string]string{864 "config.yaml": `865targets:866 test:867 mount:868 path: /test/869 bucket:870 name: bucket1871 region: us-east-1872 credentials:873 accessKey:874 path: ` + secret1Filename + `875 secretKey:876 value: VALUE2`,877 },878 secretFiles: map[string]string{879 secret1Filename: "VALUE1",880 },881 wantErr: false,882 expectedResult: &Config{883 Log: &LogConfig{884 Level: "info",885 Format: "json",886 },887 Server: &ServerConfig{888 Port: 8080,889 Compress: svrCompressCfg,890 Timeouts: svrTimeoutsCfg,891 },892 InternalServer: &ServerConfig{893 Port: 9090,894 Compress: svrCompressCfg,895 Timeouts: svrTimeoutsCfg,896 },897 Templates: defaultTemplateCfg,898 Tracing: &TracingConfig{Enabled: false},899 ListTargets: &ListTargetsConfig{900 Enabled: false,901 },902 Targets: map[string]*TargetConfig{903 "test": {904 Name: "test",905 Mount: &MountConfig{906 Path: []string{"/test/"},907 },908 Bucket: &BucketConfig{909 Name: "bucket1",910 Region: "us-east-1",911 S3ListMaxKeys: 1000,912 Credentials: &BucketCredentialConfig{913 AccessKey: &CredentialConfig{914 Path: secret1Filename,915 Value: "VALUE1",916 },917 SecretKey: &CredentialConfig{918 Value: "VALUE2",919 },920 },921 },922 Actions: &ActionsConfig{923 GET: &GetActionConfig{Enabled: true},924 },925 Templates: &TargetTemplateConfig{},926 },927 },928 },929 },930 {931 name: "Test secrets from a file and direct value with new lines cleaned",932 configs: map[string]string{933 "config.yaml": `934targets:935 test:936 mount:937 path: /test/938 bucket:939 name: bucket1940 region: us-east-1941 credentials:942 accessKey:943 path: ` + secretWithNewLineFilename + `944 secretKey:945 value: VALUE2`,946 },947 secretFiles: map[string]string{948 secretWithNewLineFilename: `949VALUE1950`,951 },952 wantErr: false,953 expectedResult: &Config{954 Log: &LogConfig{955 Level: "info",956 Format: "json",957 },958 Server: &ServerConfig{959 Port: 8080,960 Compress: svrCompressCfg,961 Timeouts: svrTimeoutsCfg,962 },963 InternalServer: &ServerConfig{964 Port: 9090,965 Compress: svrCompressCfg,966 Timeouts: svrTimeoutsCfg,967 },968 Templates: defaultTemplateCfg,969 Tracing: &TracingConfig{Enabled: false},970 ListTargets: &ListTargetsConfig{971 Enabled: false,972 },973 Targets: map[string]*TargetConfig{974 "test": {975 Name: "test",976 Mount: &MountConfig{977 Path: []string{"/test/"},978 },979 Bucket: &BucketConfig{980 Name: "bucket1",981 Region: "us-east-1",982 S3ListMaxKeys: 1000,983 Credentials: &BucketCredentialConfig{984 AccessKey: &CredentialConfig{985 Path: secretWithNewLineFilename,986 Value: "VALUE1",987 },988 SecretKey: &CredentialConfig{989 Value: "VALUE2",990 },991 },992 },993 Actions: &ActionsConfig{994 GET: &GetActionConfig{Enabled: true},995 },996 Templates: &TargetTemplateConfig{},997 },998 },999 },1000 },1001 {1002 name: "should fail when target templates configuration are invalid",1003 configs: map[string]string{1004 "config.yaml": `1005targets:1006 test:1007 mount:1008 path: /test/1009 templates:1010 notFoundError:1011 inBucket: false1012 bucket:1013 name: bucket11014 region: us-east-11015 credentials:1016 accessKey:1017 value: ENV11018 secretKey:1019 value: ENV2`,1020 },1021 wantErr: true,1022 },1023 {1024 name: "should load complete configuration with target custom templates",1025 configs: map[string]string{1026 "config.yaml": `1027targets:1028 test:1029 mount:1030 path: /test/1031 templates:1032 notFoundError:1033 inBucket: false1034 path: "/fake"1035 headers:1036 h1: "{{ .Fake }}"1037 internalServerError:1038 inBucket: true1039 path: "/fake2"1040 headers:1041 h2: "{{ .Fake }}"1042 bucket:1043 name: bucket11044 region: us-east-11045 credentials:1046 accessKey:1047 value: VALUE11048 secretKey:1049 value: VALUE2`,1050 },1051 wantErr: false,1052 expectedResult: &Config{1053 Log: &LogConfig{1054 Level: "info",1055 Format: "json",1056 },1057 Server: &ServerConfig{1058 Port: 8080,1059 Compress: svrCompressCfg,1060 Timeouts: svrTimeoutsCfg,1061 },1062 InternalServer: &ServerConfig{1063 Port: 9090,1064 Compress: svrCompressCfg,1065 Timeouts: svrTimeoutsCfg,1066 },1067 Templates: defaultTemplateCfg,1068 Tracing: &TracingConfig{Enabled: false},1069 ListTargets: &ListTargetsConfig{1070 Enabled: false,1071 },1072 Targets: map[string]*TargetConfig{1073 "test": {1074 Name: "test",1075 Mount: &MountConfig{1076 Path: []string{"/test/"},1077 },1078 Bucket: &BucketConfig{1079 Name: "bucket1",1080 Region: "us-east-1",1081 S3ListMaxKeys: 1000,1082 Credentials: &BucketCredentialConfig{1083 AccessKey: &CredentialConfig{1084 Value: "VALUE1",1085 },1086 SecretKey: &CredentialConfig{1087 Value: "VALUE2",1088 },1089 },1090 },1091 Actions: &ActionsConfig{1092 GET: &GetActionConfig{Enabled: true},1093 },1094 Templates: &TargetTemplateConfig{1095 NotFoundError: &TargetTemplateConfigItem{1096 InBucket: false,1097 Path: "/fake",1098 Headers: map[string]string{"h1": "{{ .Fake }}"},1099 },1100 InternalServerError: &TargetTemplateConfigItem{1101 InBucket: true,1102 Path: "/fake2",1103 Headers: map[string]string{"h2": "{{ .Fake }}"},1104 },1105 },1106 },1107 },1108 },1109 },1110 {1111 name: "Test with multiple configuration files",1112 configs: map[string]string{1113 "log.yaml": `1114log:1115 level: error1116`,1117 "targets.yaml": `1118targets:1119 test:1120 mount:1121 path: /test/1122 bucket:1123 name: bucket11124 region: us-east-11125 credentials:1126 accessKey:1127 value: value11128 secretKey:1129 value: value2`,1130 },1131 wantErr: false,1132 expectedResult: &Config{1133 Log: &LogConfig{1134 Level: "error",1135 Format: "json",1136 },1137 Server: &ServerConfig{1138 Port: 8080,1139 Compress: svrCompressCfg,1140 Timeouts: svrTimeoutsCfg,1141 },1142 InternalServer: &ServerConfig{1143 Port: 9090,1144 Compress: svrCompressCfg,1145 Timeouts: svrTimeoutsCfg,1146 },1147 Templates: defaultTemplateCfg,1148 Tracing: &TracingConfig{Enabled: false},1149 ListTargets: &ListTargetsConfig{1150 Enabled: false,1151 },1152 Targets: map[string]*TargetConfig{1153 "test": {1154 Name: "test",1155 Mount: &MountConfig{1156 Path: []string{"/test/"},1157 },1158 Bucket: &BucketConfig{1159 Name: "bucket1",1160 Region: "us-east-1",1161 S3ListMaxKeys: 1000,1162 Credentials: &BucketCredentialConfig{1163 AccessKey: &CredentialConfig{1164 Value: "value1",1165 },1166 SecretKey: &CredentialConfig{1167 Value: "value2",1168 },1169 },1170 },1171 Actions: &ActionsConfig{1172 GET: &GetActionConfig{Enabled: true},1173 },1174 Templates: &TargetTemplateConfig{},1175 },1176 },1177 },1178 },1179 {1180 name: "Test key rewrite list",1181 configs: map[string]string{1182 "cfg.yaml": `1183log:1184 level: error1185targets:1186 test:1187 mount:1188 path: /test/1189 keyRewriteList:1190 - source: ^/(?P<one>\w+)/file.html$1191 target: /$one/fake/$one/file.html1192 bucket:1193 name: bucket11194 region: us-east-11195 credentials:1196 accessKey:1197 value: value11198 secretKey:1199 value: value2`,1200 },1201 wantErr: false,1202 expectedResult: &Config{1203 Log: &LogConfig{1204 Level: "error",1205 Format: "json",1206 },1207 Server: &ServerConfig{1208 Port: 8080,1209 Compress: svrCompressCfg,1210 Timeouts: svrTimeoutsCfg,1211 },1212 InternalServer: &ServerConfig{1213 Port: 9090,1214 Compress: svrCompressCfg,1215 Timeouts: svrTimeoutsCfg,1216 },1217 Templates: defaultTemplateCfg,1218 Tracing: &TracingConfig{Enabled: false},1219 ListTargets: &ListTargetsConfig{1220 Enabled: false,1221 },1222 Targets: map[string]*TargetConfig{1223 "test": {1224 Name: "test",1225 Mount: &MountConfig{1226 Path: []string{"/test/"},1227 },1228 KeyRewriteList: []*TargetKeyRewriteConfig{{1229 Source: `^/(?P<one>\w+)/file.html$`,1230 SourceRegex: regexp.MustCompile(`^/(?P<one>\w+)/file.html$`),1231 Target: "/$one/fake/$one/file.html",1232 TargetType: RegexTargetKeyRewriteTargetType,1233 }},1234 Bucket: &BucketConfig{1235 Name: "bucket1",1236 Region: "us-east-1",1237 S3ListMaxKeys: 1000,1238 Credentials: &BucketCredentialConfig{1239 AccessKey: &CredentialConfig{1240 Value: "value1",1241 },1242 SecretKey: &CredentialConfig{1243 Value: "value2",1244 },1245 },1246 },1247 Actions: &ActionsConfig{1248 GET: &GetActionConfig{Enabled: true},1249 },1250 Templates: &TargetTemplateConfig{},1251 },1252 },1253 },1254 },1255 }1256 for _, tt := range tests {1257 t.Run(tt.name, func(t *testing.T) {1258 dir, err := ioutil.TempDir("", "s3-proxy-config")1259 if err != nil {1260 t.Error(err)1261 return1262 }1263 defer os.RemoveAll(dir) // clean up1264 for k, v := range tt.configs {1265 tmpfn := filepath.Join(dir, k)1266 err = ioutil.WriteFile(tmpfn, []byte(v), 0666)1267 if err != nil {1268 t.Error(err)1269 return1270 }1271 }1272 // Set environment variables1273 for k, v := range tt.envVariables {1274 os.Setenv(k, v)1275 defer os.Unsetenv(k)1276 }1277 // Create secret files1278 for k, v := range tt.secretFiles {1279 dirToCr := filepath.Dir(k)1280 err = os.MkdirAll(dirToCr, 0666)1281 if err != nil {1282 t.Error(err)1283 return1284 }1285 err = ioutil.WriteFile(k, []byte(v), 0666)1286 if err != nil {1287 t.Error(err)1288 return1289 }1290 defer os.Remove(k)1291 }1292 // Change var for main configuration file1293 mainConfigFolderPath = dir1294 ctx := &managercontext{1295 logger: log.NewLogger(),1296 }1297 // Load config1298 err = ctx.Load()1299 if (err != nil) != tt.wantErr {1300 t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)1301 return1302 }1303 // Get configuration1304 res := ctx.GetConfig()1305 assert.Equal(t, tt.expectedResult, res)1306 })1307 }1308}1309func Test_Load_reload_config(t *testing.T) {1310 svrCompressCfg := &ServerCompressConfig{1311 Enabled: &DefaultServerCompressEnabled,1312 Level: DefaultServerCompressLevel,1313 Types: DefaultServerCompressTypes,1314 }1315 svrTimeoutsCfg := &ServerTimeoutsConfig{1316 ReadHeaderTimeout: DefaultServerTimeoutsReadHeaderTimeout,1317 }1318 // Channel for wait watch1319 waitCh := make(chan bool)1320 dir, err := ioutil.TempDir("", "s3-proxy-config-reload")1321 assert.NoError(t, err)1322 secret1Filename := path.Join(os.TempDir(), "secret1")1323 configs := map[string]string{1324 "log.yaml": `1325log:1326 level: error1327`,1328 "targets.yaml": `1329targets:1330 test:1331 mount:1332 path: /test/1333 bucket:1334 name: bucket11335 region: us-east-11336 credentials:1337 accessKey:1338 path: ` + secret1Filename + `1339 secretKey:1340 value: value2`,1341 }1342 defer os.RemoveAll(dir) // clean up1343 for k, v := range configs {1344 tmpfn := filepath.Join(dir, k)1345 err = ioutil.WriteFile(tmpfn, []byte(v), 0666)1346 assert.NoError(t, err)1347 }1348 secretFiles := map[string]string{1349 secret1Filename: "VALUE1",1350 }1351 // Create secret files1352 for k, v := range secretFiles {1353 dirToCr := filepath.Dir(k)1354 err = os.MkdirAll(dirToCr, 0666)1355 assert.NoError(t, err)1356 err = ioutil.WriteFile(k, []byte(v), 0666)1357 assert.NoError(t, err)1358 defer os.Remove(k)1359 }1360 // Change var for main configuration file1361 mainConfigFolderPath = dir1362 ctx := &managercontext{1363 logger: log.NewLogger(),1364 }1365 ctx.AddOnChangeHook(func() {1366 waitCh <- true1367 })1368 // Load config1369 err = ctx.Load()1370 assert.NoError(t, err)1371 // Get configuration1372 res := ctx.GetConfig()1373 assert.Equal(t, &Config{1374 Log: &LogConfig{1375 Level: "error",1376 Format: "json",1377 },1378 Server: &ServerConfig{1379 Port: 8080,1380 Compress: svrCompressCfg,1381 Timeouts: svrTimeoutsCfg,1382 },1383 InternalServer: &ServerConfig{1384 Port: 9090,1385 Compress: svrCompressCfg,1386 Timeouts: svrTimeoutsCfg,1387 },1388 Templates: defaultTemplateCfg,1389 Tracing: &TracingConfig{Enabled: false},1390 ListTargets: &ListTargetsConfig{1391 Enabled: false,1392 },1393 Targets: map[string]*TargetConfig{1394 "test": {1395 Name: "test",1396 Mount: &MountConfig{1397 Path: []string{"/test/"},1398 },1399 Bucket: &BucketConfig{1400 Name: "bucket1",1401 Region: "us-east-1",1402 S3ListMaxKeys: 1000,1403 Credentials: &BucketCredentialConfig{1404 AccessKey: &CredentialConfig{1405 Value: "VALUE1",1406 Path: secret1Filename,1407 },1408 SecretKey: &CredentialConfig{1409 Value: "value2",1410 },1411 },1412 },1413 Actions: &ActionsConfig{1414 GET: &GetActionConfig{Enabled: true},1415 },1416 Templates: &TargetTemplateConfig{},1417 },1418 },1419 }, res)1420 configs = map[string]string{1421 "log.yaml": `1422log:1423 level: debug1424 format: text1425`,1426 }1427 defer os.RemoveAll(dir) // clean up1428 for k, v := range configs {1429 tmpfn := filepath.Join(dir, k)1430 err = ioutil.WriteFile(tmpfn, []byte(v), 0666)1431 assert.NoError(t, err)1432 }1433 select {1434 case <-waitCh:1435 // Get configuration1436 res = ctx.GetConfig()1437 assert.Equal(t, &Config{1438 Log: &LogConfig{1439 Level: "debug",1440 Format: "text",1441 },1442 Server: &ServerConfig{1443 Port: 8080,1444 Compress: svrCompressCfg,1445 Timeouts: svrTimeoutsCfg,1446 },1447 InternalServer: &ServerConfig{1448 Port: 9090,1449 Compress: svrCompressCfg,1450 Timeouts: svrTimeoutsCfg,1451 },1452 Templates: defaultTemplateCfg,1453 Tracing: &TracingConfig{Enabled: false},1454 ListTargets: &ListTargetsConfig{1455 Enabled: false,1456 },1457 Targets: map[string]*TargetConfig{1458 "test": {1459 Name: "test",1460 Mount: &MountConfig{1461 Path: []string{"/test/"},1462 },1463 Bucket: &BucketConfig{1464 Name: "bucket1",1465 Region: "us-east-1",1466 S3ListMaxKeys: 1000,1467 Credentials: &BucketCredentialConfig{1468 AccessKey: &CredentialConfig{1469 Value: "VALUE1",1470 Path: secret1Filename,1471 },1472 SecretKey: &CredentialConfig{1473 Value: "value2",1474 },1475 },1476 },1477 Actions: &ActionsConfig{1478 GET: &GetActionConfig{Enabled: true},1479 },1480 Templates: &TargetTemplateConfig{},1481 },1482 },1483 }, res)1484 return1485 case <-time.After(5 * time.Second):1486 assert.FailNow(t, "shouldn't call this")1487 }1488}1489func Test_Load_reload_secret(t *testing.T) {1490 svrCompressCfg := &ServerCompressConfig{1491 Enabled: &DefaultServerCompressEnabled,1492 Level: DefaultServerCompressLevel,1493 Types: DefaultServerCompressTypes,1494 }1495 svrTimeoutsCfg := &ServerTimeoutsConfig{1496 ReadHeaderTimeout: DefaultServerTimeoutsReadHeaderTimeout,1497 }1498 // Channel for wait watch1499 waitCh := make(chan bool)1500 dir, err := ioutil.TempDir("", "s3-proxy-config-reload-secret")1501 assert.NoError(t, err)1502 secret1Filename := path.Join(os.TempDir(), "secret1")1503 configs := map[string]string{1504 "log.yaml": `1505log:1506 level: error1507 format: text1508`,1509 "targets.yaml": `1510targets:1511 test:1512 mount:1513 path: /test/1514 bucket:1515 name: bucket11516 region: us-east-11517 credentials:1518 accessKey:1519 path: ` + secret1Filename + `1520 secretKey:1521 value: value2`,1522 }1523 defer os.RemoveAll(dir) // clean up1524 for k, v := range configs {1525 tmpfn := filepath.Join(dir, k)1526 err = ioutil.WriteFile(tmpfn, []byte(v), 0666)1527 assert.NoError(t, err)1528 }1529 secretFiles := map[string]string{1530 secret1Filename: "VALUE1",1531 }1532 // Create secret files1533 for k, v := range secretFiles {1534 dirToCr := filepath.Dir(k)1535 err = os.MkdirAll(dirToCr, 0666)1536 assert.NoError(t, err)1537 err = ioutil.WriteFile(k, []byte(v), 0666)1538 assert.NoError(t, err)1539 defer os.Remove(k)1540 }1541 // Change var for main configuration file1542 mainConfigFolderPath = dir1543 ctx := &managercontext{1544 logger: log.NewLogger(),1545 }1546 ctx.AddOnChangeHook(func() {1547 waitCh <- true1548 })1549 // Load config1550 err = ctx.Load()1551 assert.NoError(t, err)1552 // Get configuration1553 res := ctx.GetConfig()1554 assert.Equal(t, &Config{1555 Log: &LogConfig{1556 Level: "error",1557 Format: "text",1558 },1559 Server: &ServerConfig{1560 Port: 8080,1561 Compress: svrCompressCfg,1562 Timeouts: svrTimeoutsCfg,1563 },1564 InternalServer: &ServerConfig{1565 Port: 9090,1566 Compress: svrCompressCfg,1567 Timeouts: svrTimeoutsCfg,1568 },1569 Templates: defaultTemplateCfg,1570 Tracing: &TracingConfig{Enabled: false},1571 ListTargets: &ListTargetsConfig{1572 Enabled: false,1573 },1574 Targets: map[string]*TargetConfig{1575 "test": {1576 Name: "test",1577 Mount: &MountConfig{1578 Path: []string{"/test/"},1579 },1580 Bucket: &BucketConfig{1581 Name: "bucket1",1582 Region: "us-east-1",1583 S3ListMaxKeys: 1000,1584 Credentials: &BucketCredentialConfig{1585 AccessKey: &CredentialConfig{1586 Value: "VALUE1",1587 Path: secret1Filename,1588 },1589 SecretKey: &CredentialConfig{1590 Value: "value2",1591 },1592 },1593 },1594 Actions: &ActionsConfig{1595 GET: &GetActionConfig{Enabled: true},1596 },1597 Templates: &TargetTemplateConfig{},1598 },1599 },1600 }, res)1601 secretFiles = make(map[string]string)1602 secretFiles[secret1Filename] = "SECRET1"1603 // Create secret files1604 for k, v := range secretFiles {1605 dirToCr := filepath.Dir(k)1606 err = os.MkdirAll(dirToCr, 0666)1607 assert.NoError(t, err)1608 err = ioutil.WriteFile(k, []byte(v), 0666)1609 assert.NoError(t, err)1610 defer os.Remove(k)1611 }1612 select {1613 case <-waitCh:1614 // Get configuration1615 res = ctx.GetConfig()1616 assert.Equal(t, &Config{1617 Log: &LogConfig{1618 Level: "error",1619 Format: "text",1620 },1621 Server: &ServerConfig{1622 Port: 8080,1623 Compress: svrCompressCfg,1624 Timeouts: svrTimeoutsCfg,1625 },1626 InternalServer: &ServerConfig{1627 Port: 9090,1628 Compress: svrCompressCfg,1629 Timeouts: svrTimeoutsCfg,1630 },1631 Templates: defaultTemplateCfg,1632 Tracing: &TracingConfig{Enabled: false},1633 ListTargets: &ListTargetsConfig{1634 Enabled: false,1635 },1636 Targets: map[string]*TargetConfig{1637 "test": {1638 Name: "test",1639 Mount: &MountConfig{1640 Path: []string{"/test/"},1641 },1642 Bucket: &BucketConfig{1643 Name: "bucket1",1644 Region: "us-east-1",1645 S3ListMaxKeys: 1000,1646 Credentials: &BucketCredentialConfig{1647 AccessKey: &CredentialConfig{1648 Value: "SECRET1",1649 Path: secret1Filename,1650 },1651 SecretKey: &CredentialConfig{1652 Value: "value2",1653 },1654 },1655 },1656 Actions: &ActionsConfig{1657 GET: &GetActionConfig{Enabled: true},1658 },1659 Templates: &TargetTemplateConfig{},1660 },1661 },1662 }, res)1663 return1664 case <-time.After(5 * time.Second):1665 assert.FailNow(t, "shouldn't call this")1666 }1667}1668func Test_Load_reload_config_with_wrong_config(t *testing.T) {1669 svrCompressCfg := &ServerCompressConfig{1670 Enabled: &DefaultServerCompressEnabled,1671 Level: DefaultServerCompressLevel,1672 Types: DefaultServerCompressTypes,1673 }1674 svrTimeoutsCfg := &ServerTimeoutsConfig{1675 ReadHeaderTimeout: DefaultServerTimeoutsReadHeaderTimeout,1676 }1677 // Channel for wait watch1678 waitCh := make(chan bool)1679 dir, err := ioutil.TempDir("", "s3-proxy-config-reload-wrong-config")1680 assert.NoError(t, err)1681 secret1Filename := path.Join(os.TempDir(), "secret1")1682 configs := map[string]string{1683 "log.yaml": `1684log:1685 level: error1686 format: text1687`,1688 "targets.yaml": `1689targets:1690 test:1691 mount:1692 path: /test/1693 bucket:1694 name: bucket11695 region: us-east-11696 credentials:1697 accessKey:1698 path: ` + secret1Filename + `1699 secretKey:1700 value: value2`,1701 }1702 defer os.RemoveAll(dir) // clean up1703 for k, v := range configs {1704 tmpfn := filepath.Join(dir, k)1705 err = ioutil.WriteFile(tmpfn, []byte(v), 0666)1706 assert.NoError(t, err)1707 }1708 secretFiles := map[string]string{1709 secret1Filename: "VALUE1",1710 }1711 // Create secret files1712 for k, v := range secretFiles {1713 dirToCr := filepath.Dir(k)1714 err = os.MkdirAll(dirToCr, 0666)1715 assert.NoError(t, err)1716 err = ioutil.WriteFile(k, []byte(v), 0666)1717 assert.NoError(t, err)1718 defer os.Remove(k)1719 }1720 // Change var for main configuration file1721 mainConfigFolderPath = dir1722 ctx := &managercontext{1723 logger: log.NewLogger(),1724 }1725 ctx.AddOnChangeHook(func() {1726 waitCh <- true1727 })1728 // Load config1729 err = ctx.Load()1730 assert.NoError(t, err)1731 // Get configuration1732 res := ctx.GetConfig()1733 assert.Equal(t, &Config{1734 Log: &LogConfig{1735 Level: "error",1736 Format: "text",1737 },1738 Server: &ServerConfig{1739 Port: 8080,1740 Compress: svrCompressCfg,1741 Timeouts: svrTimeoutsCfg,1742 },1743 InternalServer: &ServerConfig{1744 Port: 9090,1745 Compress: svrCompressCfg,1746 Timeouts: svrTimeoutsCfg,1747 },1748 Templates: defaultTemplateCfg,1749 Tracing: &TracingConfig{Enabled: false},1750 ListTargets: &ListTargetsConfig{1751 Enabled: false,1752 },1753 Targets: map[string]*TargetConfig{1754 "test": {1755 Name: "test",1756 Mount: &MountConfig{1757 Path: []string{"/test/"},1758 },1759 Bucket: &BucketConfig{1760 Name: "bucket1",1761 Region: "us-east-1",1762 S3ListMaxKeys: 1000,1763 Credentials: &BucketCredentialConfig{1764 AccessKey: &CredentialConfig{1765 Value: "VALUE1",1766 Path: path.Join(os.TempDir(), "/secret1"),1767 },1768 SecretKey: &CredentialConfig{1769 Value: "value2",1770 },1771 },1772 },1773 Actions: &ActionsConfig{1774 GET: &GetActionConfig{Enabled: true},1775 },1776 Templates: &TargetTemplateConfig{},1777 },1778 },1779 }, res)1780 configs = make(map[string]string)1781 configs["log.yaml"] = `1782configuration with error1783`1784 defer os.RemoveAll(dir) // clean up1785 for k, v := range configs {1786 tmpfn := filepath.Join(dir, k)1787 err = ioutil.WriteFile(tmpfn, []byte(v), 0666)1788 assert.NoError(t, err)1789 }1790 select {1791 case <-waitCh:1792 assert.FailNow(t, "shouldn't call this")1793 return1794 case <-time.After(5 * time.Second):1795 // Get configuration1796 res = ctx.GetConfig()1797 assert.Equal(t, &Config{1798 Log: &LogConfig{1799 Level: "error",1800 Format: "text",1801 },1802 Server: &ServerConfig{1803 Port: 8080,1804 Compress: svrCompressCfg,1805 Timeouts: svrTimeoutsCfg,1806 },1807 InternalServer: &ServerConfig{1808 Port: 9090,1809 Compress: svrCompressCfg,1810 Timeouts: svrTimeoutsCfg,1811 },1812 Templates: defaultTemplateCfg,1813 Tracing: &TracingConfig{Enabled: false},1814 ListTargets: &ListTargetsConfig{1815 Enabled: false,1816 },1817 Targets: map[string]*TargetConfig{1818 "test": {1819 Name: "test",1820 Mount: &MountConfig{1821 Path: []string{"/test/"},1822 },1823 Bucket: &BucketConfig{1824 Name: "bucket1",1825 Region: "us-east-1",1826 S3ListMaxKeys: 1000,1827 Credentials: &BucketCredentialConfig{1828 AccessKey: &CredentialConfig{1829 Value: "VALUE1",1830 Path: secret1Filename,1831 },1832 SecretKey: &CredentialConfig{1833 Value: "value2",1834 },1835 },1836 },1837 Actions: &ActionsConfig{1838 GET: &GetActionConfig{Enabled: true},1839 },1840 Templates: &TargetTemplateConfig{},1841 },1842 },1843 }, res)1844 }1845}1846func Test_Load_reload_config_map_structure(t *testing.T) {1847 svrCompressCfg := &ServerCompressConfig{1848 Enabled: &DefaultServerCompressEnabled,1849 Level: DefaultServerCompressLevel,1850 Types: DefaultServerCompressTypes,1851 }1852 svrTimeoutsCfg := &ServerTimeoutsConfig{1853 ReadHeaderTimeout: DefaultServerTimeoutsReadHeaderTimeout,1854 }1855 // Channel for wait watch1856 waitCh := make(chan bool)1857 dir, err := ioutil.TempDir("", "s3-proxy-config-reload-map-structure")1858 assert.NoError(t, err)1859 secret1Filename := path.Join(os.TempDir(), "secret1")1860 configs := map[string]string{1861 "log.yaml": `1862log:1863 level: error1864`,1865 "providers.yaml": `1866authProviders:1867 basic:1868 provider1:1869 realm: prov11870 provider2:1871 realm: prov21872`,1873 "targets.yaml": `1874targets:1875 test:1876 mount:1877 path: /test/1878 bucket:1879 name: bucket11880 region: us-east-11881 credentials:1882 accessKey:1883 path: ` + secret1Filename + `1884 secretKey:1885 value: value2`,1886 }1887 defer os.RemoveAll(dir) // clean up1888 for k, v := range configs {1889 tmpfn := filepath.Join(dir, k)1890 err = ioutil.WriteFile(tmpfn, []byte(v), 0666)1891 assert.NoError(t, err)1892 }1893 secretFiles := map[string]string{1894 secret1Filename: "VALUE1",1895 }1896 // Create secret files1897 for k, v := range secretFiles {1898 dirToCr := filepath.Dir(k)1899 err = os.MkdirAll(dirToCr, 0666)1900 assert.NoError(t, err)1901 err = ioutil.WriteFile(k, []byte(v), 0666)1902 assert.NoError(t, err)1903 defer os.Remove(k)1904 }1905 // Change var for main configuration file1906 mainConfigFolderPath = dir1907 ctx := &managercontext{1908 logger: log.NewLogger(),1909 }1910 ctx.AddOnChangeHook(func() {1911 waitCh <- true1912 })1913 // Load config1914 err = ctx.Load()1915 assert.NoError(t, err)1916 // Get configuration1917 res := ctx.GetConfig()1918 assert.Equal(t, &Config{1919 Log: &LogConfig{1920 Level: "error",1921 Format: "json",1922 },1923 Server: &ServerConfig{1924 Port: 8080,1925 Compress: svrCompressCfg,1926 Timeouts: svrTimeoutsCfg,1927 },1928 InternalServer: &ServerConfig{1929 Port: 9090,1930 Compress: svrCompressCfg,1931 Timeouts: svrTimeoutsCfg,1932 },1933 Templates: defaultTemplateCfg,1934 AuthProviders: &AuthProviderConfig{1935 Basic: map[string]*BasicAuthConfig{1936 "provider1": {Realm: "prov1"},1937 "provider2": {Realm: "prov2"},1938 },1939 },1940 Tracing: &TracingConfig{Enabled: false},1941 ListTargets: &ListTargetsConfig{1942 Enabled: false,1943 },1944 Targets: map[string]*TargetConfig{1945 "test": {1946 Name: "test",1947 Mount: &MountConfig{1948 Path: []string{"/test/"},1949 },1950 Bucket: &BucketConfig{1951 Name: "bucket1",1952 Region: "us-east-1",1953 S3ListMaxKeys: 1000,1954 Credentials: &BucketCredentialConfig{1955 AccessKey: &CredentialConfig{1956 Value: "VALUE1",1957 Path: secret1Filename,1958 },1959 SecretKey: &CredentialConfig{1960 Value: "value2",1961 },1962 },1963 },1964 Actions: &ActionsConfig{1965 GET: &GetActionConfig{Enabled: true},1966 },1967 Templates: &TargetTemplateConfig{},1968 },1969 },1970 }, res)1971 configs = map[string]string{1972 "providers.yaml": `1973authProviders:1974 basic:1975 provider1:1976 realm: prov11977`,1978 }1979 defer os.RemoveAll(dir) // clean up1980 for k, v := range configs {1981 tmpfn := filepath.Join(dir, k)1982 err = ioutil.WriteFile(tmpfn, []byte(v), 0666)1983 assert.NoError(t, err)1984 }1985 select {1986 case <-waitCh:1987 // Get configuration1988 res = ctx.GetConfig()1989 assert.Equal(t, &Config{1990 Log: &LogConfig{1991 Level: "error",1992 Format: "json",1993 },1994 Server: &ServerConfig{1995 Port: 8080,1996 Compress: svrCompressCfg,1997 Timeouts: svrTimeoutsCfg,1998 },1999 InternalServer: &ServerConfig{2000 Port: 9090,2001 Compress: svrCompressCfg,2002 Timeouts: svrTimeoutsCfg,2003 },2004 Templates: defaultTemplateCfg,2005 Tracing: &TracingConfig{Enabled: false},2006 ListTargets: &ListTargetsConfig{2007 Enabled: false,2008 },2009 AuthProviders: &AuthProviderConfig{2010 Basic: map[string]*BasicAuthConfig{2011 "provider1": {Realm: "prov1"},2012 },2013 },2014 Targets: map[string]*TargetConfig{2015 "test": {2016 Name: "test",2017 Mount: &MountConfig{2018 Path: []string{"/test/"},2019 },2020 Bucket: &BucketConfig{2021 Name: "bucket1",2022 Region: "us-east-1",2023 S3ListMaxKeys: 1000,2024 Credentials: &BucketCredentialConfig{2025 AccessKey: &CredentialConfig{2026 Value: "VALUE1",2027 Path: secret1Filename,2028 },2029 SecretKey: &CredentialConfig{2030 Value: "value2",2031 },2032 },2033 },2034 Actions: &ActionsConfig{2035 GET: &GetActionConfig{Enabled: true},2036 },2037 Templates: &TargetTemplateConfig{},2038 },2039 },2040 }, res)2041 return2042 case <-time.After(5 * time.Second):2043 assert.FailNow(t, "shouldn't call this")2044 }2045}2046func Test_Load_reload_config_ignore_hidden_file_and_directory(t *testing.T) {2047 svrCompressCfg := &ServerCompressConfig{2048 Enabled: &DefaultServerCompressEnabled,2049 Level: DefaultServerCompressLevel,2050 Types: DefaultServerCompressTypes,2051 }2052 svrTimeoutsCfg := &ServerTimeoutsConfig{2053 ReadHeaderTimeout: DefaultServerTimeoutsReadHeaderTimeout,2054 }2055 // Channel for wait watch2056 waitCh := make(chan bool)2057 dir, err := ioutil.TempDir("", "s3-proxy-config-reload-ignore")2058 assert.NoError(t, err)2059 err = os.MkdirAll(path.Join(dir, "dir1"), os.ModePerm)2060 assert.NoError(t, err)2061 secret1Filename := path.Join(os.TempDir(), "secret1")2062 configs := map[string]string{2063 "..log.yaml": `2064log:2065 level: error2066`,2067 ".log2.yaml": `2068log:2069 format: fake2070`,2071 "dir1/log2.yaml": `2072server:2073 port: 81812074`,2075 "providers.yaml": `2076authProviders:2077 basic:2078 provider1:2079 realm: prov12080 provider2:2081 realm: prov22082`,2083 "targets.yaml": `2084targets:2085 test:2086 mount:2087 path: /test/2088 bucket:2089 name: bucket12090 region: us-east-12091 credentials:2092 accessKey:2093 path: ` + secret1Filename + `2094 secretKey:2095 value: value2`,2096 }2097 defer os.RemoveAll(dir) // clean up2098 for k, v := range configs {2099 tmpfn := filepath.Join(dir, k)2100 err = ioutil.WriteFile(tmpfn, []byte(v), 0666)2101 assert.NoError(t, err)2102 }2103 secretFiles := map[string]string{2104 secret1Filename: "VALUE1",2105 }2106 // Create secret files2107 for k, v := range secretFiles {2108 dirToCr := filepath.Dir(k)2109 err = os.MkdirAll(dirToCr, 0666)2110 assert.NoError(t, err)2111 err = ioutil.WriteFile(k, []byte(v), 0666)2112 assert.NoError(t, err)2113 defer os.Remove(k)2114 }2115 // Change var for main configuration file2116 mainConfigFolderPath = dir2117 ctx := &managercontext{2118 logger: log.NewLogger(),2119 }2120 ctx.AddOnChangeHook(func() {2121 waitCh <- true2122 })2123 // Load config2124 err = ctx.Load()2125 assert.NoError(t, err)2126 // Get configuration2127 res := ctx.GetConfig()2128 assert.Equal(t, &Config{2129 Log: &LogConfig{2130 Level: "info",2131 Format: "json",2132 },2133 Server: &ServerConfig{2134 Port: 8080,2135 Compress: svrCompressCfg,2136 Timeouts: svrTimeoutsCfg,2137 },2138 InternalServer: &ServerConfig{2139 Port: 9090,2140 Compress: svrCompressCfg,2141 Timeouts: svrTimeoutsCfg,2142 },2143 Tracing: &TracingConfig{Enabled: false},2144 Templates: defaultTemplateCfg,2145 AuthProviders: &AuthProviderConfig{2146 Basic: map[string]*BasicAuthConfig{2147 "provider1": {Realm: "prov1"},2148 "provider2": {Realm: "prov2"},2149 },2150 },2151 ListTargets: &ListTargetsConfig{2152 Enabled: false,2153 },2154 Targets: map[string]*TargetConfig{2155 "test": {...

Full Screen

Full Screen

definition_test.go

Source:definition_test.go Github

copy

Full Screen

...38 function: testAddMiddlewares,39 },40 {41 scenario: "marshal forwarding_timeouts to json",42 function: testMarshalForwardingTimeoutsToJSON,43 },44 {45 scenario: "unmarshal forwarding_timeouts from json",46 function: testUnmarshalForwardingTimeoutsFromJSON,47 },48 }49 for _, test := range tests {50 t.Run(test.scenario, func(t *testing.T) {51 test.function(t)52 })53 }54}55func testNewDefinitions(t *testing.T) {56 definition := NewDefinition()57 assert.Equal(t, []string{"GET"}, definition.Methods)58 assert.NotNil(t, definition)59}60func testSuccessfulValidation(t *testing.T) {61 definition := Definition{62 ListenPath: "/*",63 Upstreams: &Upstreams{64 Balancing: "roundrobin",65 Targets: Targets{66 {Target: "http://test.com"},67 },68 },69 }70 isValid, err := definition.Validate()71 assert.NoError(t, err)72 assert.True(t, isValid)73}74func testEmptyListenPathValidation(t *testing.T) {75 definition := Definition{}76 isValid, err := definition.Validate()77 assert.Error(t, err)78 assert.False(t, isValid)79}80func testInvalidTargetURLValidation(t *testing.T) {81 definition := Definition{82 ListenPath: " ",83 Upstreams: &Upstreams{84 Balancing: "roundrobin",85 Targets: Targets{86 {Target: "wrong"},87 },88 },89 }90 isValid, err := definition.Validate()91 assert.Error(t, err)92 assert.False(t, isValid)93}94func testIsBalancerDefined(t *testing.T) {95 definition := NewDefinition()96 assert.False(t, definition.IsBalancerDefined())97 target := &Target{Target: "http://localhost:8080/api-name"}98 definition.Upstreams.Targets = append(definition.Upstreams.Targets, target)99 assert.True(t, definition.IsBalancerDefined())100 assert.Len(t, definition.Upstreams.Targets.ToBalancerTargets(), 1)101}102func testAddMiddlewares(t *testing.T) {103 routerDefinition := NewRouterDefinition(NewDefinition())104 routerDefinition.AddMiddleware(middleware.NewLogger().Handler)105 assert.Len(t, routerDefinition.Middleware(), 1)106}107func testMarshalForwardingTimeoutsToJSON(t *testing.T) {108 definition := Definition{109 ListenPath: "/*",110 Upstreams: &Upstreams{111 Balancing: "roundrobin",112 Targets: Targets{113 {Target: "http://test.com"},114 },115 },116 ForwardingTimeouts: ForwardingTimeouts{117 DialTimeout: Duration(30 * time.Second),118 ResponseHeaderTimeout: Duration(31 * time.Second),119 },120 }121 jsonDefinition, err := json.Marshal(&definition)122 require.NoError(t, err)123 assert.Contains(t, string(jsonDefinition), `"dial_timeout":"30s"`)124 assert.Contains(t, string(jsonDefinition), `"response_header_timeout":"31s"`)125}126func testUnmarshalForwardingTimeoutsFromJSON(t *testing.T) {127 rawDefinition := []byte(`128 {129 "preserve_host":false,130 "listen_path":"/example/*",131 "upstreams":{132 "balancing":"roundrobin",133 "targets":[134 {135 "target":"http://localhost:9089/hello-world"136 }137 ]138 },139 "strip_path":false,140 "append_path":false,141 "methods":[142 "GET"143 ],144 "forwarding_timeouts": {145 "dial_timeout": "30s",146 "response_header_timeout": "31s"147 }148 }149`)150 definition := NewDefinition()151 err := json.Unmarshal(rawDefinition, &definition)152 require.NoError(t, err)153 assert.Equal(t, 30*time.Second, time.Duration(definition.ForwardingTimeouts.DialTimeout))154 assert.Equal(t, 31*time.Second, time.Duration(definition.ForwardingTimeouts.ResponseHeaderTimeout))155}...

Full Screen

Full Screen

typing.go

Source:typing.go Github

copy

Full Screen

1package irc2import (3 "sync"4 "time"5 "golang.org/x/time/rate"6)7// Typing is an event of Name actively typing in Target.8type Typing struct {9 Target string10 Name string11}12// Typings keeps track of typing notification timeouts.13type Typings struct {14 l sync.Mutex15 closed bool // whether Close has been called16 targets map[Typing]time.Time // @+typing TAGMSG timestamps.17 timeouts chan Typing // transmits unfiltered timeout notifications.18 stops chan Typing // transmits filtered timeout notifications.19}20// NewTypings initializes the Typings structures and filtering coroutine.21func NewTypings() *Typings {22 ts := &Typings{23 targets: map[Typing]time.Time{},24 timeouts: make(chan Typing, 16),25 stops: make(chan Typing, 16),26 }27 go func() {28 for t := range ts.timeouts {29 now := time.Now()30 ts.l.Lock()31 oldT, ok := ts.targets[t]32 if ok && 6.0 < now.Sub(oldT).Seconds() {33 delete(ts.targets, t)34 ts.l.Unlock()35 ts.stops <- t36 } else {37 ts.l.Unlock()38 }39 }40 }()41 return ts42}43// Close cleanly closes all channels and stops all goroutines.44func (ts *Typings) Close() {45 ts.l.Lock()46 defer ts.l.Unlock()47 close(ts.timeouts)48 close(ts.stops)49 ts.closed = true50}51// Stops is a channel that transmits typing timeouts.52func (ts *Typings) Stops() <-chan Typing {53 return ts.stops54}55// Active should be called when a user is typing to some target.56func (ts *Typings) Active(target, name string) {57 ts.l.Lock()58 t := Typing{target, name}59 ts.targets[t] = time.Now()60 ts.l.Unlock()61 go func() {62 time.Sleep(6 * time.Second)63 ts.l.Lock()64 defer ts.l.Unlock()65 if !ts.closed {66 ts.timeouts <- t67 }68 }()69}70// Done should be called when a user is done typing to some target.71func (ts *Typings) Done(target, name string) {72 ts.l.Lock()73 delete(ts.targets, Typing{target, name})74 ts.l.Unlock()75}76func (ts *Typings) List(target string) []string {77 ts.l.Lock()78 defer ts.l.Unlock()79 var res []string80 for t := range ts.targets {81 if target == t.Target {82 res = append(res, t.Name)83 }84 }85 return res86}87type typingStamp struct {88 Last time.Time89 Type int90 Limit *rate.Limiter91}...

Full Screen

Full Screen

Timeouts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c1 := make(chan string, 1)4 go func() {5 time.Sleep(2 * time.Second)6 }()7 select {8 fmt.Println(res)9 case <-time.After(1 * time.Second):10 fmt.Println("timeout 1")11 }12 c2 := make(chan string, 1)13 go func() {14 time.Sleep(2 * time.Second)15 }()16 select {17 fmt.Println(res)18 case <-time.After(3 * time.Second):19 fmt.Println("timeout 2")20 }21}22c1 := make(chan string, 1)23go func() {24 time.Sleep(time.Second * 1)25}()26select {27 fmt.Println(res)28case <-time.After(time.Second * 2):29 fmt.Println("timeout 1")30}31c2 := make(chan string, 1)32go func() {33 time.Sleep(time.Second * 2)34}()35select {36 fmt.Println(res)37case <-time.After(time.Second * 1):38 fmt.Println("timeout 2")39}

Full Screen

Full Screen

Timeouts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 vm := otto.New()4 _, err := vm.Run(`5 var targets = {6 Timeouts: function() {7 var time = new Date();8 return time.getTime();9 }10 };11 if err != nil {12 panic(err)13 }14 vm.Set("time", time.Now().Unix())15 result, err := vm.Run(`16 targets.Timeouts();17 if err != nil {18 panic(err)19 }20 fmt.Println(result)21}

Full Screen

Full Screen

Timeouts

Using AI Code Generation

copy

Full Screen

1import (2func TestTimeouts(t *testing.T) {3 conn := redigomock.NewConn()4 conn.Command("TIMEOUTS").Expect(1).Expect(2).Expect(3).Expect(4).Expect(5)5 targets := NewTargets(conn)6 targets.Timeouts()7 assert.Equal(t, 1, targets.ReadTimeout)8 assert.Equal(t, 2, targets.WriteTimeout)9 assert.Equal(t, 3, targets.ConnectTimeout)10 assert.Equal(t, 4, targets.IdleTimeout)11 assert.Equal(t, 5, targets.IdleCheckFrequency)12}13import (14func TestTimeouts(t *testing.T) {15 conn := redigomock.NewConn()16 conn.Command("TIMEOUTS").Expect(1).Expect(2).Expect(3).Expect(4).Expect(5)17 targets := NewTargets(conn)18 targets.Timeouts()19 assert.Equal(t, 1, targets.ReadTimeout)20 assert.Equal(t, 2, targets.WriteTimeout)21 assert.Equal(t, 3, targets.ConnectTimeout)22 assert.Equal(t, 4, targets.IdleTimeout)23 assert.Equal(t, 5, targets.IdleCheckFrequency)24}25import (26func TestTimeouts(t *testing.T) {27 conn := redigomock.NewConn()28 conn.Command("TIMEOUTS").Expect(1).Expect(2).Expect(3).Expect(4).Expect(5)29 targets := NewTargets(conn)30 targets.Timeouts()31 assert.Equal(t, 1, targets.ReadTimeout)32 assert.Equal(t, 2, targets.WriteTimeout)33 assert.Equal(t, 3, targets.ConnectTimeout)34 assert.Equal(t, 4, targets

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful