blob: 0ee0ca75dea54fba43eb25f53eb31b36c141b6fe [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
26func TestDefaultVersion(t *testing.T) {
27 os.Setenv("VOLTCONFIG", "__DOES_NOT_EXIST__")
28
29 parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.Default|flags.PassAfterNonOption)
30 _, err := parser.AddGroup("Global Options", "", &GlobalOptions)
31 assert.Nil(t, err, "unexpected error adding group")
32 RegisterConfigCommands(parser)
33 _, err = parser.ParseArgs([]string{"config"})
34 assert.Nil(t, err, "unexpected error paring arguments")
35 ProcessGlobalOptions()
36
kesavand12cd8eb2020-01-20 22:25:22 -050037 assert.Equal(t, "v3", GlobalConfig.ApiVersion, "wrong default version for API version")
David Bainbridgec4029aa2019-09-26 18:56:39 +000038}
divyadesai19009132020-03-04 12:58:08 +000039
40func TestSplitHostPort(t *testing.T) {
41 data := []struct {
42 name string
43 endpoint string
44 defaultHost string
45 defaultPort int
46 host string
47 port int
48 err bool
49 }{
50 {"Host and port specified", "host:1234", "default", 4321, "host", 1234, false},
51 {"Host only specified", "host", "default", 4321, "host", 4321, false},
52 {"Host: only specified", "host:", "default", 4321, "host", 4321, false},
53 {"Port only specified", ":1234", "default", 4321, "default", 1234, false},
54 {"Colon only", ":", "default", 4321, "default", 4321, false},
55 {"Empty endpoint", "", "default", 4321, "default", 4321, false},
56 {"IPv4 and port specified", "1.2.3.4:1234", "4.3.2.1", 4321, "1.2.3.4", 1234, false},
57 {"IPv4 only specified", "1.2.3.4", "4.3.2.1", 4321, "1.2.3.4", 4321, false},
58 {"IPv4: only specified", "1.2.3.4:", "4.3.2.1", 4321, "1.2.3.4", 4321, false},
59 {"IPv4 Port only specified", ":1234", "4.3.2.1", 4321, "4.3.2.1", 1234, false},
60 {"IPv4 Colon only", ":", "4.3.2.1", 4321, "4.3.2.1", 4321, false},
61 {"IPv4 Empty endpoint", "", "4.3.2.1", 4321, "4.3.2.1", 4321, false},
62 {"IPv6 and port specified", "[0001:c0ff:eec0::::ffff]:1234", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::ffff", 1234, false},
63 {"IPv6 only specified", "[0001:c0ff:eec0::::ffff]", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::ffff", 4321, false},
64 {"IPv6: only specified", "[0001:c0ff:eec0::::ffff]:", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::ffff", 4321, false},
65 {"IPv6 Port only specified", ":1234", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::aaaa", 1234, false},
66 {"IPv6 Colon only", ":", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::aaaa", 4321, false},
67 {"IPv6 Empty endpoint", "", "0001:c0ff:eec0::::aaaa", 4321, "0001:c0ff:eec0::::aaaa", 4321, false},
68 {"Invalid port", "host:1b", "default", 4321, "", 0, true},
69 {"Too many colons", "ho:st:1b", "default", 4321, "", 0, true},
70 {"IPv4 Invalid port", "1.2.3.4:1b", "4.3.2.1", 4321, "", 0, true},
71 {"IPv4 Too many colons", "1.2.3.4::1234", "4.3.2.1", 4321, "", 0, true},
72 {"IPv6 Invalid port", "[0001:c0ff:eec0::::ffff]:1b", "0001:c0ff:eec0::::aaaa", 4321, "", 0, true},
73 {"IPv6 Too many colons", "0001:c0ff:eec0::::ffff:1234", "0001:c0ff:eec0::::aaaa", 4321, "", 0, true},
74 }
75
76 for _, args := range data {
77 t.Run(args.name, func(t *testing.T) {
78 h, p, err := splitEndpoint(args.endpoint, args.defaultHost, args.defaultPort)
79 if args.err {
80 assert.NotNil(t, err, "unexpected non-error result")
81 } else {
82 assert.Nil(t, err, "unexpected error result")
83 }
84 if !args.err && err == nil {
85 assert.Equal(t, args.host, h, "unexpected host value")
86 assert.Equal(t, args.port, p, "unexpected port value")
87 }
88 })
89 }
90}