David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 1 | /* |
Joey Armstrong | 903c69d | 2024-02-01 19:46:39 -0500 | [diff] [blame] | 2 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 19 | "os" |
| 20 | "path" |
| 21 | "testing" |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 22 | |
| 23 | flags "github.com/jessevdk/go-flags" |
| 24 | "github.com/stretchr/testify/assert" |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
Scott Baker | 80126ab | 2020-06-04 11:49:07 -0700 | [diff] [blame] | 27 | // Test that ProcessGlobalOptions does not interfere with GlobalConfig |
| 28 | // default. |
| 29 | func TestProcessGlobalOptionsWithDefaults(t *testing.T) { |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 30 | os.Setenv("VOLTCONFIG", "__DOES_NOT_EXIST__") |
| 31 | |
| 32 | parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.Default|flags.PassAfterNonOption) |
| 33 | _, err := parser.AddGroup("Global Options", "", &GlobalOptions) |
| 34 | assert.Nil(t, err, "unexpected error adding group") |
| 35 | RegisterConfigCommands(parser) |
| 36 | _, err = parser.ParseArgs([]string{"config"}) |
| 37 | assert.Nil(t, err, "unexpected error paring arguments") |
| 38 | ProcessGlobalOptions() |
| 39 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 40 | assert.Equal(t, "localhost:55555", GlobalConfig.Current().Server, "wrong default hostname for server") |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 41 | } |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 42 | |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 43 | func TestParseSize(t *testing.T) { |
| 44 | var res uint64 |
| 45 | var err error |
| 46 | |
| 47 | res, err = parseSize("8M") |
| 48 | assert.Nil(t, err) |
| 49 | assert.Equal(t, uint64(8388608), res) |
| 50 | |
| 51 | res, err = parseSize("8MB") |
| 52 | assert.Nil(t, err) |
| 53 | assert.Equal(t, uint64(8388608), res) |
| 54 | |
| 55 | res, err = parseSize("8MiB") |
| 56 | assert.Nil(t, err) |
| 57 | assert.Equal(t, uint64(8388608), res) |
| 58 | |
| 59 | res, err = parseSize("foobar") |
| 60 | assert.NotNil(t, err) |
| 61 | assert.Equal(t, uint64(0), res) |
| 62 | } |