refactor: migrate e2e tests from efficientgo/e2e to capytest
This commit is contained in:
		| @@ -19,84 +19,13 @@ | ||||
| package e2etests_test | ||||
|  | ||||
| import ( | ||||
| 	"crypto/sha256" | ||||
| 	"encoding/hex" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/efficientgo/e2e" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
|  | ||||
| 	expect "github.com/tailscale/goexpect" | ||||
| 	"go.alt-gnome.ru/capytest" | ||||
| 	"go.alt-gnome.ru/capytest/providers/podman" | ||||
| ) | ||||
|  | ||||
| // DebugWriter оборачивает io.Writer и логирует все записываемые данные. | ||||
| type DebugWriter struct { | ||||
| 	prefix string | ||||
| 	writer io.Writer | ||||
| } | ||||
|  | ||||
| func (d *DebugWriter) Write(p []byte) (n int, err error) { | ||||
| 	log.Printf("%s: Writing data: %q", d.prefix, p) // Логируем данные | ||||
| 	return d.writer.Write(p) | ||||
| } | ||||
|  | ||||
| // DebugReader оборачивает io.Reader и логирует все читаемые данные. | ||||
| type DebugReader struct { | ||||
| 	prefix string | ||||
| 	reader io.Reader | ||||
| } | ||||
|  | ||||
| func (d *DebugReader) Read(p []byte) (n int, err error) { | ||||
| 	n, err = d.reader.Read(p) | ||||
| 	if n > 0 { | ||||
| 		log.Printf("%s: Read data: %q", d.prefix, p[:n]) // Логируем данные | ||||
| 	} | ||||
| 	return n, err | ||||
| } | ||||
|  | ||||
| func e2eSpawn(runnable e2e.Runnable, command e2e.Command, timeout time.Duration, opts ...expect.Option) (expect.Expecter, <-chan error, error, *io.PipeWriter) { | ||||
| 	resCh := make(chan error) | ||||
|  | ||||
| 	// Создаем pipe для stdin и stdout | ||||
| 	stdinReader, stdinWriter := io.Pipe() | ||||
| 	stdoutReader, stdoutWriter := io.Pipe() | ||||
|  | ||||
| 	debugStdinReader := &DebugReader{prefix: "STDIN", reader: stdinReader} | ||||
| 	debugStdoutWriter := &DebugWriter{prefix: "STDOUT", writer: stdoutWriter} | ||||
|  | ||||
| 	go func() { | ||||
| 		err := runnable.Exec( | ||||
| 			command, | ||||
| 			e2e.WithExecOptionStdout(debugStdoutWriter), | ||||
| 			e2e.WithExecOptionStdin(debugStdinReader), | ||||
| 			e2e.WithExecOptionStderr(debugStdoutWriter), | ||||
| 		) | ||||
|  | ||||
| 		resCh <- err | ||||
| 	}() | ||||
|  | ||||
| 	exp, chnErr, err := expect.SpawnGeneric(&expect.GenOptions{ | ||||
| 		In:  stdinWriter, | ||||
| 		Out: stdoutReader, | ||||
| 		Wait: func() error { | ||||
| 			return <-resCh | ||||
| 		}, | ||||
| 		Close: func() error { | ||||
| 			stdinWriter.Close() | ||||
| 			stdoutReader.Close() | ||||
| 			return nil | ||||
| 		}, | ||||
| 		Check: func() bool { return true }, | ||||
| 	}, timeout, expect.Verbose(true), expect.VerboseWriter(os.Stdout)) | ||||
|  | ||||
| 	return exp, chnErr, err, stdinWriter | ||||
| } | ||||
|  | ||||
| var ALL_SYSTEMS []string = []string{ | ||||
| 	"ubuntu-24.04", | ||||
| 	"alt-sisyphus", | ||||
| @@ -120,71 +49,20 @@ var COMMON_SYSTEMS []string = []string{ | ||||
| 	"ubuntu-24.04", | ||||
| } | ||||
|  | ||||
| func dockerMultipleRun(t *testing.T, name string, ids []string, f func(t *testing.T, runnable e2e.Runnable)) { | ||||
| 	t.Run(name, func(t *testing.T) { | ||||
| 		for _, id := range ids { | ||||
| 			t.Run(id, func(t *testing.T) { | ||||
| 				t.Parallel() | ||||
| 				dockerName := fmt.Sprintf("alr-test-%s-%s", name, id) | ||||
| 				hash := sha256.New() | ||||
| 				hash.Write([]byte(dockerName)) | ||||
| 				hashSum := hash.Sum(nil) | ||||
| 				hashString := hex.EncodeToString(hashSum) | ||||
| 				truncatedHash := hashString[:8] | ||||
| 				e, err := e2e.New(e2e.WithVerbose(), e2e.WithName(fmt.Sprintf("alr-%s", truncatedHash))) | ||||
| 				assert.NoError(t, err) | ||||
| 				t.Cleanup(e.Close) | ||||
| 				imageId := fmt.Sprintf("ghcr.io/maks1ms/alr-e2e-test-image-%s", id) | ||||
| 				runnable := e.Runnable(dockerName).Init( | ||||
| 					e2e.StartOptions{ | ||||
| 						Image: imageId, | ||||
| 						Volumes: []string{ | ||||
| 							"./alr:/tmp/alr", | ||||
| 						}, | ||||
| 						Privileged: true, | ||||
| 					}, | ||||
| 				) | ||||
| 				assert.NoError(t, e2e.StartAndWaitReady(runnable)) | ||||
| 				err = runnable.Exec(e2e.NewCommand("/bin/alr-test-setup", "alr-install")) | ||||
| 				if err != nil { | ||||
| 					panic(err) | ||||
| 				} | ||||
| 				err = runnable.Exec(e2e.NewCommand("/bin/alr-test-setup", "passwordless-sudo-setup")) | ||||
| 				if err != nil { | ||||
| 					panic(err) | ||||
| 				} | ||||
| 				f(t, runnable) | ||||
| 			}) | ||||
| 		} | ||||
| 	}) | ||||
| func execShouldNoError(t *testing.T, r capytest.Runner, cmd string, args ...string) { | ||||
| 	t.Helper() | ||||
| 	r.Command(cmd, args...).ExpectSuccess().Run(t) | ||||
| } | ||||
|  | ||||
| func execShouldNoError(t *testing.T, r e2e.Runnable, cmd string, args ...string) { | ||||
| 	assert.NoError(t, r.Exec(e2e.NewCommand(cmd, args...))) | ||||
| } | ||||
|  | ||||
| func execShouldError(t *testing.T, r e2e.Runnable, cmd string, args ...string) { | ||||
| 	assert.Error(t, r.Exec(e2e.NewCommand(cmd, args...))) | ||||
| } | ||||
|  | ||||
| func runTestCommands(t *testing.T, r e2e.Runnable, timeout time.Duration, expects []expect.Batcher) { | ||||
| 	exp, _, err, _ := e2eSpawn( | ||||
| 		r, | ||||
| 		e2e.NewCommand("/bin/bash"), 25*time.Second, | ||||
| 		expect.Verbose(true), | ||||
| 	) | ||||
| 	assert.NoError(t, err) | ||||
| 	_, err = exp.ExpectBatch( | ||||
| 		expects, | ||||
| 		timeout, | ||||
| 	) | ||||
| 	assert.NoError(t, err) | ||||
| func execShouldError(t *testing.T, r capytest.Runner, cmd string, args ...string) { | ||||
| 	t.Helper() | ||||
| 	r.Command(cmd, args...).ExpectFailure().Run(t) | ||||
| } | ||||
|  | ||||
| const REPO_NAME_FOR_E2E_TESTS = "alr-repo" | ||||
| const REPO_URL_FOR_E2E_TESTS = "https://gitea.plemya-x.ru/Plemya-x/repo-for-tests.git" | ||||
|  | ||||
| func defaultPrepare(t *testing.T, r e2e.Runnable) { | ||||
| func defaultPrepare(t *testing.T, r capytest.Runner) { | ||||
| 	execShouldNoError(t, r, | ||||
| 		"sudo", | ||||
| 		"alr", | ||||
| @@ -200,3 +78,19 @@ func defaultPrepare(t *testing.T, r e2e.Runnable) { | ||||
| 		"ref", | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func runMatrixSuite(t *testing.T, name string, images []string, test func(t *testing.T, r capytest.Runner)) { | ||||
| 	t.Helper() | ||||
| 	for _, image := range images { | ||||
| 		ts := capytest.NewTestSuite(t, podman.Provider( | ||||
| 			podman.WithImage(fmt.Sprintf("ghcr.io/maks1ms/alr-e2e-test-image-%s", image)), | ||||
| 			podman.WithVolumes("./alr:/tmp/alr"), | ||||
| 			podman.WithPrivileged(true), | ||||
| 		)) | ||||
| 		ts.BeforeEach(func(t *testing.T, r capytest.Runner) { | ||||
| 			execShouldNoError(t, r, "/bin/alr-test-setup", "alr-install") | ||||
| 			execShouldNoError(t, r, "/bin/alr-test-setup", "passwordless-sudo-setup") | ||||
| 		}) | ||||
| 		ts.Run(fmt.Sprintf("%s/%s", name, image), test) | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user