blob: 8dd449b689c6e51d06f91517a1a8376b322f6723 [file] [log] [blame]
David Bainbridgec4029aa2019-09-26 18:56:39 +00001/*
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 */
16package commands
17
18import (
19 flags "github.com/jessevdk/go-flags"
20 "github.com/stretchr/testify/assert"
21 "os"
22 "path"
23 "testing"
24)
25
Scott Baker80126ab2020-06-04 11:49:07 -070026// Test that ProcessGlobalOptions does not interfere with GlobalConfig
27// default.
28func TestProcessGlobalOptionsWithDefaults(t *testing.T) {
David Bainbridgec4029aa2019-09-26 18:56:39 +000029 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 Baker80126ab2020-06-04 11:49:07 -070039 assert.Equal(t, "localhost:55555", GlobalConfig.Server, "wrong default hostname for server")
David Bainbridgec4029aa2019-09-26 18:56:39 +000040}
divyadesai19009132020-03-04 12:58:08 +000041
42func 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}