How to use loadConfig method of main Package

Best Syzkaller code snippet using main.loadConfig

config_test.go

Source:config_test.go Github

copy

Full Screen

1// Copyright 2021 Northern.tech AS2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14package config15import (16 "bytes"17 "io/ioutil"18 "os"19 "path"20 "reflect"21 "testing"22 log "github.com/sirupsen/logrus"23 "github.com/stretchr/testify/assert"24)25const testConfig = `{26 "User":"root",27 "Terminal": {28 "Height": 80,29 "Width": 2430 },31 "Sessions": {32 "StopExpired": true,33 "ExpireAfter": 16,34 "ExpireAfterIdle": 8,35 "MaxPerUser": 436 }37}`38const testBrokenConfig = `{39 "User":"root"40 "Terminal": {41 "Height": 80,42 "Width": 2443 },44}`45const testShellIsNotAbsolutePathConfig = `{46 "ShellCommand": "bash",47 "User": "root"48}`49const testShellIsNotExecutablePathConfig = `{50 "ShellCommand": "/etc/profile",51 "User": "root"52}`53const testShellIsNotPresentConfig = `{54 "ShellCommand": "/most/not/here/now",55 "User": "root"56}`57const testUserNotFoundConfig = `{58 "ShellCommand": "/bin/bash",59 "User": "thisoneisnotknown"60}`61const testShellNotInShellsConfig = `{62 "ShellCommand": "/bin/ls",63 "User": "root"64}`65const testParsingErrorConfig = `{66 "ShellCommand": "/bin/ls"67 "User": "root"68}`69const testOtherErrorConfig = `{70 "ShellCommand": 0.0e14,71 "User": "root"72}`73func Test_readConfigFile_noFile_returnsError(t *testing.T) {74 err := readConfigFile(nil, "non-existing-file")75 assert.Error(t, err)76}77func Test_readConfigFile_brokenContent_returnsError(t *testing.T) {78 // create a temporary mender-connect.conf file79 tdir, err := ioutil.TempDir("", "mendertest")80 assert.NoError(t, err)81 defer os.RemoveAll(tdir)82 configPath := path.Join(tdir, "mender-connect.conf")83 configFile, err := os.Create(configPath)84 assert.NoError(t, err)85 configFile.WriteString(testBrokenConfig)86 // fail on first call to readConfigFile (invalid JSON)87 confFromFile, err := LoadConfig(configPath, "does-not-exist.config")88 assert.Error(t, err)89 assert.Nil(t, confFromFile)90 // fail on second call to readConfigFile (invalid JSON)91 confFromFile, err = LoadConfig("does-not-exist.config", configPath)92 assert.Error(t, err)93 assert.Nil(t, confFromFile)94}95func validateConfiguration(t *testing.T, actual *MenderShellConfig) {96 expectedConfig := NewMenderShellConfig()97 expectedConfig.MenderShellConfigFromFile = MenderShellConfigFromFile{98 User: "root",99 ShellCommand: DefaultShellCommand,100 ShellArguments: DefaultShellArguments,101 Terminal: TerminalConfig{102 Width: 24,103 Height: 80,104 },105 Sessions: SessionsConfig{106 StopExpired: true,107 ExpireAfter: 16,108 ExpireAfterIdle: 8,109 MaxPerUser: 4,110 },111 ReconnectIntervalSeconds: DefaultReconnectIntervalsSeconds,112 Limits: Limits{113 Enabled: false,114 FileTransfer: FileTransferLimits{115 Chroot: "",116 FollowSymLinks: false,117 AllowOverwrite: false,118 OwnerPut: "",119 GroupPut: "",120 Umask: "",121 MaxFileSize: 0,122 Counters: RateLimits{123 MaxBytesTxPerMinute: 0,124 MaxBytesRxPerMinute: 0,125 },126 AllowSuid: false,127 RegularFilesOnly: false,128 PreserveMode: true,129 PreserveOwner: true,130 },131 },132 }133 if !assert.True(t, reflect.DeepEqual(actual, expectedConfig)) {134 t.Logf("got: %+v", actual)135 t.Logf("expected: %+v", expectedConfig)136 }137}138func Test_LoadConfig_correctConfFile_returnsConfiguration(t *testing.T) {139 // create a temporary mender-connect.conf file140 tdir, err := ioutil.TempDir("", "mendertest")141 assert.NoError(t, err)142 defer os.RemoveAll(tdir)143 configPath := path.Join(tdir, "mender-connect.conf")144 configFile, err := os.Create(configPath)145 assert.NoError(t, err)146 configFile.WriteString(testConfig)147 // fallback configuration file does not exist148 config, err := LoadConfig(configPath, "does-not-exist.config")149 assert.NoError(t, err)150 assert.NotNil(t, config)151 err = config.Validate()152 assert.NoError(t, err)153 validateConfiguration(t, config)154 assert.Equal(t, "root", config.User)155 // main configuration file does not exist156 config2, err2 := LoadConfig("does-not-exist.config", configPath)157 assert.NoError(t, err2)158 assert.NotNil(t, config2)159 err = config2.Validate()160 assert.NoError(t, err)161 validateConfiguration(t, config2)162 assert.Equal(t, "root", config.User)163}164func TestConfigurationMergeSettings(t *testing.T) {165 var mainConfigJSON = `{166 "ShellCommand": "/bin/bash",167 "ReconnectIntervalSeconds": 123168 }`169 var fallbackConfigJSON = `{170 "ShellCommand": "/bin/sh",171 "User": "mender"172 }`173 mainConfigFile, err := os.Create("main.config")174 assert.NoError(t, err)175 defer os.Remove("main.config")176 mainConfigFile.WriteString(mainConfigJSON)177 fallbackConfigFile, err := os.Create("fallback.config")178 assert.NoError(t, err)179 defer os.Remove("fallback.config")180 fallbackConfigFile.WriteString(fallbackConfigJSON)181 config, err := LoadConfig("main.config", "fallback.config")182 assert.NoError(t, err)183 assert.NotNil(t, config)184 // when a setting appears in neither file, it is left with its default value.185 assert.Equal(t, uint16(0), config.Terminal.Width)186 assert.Equal(t, uint16(0), config.Terminal.Height)187 // when a setting appears in both files, the main file takes precedence.188 assert.Equal(t, "/bin/bash", config.ShellCommand)189 // when a setting appears in only one file, its value is used.190 assert.Equal(t, "mender", config.User)191 assert.Equal(t, 123, config.ReconnectIntervalSeconds)192}193func Test_LoadConfig_various_errors(t *testing.T) {194 // create a temporary mender-connect.conf file195 tdir, err := ioutil.TempDir("", "mendertest")196 assert.NoError(t, err)197 defer os.RemoveAll(tdir)198 //one of the serverURLs is not valid199 configPath := path.Join(tdir, "mender-connect.conf")200 configFile, err := os.Create(configPath)201 assert.NoError(t, err)202 //shell is not an absolute path203 configFile, err = os.Create(configPath)204 assert.NoError(t, err)205 configFile.WriteString(testShellIsNotAbsolutePathConfig)206 config, err := LoadConfig(configPath, "")207 assert.NoError(t, err)208 assert.NotNil(t, config)209 err = config.Validate()210 assert.Error(t, err)211 //shell is not executable212 configFile, err = os.Create(configPath)213 assert.NoError(t, err)214 configFile.WriteString(testShellIsNotExecutablePathConfig)215 config, err = LoadConfig(configPath, "")216 assert.NoError(t, err)217 assert.NotNil(t, config)218 err = config.Validate()219 assert.Error(t, err)220 //shell is not present221 configFile, err = os.Create(configPath)222 assert.NoError(t, err)223 configFile.WriteString(testShellIsNotPresentConfig)224 config, err = LoadConfig(configPath, "")225 assert.NoError(t, err)226 assert.NotNil(t, config)227 err = config.Validate()228 assert.Error(t, err)229 //user not found230 configFile, err = os.Create(configPath)231 assert.NoError(t, err)232 configFile.WriteString(testUserNotFoundConfig)233 config, err = LoadConfig(configPath, "")234 assert.NoError(t, err)235 assert.NotNil(t, config)236 err = config.Validate()237 assert.Error(t, err)238 //shell is not found in /etc/shells239 configFile, err = os.Create(configPath)240 assert.NoError(t, err)241 configFile.WriteString(testShellNotInShellsConfig)242 config, err = LoadConfig(configPath, "")243 assert.NoError(t, err)244 assert.NotNil(t, config)245 err = config.Validate()246 assert.Error(t, err)247 //parsing error248 configFile, err = os.Create(configPath)249 assert.NoError(t, err)250 configFile.WriteString(testParsingErrorConfig)251 _, err = LoadConfig(configPath, "")252 assert.Error(t, err)253 //other loading error254 configFile, err = os.Create(configPath)255 assert.NoError(t, err)256 configFile.WriteString(testOtherErrorConfig)257 _, err = LoadConfig(configPath, "")258 assert.Error(t, err)259}260func TestConfigurationNeitherFileExistsIsNotError(t *testing.T) {261 config, err := LoadConfig("does-not-exist", "also-does-not-exist")262 assert.NoError(t, err)263 assert.IsType(t, &MenderShellConfig{}, config)264}265func TestShellArgumentsEmptyDefaults(t *testing.T) {266 // Test default shell arguments267 var mainConfigJSON = `{268 "ShellCommand": "/bin/bash"269 }`270 mainConfigFile, err := os.Create("main.config")271 assert.NoError(t, err)272 defer os.Remove("main.config")273 mainConfigFile.WriteString(mainConfigJSON)274 config, err := LoadConfig("main.config", "")275 assert.NoError(t, err)276 assert.NotNil(t, config)277 config.Validate()278 assert.Equal(t, []string{"--login"}, config.ShellArguments)279 // Test empty shell arguments280 mainConfigJSON = `{281 "ShellCommand": "/bin/bash",282 "ShellArguments": [""]283 }`284 mainConfigFile, err = os.Create("main.config")285 assert.NoError(t, err)286 defer os.Remove("main.config")287 mainConfigFile.WriteString(mainConfigJSON)288 config, err = LoadConfig("main.config", "")289 assert.NoError(t, err)290 config.Validate()291 assert.Equal(t, []string{""}, config.ShellArguments)292 // Test setting custom shell arguments293 mainConfigJSON = `{294 "ShellCommand": "/bin/bash",295 "ShellArguments": ["--no-profile", "--norc", "--restricted"]296 }`297 mainConfigFile, err = os.Create("main.config")298 assert.NoError(t, err)299 defer os.Remove("main.config")300 mainConfigFile.WriteString(mainConfigJSON)301 config, err = LoadConfig("main.config", "")302 assert.NoError(t, err)303 config.Validate()304 assert.Equal(t, []string{"--no-profile", "--norc", "--restricted"}, config.ShellArguments)305}306func TestServerArgumentsDeprecated(t *testing.T) {307 // create a temporary mender-connect.conf file308 tdir, err := ioutil.TempDir("", "mendertest")309 assert.NoError(t, err)310 defer os.RemoveAll(tdir)311 configPath := path.Join(tdir, "mender-connect.conf")312 configFile, err := os.Create(configPath)313 assert.NoError(t, err)314 var buf bytes.Buffer315 log.SetOutput(&buf)316 defer func() {317 log.SetOutput(os.Stderr)318 }()319 configFile.WriteString(`{"ServerURL": "https://mender.io/"}`)320 _, err = LoadConfig(configPath, "does-not-exist.config")321 assert.NoError(t, err)322 assert.Contains(t, buf.String(), "ServerURL field is deprecated")323 configFile, err = os.Create(configPath)324 assert.NoError(t, err)325 configFile.WriteString(`{"Servers": [{"ServerURL": "https://hosted.mender.io"}]}`)326 buf.Reset()327 _, err = LoadConfig(configPath, "does-not-exist.config")328 assert.NoError(t, err)329 assert.Contains(t, buf.String(), "Servers field is deprecated")330 configFile, err = os.Create(configPath)331 assert.NoError(t, err)332 configFile.WriteString(`{333 "ServerURL": "https://hosted.mender.io",334 "User":"root",335 "ShellCommand": "/bin/bash",336 "Servers": [{"ServerURL": "https://hosted.mender.io"}]337 }`)338 buf.Reset()339 _, err = LoadConfig(configPath, "does-not-exist.config")340 assert.NoError(t, err)341 assert.Contains(t, buf.String(), "ServerURL field is deprecated")342 assert.Contains(t, buf.String(), "Servers field is deprecated")343 configFile, err = os.Create(configPath)344 assert.NoError(t, err)345 configFile.WriteString(`{"ClientProtocol": "something"}`)346 buf.Reset()347 _, err = LoadConfig(configPath, "does-not-exist.config")348 assert.NoError(t, err)349 assert.Contains(t, buf.String(), "ClientProtocol field is deprecated")350 configFile, err = os.Create(configPath)351 assert.NoError(t, err)352 configFile.WriteString(`{"HTTPSClient": {"Certificate": "client.crt"}}`)353 buf.Reset()354 _, err = LoadConfig(configPath, "does-not-exist.config")355 assert.NoError(t, err)356 assert.Contains(t, buf.String(), "HTTPSClient field is deprecated")357 configFile, err = os.Create(configPath)358 assert.NoError(t, err)359 configFile.WriteString(`{"HTTPSClient": {"Key": "key.secret"}}`)360 buf.Reset()361 _, err = LoadConfig(configPath, "does-not-exist.config")362 assert.NoError(t, err)363 assert.Contains(t, buf.String(), "HTTPSClient field is deprecated")364 configFile, err = os.Create(configPath)365 assert.NoError(t, err)366 configFile.WriteString(`{"HTTPSClient": {"SSLEngine": "engine.power"}}`)367 buf.Reset()368 _, err = LoadConfig(configPath, "does-not-exist.config")369 assert.NoError(t, err)370 assert.Contains(t, buf.String(), "HTTPSClient field is deprecated")371 configFile, err = os.Create(configPath)372 assert.NoError(t, err)373 configFile.WriteString(`{"SkipVerify": true}`)374 buf.Reset()375 _, err = LoadConfig(configPath, "does-not-exist.config")376 assert.NoError(t, err)377 assert.Contains(t, buf.String(), "SkipVerify field is deprecated")378 configFile, err = os.Create(configPath)379 assert.NoError(t, err)380 configFile.WriteString(`{"ServerCertificate": "certificate.crt"}`)381 buf.Reset()382 _, err = LoadConfig(configPath, "does-not-exist.config")383 assert.NoError(t, err)384 assert.Contains(t, buf.String(), "ServerCertificate field is deprecated")385}...

Full Screen

Full Screen

main_test.go

Source:main_test.go Github

copy

Full Screen

...17}18func TestMain_LoadConfigFile(t *testing.T) {19 t.Parallel()20 file := "sample_config.json"21 config, err := loadConfig(&file)22 if err != nil {23 t.Errorf("Error in config file loading but should not %v", err)24 }25 if config.SMTPHost != "mail.example.com" {26 t.Errorf("Error in loadConfig: %v is %v but should be %v", "SMTPHost", config.SMTPHost, "mail.example.com")27 }28 if config.SMTPPort != "25" {29 t.Errorf("Error in loadConfig: %v is %v but should be %v", "SMTPPort", config.SMTPPort, "25")30 }31 if config.Port != "8081" {32 t.Errorf("Error in loadConfig: %v is %v but should be %v", "Port", config.SMTPPort, "8081")33 }34 if config.SMTPAuthUser != "SMTP_USER" {35 t.Errorf("Error in loadConfig: %v is %v but should be %v", "AuthUser", config.SMTPAuthUser, "SMTP_USER")36 }37 if config.SMTPAuthPassword != "SMTP_PASSWORD" {38 t.Errorf("Error in loadConfig: %v is %v but should be %v", "AuthPassword", config.SMTPAuthPassword, "SMTP_PASSWORD")39 }40 if config.Lifetime != 60 {41 t.Errorf("Error in loadConfig: %v is %v but should be %v", "Lifetime", config.Lifetime, 60)42 }43 if config.CleanupInterval != 10 {44 t.Errorf("Error in loadConfig: %v is %v but should be %v", "CleanupInterval", config.CleanupInterval, 10)45 }46 if config.TarpitInterval != 10 {47 t.Errorf("Error in loadConfig: %v is %v but should be %v", "TarpitInterval", config.TarpitInterval, 10)48 }49 if len(config.RecipientMap) != 2 {50 t.Errorf("Error in loadConfig: %v is %v but should be %v", "length of recipient map", len(config.RecipientMap), 2)51 }52}...

Full Screen

Full Screen

server_test.go

Source:server_test.go Github

copy

Full Screen

1// server_test.go2package main3import (4 "testing"5 "github.com/sirupsen/logrus/hooks/test"6 "github.com/stretchr/testify/assert"7)8var loadC, loadP, loadA, srvS bool9func TestMain(t *testing.T) {10 assert := assert.New(t)11 log, _ = test.NewNullLogger()12 // save original functions13 origLoadConfig := LoadConfig14 origLoadProviders := LoadProviders15 origLoadOAuthClients := LoadOAuthClients16 origServerStart := ServerStart17 defer func() {18 // restore19 LoadConfig = origLoadConfig20 LoadProviders = origLoadProviders21 LoadOAuthClients = origLoadOAuthClients22 ServerStart = origServerStart23 }()24 LoadConfig = func(string) {25 loadC = true26 }27 LoadProviders = func() {28 loadP = true29 }30 LoadOAuthClients = func() {31 loadA = true32 }33 ServerStart = func() {34 srvS = true35 }36 main()37 assert.True(loadC, "LoadConfig() was not execute in main()")38 assert.True(loadP, "LoadProviders() was not execute in main()")39 assert.True(loadA, "LoadOAuthClients() was not execute in main()")40 assert.True(loadA, "ServerStart() was not execute in main()")41}...

Full Screen

Full Screen

loadConfig

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 dir, err := filepath.Abs(filepath.Dir(os.Args[0]))4 if err != nil {5 log.Fatal(err)6 }7 fmt.Println(dir)8 main.LoadConfig(dir)9}10import (11func main() {12 dir, err := filepath.Abs(filepath.Dir(os.Args[0]))13 if err != nil {14 log.Fatal(err)15 }16 fmt.Println(dir)17 main.LoadConfig(dir)18}

Full Screen

Full Screen

loadConfig

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World")4 loadConfig()5}6import (7func main() {8 fmt.Println("Hello World")9}10import (11func main() {12 fmt.Println("Hello World")13}14import (15func main() {16 fmt.Println("Hello World")17}18import (19func main() {20 fmt.Println("Hello World")21}22import (23func main() {24 fmt.Println("Hello World")25}26import (27func main() {28 fmt.Println("Hello World")29}30import (31func main() {32 fmt.Println("Hello World")33}34import (35func main() {36 fmt.Println("Hello World")37}38import (39func main() {40 fmt.Println("Hello World")41}42import (43func main() {44 fmt.Println("Hello World")45}46import (47func main() {48 fmt.Println("Hello World")49}50import (51func main() {52 fmt.Println("Hello World")53}54import (55func main() {56 fmt.Println("Hello World")57}58import (59func main() {60 fmt.Println("Hello World")61}62import (63func main() {64 fmt.Println("Hello World")65}66import (67func main()

Full Screen

Full Screen

loadConfig

Using AI Code Generation

copy

Full Screen

1import (2func main(){3 config.LoadConfig()4 fmt.Println(config.ConfigData)5}6import (7func main(){8 config.LoadConfig()9 fmt.Println(config.ConfigData)10}

Full Screen

Full Screen

loadConfig

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 excelFile, err := xlsx.OpenFile("/home/ashish/Desktop/GoLang/Excel/Excel.xlsx")4 if err != nil {5 fmt.Println(err)6 }7 sheet, err := excelFile.AddSheet("Sheet2")8 if err != nil {9 fmt.Println(err)10 }11 row := sheet.AddRow()12 cell := row.AddCell()13 err = excelFile.Save("/home/ashish/Desktop/GoLang/Excel/Excel.xlsx")14 if err != nil {15 fmt.Println(err)16 }17}

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.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful