github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/network/filter_test.go (about) 1 // +build !windows 2 3 package network // import "github.com/demonoid81/moby/daemon/network" 4 5 import ( 6 "strings" 7 "testing" 8 9 "github.com/demonoid81/moby/api/types" 10 "github.com/demonoid81/moby/api/types/filters" 11 ) 12 13 func TestFilterNetworks(t *testing.T) { 14 networks := []types.NetworkResource{ 15 { 16 Name: "host", 17 Driver: "host", 18 Scope: "local", 19 }, 20 { 21 Name: "bridge", 22 Driver: "bridge", 23 Scope: "local", 24 }, 25 { 26 Name: "none", 27 Driver: "null", 28 Scope: "local", 29 }, 30 { 31 Name: "myoverlay", 32 Driver: "overlay", 33 Scope: "swarm", 34 }, 35 { 36 Name: "mydrivernet", 37 Driver: "mydriver", 38 Scope: "local", 39 }, 40 { 41 Name: "mykvnet", 42 Driver: "mykvdriver", 43 Scope: "global", 44 }, 45 { 46 Name: "networkwithcontainer", 47 Driver: "nwc", 48 Scope: "local", 49 Containers: map[string]types.EndpointResource{ 50 "customcontainer": { 51 Name: "customendpoint", 52 }, 53 }, 54 }, 55 } 56 57 bridgeDriverFilters := filters.NewArgs() 58 bridgeDriverFilters.Add("driver", "bridge") 59 60 overlayDriverFilters := filters.NewArgs() 61 overlayDriverFilters.Add("driver", "overlay") 62 63 nonameDriverFilters := filters.NewArgs() 64 nonameDriverFilters.Add("driver", "noname") 65 66 customDriverFilters := filters.NewArgs() 67 customDriverFilters.Add("type", "custom") 68 69 builtinDriverFilters := filters.NewArgs() 70 builtinDriverFilters.Add("type", "builtin") 71 72 invalidDriverFilters := filters.NewArgs() 73 invalidDriverFilters.Add("type", "invalid") 74 75 localScopeFilters := filters.NewArgs() 76 localScopeFilters.Add("scope", "local") 77 78 swarmScopeFilters := filters.NewArgs() 79 swarmScopeFilters.Add("scope", "swarm") 80 81 globalScopeFilters := filters.NewArgs() 82 globalScopeFilters.Add("scope", "global") 83 84 trueDanglingFilters := filters.NewArgs() 85 trueDanglingFilters.Add("dangling", "true") 86 87 falseDanglingFilters := filters.NewArgs() 88 falseDanglingFilters.Add("dangling", "false") 89 90 testCases := []struct { 91 filter filters.Args 92 resultCount int 93 err string 94 name string 95 results []string 96 }{ 97 { 98 filter: bridgeDriverFilters, 99 resultCount: 1, 100 err: "", 101 name: "bridge driver filters", 102 }, 103 { 104 filter: overlayDriverFilters, 105 resultCount: 1, 106 err: "", 107 name: "overlay driver filters", 108 }, 109 { 110 filter: nonameDriverFilters, 111 resultCount: 0, 112 err: "", 113 name: "no name driver filters", 114 }, 115 { 116 filter: customDriverFilters, 117 resultCount: 4, 118 err: "", 119 name: "custom driver filters", 120 }, 121 { 122 filter: builtinDriverFilters, 123 resultCount: 3, 124 err: "", 125 name: "builtin driver filters", 126 }, 127 { 128 filter: invalidDriverFilters, 129 resultCount: 0, 130 err: "invalid filter: 'type'='invalid'", 131 name: "invalid driver filters", 132 }, 133 { 134 filter: localScopeFilters, 135 resultCount: 5, 136 err: "", 137 name: "local scope filters", 138 }, 139 { 140 filter: swarmScopeFilters, 141 resultCount: 1, 142 err: "", 143 name: "swarm scope filters", 144 }, 145 { 146 filter: globalScopeFilters, 147 resultCount: 1, 148 err: "", 149 name: "global scope filters", 150 }, 151 { 152 filter: trueDanglingFilters, 153 resultCount: 3, 154 err: "", 155 name: "dangling filter is 'True'", 156 results: []string{"myoverlay", "mydrivernet", "mykvnet"}, 157 }, 158 { 159 filter: falseDanglingFilters, 160 resultCount: 4, 161 err: "", 162 name: "dangling filter is 'False'", 163 results: []string{"host", "bridge", "none", "networkwithcontainer"}, 164 }, 165 } 166 167 for _, testCase := range testCases { 168 t.Run(testCase.name, func(t *testing.T) { 169 ls := make([]types.NetworkResource, 0, len(networks)) 170 ls = append(ls, networks...) 171 result, err := FilterNetworks(ls, testCase.filter) 172 if testCase.err != "" { 173 if err == nil { 174 t.Fatalf("expect error '%s', got no error", testCase.err) 175 176 } else if !strings.Contains(err.Error(), testCase.err) { 177 t.Fatalf("expect error '%s', got '%s'", testCase.err, err) 178 } 179 } else { 180 if err != nil { 181 t.Fatalf("expect no error, got error '%s'", err) 182 } 183 // Make sure result is not nil 184 if result == nil { 185 t.Fatal("filterNetworks should not return nil") 186 } 187 188 if len(result) != testCase.resultCount { 189 t.Fatalf("expect '%d' networks, got '%d' networks", testCase.resultCount, len(result)) 190 } 191 192 if len(testCase.results) > 0 { 193 resultMap := make(map[string]bool) 194 for _, r := range result { 195 resultMap[r.Name] = true 196 } 197 for _, r := range testCase.results { 198 if _, ok := resultMap[r]; !ok { 199 t.Fatalf("expected result: '%s' not found", r) 200 } 201 } 202 } 203 } 204 }) 205 } 206 }