etcd/tests/e2e/ctl_v3_txn_test.go
Dan Mace 37c95c9fd4 tests: prevent cross-test contamination via shared state
The e2e tests can be flaky due to various tests mutating shared mutable
fixtures, causing non-deterministic behavior depending on the test set, order,
etc.

For example, `configTLS` is mutated in at least two tests in such a way that the
config is potentially invalidated for any subsequent test running in the same
process (e.g. by setting the `enableV2` field). This particular example caused
a substantial amount of confusion diagnosing the new test introduced for
https://github.com/etcd-io/etcd/pull/12370.

Independent tests should not share mutable state unless deliberately. This patch
refactors the e2e test config fixtures to safeguard against these problems by
replacing the package variables (which cannot easily be made immutable) with
functions that return new instances.
2020-10-14 13:58:00 -04:00

156 lines
4.1 KiB
Go

// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package e2e
import "testing"
func TestCtlV3TxnInteractiveSuccess(t *testing.T) {
testCtl(t, txnTestSuccess, withInteractive())
}
func TestCtlV3TxnInteractiveSuccessNoTLS(t *testing.T) {
testCtl(t, txnTestSuccess, withInteractive(), withCfg(*newConfigNoTLS()))
}
func TestCtlV3TxnInteractiveSuccessClientTLS(t *testing.T) {
testCtl(t, txnTestSuccess, withInteractive(), withCfg(*newConfigClientTLS()))
}
func TestCtlV3TxnInteractiveSuccessPeerTLS(t *testing.T) {
testCtl(t, txnTestSuccess, withInteractive(), withCfg(*newConfigPeerTLS()))
}
func TestCtlV3TxnInteractiveFail(t *testing.T) {
testCtl(t, txnTestFail, withInteractive())
}
func txnTestSuccess(cx ctlCtx) {
if err := ctlV3Put(cx, "key1", "value1", ""); err != nil {
cx.t.Fatalf("txnTestSuccess ctlV3Put error (%v)", err)
}
if err := ctlV3Put(cx, "key2", "value2", ""); err != nil {
cx.t.Fatalf("txnTestSuccess ctlV3Put error (%v)", err)
}
rqs := []txnRequests{
{
compare: []string{`value("key1") != "value2"`, `value("key2") != "value1"`},
ifSucess: []string{"get key1", "get key2"},
results: []string{"SUCCESS", "key1", "value1", "key2", "value2"},
},
{
compare: []string{`version("key1") = "1"`, `version("key2") = "1"`},
ifSucess: []string{"get key1", "get key2", `put "key \"with\" space" "value \x23"`},
ifFail: []string{`put key1 "fail"`, `put key2 "fail"`},
results: []string{"SUCCESS", "key1", "value1", "key2", "value2"},
},
{
compare: []string{`version("key \"with\" space") = "1"`},
ifSucess: []string{`get "key \"with\" space"`},
results: []string{"SUCCESS", `key "with" space`, "value \x23"},
},
}
for _, rq := range rqs {
if err := ctlV3Txn(cx, rq); err != nil {
cx.t.Fatal(err)
}
}
}
func txnTestFail(cx ctlCtx) {
if err := ctlV3Put(cx, "key1", "value1", ""); err != nil {
cx.t.Fatalf("txnTestSuccess ctlV3Put error (%v)", err)
}
rqs := []txnRequests{
{
compare: []string{`version("key") < "0"`},
ifSucess: []string{`put key "success"`},
ifFail: []string{`put key "fail"`},
results: []string{"FAILURE", "OK"},
},
{
compare: []string{`value("key1") != "value1"`},
ifSucess: []string{`put key1 "success"`},
ifFail: []string{`put key1 "fail"`},
results: []string{"FAILURE", "OK"},
},
}
for _, rq := range rqs {
if err := ctlV3Txn(cx, rq); err != nil {
cx.t.Fatal(err)
}
}
}
type txnRequests struct {
compare []string
ifSucess []string
ifFail []string
results []string
}
func ctlV3Txn(cx ctlCtx, rqs txnRequests) error {
// TODO: support non-interactive mode
cmdArgs := append(cx.PrefixArgs(), "txn")
if cx.interactive {
cmdArgs = append(cmdArgs, "--interactive")
}
proc, err := spawnCmd(cmdArgs)
if err != nil {
return err
}
_, err = proc.Expect("compares:")
if err != nil {
return err
}
for _, req := range rqs.compare {
if err = proc.Send(req + "\r"); err != nil {
return err
}
}
if err = proc.Send("\r"); err != nil {
return err
}
_, err = proc.Expect("success requests (get, put, del):")
if err != nil {
return err
}
for _, req := range rqs.ifSucess {
if err = proc.Send(req + "\r"); err != nil {
return err
}
}
if err = proc.Send("\r"); err != nil {
return err
}
_, err = proc.Expect("failure requests (get, put, del):")
if err != nil {
return err
}
for _, req := range rqs.ifFail {
if err = proc.Send(req + "\r"); err != nil {
return err
}
}
if err = proc.Send("\r"); err != nil {
return err
}
for _, line := range rqs.results {
_, err = proc.Expect(line)
if err != nil {
return err
}
}
return proc.Close()
}