github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/remoteclientconfig/configfile_test.go (about) 1 package remoteclientconfig 2 3 import ( 4 "io" 5 "reflect" 6 "strings" 7 "testing" 8 ) 9 10 var goodConfig = ` 11 [connections] 12 13 [connections.homer] 14 destination = "192.168.1.1" 15 username = "myuser" 16 port = 22 17 default = true 18 19 [connections.bart] 20 destination = "foobar.com" 21 username = "root" 22 port = 22 23 ` 24 var noDest = ` 25 [connections] 26 27 [connections.homer] 28 destination = "192.168.1.1" 29 username = "myuser" 30 default = true 31 port = 22 32 33 [connections.bart] 34 username = "root" 35 port = 22 36 ` 37 38 var noUser = ` 39 [connections] 40 41 [connections.homer] 42 destination = "192.168.1.1" 43 port = 22 44 ` 45 46 func makeGoodResult() *RemoteConfig { 47 var goodConnections = make(map[string]RemoteConnection) 48 goodConnections["homer"] = RemoteConnection{ 49 Destination: "192.168.1.1", 50 Username: "myuser", 51 IsDefault: true, 52 Port: 22, 53 } 54 goodConnections["bart"] = RemoteConnection{ 55 Destination: "foobar.com", 56 Username: "root", 57 Port: 22, 58 } 59 var goodResult = RemoteConfig{ 60 Connections: goodConnections, 61 } 62 return &goodResult 63 } 64 65 func makeNoUserResult() *RemoteConfig { 66 var goodConnections = make(map[string]RemoteConnection) 67 goodConnections["homer"] = RemoteConnection{ 68 Destination: "192.168.1.1", 69 Port: 22, 70 } 71 var goodResult = RemoteConfig{ 72 Connections: goodConnections, 73 } 74 return &goodResult 75 } 76 77 func TestReadRemoteConfig(t *testing.T) { 78 type args struct { 79 reader io.Reader 80 } 81 tests := []struct { 82 name string 83 args args 84 want *RemoteConfig 85 wantErr bool 86 }{ 87 // good test should pass 88 {"good", args{reader: strings.NewReader(goodConfig)}, makeGoodResult(), false}, 89 // a connection with no destination is an error 90 {"nodest", args{reader: strings.NewReader(noDest)}, nil, true}, 91 // a connection with no user is OK 92 {"nouser", args{reader: strings.NewReader(noUser)}, makeNoUserResult(), false}, 93 } 94 for _, tt := range tests { 95 test := tt 96 t.Run(tt.name, func(t *testing.T) { 97 got, err := ReadRemoteConfig(test.args.reader) 98 if (err != nil) != test.wantErr { 99 t.Errorf("ReadRemoteConfig() error = %v, wantErr %v", err, test.wantErr) 100 return 101 } 102 if !reflect.DeepEqual(got, test.want) { 103 t.Errorf("ReadRemoteConfig() = %v, want %v", got, test.want) 104 } 105 }) 106 } 107 } 108 109 func TestRemoteConfig_GetDefault(t *testing.T) { 110 good := make(map[string]RemoteConnection) 111 good["homer"] = RemoteConnection{ 112 Username: "myuser", 113 Destination: "192.168.1.1", 114 IsDefault: true, 115 } 116 good["bart"] = RemoteConnection{ 117 Username: "root", 118 Destination: "foobar.com", 119 } 120 noDefault := make(map[string]RemoteConnection) 121 noDefault["homer"] = RemoteConnection{ 122 Username: "myuser", 123 Destination: "192.168.1.1", 124 } 125 noDefault["bart"] = RemoteConnection{ 126 Username: "root", 127 Destination: "foobar.com", 128 } 129 single := make(map[string]RemoteConnection) 130 single["homer"] = RemoteConnection{ 131 Username: "myuser", 132 Destination: "192.168.1.1", 133 } 134 135 none := make(map[string]RemoteConnection) 136 137 type fields struct { 138 Connections map[string]RemoteConnection 139 } 140 tests := []struct { 141 name string 142 fields fields 143 want *RemoteConnection 144 wantErr bool 145 }{ 146 // A good toml should return the connection that is marked isDefault 147 {"good", fields{Connections: makeGoodResult().Connections}, &RemoteConnection{"192.168.1.1", "myuser", true, 22, "", false}, false}, 148 // If nothing is marked as isDefault and there is more than one connection, error should occur 149 {"nodefault", fields{Connections: noDefault}, nil, true}, 150 // if nothing is marked as isDefault but there is only one connection, the one connection is considered the default 151 {"single", fields{Connections: none}, nil, true}, 152 } 153 for _, tt := range tests { 154 test := tt 155 t.Run(test.name, func(t *testing.T) { 156 r := &RemoteConfig{ 157 Connections: test.fields.Connections, 158 } 159 got, err := r.GetDefault() 160 if (err != nil) != test.wantErr { 161 t.Errorf("RemoteConfig.GetDefault() error = %v, wantErr %v", err, test.wantErr) 162 return 163 } 164 if !reflect.DeepEqual(got, test.want) { 165 t.Errorf("RemoteConfig.GetDefault() = %v, want %v", got, test.want) 166 } 167 }) 168 } 169 } 170 171 func TestRemoteConfig_GetRemoteConnection(t *testing.T) { 172 type fields struct { 173 Connections map[string]RemoteConnection 174 } 175 type args struct { 176 name string 177 } 178 179 blank := make(map[string]RemoteConnection) 180 tests := []struct { 181 name string 182 fields fields 183 args args 184 want *RemoteConnection 185 wantErr bool 186 }{ 187 // Good connection 188 {"goodhomer", fields{Connections: makeGoodResult().Connections}, args{name: "homer"}, &RemoteConnection{"192.168.1.1", "myuser", true, 22, "", false}, false}, 189 // Good connection 190 {"goodbart", fields{Connections: makeGoodResult().Connections}, args{name: "bart"}, &RemoteConnection{"foobar.com", "root", false, 22, "", false}, false}, 191 // Getting an unknown connection should result in error 192 {"noexist", fields{Connections: makeGoodResult().Connections}, args{name: "foobar"}, nil, true}, 193 // Getting a connection when there are none should result in an error 194 {"none", fields{Connections: blank}, args{name: "foobar"}, nil, true}, 195 } 196 for _, tt := range tests { 197 test := tt 198 t.Run(test.name, func(t *testing.T) { 199 r := &RemoteConfig{ 200 Connections: test.fields.Connections, 201 } 202 got, err := r.GetRemoteConnection(test.args.name) 203 if (err != nil) != test.wantErr { 204 t.Errorf("RemoteConfig.GetRemoteConnection() error = %v, wantErr %v", err, test.wantErr) 205 return 206 } 207 if !reflect.DeepEqual(got, test.want) { 208 t.Errorf("RemoteConfig.GetRemoteConnection() = %v, want %v", got, test.want) 209 } 210 }) 211 } 212 }