github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/e2e/ctl_v3_watch_no_cov_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  //go:build !cov
    16  // +build !cov
    17  
    18  package e2e
    19  
    20  import (
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/lfch/etcd-io/tests/v3/framework/e2e"
    25  )
    26  
    27  func TestCtlV3Watch(t *testing.T)          { testCtl(t, watchTest) }
    28  func TestCtlV3WatchNoTLS(t *testing.T)     { testCtl(t, watchTest, withCfg(*e2e.NewConfigNoTLS())) }
    29  func TestCtlV3WatchClientTLS(t *testing.T) { testCtl(t, watchTest, withCfg(*e2e.NewConfigClientTLS())) }
    30  func TestCtlV3WatchPeerTLS(t *testing.T)   { testCtl(t, watchTest, withCfg(*e2e.NewConfigPeerTLS())) }
    31  func TestCtlV3WatchTimeout(t *testing.T)   { testCtl(t, watchTest, withDialTimeout(0)) }
    32  
    33  func TestCtlV3WatchInteractive(t *testing.T) {
    34  	testCtl(t, watchTest, withInteractive())
    35  }
    36  func TestCtlV3WatchInteractiveNoTLS(t *testing.T) {
    37  	testCtl(t, watchTest, withInteractive(), withCfg(*e2e.NewConfigNoTLS()))
    38  }
    39  func TestCtlV3WatchInteractiveClientTLS(t *testing.T) {
    40  	testCtl(t, watchTest, withInteractive(), withCfg(*e2e.NewConfigClientTLS()))
    41  }
    42  func TestCtlV3WatchInteractivePeerTLS(t *testing.T) {
    43  	testCtl(t, watchTest, withInteractive(), withCfg(*e2e.NewConfigPeerTLS()))
    44  }
    45  
    46  func watchTest(cx ctlCtx) {
    47  	tests := []struct {
    48  		puts     []kv
    49  		envKey   string
    50  		envRange string
    51  		args     []string
    52  
    53  		wkv []kvExec
    54  	}{
    55  		{ // watch 1 key with env
    56  			puts:   []kv{{"sample", "value"}},
    57  			envKey: "sample",
    58  			args:   []string{"--rev", "1"},
    59  			wkv:    []kvExec{{key: "sample", val: "value"}},
    60  		},
    61  		{ // watch 1 key with ${ETCD_WATCH_VALUE}
    62  			puts: []kv{{"sample", "value"}},
    63  			args: []string{"sample", "--rev", "1", "--", "env"},
    64  			wkv:  []kvExec{{key: "sample", val: "value", execOutput: `ETCD_WATCH_VALUE="value"`}},
    65  		},
    66  		{ // watch 1 key with "echo watch event received", with env
    67  			puts:   []kv{{"sample", "value"}},
    68  			envKey: "sample",
    69  			args:   []string{"--rev", "1", "--", "echo", "watch event received"},
    70  			wkv:    []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
    71  		},
    72  		{ // watch 1 key with "echo watch event received"
    73  			puts: []kv{{"sample", "value"}},
    74  			args: []string{"--rev", "1", "sample", "--", "echo", "watch event received"},
    75  			wkv:  []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
    76  		},
    77  		{ // watch 1 key with "echo \"Hello World!\""
    78  			puts: []kv{{"sample", "value"}},
    79  			args: []string{"--rev", "1", "sample", "--", "echo", "\"Hello World!\""},
    80  			wkv:  []kvExec{{key: "sample", val: "value", execOutput: "Hello World!"}},
    81  		},
    82  		{ // watch 1 key with "echo watch event received"
    83  			puts: []kv{{"sample", "value"}},
    84  			args: []string{"sample", "samplx", "--rev", "1", "--", "echo", "watch event received"},
    85  			wkv:  []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
    86  		},
    87  		{ // watch 1 key with "echo watch event received"
    88  			puts:     []kv{{"sample", "value"}},
    89  			envKey:   "sample",
    90  			envRange: "samplx",
    91  			args:     []string{"--rev", "1", "--", "echo", "watch event received"},
    92  			wkv:      []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
    93  		},
    94  		{ // watch 1 key with "echo watch event received"
    95  			puts: []kv{{"sample", "value"}},
    96  			args: []string{"sample", "--rev", "1", "samplx", "--", "echo", "watch event received"},
    97  			wkv:  []kvExec{{key: "sample", val: "value", execOutput: "watch event received"}},
    98  		},
    99  		{ // watch 3 keys by prefix, with env
   100  			puts:   []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
   101  			envKey: "key",
   102  			args:   []string{"--rev", "1", "--prefix"},
   103  			wkv:    []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}, {key: "key3", val: "val3"}},
   104  		},
   105  		{ // watch 3 keys by range, with env
   106  			puts:     []kv{{"key1", "val1"}, {"key3", "val3"}, {"key2", "val2"}},
   107  			envKey:   "key",
   108  			envRange: "key3",
   109  			args:     []string{"--rev", "1"},
   110  			wkv:      []kvExec{{key: "key1", val: "val1"}, {key: "key2", val: "val2"}},
   111  		},
   112  	}
   113  
   114  	for i, tt := range tests {
   115  		donec := make(chan struct{})
   116  		go func(i int, puts []kv) {
   117  			for j := range puts {
   118  				if err := ctlV3Put(cx, puts[j].key, puts[j].val, ""); err != nil {
   119  					cx.t.Errorf("watchTest #%d-%d: ctlV3Put error (%v)", i, j, err)
   120  				}
   121  			}
   122  			close(donec)
   123  		}(i, tt.puts)
   124  
   125  		unsetEnv := func() {}
   126  		if tt.envKey != "" || tt.envRange != "" {
   127  			if tt.envKey != "" {
   128  				os.Setenv("ETCDCTL_WATCH_KEY", tt.envKey)
   129  				unsetEnv = func() { os.Unsetenv("ETCDCTL_WATCH_KEY") }
   130  			}
   131  			if tt.envRange != "" {
   132  				os.Setenv("ETCDCTL_WATCH_RANGE_END", tt.envRange)
   133  				unsetEnv = func() { os.Unsetenv("ETCDCTL_WATCH_RANGE_END") }
   134  			}
   135  			if tt.envKey != "" && tt.envRange != "" {
   136  				unsetEnv = func() {
   137  					os.Unsetenv("ETCDCTL_WATCH_KEY")
   138  					os.Unsetenv("ETCDCTL_WATCH_RANGE_END")
   139  				}
   140  			}
   141  		}
   142  		if err := ctlV3Watch(cx, tt.args, tt.wkv...); err != nil {
   143  			if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
   144  				cx.t.Errorf("watchTest #%d: ctlV3Watch error (%v)", i, err)
   145  			}
   146  		}
   147  		unsetEnv()
   148  		<-donec
   149  	}
   150  }