github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/framework/e2e/util.go (about) 1 // Copyright 2017 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 "context" 19 "encoding/json" 20 "fmt" 21 "math/rand" 22 "os" 23 "strings" 24 "testing" 25 "time" 26 27 "github.com/lfch/etcd-io/client/pkg/v3/testutil" 28 "github.com/lfch/etcd-io/pkg/v3/expect" 29 ) 30 31 func WaitReadyExpectProc(ctx context.Context, exproc *expect.ExpectProcess, readyStrs []string) error { 32 matchSet := func(l string) bool { 33 for _, s := range readyStrs { 34 if strings.Contains(l, s) { 35 return true 36 } 37 } 38 return false 39 } 40 _, err := exproc.ExpectFunc(ctx, matchSet) 41 return err 42 } 43 44 func SpawnWithExpect(args []string, expected string) error { 45 return SpawnWithExpects(args, nil, []string{expected}...) 46 } 47 48 func SpawnWithExpectWithEnv(args []string, envVars map[string]string, expected string) error { 49 return SpawnWithExpects(args, envVars, []string{expected}...) 50 } 51 52 func SpawnWithExpects(args []string, envVars map[string]string, xs ...string) error { 53 _, err := SpawnWithExpectLines(context.TODO(), args, envVars, xs...) 54 return err 55 } 56 57 func SpawnWithExpectLines(ctx context.Context, args []string, envVars map[string]string, xs ...string) ([]string, error) { 58 proc, err := SpawnCmd(args, envVars) 59 if err != nil { 60 return nil, err 61 } 62 defer proc.Close() 63 // process until either stdout or stderr contains 64 // the expected string 65 var ( 66 lines []string 67 ) 68 for _, txt := range xs { 69 l, lerr := proc.ExpectWithContext(ctx, txt) 70 if lerr != nil { 71 proc.Close() 72 return nil, fmt.Errorf("%v %v (expected %q, got %q). Try EXPECT_DEBUG=TRUE", args, lerr, txt, lines) 73 } 74 lines = append(lines, l) 75 } 76 perr := proc.Close() 77 l := proc.LineCount() 78 if len(xs) == 0 && l != noOutputLineCount { // expect no output 79 return nil, fmt.Errorf("unexpected output from %v (got lines %q, line count %d) %v. Try EXPECT_DEBUG=TRUE", args, lines, l, l != noOutputLineCount) 80 } 81 return lines, perr 82 } 83 84 func RandomLeaseID() int64 { 85 return rand.New(rand.NewSource(time.Now().UnixNano())).Int63() 86 } 87 88 func DataMarshal(data interface{}) (d string, e error) { 89 m, err := json.Marshal(data) 90 if err != nil { 91 return "", err 92 } 93 return string(m), nil 94 } 95 96 func CloseWithTimeout(p *expect.ExpectProcess, d time.Duration) error { 97 errc := make(chan error, 1) 98 go func() { errc <- p.Close() }() 99 select { 100 case err := <-errc: 101 return err 102 case <-time.After(d): 103 p.Stop() 104 // retry close after stopping to collect SIGQUIT data, if any 105 CloseWithTimeout(p, time.Second) 106 } 107 return fmt.Errorf("took longer than %v to Close process %+v", d, p) 108 } 109 110 func ToTLS(s string) string { 111 return strings.Replace(s, "http://", "https://", 1) 112 } 113 114 func SkipInShortMode(t testing.TB) { 115 testutil.SkipTestIfShortMode(t, "e2e tests are not running in --short mode") 116 } 117 118 func mergeEnvVariables(envVars map[string]string) []string { 119 var env []string 120 // Environment variables are passed as parameter have higher priority 121 // than os environment variables. 122 for k, v := range envVars { 123 env = append(env, fmt.Sprintf("%s=%s", k, v)) 124 } 125 126 // Now, we can set os environment variables not passed as parameter. 127 currVars := os.Environ() 128 for _, v := range currVars { 129 p := strings.Split(v, "=") 130 if _, ok := envVars[p[0]]; !ok { 131 env = append(env, fmt.Sprintf("%s=%s", p[0], p[1])) 132 } 133 } 134 135 return env 136 }