go.etcd.io/etcd@v3.3.27+incompatible/tests/e2e/ctl_v3_watch_test.go (about) 1 // Copyright 2018 The etcd 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 e2e 16 17 import ( 18 "fmt" 19 "strings" 20 ) 21 22 type kvExec struct { 23 key, val string 24 execOutput string 25 } 26 27 func setupWatchArgs(cx ctlCtx, args []string) []string { 28 cmdArgs := append(cx.PrefixArgs(), "watch") 29 if cx.interactive { 30 cmdArgs = append(cmdArgs, "--interactive") 31 } else { 32 cmdArgs = append(cmdArgs, args...) 33 } 34 35 return cmdArgs 36 } 37 38 func ctlV3Watch(cx ctlCtx, args []string, kvs ...kvExec) error { 39 cmdArgs := setupWatchArgs(cx, args) 40 41 proc, err := spawnCmd(cmdArgs) 42 if err != nil { 43 return err 44 } 45 46 if cx.interactive { 47 wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r" 48 if err = proc.Send(wl); err != nil { 49 return err 50 } 51 } 52 53 for _, elem := range kvs { 54 if _, err = proc.Expect(elem.key); err != nil { 55 return err 56 } 57 if _, err = proc.Expect(elem.val); err != nil { 58 return err 59 } 60 if elem.execOutput != "" { 61 if _, err = proc.Expect(elem.execOutput); err != nil { 62 return err 63 } 64 } 65 } 66 return proc.Stop() 67 } 68 69 func ctlV3WatchFailPerm(cx ctlCtx, args []string) error { 70 cmdArgs := setupWatchArgs(cx, args) 71 72 proc, err := spawnCmd(cmdArgs) 73 if err != nil { 74 return err 75 } 76 77 if cx.interactive { 78 wl := strings.Join(append([]string{"watch"}, args...), " ") + "\r" 79 if err = proc.Send(wl); err != nil { 80 return err 81 } 82 } 83 84 // TODO(mitake): after printing accurate error message that includes 85 // "permission denied", the above string argument of proc.Expect() 86 // should be updated. 87 _, err = proc.Expect("watch is canceled by the server") 88 if err != nil { 89 return err 90 } 91 return proc.Close() 92 } 93 94 func ctlV3Put(cx ctlCtx, key, value, leaseID string, flags ...string) error { 95 skipValue := false 96 skipLease := false 97 for _, f := range flags { 98 if f == "--ignore-value" { 99 skipValue = true 100 } 101 if f == "--ignore-lease" { 102 skipLease = true 103 } 104 } 105 cmdArgs := append(cx.PrefixArgs(), "put", key) 106 if !skipValue { 107 cmdArgs = append(cmdArgs, value) 108 } 109 if leaseID != "" && !skipLease { 110 cmdArgs = append(cmdArgs, "--lease", leaseID) 111 } 112 if len(flags) != 0 { 113 cmdArgs = append(cmdArgs, flags...) 114 } 115 return spawnWithExpect(cmdArgs, "OK") 116 } 117 118 type kv struct { 119 key, val string 120 } 121 122 func ctlV3Get(cx ctlCtx, args []string, kvs ...kv) error { 123 cmdArgs := append(cx.PrefixArgs(), "get") 124 cmdArgs = append(cmdArgs, args...) 125 if !cx.quorum { 126 cmdArgs = append(cmdArgs, "--consistency", "s") 127 } 128 var lines []string 129 for _, elem := range kvs { 130 lines = append(lines, elem.key, elem.val) 131 } 132 return spawnWithExpects(cmdArgs, lines...) 133 } 134 135 // ctlV3GetWithErr runs "get" command expecting no output but error 136 func ctlV3GetWithErr(cx ctlCtx, args []string, errs []string) error { 137 cmdArgs := append(cx.PrefixArgs(), "get") 138 cmdArgs = append(cmdArgs, args...) 139 if !cx.quorum { 140 cmdArgs = append(cmdArgs, "--consistency", "s") 141 } 142 return spawnWithExpects(cmdArgs, errs...) 143 } 144 145 func ctlV3Del(cx ctlCtx, args []string, num int) error { 146 cmdArgs := append(cx.PrefixArgs(), "del") 147 cmdArgs = append(cmdArgs, args...) 148 return spawnWithExpects(cmdArgs, fmt.Sprintf("%d", num)) 149 }