github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/rkt/cli_apps_test.go (about) 1 // Copyright 2014 The rkt Authors 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 15 package main 16 17 import ( 18 "reflect" 19 "strings" 20 "testing" 21 22 "github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema/types" 23 flag "github.com/coreos/rkt/Godeps/_workspace/src/github.com/spf13/pflag" 24 ) 25 26 func TestParseAppArgs(t *testing.T) { 27 flags := flag.NewFlagSet("test", flag.ExitOnError) 28 flags.SetInterspersed(false) 29 tests := []struct { 30 in string 31 images []string 32 args [][]string 33 werr bool 34 }{ 35 { 36 "example.com/foo example.com/bar -- --help --- example.com/baz -- --verbose", 37 []string{"example.com/foo", "example.com/bar", "example.com/baz"}, 38 [][]string{ 39 nil, 40 []string{"--help"}, 41 []string{"--verbose"}, 42 }, 43 false, 44 }, 45 { 46 "example.com/foo --- example.com/bar --- example.com/baz ---", 47 []string{"example.com/foo", "example.com/bar", "example.com/baz"}, 48 [][]string{ 49 nil, 50 nil, 51 nil, 52 }, 53 false, 54 }, 55 { 56 "example.com/foo example.com/bar example.com/baz", 57 []string{"example.com/foo", "example.com/bar", "example.com/baz"}, 58 [][]string{ 59 nil, 60 nil, 61 nil, 62 }, 63 false, 64 }, 65 } 66 67 for i, tt := range tests { 68 rktApps.Reset() 69 err := parseApps(&rktApps, strings.Split(tt.in, " "), flags, true) 70 ga := rktApps.GetArgs() 71 gi := rktApps.GetImages() 72 if gerr := (err != nil); gerr != tt.werr { 73 t.Errorf("#%d: err==%v, want errstate %t", i, err, tt.werr) 74 } 75 if !reflect.DeepEqual(ga, tt.args) { 76 t.Errorf("#%d: got args %v, want args %v", i, ga, tt.args) 77 } 78 if !reflect.DeepEqual(gi, tt.images) { 79 t.Errorf("#%d: got images %v, want images %v", i, gi, tt.images) 80 } 81 } 82 83 } 84 85 func TestParsePortFlag(t *testing.T) { 86 tests := []struct { 87 in string 88 ex types.ExposedPort 89 err bool 90 }{ 91 { 92 in: "foo:123", 93 ex: types.ExposedPort{ 94 Name: "foo", 95 HostPort: 123, 96 }, 97 err: false, 98 }, 99 { 100 in: "f$o:123", 101 ex: types.ExposedPort{}, 102 err: true, 103 }, 104 { 105 in: "foo:12345", 106 ex: types.ExposedPort{}, 107 err: true, 108 }, 109 } 110 111 for _, tt := range tests { 112 pl := portList{} 113 err := pl.Set(tt.in) 114 115 if err != nil { 116 if !tt.err { 117 t.Errorf("%q failed to parse: %v", tt.in, err) 118 } 119 return 120 } 121 122 if tt.err { 123 t.Errorf("%q unexpectedly parsed", tt.in) 124 return 125 } 126 127 if len(pl) == 0 { 128 t.Errorf("%q parsed into a empty list", tt.in) 129 return 130 } 131 132 if pl[0].Name != tt.ex.Name { 133 t.Errorf("%q parsed but Name mismatch: got %v, expected %v", tt.in, pl[0].Name, tt.ex.Name) 134 } 135 136 if pl[0].HostPort != tt.ex.HostPort { 137 t.Errorf("%q parsed but HostPort mismatch: got %v, expected %v", tt.in, pl[0].HostPort, tt.ex.HostPort) 138 } 139 } 140 }