summaryrefslogtreecommitdiff
path: root/test/framework/fixtures/expectFixture.go
blob: 92d2daed805c5587468d8f4f5223965ee9c462db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package fixtures

import (
	"bytes"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"regexp"
	"strings"
	"testing"

	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

// ExpectFixture is a wrapper for running expect tests
type ExpectFixture struct {
	testDir     string
	testDataDir string
	testDirTmp  bool
	t           *testing.T
	testFilter  string
	expectFiles map[string]string
}

func (ef *ExpectFixture) initialize(t *testing.T) (err error) {
	ef.t = t
	ef.testDir = os.Getenv("TESTDIR")
	if ef.testDir == "" {
		ef.testDir = filepath.Join(t.TempDir(), "expect")
		err = os.MkdirAll(ef.testDir, 0755)
		if err != nil {
			ef.t.Errorf("error creating test dir %s, with error %v", ef.testDir, err)
			return
		}
		ef.testDirTmp = true
	}
	ef.testDataDir = os.Getenv("TESTDATADIR")
	if ef.testDataDir == "" {
		ef.testDataDir = filepath.Join(getTestDir(), "testdata")
	}

	ef.testFilter = os.Getenv("TESTFILTER")
	if ef.testFilter == "" {
		ef.testFilter = ".*"
	}
	return
}

func (ef *ExpectFixture) getTestDir(testName string) (workingDir, algoDir string, err error) {
	testName = strings.Replace(testName, ".exp", "", -1)
	workingDir = filepath.Join(ef.testDir, testName)
	err = os.Mkdir(workingDir, 0755)
	if err != nil {
		ef.t.Errorf("error creating test dir %s, with error %v", workingDir, err)
		return
	}
	algoDir = filepath.Join(workingDir, "algod")
	err = os.Mkdir(algoDir, 0755)
	if err != nil {
		ef.t.Errorf("error creating algo dir %s, with error %v", algoDir, err)
		return
	}
	return
}

func (ef *ExpectFixture) removeTestDir(workingDir string) (err error) {
	err = os.RemoveAll(workingDir)
	if err != nil {
		ef.t.Errorf("error removing test dir %s, with error %v", workingDir, err)
		return
	}
	return
}

// MakeExpectTest creates an expect test fixture for the current directory
func MakeExpectTest(t *testing.T) *ExpectFixture {
	if skipExpectTests() {
		t.Skip("Expect tests disabled by environment variables.")
	}

	ef := &ExpectFixture{}
	ef.expectFiles = make(map[string]string) // map expect test to full file name.
	err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
		if strings.HasSuffix(info.Name(), "Test.exp") {
			ef.expectFiles[info.Name()] = path
		}
		return nil
	})
	require.NoError(SynchronizedTest(t), err)
	err = ef.initialize(t)
	require.NoError(SynchronizedTest(t), err)
	return ef
}

func skipExpectTests() bool {
	// Explicitly enabled.
	if strings.ToUpper(os.Getenv("RUN_EXPECT")) == "TRUE" {
		return false
	}
	if strings.ToUpper(os.Getenv("RUN_EXPECT")) == "FALSE" {
		return true
	}

	// If any of the CI systems didn't set RUN_EXPECT, disable them.
	if strings.ToUpper(os.Getenv("CI")) == "TRUE" {
		return true
	}
	if strings.ToUpper(os.Getenv("CIRCLECI")) == "TRUE" {
		return true
	}
	if strings.ToUpper(os.Getenv("TRAVIS")) == "TRUE" {
		return true
	}
	if strings.ToUpper(os.Getenv("JENKINS_URL")) != "" {
		return true
	}

	// Implicitly enable for devs running tests.
	return false
}

// Run Process all expect script files with suffix Test.exp within the current directory
func (ef *ExpectFixture) Run() {
	disabledTest := map[string]string{
		"pingpongTest.exp":                    "broken",
		"listExpiredParticipationKeyTest.exp": "flaky",
	}
	for testName := range ef.expectFiles {
		if match, _ := regexp.MatchString(ef.testFilter, testName); match {
			ef.t.Run(testName, func(t *testing.T) {
				if reason, ok := disabledTest[testName]; ok {
					t.Skipf("Skipping %s test: %s", testName, reason)
				}
				partitiontest.PartitionTest(t) // Check if this expect test should by run, may SKIP

				syncTest := SynchronizedTest(t)
				workingDir, algoDir, err := ef.getTestDir(testName)
				require.NoError(SynchronizedTest(t), err)
				syncTest.Logf("algoDir: %s\ntestDataDir:%s\n", algoDir, ef.testDataDir)
				cmd := exec.Command("expect", testName, algoDir, ef.testDataDir)
				var outBuf bytes.Buffer
				cmd.Stdout = &outBuf

				// Set stderr to be a file descriptor. In other way Go's exec.Cmd::writerDescriptor
				// attaches a goroutine reading stderr that blocks on io.Copy from stderr.
				// Cmd::CombinedOutput sets stderr to stdout and also blocks.
				// Cmd::Start + Cmd::Wait with manual pipes redirection etc also blocks.
				// Wrapping 'expect' with 'expect "$@" 2>&1' also blocks on stdout reading.
				// Cmd::Output with Cmd::Stderr == nil works but stderr get lost.
				// Using os.File as stderr does not trigger goroutine creation, instead exec.Cmd relies on os.File implementation.
				errFile, err := os.OpenFile(path.Join(workingDir, "stderr.txt"), os.O_CREATE|os.O_RDWR, 0)
				if err != nil {
					syncTest.Logf("failed opening stderr temp file: %s\n", err.Error())
					syncTest.Fail()
				}
				defer errFile.Close() // Close might error but we Sync it before leaving the scope
				cmd.Stderr = errFile

				err = cmd.Run()
				if err != nil {
					var stderr string
					var ferr error
					if ferr = errFile.Sync(); ferr == nil {
						if _, ferr = errFile.Seek(0, 0); ferr == nil {
							if info, ferr := errFile.Stat(); ferr == nil {
								errData := make([]byte, info.Size())
								if _, ferr = errFile.Read(errData); ferr == nil {
									stderr = string(errData)
								}
							}
						}
					}
					if ferr != nil {
						stderr = ferr.Error()
					}
					syncTest.Logf("err running '%s': %s\nstdout: %s\nstderr: %s\n", testName, err, string(outBuf.Bytes()), stderr)
					syncTest.Fail()
				} else {
					// t.Logf("stdout: %s", string(outBuf.Bytes()))
					ef.removeTestDir(workingDir)
				}
			})
		}
	}
}