starter_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package starter
  15. import (
  16. "flag"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestParseConfig(t *testing.T) {
  21. tests := []struct {
  22. args []string
  23. wvals map[string]string
  24. }{
  25. {
  26. []string{"--name", "etcd", "--data-dir", "dir"},
  27. map[string]string{
  28. "name": "etcd",
  29. "data-dir": "dir",
  30. },
  31. },
  32. {
  33. []string{"--name=etcd", "--data-dir=dir"},
  34. map[string]string{
  35. "name": "etcd",
  36. "data-dir": "dir",
  37. },
  38. },
  39. {
  40. []string{"--version", "--name", "etcd"},
  41. map[string]string{
  42. "version": "true",
  43. "name": "etcd",
  44. },
  45. },
  46. {
  47. []string{"--version=true", "--name", "etcd"},
  48. map[string]string{
  49. "version": "true",
  50. "name": "etcd",
  51. },
  52. },
  53. }
  54. for i, tt := range tests {
  55. fs, err := parseConfig(tt.args)
  56. if err != nil {
  57. t.Fatalf("#%d: unexpected parseConfig error: %v", i, err)
  58. }
  59. vals := make(map[string]string)
  60. fs.Visit(func(f *flag.Flag) {
  61. vals[f.Name] = f.Value.String()
  62. })
  63. if !reflect.DeepEqual(vals, tt.wvals) {
  64. t.Errorf("#%d: vals = %+v, want %+v", i, vals, tt.wvals)
  65. }
  66. }
  67. }