How to use WithServer method of toxiproxy_test Package

Best Toxiproxy code snippet using toxiproxy_test.WithServer

api_test.go

Source:api_test.go Github

copy

Full Screen

...9 tclient "github.com/Shopify/toxiproxy/client"10)11var testServer *toxiproxy.ApiServer12var client = tclient.NewClient("http://127.0.0.1:8475")13func WithServer(t *testing.T, f func(string)) {14 // Make sure only one server is running at a time. Apparently there's no clean15 // way to shut it down between each test run.16 if testServer == nil {17 testServer = toxiproxy.NewServer()18 go testServer.Listen("localhost", "8475")19 // Allow server to start. There's no clean way to know when it listens.20 time.Sleep(50 * time.Millisecond)21 }22 defer func() {23 err := testServer.Collection.Clear()24 if err != nil {25 t.Error("Failed to clear collection", err)26 }27 }()28 f("http://localhost:8475")29}30func TestBrowserGets403(t *testing.T) {31 WithServer(t, func(addr string) {32 client := http.Client{}33 req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil)34 req.Header.Add("User-Agent", "Mozilla/5.0 (Linux; Android 4.4.2); Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Mobile Safari/537.36 OPR/20.0.1396.72047")35 resp, _ := client.Do(req)36 if resp.StatusCode != 403 {37 t.Fatal("Browser-like UserAgent was not denied access to Toxiproxy")38 }39 })40}41func TestNonBrowserGets200(t *testing.T) {42 WithServer(t, func(addr string) {43 client := http.Client{}44 req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil)45 req.Header.Add("User-Agent", "Wget/2.1")46 resp, _ := client.Do(req)47 if resp.StatusCode == 403 {48 t.Fatal("Non-Browser-like UserAgent was denied access to Toxiproxy")49 }50 })51}52func TestIndexWithNoProxies(t *testing.T) {53 WithServer(t, func(addr string) {54 client := tclient.NewClient(addr)55 proxies, err := client.Proxies()56 if err != nil {57 t.Fatal("Failed getting proxies:", err)58 }59 if len(proxies) > 0 {60 t.Fatal("Expected no proxies, got:", proxies)61 }62 })63}64func TestCreateProxyBlankName(t *testing.T) {65 WithServer(t, func(addr string) {66 _, err := client.CreateProxy("", "", "")67 if err == nil {68 t.Fatal("Expected error creating proxy, got nil")69 } else if err.Error() != "Create: HTTP 400: missing required field: name" {70 t.Fatal("Expected different error creating proxy:", err)71 }72 })73}74func TestCreateProxyBlankUpstream(t *testing.T) {75 WithServer(t, func(addr string) {76 _, err := client.CreateProxy("test", "", "")77 if err == nil {78 t.Fatal("Expected error creating proxy, got nil")79 } else if err.Error() != "Create: HTTP 400: missing required field: upstream" {80 t.Fatal("Expected different error creating proxy:", err)81 }82 })83}84func TestPopulateProxy(t *testing.T) {85 WithServer(t, func(addr string) {86 testProxies, err := client.Populate([]tclient.Proxy{87 {88 Name: "one",89 Listen: "localhost:7070",90 Upstream: "localhost:7171",91 Enabled: true,92 },93 {94 Name: "two",95 Listen: "localhost:7373",96 Upstream: "localhost:7474",97 Enabled: true,98 },99 })100 if err != nil {101 t.Fatal("Unable to populate:", err)102 } else if len(testProxies) != 2 {103 t.Fatalf("Wrong number of proxies returned: %d != 2", len(testProxies))104 } else if testProxies[0].Name != "one" || testProxies[1].Name != "two" {105 t.Fatalf("Wrong proxy names returned: %s, %s", testProxies[0].Name, testProxies[1].Name)106 }107 for _, p := range testProxies {108 AssertProxyUp(t, p.Listen, true)109 }110 })111}112func TestPopulateDefaultEnabled(t *testing.T) {113 WithServer(t, func(addr string) {114 request := []byte(`[{"name": "test", "listen": "localhost:7070", "upstream": "localhost:7171"}]`)115 resp, err := http.Post(addr+"/populate", "application/json", bytes.NewReader(request))116 if err != nil {117 t.Fatal("Failed to send POST to /populate:", err)118 }119 if resp.StatusCode != http.StatusCreated {120 message, _ := ioutil.ReadAll(resp.Body)121 t.Fatalf("Failed to populate proxy list: HTTP %s\n%s", resp.Status, string(message))122 }123 proxies, err := client.Proxies()124 if err != nil {125 t.Fatal(err)126 } else if len(proxies) != 1 {127 t.Fatalf("Wrong number of proxies created: %d != 1", len(proxies))128 } else if _, ok := proxies["test"]; !ok {129 t.Fatalf("Wrong proxy name returned")130 }131 for _, p := range proxies {132 AssertProxyUp(t, p.Listen, true)133 }134 })135}136func TestPopulateDisabledProxy(t *testing.T) {137 WithServer(t, func(addr string) {138 testProxies, err := client.Populate([]tclient.Proxy{139 {140 Name: "one",141 Listen: "localhost:7070",142 Upstream: "localhost:7171",143 Enabled: false,144 },145 {146 Name: "two",147 Listen: "localhost:7373",148 Upstream: "localhost:7474",149 Enabled: true,150 },151 })152 if err != nil {153 t.Fatal("Unable to populate:", err)154 } else if len(testProxies) != 2 {155 t.Fatalf("Wrong number of proxies returned: %d != 2", len(testProxies))156 } else if testProxies[0].Name != "one" || testProxies[1].Name != "two" {157 t.Fatalf("Wrong proxy names returned: %s, %s", testProxies[0].Name, testProxies[1].Name)158 }159 AssertProxyUp(t, "localhost:7070", false)160 AssertProxyUp(t, "localhost:7373", true)161 })162}163func TestPopulateExistingProxy(t *testing.T) {164 WithServer(t, func(addr string) {165 testProxy, err := client.CreateProxy("one", "localhost:7070", "localhost:7171")166 if err != nil {167 t.Fatal("Unable to create proxy:", err)168 }169 _, err = client.CreateProxy("two", "localhost:7373", "localhost:7474")170 if err != nil {171 t.Fatal("Unable to create proxy:", err)172 }173 // Create a toxic so we can make sure the proxy wasn't replaced174 _, err = testProxy.AddToxic("", "latency", "downstream", 1, nil)175 if err != nil {176 t.Fatal("Unable to create toxic:", err)177 }178 testProxies, err := client.Populate([]tclient.Proxy{179 {180 Name: "one",181 Listen: "127.0.0.1:7070", // TODO(xthexder): Will replace existing proxy if not resolved ip...182 Upstream: "localhost:7171",183 Enabled: true,184 },185 {186 Name: "two",187 Listen: "127.0.0.1:7575",188 Upstream: "localhost:7676",189 Enabled: true,190 },191 })192 if err != nil {193 t.Fatal("Unable to populate:", err)194 } else if len(testProxies) != 2 {195 t.Fatalf("Wrong number of proxies returned: %d != 2", len(testProxies))196 } else if testProxies[0].Name != "one" || testProxies[1].Name != "two" {197 t.Fatalf("Wrong proxy names returned: %s, %s", testProxies[0].Name, testProxies[1].Name)198 } else if testProxies[0].Listen != "127.0.0.1:7070" || testProxies[1].Listen != "127.0.0.1:7575" {199 t.Fatalf("Wrong proxy listen addresses returned: %s, %s", testProxies[0].Listen, testProxies[1].Listen)200 }201 toxics, err := testProxy.Toxics()202 if err != nil {203 t.Fatal("Unable to get toxics:", err)204 }205 if len(toxics) != 1 || toxics[0].Type != "latency" {206 t.Fatalf("Populate did not preseve existing proxy. (%d toxics)", len(toxics))207 }208 for _, p := range testProxies {209 AssertProxyUp(t, p.Listen, true)210 }211 })212}213func TestPopulateWithBadName(t *testing.T) {214 WithServer(t, func(addr string) {215 testProxies, err := client.Populate([]tclient.Proxy{216 {217 Name: "one",218 Listen: "localhost:7070",219 Upstream: "localhost:7171",220 Enabled: true,221 },222 {223 Name: "",224 Listen: "",225 Enabled: true,226 },227 })228 if err == nil {229 t.Fatal("Expected Populate to fail.")230 } else if err.Error() != "Populate: HTTP 400: missing required field: name at proxy 2" {231 t.Fatal("Expected different error during populate:", err)232 } else if len(testProxies) != 0 {233 t.Fatalf("Wrong number of proxies returned: %d != 0", len(testProxies))234 }235 proxies, err := client.Proxies()236 if err != nil {237 t.Fatal(err)238 } else if len(proxies) != 0 {239 t.Fatalf("Expected no proxies to be created: %d != 0", len(proxies))240 }241 })242}243func TestPopulateProxyWithBadDataShouldReturnError(t *testing.T) {244 WithServer(t, func(addr string) {245 testProxies, err := client.Populate([]tclient.Proxy{246 {247 Name: "one",248 Listen: "localhost:7070",249 Upstream: "localhost:7171",250 Enabled: true,251 },252 {253 Name: "two",254 Listen: "local373",255 Upstream: "localhost:7474",256 Enabled: true,257 },258 {259 Name: "three",260 Listen: "localhost:7575",261 Upstream: "localhost:7676",262 Enabled: true,263 },264 })265 if err == nil {266 t.Fatal("Expected Populate to fail.")267 } else if len(testProxies) != 1 {268 t.Fatalf("Wrong number of proxies returned: %d != %d", len(testProxies), 1)269 } else if testProxies[0].Name != "one" {270 t.Fatalf("Wrong proxy name returned: %s != one", testProxies[0].Name)271 }272 for _, p := range testProxies {273 AssertProxyUp(t, p.Listen, true)274 }275 proxies, err := client.Proxies()276 if err != nil {277 t.Fatal(err)278 }279 for _, p := range proxies {280 if p.Name == "two" || p.Name == "three" {281 t.Fatalf("Proxy %s exists, populate did not fail correctly.", p.Name)282 }283 }284 })285}286func TestListingProxies(t *testing.T) {287 WithServer(t, func(addr string) {288 _, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")289 if err != nil {290 t.Fatal("Unable to create proxy:", err)291 }292 proxies, err := client.Proxies()293 if err != nil {294 t.Fatal("Error listing proxies:", err)295 }296 if len(proxies) == 0 {297 t.Fatal("Expected new proxy in list")298 }299 proxy, ok := proxies["mysql_master"]300 if !ok {301 t.Fatal("Expected to see mysql_master proxy in list")302 }303 if proxy.Name != "mysql_master" || proxy.Listen != "127.0.0.1:3310" || proxy.Upstream != "localhost:20001" {304 t.Fatalf("Unexpected proxy metadata: %s, %s, %s", proxy.Name, proxy.Listen, proxy.Upstream)305 }306 AssertToxicExists(t, proxy.ActiveToxics, "latency", "", "", false)307 })308}309func TestCreateAndGetProxy(t *testing.T) {310 WithServer(t, func(addr string) {311 _, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")312 if err != nil {313 t.Fatal("Unable to create proxy:", err)314 }315 proxy, err := client.Proxy("mysql_master")316 if err != nil {317 t.Fatal("Unable to retriecve proxy:", err)318 }319 if proxy.Name != "mysql_master" || proxy.Listen != "127.0.0.1:3310" || proxy.Upstream != "localhost:20001" || !proxy.Enabled {320 t.Fatalf("Unexpected proxy metadata: %s, %s, %s, %v", proxy.Name, proxy.Listen, proxy.Upstream, proxy.Enabled)321 }322 AssertToxicExists(t, proxy.ActiveToxics, "latency", "", "", false)323 })324}325func TestCreateProxyWithSave(t *testing.T) {326 WithServer(t, func(addr string) {327 testProxy := client.NewProxy()328 testProxy.Name = "mysql_master"329 testProxy.Listen = "localhost:3310"330 testProxy.Upstream = "localhost:20001"331 testProxy.Enabled = true332 err := testProxy.Save()333 if err != nil {334 t.Fatal("Unable to create proxy:", err)335 }336 proxy, err := client.Proxy("mysql_master")337 if err != nil {338 t.Fatal("Unable to retriecve proxy:", err)339 }340 if proxy.Name != "mysql_master" || proxy.Listen != "127.0.0.1:3310" || proxy.Upstream != "localhost:20001" || !proxy.Enabled {341 t.Fatalf("Unexpected proxy metadata: %s, %s, %s, %v", proxy.Name, proxy.Listen, proxy.Upstream, proxy.Enabled)342 }343 AssertProxyUp(t, proxy.Listen, true)344 })345}346func TestCreateDisabledProxy(t *testing.T) {347 WithServer(t, func(addr string) {348 disabledProxy := client.NewProxy()349 disabledProxy.Name = "mysql_master"350 disabledProxy.Listen = "localhost:3310"351 disabledProxy.Upstream = "localhost:20001"352 err := disabledProxy.Save()353 if err != nil {354 t.Fatal("Unable to create proxy:", err)355 }356 proxy, err := client.Proxy("mysql_master")357 if err != nil {358 t.Fatal("Unable to retriecve proxy:", err)359 }360 if proxy.Name != "mysql_master" || proxy.Listen != "localhost:3310" || proxy.Upstream != "localhost:20001" || proxy.Enabled {361 t.Fatalf("Unexpected proxy metadata: %s, %s, %s, %v", proxy.Name, proxy.Listen, proxy.Upstream, proxy.Enabled)362 }363 AssertProxyUp(t, proxy.Listen, false)364 })365}366func TestCreateDisabledProxyAndEnable(t *testing.T) {367 WithServer(t, func(addr string) {368 disabledProxy := client.NewProxy()369 disabledProxy.Name = "mysql_master"370 disabledProxy.Listen = "localhost:3310"371 disabledProxy.Upstream = "localhost:20001"372 err := disabledProxy.Save()373 if err != nil {374 t.Fatal("Unable to create proxy:", err)375 }376 proxy, err := client.Proxy("mysql_master")377 if err != nil {378 t.Fatal("Unable to retriecve proxy:", err)379 }380 if proxy.Name != "mysql_master" || proxy.Listen != "localhost:3310" || proxy.Upstream != "localhost:20001" || proxy.Enabled {381 t.Fatalf("Unexpected proxy metadata: %s, %s, %s, %v", proxy.Name, proxy.Listen, proxy.Upstream, proxy.Enabled)382 }383 proxy.Enabled = true384 err = proxy.Save()385 if err != nil {386 t.Fatal("Failed to update proxy:", err)387 }388 AssertProxyUp(t, proxy.Listen, true)389 proxy.Enabled = false390 err = proxy.Save()391 if err != nil {392 t.Fatal("Failed to update proxy:", err)393 }394 AssertProxyUp(t, proxy.Listen, false)395 })396}397func TestDeleteProxy(t *testing.T) {398 WithServer(t, func(addr string) {399 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")400 if err != nil {401 t.Fatal("Unable to create proxy:", err)402 }403 proxies, err := client.Proxies()404 if err != nil {405 t.Fatal("Error listing proxies:", err)406 }407 if len(proxies) == 0 {408 t.Fatal("Expected new proxy in list")409 }410 AssertProxyUp(t, testProxy.Listen, true)411 err = testProxy.Delete()412 if err != nil {413 t.Fatal("Failed deleting proxy:", err)414 }415 AssertProxyUp(t, testProxy.Listen, false)416 proxies, err = client.Proxies()417 if err != nil {418 t.Fatal("Error listing proxies:", err)419 }420 if len(proxies) > 0 {421 t.Fatal("Expected proxy to be deleted from list")422 }423 err = testProxy.Delete()424 if err == nil {425 t.Fatal("Proxy did not result in not found.")426 } else if err.Error() != "Delete: HTTP 404: proxy not found" {427 t.Fatal("Incorrect error removing proxy:", err)428 }429 })430}431func TestCreateProxyPortConflict(t *testing.T) {432 WithServer(t, func(addr string) {433 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")434 if err != nil {435 t.Fatal("Unable to create proxy:", err)436 }437 _, err = client.CreateProxy("test", "localhost:3310", "localhost:20001")438 if err == nil {439 t.Fatal("Proxy did not result in conflict.")440 } else if err.Error() != "Create: HTTP 500: listen tcp 127.0.0.1:3310: bind: address already in use" {441 t.Fatal("Incorrect error adding proxy:", err)442 }443 err = testProxy.Delete()444 if err != nil {445 t.Fatal("Unable to delete proxy:", err)446 }447 _, err = client.CreateProxy("test", "localhost:3310", "localhost:20001")448 if err != nil {449 t.Fatal("Unable to create proxy:", err)450 }451 })452}453func TestCreateProxyNameConflict(t *testing.T) {454 WithServer(t, func(addr string) {455 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")456 if err != nil {457 t.Fatal("Unable to create proxy:", err)458 }459 _, err = client.CreateProxy("mysql_master", "localhost:3311", "localhost:20001")460 if err == nil {461 t.Fatal("Proxy did not result in conflict.")462 } else if err.Error() != "Create: HTTP 409: proxy already exists" {463 t.Fatal("Incorrect error adding proxy:", err)464 }465 err = testProxy.Delete()466 if err != nil {467 t.Fatal("Unable to delete proxy:", err)468 }469 _, err = client.CreateProxy("mysql_master", "localhost:3311", "localhost:20001")470 if err != nil {471 t.Fatal("Unable to create proxy:", err)472 }473 })474}475func TestResetState(t *testing.T) {476 WithServer(t, func(addr string) {477 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")478 if err != nil {479 t.Fatal("Unable to create proxy:", err)480 }481 latency, err := testProxy.AddToxic("", "latency", "downstream", 1, tclient.Attributes{482 "latency": 100,483 "jitter": 10,484 })485 if err != nil {486 t.Fatal("Error setting toxic:", err)487 }488 if latency.Attributes["latency"] != 100.0 || latency.Attributes["jitter"] != 10.0 {489 t.Fatal("Latency toxic did not start up with correct settings")490 }491 err = client.ResetState()492 if err != nil {493 t.Fatal("unable to reset state:", err)494 }495 proxies, err := client.Proxies()496 if err != nil {497 t.Fatal("Error listing proxies:", err)498 }499 proxy, ok := proxies["mysql_master"]500 if !ok {501 t.Fatal("Expected proxy to still exist")502 }503 if !proxy.Enabled {504 t.Fatal("Expected proxy to be enabled")505 }506 toxics, err := proxy.Toxics()507 if err != nil {508 t.Fatal("Error requesting toxics:", err)509 }510 AssertToxicExists(t, toxics, "latency", "", "", false)511 AssertProxyUp(t, proxy.Listen, true)512 })513}514func TestListingToxics(t *testing.T) {515 WithServer(t, func(addr string) {516 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")517 if err != nil {518 t.Fatal("Unable to create proxy:", err)519 }520 toxics, err := testProxy.Toxics()521 if err != nil {522 t.Fatal("Error returning toxics:", err)523 }524 AssertToxicExists(t, toxics, "latency", "", "", false)525 })526}527func TestAddToxic(t *testing.T) {528 WithServer(t, func(addr string) {529 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")530 if err != nil {531 t.Fatal("Unable to create proxy:", err)532 }533 latency, err := testProxy.AddToxic("foobar", "latency", "downstream", 1, tclient.Attributes{534 "latency": 100,535 "jitter": 10,536 })537 if err != nil {538 t.Fatal("Error setting toxic:", err)539 }540 if latency.Attributes["latency"] != 100.0 || latency.Attributes["jitter"] != 10.0 {541 t.Fatal("Latency toxic did not start up with correct settings")542 }543 toxics, err := testProxy.Toxics()544 if err != nil {545 t.Fatal("Error returning toxics:", err)546 }547 toxic := AssertToxicExists(t, toxics, "foobar", "latency", "downstream", true)548 if toxic.Toxicity != 1.0 || toxic.Attributes["latency"] != 100.0 || toxic.Attributes["jitter"] != 10.0 {549 t.Fatal("Toxic was not read back correctly:", toxic)550 }551 })552}553func TestAddMultipleToxics(t *testing.T) {554 WithServer(t, func(addr string) {555 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")556 if err != nil {557 t.Fatal("Unable to create proxy:", err)558 }559 _, err = testProxy.AddToxic("latency1", "latency", "downstream", 1, nil)560 if err != nil {561 t.Fatal("Error setting toxic:", err)562 }563 _, err = testProxy.AddToxic("latency2", "latency", "downstream", 1, nil)564 if err != nil {565 t.Fatal("Error setting toxic:", err)566 }567 toxics, err := testProxy.Toxics()568 if err != nil {569 t.Fatal("Error returning toxics:", err)570 }571 AssertToxicExists(t, toxics, "latency1", "latency", "downstream", true)572 toxic := AssertToxicExists(t, toxics, "latency2", "latency", "downstream", true)573 if toxic.Toxicity != 1.0 || toxic.Attributes["latency"] != 0.0 || toxic.Attributes["jitter"] != 0.0 {574 t.Fatal("Toxic was not read back correctly:", toxic)575 }576 AssertToxicExists(t, toxics, "latency1", "", "upstream", false)577 AssertToxicExists(t, toxics, "latency2", "", "upstream", false)578 })579}580func TestAddConflictingToxic(t *testing.T) {581 WithServer(t, func(addr string) {582 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")583 if err != nil {584 t.Fatal("Unable to create proxy:", err)585 }586 _, err = testProxy.AddToxic("foobar", "latency", "downstream", 1, nil)587 if err != nil {588 t.Fatal("Error setting toxic:", err)589 }590 _, err = testProxy.AddToxic("foobar", "slow_close", "downstream", 1, nil)591 if err == nil {592 t.Fatal("Toxic did not result in conflict.")593 } else if err.Error() != "AddToxic: HTTP 409: toxic already exists" {594 t.Fatal("Incorrect error setting toxic:", err)595 }596 toxics, err := testProxy.Toxics()597 if err != nil {598 t.Fatal("Error returning toxics:", err)599 }600 toxic := AssertToxicExists(t, toxics, "foobar", "latency", "downstream", true)601 if toxic.Toxicity != 1.0 || toxic.Attributes["latency"] != 0.0 || toxic.Attributes["jitter"] != 0.0 {602 t.Fatal("Toxic was not read back correctly:", toxic)603 }604 AssertToxicExists(t, toxics, "foobar", "", "upstream", false)605 })606}607func TestAddConflictingToxicsMultistream(t *testing.T) {608 WithServer(t, func(addr string) {609 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")610 if err != nil {611 t.Fatal("Unable to create proxy:", err)612 }613 _, err = testProxy.AddToxic("foobar", "latency", "upstream", 1, nil)614 if err != nil {615 t.Fatal("Error setting toxic:", err)616 }617 _, err = testProxy.AddToxic("foobar", "latency", "downstream", 1, nil)618 if err == nil {619 t.Fatal("Toxic did not result in conflict.")620 } else if err.Error() != "AddToxic: HTTP 409: toxic already exists" {621 t.Fatal("Incorrect error setting toxic:", err)622 }623 toxics, err := testProxy.Toxics()624 if err != nil {625 t.Fatal("Error returning toxics:", err)626 }627 toxic := AssertToxicExists(t, toxics, "foobar", "latency", "upstream", true)628 if toxic.Toxicity != 1.0 || toxic.Attributes["latency"] != 0.0 || toxic.Attributes["jitter"] != 0.0 {629 t.Fatal("Toxic was not read back correctly:", toxic)630 }631 AssertToxicExists(t, toxics, "foobar", "", "downstream", false)632 })633}634func TestAddConflictingToxicsMultistreamDefaults(t *testing.T) {635 WithServer(t, func(addr string) {636 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")637 if err != nil {638 t.Fatal("Unable to create proxy:", err)639 }640 _, err = testProxy.AddToxic("", "latency", "upstream", 1, nil)641 if err != nil {642 t.Fatal("Error setting toxic:", err)643 }644 _, err = testProxy.AddToxic("", "latency", "downstream", 1, nil)645 if err != nil {646 t.Fatal("Error setting toxic:", err)647 }648 toxics, err := testProxy.Toxics()649 if err != nil {650 t.Fatal("Error returning toxics:", err)651 }652 toxic := AssertToxicExists(t, toxics, "latency_upstream", "latency", "upstream", true)653 if toxic.Toxicity != 1.0 || toxic.Attributes["latency"] != 0.0 || toxic.Attributes["jitter"] != 0.0 {654 t.Fatal("Toxic was not read back correctly:", toxic)655 }656 toxic = AssertToxicExists(t, toxics, "latency_downstream", "latency", "downstream", true)657 if toxic.Toxicity != 1.0 || toxic.Attributes["latency"] != 0.0 || toxic.Attributes["jitter"] != 0.0 {658 t.Fatal("Toxic was not read back correctly:", toxic)659 }660 })661}662func TestAddToxicWithToxicity(t *testing.T) {663 WithServer(t, func(addr string) {664 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")665 if err != nil {666 t.Fatal("Unable to create proxy:", err)667 }668 latency, err := testProxy.AddToxic("", "latency", "downstream", 0.2, tclient.Attributes{669 "latency": 100,670 "jitter": 10,671 })672 if err != nil {673 t.Fatal("Error setting toxic:", err)674 }675 if latency.Toxicity != 0.2 || latency.Attributes["latency"] != 100.0 || latency.Attributes["jitter"] != 10.0 {676 t.Fatal("Latency toxic did not start up with correct settings:", latency)677 }678 toxics, err := testProxy.Toxics()679 if err != nil {680 t.Fatal("Error returning toxics:", err)681 }682 toxic := AssertToxicExists(t, toxics, "latency_downstream", "latency", "downstream", true)683 if toxic.Toxicity != 0.2 || toxic.Attributes["latency"] != 100.0 || toxic.Attributes["jitter"] != 10.0 {684 t.Fatal("Toxic was not read back correctly:", toxic)685 }686 })687}688func TestAddNoop(t *testing.T) {689 WithServer(t, func(addr string) {690 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")691 if err != nil {692 t.Fatal("Unable to create proxy:", err)693 }694 noop, err := testProxy.AddToxic("foobar", "noop", "", 1, nil)695 if err != nil {696 t.Fatal("Error setting toxic:", err)697 }698 if noop.Toxicity != 1.0 || noop.Name != "foobar" || noop.Type != "noop" || noop.Stream != "downstream" {699 t.Fatal("Noop toxic did not start up with correct settings:", noop)700 }701 toxics, err := testProxy.Toxics()702 if err != nil {703 t.Fatal("Error returning toxics:", err)704 }705 toxic := AssertToxicExists(t, toxics, "foobar", "noop", "downstream", true)706 if toxic.Toxicity != 1.0 {707 t.Fatal("Toxic was not read back correctly:", toxic)708 }709 })710}711func TestUpdateToxics(t *testing.T) {712 WithServer(t, func(addr string) {713 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")714 if err != nil {715 t.Fatal("Unable to create proxy:", err)716 }717 latency, err := testProxy.AddToxic("", "latency", "downstream", -1, tclient.Attributes{718 "latency": 100,719 "jitter": 10,720 })721 if err != nil {722 t.Fatal("Error setting toxic:", err)723 }724 if latency.Toxicity != 1.0 || latency.Attributes["latency"] != 100.0 || latency.Attributes["jitter"] != 10.0 {725 t.Fatal("Latency toxic did not start up with correct settings:", latency)726 }727 latency, err = testProxy.UpdateToxic("latency_downstream", 0.5, tclient.Attributes{728 "latency": 1000,729 })730 if err != nil {731 t.Fatal("Error setting toxic:", err)732 }733 if latency.Toxicity != 0.5 || latency.Attributes["latency"] != 1000.0 || latency.Attributes["jitter"] != 10.0 {734 t.Fatal("Latency toxic did not get updated with the correct settings:", latency)735 }736 latency, err = testProxy.UpdateToxic("latency_downstream", -1, tclient.Attributes{737 "latency": 500,738 })739 if err != nil {740 t.Fatal("Error setting toxic:", err)741 }742 if latency.Toxicity != 0.5 || latency.Attributes["latency"] != 500.0 || latency.Attributes["jitter"] != 10.0 {743 t.Fatal("Latency toxic did not get updated with the correct settings:", latency)744 }745 toxics, err := testProxy.Toxics()746 if err != nil {747 t.Fatal("Error returning toxics:", err)748 }749 toxic := AssertToxicExists(t, toxics, "latency_downstream", "latency", "downstream", true)750 if toxic.Toxicity != 0.5 || toxic.Attributes["latency"] != 500.0 || toxic.Attributes["jitter"] != 10.0 {751 t.Fatal("Toxic was not read back correctly:", toxic)752 }753 })754}755func TestRemoveToxic(t *testing.T) {756 WithServer(t, func(addr string) {757 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")758 if err != nil {759 t.Fatal("Unable to create proxy:", err)760 }761 _, err = testProxy.AddToxic("", "latency", "downstream", 1, nil)762 if err != nil {763 t.Fatal("Error setting toxic:", err)764 }765 toxics, err := testProxy.Toxics()766 if err != nil {767 t.Fatal("Error returning toxics:", err)768 }769 toxic := AssertToxicExists(t, toxics, "latency_downstream", "latency", "downstream", true)770 if toxic.Toxicity != 1.0 || toxic.Attributes["latency"] != 0.0 || toxic.Attributes["jitter"] != 0.0 {771 t.Fatal("Toxic was not read back correctly:", toxic)772 }773 err = testProxy.RemoveToxic("latency_downstream")774 if err != nil {775 t.Fatal("Error removing toxic:", err)776 }777 toxics, err = testProxy.Toxics()778 if err != nil {779 t.Fatal("Error returning toxics:", err)780 }781 AssertToxicExists(t, toxics, "latency_downstream", "", "", false)782 })783}784func TestVersionEndpointReturnsVersion(t *testing.T) {785 WithServer(t, func(addr string) {786 resp, err := http.Get(addr + "/version")787 if err != nil {788 t.Fatal("Failed to get index", err)789 }790 body, err := ioutil.ReadAll(resp.Body)791 if err != nil {792 t.Fatal("Unable to read body from response")793 }794 if string(body) != toxiproxy.Version {795 t.Fatal("Expected to return Version from /version, got:", string(body))796 }797 })798}799func TestInvalidStream(t *testing.T) {800 WithServer(t, func(addr string) {801 testProxy, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001")802 if err != nil {803 t.Fatal("Unable to create proxy:", err)804 }805 _, err = testProxy.AddToxic("", "latency", "walrustream", 1, nil)806 if err == nil {807 t.Fatal("Error setting toxic:", err)808 }809 })810}811func AssertToxicExists(t *testing.T, toxics tclient.Toxics, name, typeName, stream string, exists bool) *tclient.Toxic {812 var toxic *tclient.Toxic813 var actualType, actualStream string814 for _, tox := range toxics {...

Full Screen

Full Screen

WithServer

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client, err := toxiproxy.NewClient("localhost:8474")4 if err != nil {5 panic(err)6 }7 toxiproxy_test, err := client.Proxy("toxiproxy_test")8 if err != nil {9 panic(err)10 }11 toxiproxy_test.WithServer("localhost:8080")12 fmt.Println(toxiproxy_test)13}14import (15func main() {16 client, err := toxiproxy.NewClient("localhost:8474")17 if err != nil {18 panic(err)19 }20 toxiproxy_test, err := client.Proxy("toxiproxy_test")21 if err != nil {22 panic(err)23 }24 toxiproxy_test.WithServer("localhost:8080")25 fmt.Println(toxiproxy_test)26}27import (28func main() {29 client, err := toxiproxy.NewClient("localhost:8474")30 if err != nil {31 panic(err)32 }33 toxiproxy_test, err := client.Proxy("toxiproxy_test")34 if err != nil {35 panic(err)36 }37 toxiproxy_test.WithServer("localhost:8080")38 fmt.Println(toxiproxy_test)39}40import (41func main() {42 client, err := toxiproxy.NewClient("localhost:8474")43 if err != nil {44 panic(err)45 }46 toxiproxy_test, err := client.Proxy("toxiproxy_test")47 if err != nil {48 panic(err)49 }50 toxiproxy_test.WithServer("localhost:8080")51 fmt.Println(toxiproxy_test)52}

Full Screen

Full Screen

WithServer

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxiproxyClient := toxiproxy.NewClient("localhost:8474")4 toxiproxyClient.CreateProxy("test", "localhost:3000", "localhost:3001")5 toxiproxyClient.EnableProxy("test")6 toxiproxyClient.WithServer("test", func(server *toxiproxy.Server) error {7 fmt.Println("server is running")8 })9}10import (11func main() {12 toxiproxyClient := toxiproxy.NewClient("localhost:8474")13 toxiproxyClient.CreateProxy("test", "localhost:3000", "localhost:3001")14 toxiproxyClient.EnableProxy("test")15 toxiproxyClient.WithProxy("test", func(proxy *toxiproxy.Proxy) error {16 fmt.Println("proxy is running")17 })18}19toxiproxyClient := toxiproxy.NewClient("localhost:8474")20toxiproxyClient.CreateProxy("test", "localhost:3000", "localhost:3001")21toxiproxyClient.EnableProxy("test")22toxiproxyClient.DisableProxy("test")

Full Screen

Full Screen

WithServer

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 proxy := toxiproxy.NewProxy("test", "localhost:6379", "localhost:6380")4 toxiproxy.WithServer("localhost:8474", func() {5 proxy.Create()6 fmt.Println("proxy created")7 proxy.Delete()8 fmt.Println("proxy deleted")9 })10}11import (12func main() {13 proxy := toxiproxy.NewProxy("test", "localhost:6379", "localhost:6380")14 toxiproxy.WithServer("localhost:8474", func() {15 proxy.Create()16 fmt.Println("proxy created")17 proxy.Delete()18 fmt.Println("proxy deleted")19 })20}21import (22func main() {23 proxy := toxiproxy.NewProxy("test", "localhost:6379", "localhost:6380")24 toxiproxy.WithServer("localhost:8474", func() {25 proxy.Create()26 fmt.Println("proxy created")27 proxy.Delete()28 fmt.Println("proxy deleted")29 })30}31import (32func main() {33 proxy := toxiproxy.NewProxy("test", "localhost:6379", "localhost:6380")34 toxiproxy.WithServer("localhost:8474", func() {35 proxy.Create()36 fmt.Println("proxy created")37 proxy.Delete()38 fmt.Println("proxy deleted")39 })40}41import (42func main() {43 proxy := toxiproxy.NewProxy("test", "localhost:6379", "localhost:6380")44 toxiproxy.WithServer("localhost:

Full Screen

Full Screen

WithServer

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 defer resp.Body.Close()7 fmt.Println(resp.Status)8}9import (10func main() {11 client := &http.Client{}12 if err != nil {13 fmt.Println(err)14 }15 resp, err := client.Do(req)16 if err != nil {17 fmt.Println(err)18 }19 defer resp.Body.Close()20 body, err := ioutil.ReadAll(resp.Body)21 if err != nil {22 fmt.Println(err)23 }24 fmt.Println(string(body))25}26import (27func main() {28 client := &http.Client{}29 if err != nil {30 fmt.Println(err)31 }32 resp, err := client.Do(req)33 if err != nil {34 fmt.Println(err)35 }36 defer resp.Body.Close()37 body, err := ioutil.ReadAll(resp.Body)38 if err != nil {39 fmt.Println(err)40 }41 fmt.Println(string(body))42}43import (44func main() {45 client := &http.Client{}46 if err != nil {47 fmt.Println(err)48 }49 req.Header.Set("Content-Type", "application/json")50 resp, err := client.Do(req)51 if err != nil {52 fmt.Println(err)53 }54 defer resp.Body.Close()

Full Screen

Full Screen

WithServer

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 proxy, _ := toxiproxyClient.CreateProxy("my_proxy", "localhost:12345", "localhost:12346")4 proxy.WithServer("localhost:12347")5}6import (7func main() {8 proxy, _ := toxiproxyClient.CreateProxy("my_proxy", "localhost:12345", "localhost:12346")9 proxy.WithUpstream("localhost:12347")10}11import (12func main() {13 proxy, _ := toxiproxyClient.CreateProxy("my_proxy", "localhost:12345", "localhost:12346")14 proxy.WithDownstream("localhost:12347")15}16import (17func main() {18 proxy, _ := toxiproxyClient.CreateProxy("my_proxy", "localhost:12345", "localhost:12346")19 proxy.WithProxy("localhost:12347")20}21import (22func main() {23 proxy, _ := toxiproxyClient.CreateProxy("my_proxy", "localhost:12345", "localhost:12346")24 proxy.WithName("localhost:12347")25}26import (27func main() {28 proxy, _ := toxiproxyClient.CreateProxy("my_proxy", "localhost:12345", "localhost:12346")29 proxy.WithListen("localhost:12347")30}

Full Screen

Full Screen

WithServer

Using AI Code Generation

copy

Full Screen

1func WithServer(t *testing.T, f func(*Toxiproxy)) {2 proxy := NewToxiproxy()3 proxy.Start()4 defer proxy.Stop()5 f(proxy)6}

Full Screen

Full Screen

WithServer

Using AI Code Generation

copy

Full Screen

1func TestWithServer(t *testing.T) {2 toxiproxy := toxiproxy.NewToxiproxy("localhost", "8474")3 toxiproxy.WithServer("localhost", "6379")4 toxiproxy.Start()5}6func TestNewToxiproxy(t *testing.T) {7 toxiproxy := toxiproxy.NewToxiproxy("localhost", "8474")8 toxiproxy.Start()9 toxiproxy.WithServer("localhost", "6379")10}11func TestNewToxiproxy(t *testing.T) {12 toxiproxy := toxiproxy.NewToxiproxy("localhost", "8474")13 toxiproxy.WithServer("localhost", "6379")14 toxiproxy.Start()15}16func TestWithServer(t *testing.T) {17 toxiproxy := toxiproxy.NewToxiproxy("localhost", "8474")18 toxiproxy.Start()19 toxiproxy.WithServer("localhost", "6379")20}21func TestWithServer(t *testing.T) {22 toxiproxy := toxiproxy.NewToxiproxy("localhost", "8474")23 toxiproxy.Start()24 toxiproxy.WithServer("localhost", "6379")25}26func TestWithServer(t *testing.T) {27 toxiproxy := toxiproxy.NewToxiproxy("localhost", "8474")28 toxiproxy.Start()29 toxiproxy.WithServer("localhost", "6379")30}31func TestWithServer(t *testing.T) {32 toxiproxy := toxiproxy.NewToxiproxy("localhost", "8474")33 toxiproxy.Start()34 toxiproxy.WithServer("localhost", "6379")35}

Full Screen

Full Screen

WithServer

Using AI Code Generation

copy

Full Screen

1func TestWithServer(t *testing.T) {2 toxiproxy := toxiproxy_test.NewTestToxiproxy(t)3 defer toxiproxy.Stop()4 toxiproxy.WithServer(func() {5 })6}7func TestWithProxy(t *testing.T) {8 toxiproxy := toxiproxy_test.NewTestToxiproxy(t)9 defer toxiproxy.Stop()10 toxiproxy.WithProxy(func(proxy *toxiproxy_test.TestProxy) {11 })12}13func TestWithToxics(t *testing.T) {14 toxiproxy := toxiproxy_test.NewTestToxiproxy(t)15 defer toxiproxy.Stop()16 toxiproxy.WithToxics(func(proxy *toxiproxy_test.TestProxy) {17 })18}19func TestWithProxyAndToxics(t *testing.T) {20 toxiproxy := toxiproxy_test.NewTestToxiproxy(t)21 defer toxiproxy.Stop()22 toxiproxy.WithProxyAndToxics(func(proxy *toxiproxy_test.TestProxy) {23 })24}25func TestWithToxic(t *testing.T) {26 toxiproxy := toxiproxy_test.NewTestToxiproxy(t)27 defer toxiproxy.Stop()28 toxiproxy.WithProxyAndToxics(func(proxy *toxiproxy_test.TestProxy) {29 })30}31func TestWithToxic(t *testing.T) {32 toxiproxy := toxiproxy_test.NewTestToxiproxy(t)

Full Screen

Full Screen

WithServer

Using AI Code Generation

copy

Full Screen

1func main() {2 toxiproxy := toxiproxy_test.NewToxiproxy()3 toxiproxy.WithServer(func(port string) {4 fmt.Println("Toxiproxy is running on port", port)5 })6}7func main() {8 toxiproxy := toxiproxy_test.NewToxiproxy()9 toxiproxy.Start()10 fmt.Println("Toxiproxy is running on port", toxiproxy.Port())11}12func main() {13 toxiproxy := toxiproxy_test.NewToxiproxy()14 toxiproxy.Start()15 defer toxiproxy.Stop()16 fmt.Println("Toxiproxy is running on port", toxiproxy.Port())17}18func main() {19 toxiproxy := toxiproxy_test.NewToxiproxy()20 toxiproxy.Start()21 defer toxiproxy.Stop()22 fmt.Println("Toxiproxy is running on port", toxiproxy.Port())23 toxiproxy.Proxy("localhost:8080", "localhost:8081")24}25func main() {26 toxiproxy := toxiproxy_test.NewToxiproxy()27 toxiproxy.Start()28 defer toxiproxy.Stop()29 fmt.Println("Toxiproxy is running on port", toxiproxy.Port())30 toxiproxy.Proxy("localhost:8080", "localhost:8081")31 toxiproxy.Proxy("localhost:8082", "localhost:8083")32}33func main() {34 toxiproxy := toxiproxy_test.NewToxiproxy()35 toxiproxy.Start()36 defer toxiproxy.Stop()37 fmt.Println("Toxiproxy is running on port", toxiproxy.Port())38 toxiproxy.Proxy("localhost:8080", "localhost:8081")39 toxiproxy.Proxy("localhost:8082", "localhost:8083")40 toxiproxy.Proxy("localhost:8084", "localhost:8085")41}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful