David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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 ( |
| 19 | flags "github.com/jessevdk/go-flags" |
| 20 | "github.com/stretchr/testify/assert" |
| 21 | "os" |
| 22 | "path" |
| 23 | "testing" |
| 24 | ) |
| 25 | |
Scott Baker | 80126ab | 2020-06-04 11:49:07 -0700 | [diff] [blame] | 26 | // Test that ProcessGlobalOptions does not interfere with GlobalConfig |
| 27 | // default. |
| 28 | func TestProcessGlobalOptionsWithDefaults(t *testing.T) { |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 29 | os.Setenv("VOLTCONFIG", "__DOES_NOT_EXIST__") |
| 30 | |
| 31 | parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.Default|flags.PassAfterNonOption) |
| 32 | _, err := parser.AddGroup("Global Options", "", &GlobalOptions) |
| 33 | assert.Nil(t, err, "unexpected error adding group") |
| 34 | RegisterConfigCommands(parser) |
| 35 | _, err = parser.ParseArgs([]string{"config"}) |
| 36 | assert.Nil(t, err, "unexpected error paring arguments") |
| 37 | ProcessGlobalOptions() |
| 38 | |
Scott Baker | 80126ab | 2020-06-04 11:49:07 -0700 | [diff] [blame] | 39 | assert.Equal(t, "localhost:55555", GlobalConfig.Server, "wrong default hostname for server") |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 40 | } |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 41 | |
| 42 | func TestSplitHostPort(t *testing.T) { |
| 43 | data := []struct { |
| 44 | name string |
| 45 | endpoint string |
| 46 | defaultHost string |
| 47 | defaultPort int |
| 48 | host string |
| 49 | port int |
| 50 | err bool |
| 51 | }{ |
| 52 | {"Host and port specified", "host:1234", "default", 4321, "host", 1234, false}, |
| 53 | {"Host only specified", "host", "default", 4321, "host", 4321, false}, |
| 54 | {"Host: only specified", "host:", "default", 4321, "host", 4321, false}, |
| 55 | {"Port only specified", ":1234", "default", 4321, "default", 1234, false}, |
| 56 | {"Colon only", ":", "default", 4321, "default", 4321, false}, |
| 57 | {"Empty endpoint", "", "default", 4321, "default", 4321, false}, |
| 58 | {"IPv4 and port specified", "1.2.3.4:1234", "4.3.2.1", 4321, "1.2.3.4", 1234, false}, |
| 59 | {"IPv4 only specified", "1.2.3.4", "4.3.2.1", 4321, "1.2.3.4", 4321, false}, |
| 60 | {"IPv4: only specified", "1.2.3.4:", "4.3.2.1", 4321, "1.2.3.4", 4321, false}, |
| 61 | {"IPv4 Port only specified", ":1234", "4.3.2.1", 4321, "4.3.2.1", 1234, false}, |
| 62 | {"IPv4 Colon only", ":", "4.3.2.1", 4321, "4.3.2.1", 4321, false}, |
| 63 | {"IPv4 Empty endpoint", "", "4.3.2.1", 4321, "4.3.2.1", 4321, false}, |
| 64 | {"IPv6 and port specified", "[0001:c0ff:eec0::::ffff]:1234", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::ffff", 1234, false}, |
| 65 | {"IPv6 only specified", "[0001:c0ff:eec0::::ffff]", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::ffff", 4321, false}, |
| 66 | {"IPv6: only specified", "[0001:c0ff:eec0::::ffff]:", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::ffff", 4321, false}, |
| 67 | {"IPv6 Port only specified", ":1234", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::aaaa", 1234, false}, |
| 68 | {"IPv6 Colon only", ":", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::aaaa", 4321, false}, |
| 69 | {"IPv6 Empty endpoint", "", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::aaaa", 4321, false}, |
| 70 | {"Invalid port", "host:1b", "default", 4321, "", 0, true}, |
| 71 | {"Too many colons", "ho:st:1b", "default", 4321, "", 0, true}, |
| 72 | {"IPv4 Invalid port", "1.2.3.4:1b", "4.3.2.1", 4321, "", 0, true}, |
| 73 | {"IPv4 Too many colons", "1.2.3.4::1234", "4.3.2.1", 4321, "", 0, true}, |
| 74 | {"IPv6 Invalid port", "[0001:c0ff:eec0::::ffff]:1b", "0001:c0ff:eec0::::aaaa", 4321, "", 0, true}, |
| 75 | {"IPv6 Too many colons", "0001:c0ff:eec0::::ffff:1234", "0001:c0ff:eec0::::aaaa", 4321, "", 0, true}, |
| 76 | } |
| 77 | |
| 78 | for _, args := range data { |
| 79 | t.Run(args.name, func(t *testing.T) { |
| 80 | h, p, err := splitEndpoint(args.endpoint, args.defaultHost, args.defaultPort) |
| 81 | if args.err { |
| 82 | assert.NotNil(t, err, "unexpected non-error result") |
| 83 | } else { |
| 84 | assert.Nil(t, err, "unexpected error result") |
| 85 | } |
| 86 | if !args.err && err == nil { |
| 87 | assert.Equal(t, args.host, h, "unexpected host value") |
| 88 | assert.Equal(t, args.port, p, "unexpected port value") |
| 89 | } |
| 90 | }) |
| 91 | } |
| 92 | } |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 93 | |
| 94 | func TestParseSize(t *testing.T) { |
| 95 | var res uint64 |
| 96 | var err error |
| 97 | |
| 98 | res, err = parseSize("8M") |
| 99 | assert.Nil(t, err) |
| 100 | assert.Equal(t, uint64(8388608), res) |
| 101 | |
| 102 | res, err = parseSize("8MB") |
| 103 | assert.Nil(t, err) |
| 104 | assert.Equal(t, uint64(8388608), res) |
| 105 | |
| 106 | res, err = parseSize("8MiB") |
| 107 | assert.Nil(t, err) |
| 108 | assert.Equal(t, uint64(8388608), res) |
| 109 | |
| 110 | res, err = parseSize("foobar") |
| 111 | assert.NotNil(t, err) |
| 112 | assert.Equal(t, uint64(0), res) |
| 113 | } |