blob: 7f4171087049b3f2655aaadbddfd02152103788a [file] [log] [blame]
David Bainbridgec4029aa2019-09-26 18:56:39 +00001/*
Joey Armstrong903c69d2024-02-01 19:46:39 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
David Bainbridgec4029aa2019-09-26 18:56:39 +00003 *
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 */
16package commands
17
18import (
David Bainbridgec4029aa2019-09-26 18:56:39 +000019 "os"
20 "path"
21 "testing"
David K. Bainbridge1d946442021-03-19 16:45:52 +000022
23 flags "github.com/jessevdk/go-flags"
24 "github.com/stretchr/testify/assert"
David Bainbridgec4029aa2019-09-26 18:56:39 +000025)
26
Scott Baker80126ab2020-06-04 11:49:07 -070027// Test that ProcessGlobalOptions does not interfere with GlobalConfig
28// default.
29func TestProcessGlobalOptionsWithDefaults(t *testing.T) {
David Bainbridgec4029aa2019-09-26 18:56:39 +000030 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. Bainbridge9189c632021-03-26 21:52:21 +000040 assert.Equal(t, "localhost:55555", GlobalConfig.Current().Server, "wrong default hostname for server")
David Bainbridgec4029aa2019-09-26 18:56:39 +000041}
divyadesai19009132020-03-04 12:58:08 +000042
serkant.uluderyad3daa902020-05-21 22:49:25 -070043func 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}