|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/hashicorp/nomad/api" |
| 7 | + "github.com/shoenig/test/must" |
| 8 | +) |
| 9 | + |
| 10 | +func TestHelpers_handleBasicAuth(t *testing.T) { |
| 11 | + cases := []struct { |
| 12 | + addr string |
| 13 | + expectedUser string |
| 14 | + expectedPass string |
| 15 | + expectedAddr string |
| 16 | + }{ |
| 17 | + {"addr", "", "", "addr"}, |
| 18 | + {"addr:port", "", "", "addr:port"}, |
| 19 | + {"user:pass@addr", "user", "pass", "addr"}, |
| 20 | + {"user:pass@addr:port", "user", "pass", "addr:port"}, |
| 21 | + {"scheme://addr", "", "", "scheme://addr"}, |
| 22 | + {"scheme://user:pass@addr", "user", "pass", "scheme://addr"}, |
| 23 | + {"scheme://user:pass@addr:port", "user", "pass", "scheme://addr:port"}, |
| 24 | + {"scheme://user:@addr:port", "user", "", "scheme://addr:port"}, |
| 25 | + {"scheme://:pass@addr:port", "", "pass", "scheme://addr:port"}, |
| 26 | + {"//user:pass@addr:port", "user", "pass", "//addr:port"}, |
| 27 | + {"foo@bar", "", "", "foo@bar"}, |
| 28 | + {"", "", "", ""}, |
| 29 | + } |
| 30 | + |
| 31 | + for _, c := range cases { |
| 32 | + t.Run(c.addr, func(t *testing.T) { |
| 33 | + user, pass, addr := handleBasicAuth(c.addr) |
| 34 | + |
| 35 | + must.Eq(t, c.expectedUser, user) |
| 36 | + must.Eq(t, c.expectedPass, pass) |
| 37 | + must.Eq(t, c.expectedAddr, addr) |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestHelpers_clientOptsFromEnvironment_Address(t *testing.T) { |
| 43 | + cases := []struct { |
| 44 | + name string |
| 45 | + addr string |
| 46 | + expectedAddress string |
| 47 | + expectedHttpAuth *api.HttpBasicAuth |
| 48 | + }{ |
| 49 | + { |
| 50 | + addr: "addr", |
| 51 | + expectedAddress: "addr", |
| 52 | + expectedHttpAuth: nil, |
| 53 | + }, |
| 54 | + { |
| 55 | + addr: "scheme://user:pass@addr", |
| 56 | + expectedAddress: "scheme://addr", |
| 57 | + expectedHttpAuth: &api.HttpBasicAuth{Username: "user", Password: "pass"}, |
| 58 | + }, |
| 59 | + { |
| 60 | + addr: "scheme://user:@addr", |
| 61 | + expectedAddress: "scheme://addr", |
| 62 | + expectedHttpAuth: &api.HttpBasicAuth{Username: "user", Password: ""}, |
| 63 | + }, |
| 64 | + { |
| 65 | + addr: "scheme://:pass@addr", |
| 66 | + expectedAddress: "scheme://addr", |
| 67 | + expectedHttpAuth: &api.HttpBasicAuth{Username: "", Password: "pass"}, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, c := range cases { |
| 72 | + t.Run(c.name, func(t *testing.T) { |
| 73 | + t.Setenv("NOMAD_ADDR", c.addr) |
| 74 | + |
| 75 | + conf := api.Config{HttpAuth: nil} |
| 76 | + clientOptsFromEnvironment(&conf) |
| 77 | + |
| 78 | + must.Eq(t, c.expectedAddress, conf.Address) |
| 79 | + must.Eq(t, c.expectedHttpAuth, conf.HttpAuth) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestHelpers_clientOptsFromFlags_Address(t *testing.T) { |
| 85 | + cases := []struct { |
| 86 | + name string |
| 87 | + addr string |
| 88 | + expectedAddress string |
| 89 | + expectedHttpAuth *api.HttpBasicAuth |
| 90 | + }{ |
| 91 | + { |
| 92 | + addr: "addr", |
| 93 | + expectedAddress: "addr", |
| 94 | + expectedHttpAuth: nil, |
| 95 | + }, |
| 96 | + { |
| 97 | + addr: "scheme://user:pass@addr", |
| 98 | + expectedAddress: "scheme://addr", |
| 99 | + expectedHttpAuth: &api.HttpBasicAuth{Username: "user", Password: "pass"}, |
| 100 | + }, |
| 101 | + { |
| 102 | + addr: "scheme://user:@addr", |
| 103 | + expectedAddress: "scheme://addr", |
| 104 | + expectedHttpAuth: &api.HttpBasicAuth{Username: "user", Password: ""}, |
| 105 | + }, |
| 106 | + { |
| 107 | + addr: "scheme://:pass@addr", |
| 108 | + expectedAddress: "scheme://addr", |
| 109 | + expectedHttpAuth: &api.HttpBasicAuth{Username: "", Password: "pass"}, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + for _, c := range cases { |
| 114 | + t.Run(c.name, func(t *testing.T) { |
| 115 | + cmd := baseCommand{nomadConfig: nomadConfig{address: c.addr}} |
| 116 | + |
| 117 | + conf := api.Config{HttpAuth: nil} |
| 118 | + clientOptsFromFlags(&cmd, &conf) |
| 119 | + |
| 120 | + must.Eq(t, c.expectedAddress, conf.Address) |
| 121 | + must.Eq(t, c.expectedHttpAuth, conf.HttpAuth) |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments