Best Toxiproxy code snippet using toxiproxy.Disable
api_db_fault_tolerance_integration_test.go
Source:api_db_fault_tolerance_integration_test.go
...58func (p dbProxy) Enable() {59 p.t.Helper()60 require.NoError(p.t, p.proxy.Enable())61}62func (p dbProxy) Disable() {63 p.t.Helper()64 require.NoError(p.t, p.proxy.Disable())65}66func (p dbProxy) Delete() {67 p.t.Helper()68 require.NoError(p.t, p.proxy.Delete())69}70func (p dbProxy) AddToxic(typeName string, attrs toxiproxy.Attributes) *toxiproxy.Toxic {71 p.t.Helper()72 toxic, err := p.proxy.AddToxic("", typeName, "", 1, attrs)73 require.NoError(p.t, err)74 return toxic75}76func (p dbProxy) RemoveToxic(toxic *toxiproxy.Toxic) {77 p.t.Helper()78 require.NoError(p.t, p.proxy.RemoveToxic(toxic.Name))79}80func newDBProxy(t *testing.T) *dbProxy {81 t.Helper()82 dsn, err := testutil.NewDSNFromEnv()83 require.NoError(t, err)84 p, err := toxiClient.CreateProxy("db", "", dsn.Address())85 require.NoError(t, err)86 return &dbProxy{p, t}87}88func TestDBFaultTolerance_ConnectionRefused_Catalog(t *testing.T) {89 dbProxy := newDBProxy(t)90 defer dbProxy.Delete()91 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))92 defer env.Shutdown()93 u, err := env.builder.BuildCatalogURL()94 require.NoError(t, err)95 // query API with proxy disabled, should fail96 dbProxy.Disable()97 assertGetResponse(t, u, http.StatusServiceUnavailable)98 // query API with proxy re-enabled, should succeed99 dbProxy.Enable()100 assertGetResponse(t, u, http.StatusOK)101}102func TestDBFaultTolerance_ConnectionRefused_TagList(t *testing.T) {103 dbProxy := newDBProxy(t)104 defer dbProxy.Delete()105 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))106 defer env.Shutdown()107 repoName := "foo"108 name, err := reference.WithName(repoName)109 require.NoError(t, err)110 u, err := env.builder.BuildTagsURL(name)111 require.NoError(t, err)112 // query API with proxy disabled, should fail113 dbProxy.Disable()114 assertGetResponse(t, u, http.StatusServiceUnavailable)115 // query API with proxy re-enabled, should succeed116 dbProxy.Enable()117 assertGetResponse(t, u, http.StatusNotFound)118}119func TestDBFaultTolerance_ConnectionRefused_TagDelete(t *testing.T) {120 dbProxy := newDBProxy(t)121 defer dbProxy.Delete()122 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))123 defer env.Shutdown()124 repoName := "foo"125 tagName := "latest"126 // query API with proxy disabled, should fail127 // create the repo, otherwise the request will halt on the filesystem search, which precedes the DB search128 createRepository(t, env, repoName, tagName)129 dbProxy.Disable()130 assertTagDeleteResponse(t, env, repoName, tagName, http.StatusServiceUnavailable)131 // query API with proxy re-enabled, should succeed132 dbProxy.Enable()133 createRepository(t, env, repoName, tagName)134 assertTagDeleteResponse(t, env, repoName, tagName, http.StatusAccepted)135}136func TestDBFaultTolerance_ConnectionRefused_BlobGet(t *testing.T) {137 dbProxy := newDBProxy(t)138 defer dbProxy.Delete()139 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))140 defer env.Shutdown()141 // we can use a non-existing repo and blob, as reads are executed against the DB first142 repoName := "foo"143 dgst := digest.FromString(repoName)144 // query API with proxy disabled, should fail145 dbProxy.Disable()146 assertBlobGetResponse(t, env, repoName, dgst, http.StatusServiceUnavailable)147 // query API with proxy re-enabled, should succeed148 dbProxy.Enable()149 assertBlobGetResponse(t, env, repoName, dgst, http.StatusNotFound)150}151func TestDBFaultTolerance_ConnectionRefused_BlobHead(t *testing.T) {152 dbProxy := newDBProxy(t)153 defer dbProxy.Delete()154 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))155 defer env.Shutdown()156 // we can use a non-existing repo and blob, as reads are executed against the DB first157 repoName := "foo"158 dgst := digest.FromString(repoName)159 // query API with proxy disabled, should fail160 dbProxy.Disable()161 assertBlobHeadResponse(t, env, repoName, dgst, http.StatusServiceUnavailable)162 // query API with proxy re-enabled, should succeed163 dbProxy.Enable()164 assertBlobHeadResponse(t, env, repoName, dgst, http.StatusNotFound)165}166func TestDBFaultTolerance_ConnectionRefused_BlobDelete(t *testing.T) {167 dbProxy := newDBProxy(t)168 defer dbProxy.Delete()169 env := newTestEnv(t, withDelete, withDBHostAndPort(dbProxy.HostAndPort()))170 defer env.Shutdown()171 // query API with proxy disabled, should fail172 // create the repo and blob, otherwise the request will halt on the filesystem search, which precedes the DB search173 args, _ := createRepoWithBlob(t, env)174 dbProxy.Disable()175 assertBlobDeleteResponse(t, env, args.imageName.String(), args.layerDigest, http.StatusServiceUnavailable)176 // query API with proxy re-enabled, should succeed177 dbProxy.Enable()178 args, _ = createRepoWithBlob(t, env)179 assertBlobDeleteResponse(t, env, args.imageName.String(), args.layerDigest, http.StatusAccepted)180}181func TestDBFaultTolerance_ConnectionRefused_BlobPut(t *testing.T) {182 dbProxy := newDBProxy(t)183 defer dbProxy.Delete()184 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))185 defer env.Shutdown()186 // query API with proxy disabled, should fail187 dbProxy.Disable()188 args := makeBlobArgs(t)189 assertBlobPutResponse(t, env, args.imageName.String(), args.layerDigest, args.layerFile, http.StatusServiceUnavailable)190 // query API with proxy re-enabled, should succeed191 dbProxy.Enable()192 args = makeBlobArgs(t)193 assertBlobPutResponse(t, env, args.imageName.String(), args.layerDigest, args.layerFile, http.StatusCreated)194}195func TestDBFaultTolerance_ConnectionRefused_BlobPostMount(t *testing.T) {196 dbProxy := newDBProxy(t)197 defer dbProxy.Delete()198 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))199 defer env.Shutdown()200 args, _ := createRepoWithBlob(t, env)201 destRepo := "foo"202 // query API with proxy disabled, should fail203 dbProxy.Disable()204 assertBlobPostMountResponse(t, env, args.imageName.String(), destRepo, args.layerDigest, http.StatusServiceUnavailable)205 // query API with proxy re-enabled, should succeed206 dbProxy.Enable()207 assertBlobPostMountResponse(t, env, args.imageName.String(), destRepo, args.layerDigest, http.StatusCreated)208}209func TestDBFaultTolerance_ConnectionRefused_ManifestGetByDigest(t *testing.T) {210 dbProxy := newDBProxy(t)211 defer dbProxy.Delete()212 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))213 defer env.Shutdown()214 repoName := "foo"215 m := seedRandomSchema2Manifest(t, env, repoName, putByDigest)216 // query API with proxy disabled, should fail217 dbProxy.Disable()218 assertManifestGetByDigestResponse(t, env, repoName, m, http.StatusServiceUnavailable)219 // query API with proxy re-enabled, should succeed220 dbProxy.Enable()221 assertManifestGetByDigestResponse(t, env, repoName, m, http.StatusOK)222}223func TestDBFaultTolerance_ConnectionRefused_ManifestGetByTag(t *testing.T) {224 dbProxy := newDBProxy(t)225 defer dbProxy.Delete()226 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))227 defer env.Shutdown()228 repoName := "test/repo"229 tagName := "latest"230 seedRandomSchema2Manifest(t, env, repoName, putByTag(tagName))231 // query API with proxy disabled, should fail232 dbProxy.Disable()233 assertManifestGetByTagResponse(t, env, repoName, tagName, http.StatusServiceUnavailable)234 // query API with proxy re-enabled, should succeed235 dbProxy.Enable()236 assertManifestGetByTagResponse(t, env, repoName, tagName, http.StatusOK)237}238func TestDBFaultTolerance_ConnectionRefused_ManifestHeadByDigest(t *testing.T) {239 dbProxy := newDBProxy(t)240 defer dbProxy.Delete()241 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))242 defer env.Shutdown()243 repoName := "foo"244 m := seedRandomSchema2Manifest(t, env, repoName, putByDigest)245 // query API with proxy disabled, should fail246 dbProxy.Disable()247 assertManifestHeadByDigestResponse(t, env, repoName, m, http.StatusServiceUnavailable)248 // query API with proxy re-enabled, should succeed249 dbProxy.Enable()250 assertManifestHeadByDigestResponse(t, env, repoName, m, http.StatusOK)251}252func TestDBFaultTolerance_ConnectionRefused_ManifestHeadByTag(t *testing.T) {253 dbProxy := newDBProxy(t)254 defer dbProxy.Delete()255 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))256 defer env.Shutdown()257 repoName := "test/repo"258 tagName := "latest"259 seedRandomSchema2Manifest(t, env, repoName, putByTag(tagName))260 // query API with proxy disabled, should fail261 dbProxy.Disable()262 assertManifestHeadByTagResponse(t, env, repoName, tagName, http.StatusServiceUnavailable)263 // query API with proxy re-enabled, should succeed264 dbProxy.Enable()265 assertManifestHeadByTagResponse(t, env, repoName, tagName, http.StatusOK)266}267func TestDBFaultTolerance_ConnectionRefused_ManifestPutByDigest(t *testing.T) {268 dbProxy := newDBProxy(t)269 defer dbProxy.Delete()270 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))271 defer env.Shutdown()272 repoName := "foo"273 m := seedRandomSchema2Manifest(t, env, repoName, putByDigest)274 // query API with proxy disabled, should fail275 dbProxy.Disable()276 assertManifestPutByDigestResponse(t, env, repoName, m, m.MediaType, http.StatusServiceUnavailable)277 // query API with proxy re-enabled, should succeed278 dbProxy.Enable()279 assertManifestPutByDigestResponse(t, env, repoName, m, m.MediaType, http.StatusCreated)280}281func TestDBFaultTolerance_ConnectionRefused_ManifestPutByTag(t *testing.T) {282 dbProxy := newDBProxy(t)283 defer dbProxy.Delete()284 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()))285 defer env.Shutdown()286 repoName := "foo"287 tagName := "latest"288 m := seedRandomSchema2Manifest(t, env, repoName, putByTag(tagName))289 // query API with proxy disabled, should fail290 dbProxy.Disable()291 assertManifestPutByTagResponse(t, env, repoName, m, m.MediaType, tagName, http.StatusServiceUnavailable)292 // query API with proxy re-enabled, should succeed293 dbProxy.Enable()294 assertManifestPutByTagResponse(t, env, repoName, m, m.MediaType, tagName, http.StatusCreated)295}296func TestDBFaultTolerance_ConnectionRefused_ManifestDelete(t *testing.T) {297 dbProxy := newDBProxy(t)298 defer dbProxy.Delete()299 env := newTestEnv(t, withDelete, withDBHostAndPort(dbProxy.HostAndPort()))300 defer env.Shutdown()301 repoName := "foo"302 m := seedRandomSchema2Manifest(t, env, repoName, putByDigest)303 // query API with proxy disabled, should fail304 dbProxy.Disable()305 assertManifestDeleteResponse(t, env, repoName, m, http.StatusServiceUnavailable)306 // query API with proxy re-enabled, should succeed307 dbProxy.Enable()308 m = seedRandomSchema2Manifest(t, env, repoName, putByDigest)309 assertManifestDeleteResponse(t, env, repoName, m, http.StatusAccepted)310}311func TestDBFaultTolerance_ConnectionTimeout_Catalog(t *testing.T) {312 dbProxy := newDBProxy(t)313 defer dbProxy.Delete()314 env := newTestEnv(t, withDBHostAndPort(dbProxy.HostAndPort()), withDBConnectTimeout(1*time.Second))315 defer env.Shutdown()316 u, err := env.builder.BuildCatalogURL()317 require.NoError(t, err)318 // query API with timeout, should fail...
client.go
Source:client.go
...7 "github.com/garyburd/redigo/redis"8)9func (S *Stub) RedisBackendDown(w http.ResponseWriter, r *http.Request) {10 // Test that redis is down11 S.RedisProxy.Disable()12 defer S.RedisProxy.Enable()13 _, err := redis.Dial("tcp", ":7379")14 if err != nil {15 S.RedisDownHits.Increment()16 fmt.Println("increm red down")17 }18}19func (S *Stub) RedisBackendUp(w http.ResponseWriter, r *http.Request) {20 // Test that redis is Up21 _, err := redis.Dial("tcp", ":7379")22 if err == nil {23 S.RedisUpHits.Increment()24 fmt.Println("increm red up")25 }26}27func (S *Stub) GitcheckUp(w http.ResponseWriter, r *http.Request) {28 // Test connection through proxy.Listen29 fmt.Println("gitup proxy listen", S.ToxiProxy.Listen)30 resp, err := http.Get("http://" + S.ToxiProxy.Listen)31 fmt.Println("response from gitup", resp)32 if err == nil {33 S.GitUpHits.Increment()34 } else {35 S.GitDownHits.Increment()36 }37}38func (S *Stub) GitcheckDown(w http.ResponseWriter, r *http.Request) {39 fmt.Println("Git checkdown proxy is ", S.ToxiProxy)40 S.ToxiProxy.Disable() //Disabling the proxy will simulate git down41 defer S.ToxiProxy.Enable()42 // Test connection through proxy.Listen43 resp, err := http.Get("http://" + S.ToxiProxy.Listen)44 fmt.Println("response from gitdown", resp)45 if err != nil || resp.StatusCode != 200 {46 S.GitDownHits.Increment()47 }48}49func (S *Stub) RedisBackendSlow(w http.ResponseWriter, r *http.Request) {50 S.RedisProxy.AddToxic("latency_downstream", "latency", "downstream", 1, toxiproxy.Attributes{51 "latency": 1000,52 }) //This adds latency to the Redis connect request53 defer S.RedisProxy.RemoveToxic("latency_downstream")54 // Test that redis is slow...
Disable
Using AI Code Generation
1import (2func main() {3 toxiproxyClient := toxiproxy.NewClient("localhost:8474")4 toxiproxyClient.Disable("redis")5}6import (7func main() {8 toxiproxyClient := toxiproxy.NewClient("localhost:8474")9 toxiproxyClient.Enable("redis")10}11import (12func main() {13 toxiproxyClient := toxiproxy.NewClient("localhost:8474")14 toxiproxyClient.Reset("redis")15}16import (17func main() {18 toxiproxyClient := toxiproxy.NewClient("localhost:8474")19 toxiproxyClient.Delete("redis")20}21import (22func main() {23 toxiproxyClient := toxiproxy.NewClient("localhost:8474")24 toxiproxyClient.GetProxy("redis")25}26import (27func main() {28 toxiproxyClient := toxiproxy.NewClient("localhost:8474")29 toxiproxyClient.GetAll()30}31import (32func main() {33 toxiproxyClient := toxiproxy.NewClient("localhost:8474")34 toxiproxyClient.Create("redis", "
Disable
Using AI Code Generation
1import (2func main() {3 proxyClient, err := client.NewClient("localhost:8474")4 if err != nil {5 fmt.Println("Error in creating client")6 }7 proxyClient.Disable("proxy1")8}9import (10func main() {11 proxyClient, err := client.NewClient("localhost:8474")12 if err != nil {13 fmt.Println("Error in creating client")14 }15 proxyClient.Enable("proxy1")16}17import (18func main() {19 proxyClient, err := client.NewClient("localhost:8474")20 if err != nil {21 fmt.Println("Error in creating client")22 }23 proxyClient.IsEnabled("proxy1")24}25import (26func main() {27 proxyClient, err := client.NewClient("localhost:8474")28 if err != nil {29 fmt.Println("Error in creating client")30 }31 proxyClient.Reset("proxy1")32}33import (34func main() {35 proxyClient, err := client.NewClient("localhost:8474")36 if err != nil {37 fmt.Println("Error in creating client")38 }39 proxy := client.Proxy{Name: "proxy1", Listen: "localhost:12345", Upstream: "localhost:54321"}40 proxyClient.Update("proxy1", proxy)41}42import (43func main() {44 proxyClient, err := client.NewClient("localhost:8474")45 if err != nil {
Disable
Using AI Code Generation
1import (2func main() {3 toxiproxy, err := client.NewClient("localhost:8474")4 if err != nil {5 panic(err)6 }7 toxiproxy.Disable("my_proxy")8 fmt.Println("Toxiproxy disabled")9}10import (11func main() {12 toxiproxy, err := client.NewClient("localhost:8474")13 if err != nil {14 panic(err)15 }16 toxiproxy.Enable("my_proxy")17 fmt.Println("Toxiproxy enabled")18}19import (20func main() {21 toxiproxy, err := client.NewClient("localhost:8474")22 if err != nil {23 panic(err)24 }25 toxiproxy.Reset("my_proxy")26 fmt.Println("Toxiproxy reset")27}28import (29func main() {30 toxiproxy, err := client.NewClient("localhost:8474")31 if err != nil {32 panic(err)33 }34 toxiproxy.Delete("my_proxy")35 fmt.Println("Toxiproxy deleted")36}37import (38func main() {39 toxiproxy, err := client.NewClient("localhost:8474")40 if err != nil {41 panic(err)42 }43 proxy, err := toxiproxy.CreateProxy(&client.Proxy{Name: "my_proxy", Listen: "localhost:12345", Upstream: "localhost:54321"})44 if err != nil {45 panic(err)46 }47 fmt.Printf("Proxy created: %s", proxy.Name)48}49import (
Disable
Using AI Code Generation
1import (2func main() {3 if err != nil {4 panic(err)5 }6 toxiproxyClient.Disable("proxy1")7}8import (9func main() {10 if err != nil {11 panic(err)12 }13 toxiproxyClient.Enable("proxy1")14}15import (16func main() {17 if err != nil {18 panic(err)19 }20 toxiproxyClient.GetProxy("proxy1")21}22import (23func main() {24 if err != nil {25 panic(err)26 }27 toxiproxyClient.GetProxies()28}29import (30func main() {31 if err != nil {32 panic(err)33 }34 toxiproxyClient.RemoveProxy("proxy1")35}
Disable
Using AI Code Generation
1import (2func main() {3 client := toxiproxy.NewClient("localhost:8474")4 proxy, err := client.Disable("proxy1")5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(proxy)9}10import (11func main() {12 client := toxiproxy.NewClient("localhost:8474")13 proxy, err := client.Enable("proxy1")14 if err != nil {15 fmt.Println(err)16 }17 fmt.Println(proxy)18}19import (20func main() {21 client := toxiproxy.NewClient("localhost:8474")22 proxy := toxiproxy.Proxy{23 }24 updatedProxy, err := client.Update(proxy)25 if err != nil {26 fmt.Println(err)27 }28 fmt.Println(updatedProxy)29}30import (31func main() {32 client := toxiproxy.NewClient("localhost:8474")33 err := client.Delete("proxy1")34 if err != nil {35 fmt.Println(err)36 }37}38import (39func main() {40 client := toxiproxy.NewClient("localhost:8474")41 err := client.Reset("proxy1")42 if err != nil {43 fmt.Println(err)44 }45}46import (
Disable
Using AI Code Generation
1import (2func main() {3 toxiproxyClient.Disable("redis")4}5import (6func main() {7 toxiproxyClient.Enable("redis")8}9import (10func main() {11 toxiproxyClient.List()12}
Disable
Using AI Code Generation
1import (2func main() {3 proxy, err := client.NewProxy("localhost:8474", "localhost:6379", "redis")4 if err != nil {5 fmt.Println("Error in creating proxy: ", err)6 }7 proxy.Disable()8}
Disable
Using AI Code Generation
1import (2func main() {3 proxy := toxiproxy.NewClient("localhost:8474")4 proxy.Disable("proxy_name")5}6import (7func main() {8 proxy := toxiproxy.NewClient("localhost:8474")9 proxy.Enable("proxy_name")10}11import (12func main() {13 proxy := toxiproxy.NewClient("localhost:8474")14 proxy.Reset("proxy_name")15}16import (17func main() {18 proxy := toxiproxy.NewClient("localhost:8474")19 proxy.Remove("proxy_name")20}21import (22func main() {23 proxy := toxiproxy.NewClient("localhost:8474")24 proxy.Create(&toxiproxy.Proxy{25 Toxics: []toxiproxy.Toxic{},26 })27}28import (29func main() {30 proxy := toxiproxy.NewClient("localhost:8474")31 proxy.Update(&toxiproxy.Proxy{32 Toxics: []toxiproxy.Toxic{},33 })34}
Disable
Using AI Code Generation
1import (2func main() {3 proxy := toxiproxy.NewProxy()4 proxy.Start()5 proxy.Disable()6 proxy.Stop()7}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!