|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/spectralops/teller/e2e/register" |
| 13 | + _ "github.com/spectralops/teller/e2e/tests" |
| 14 | + "github.com/spectralops/teller/e2e/testutils" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + replaceToStaticPath = "DYNAMIC-FULL-PATH" |
| 20 | + replaceToShortStaticPath = "DYNAMIC-SHORT-PATH" |
| 21 | + removeBinaryPlaceholder = "<binary-path>" |
| 22 | + testsFolder = "tests" |
| 23 | +) |
| 24 | + |
| 25 | +func TestE2E(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + |
| 28 | + // validate given binary path |
| 29 | + binaryPath, err := getBinaryPath() |
| 30 | + if err != nil { |
| 31 | + assert.FailNow(t, err.Error()) |
| 32 | + } |
| 33 | + |
| 34 | + snapshotSuites, err := testutils.GetYmlSnapshotSuites(testsFolder) |
| 35 | + assert.Nil(t, err) |
| 36 | + |
| 37 | + // consider to replace `diff` command which is depended on OS to golang plugin. |
| 38 | + // could't find something better |
| 39 | + differ := testutils.NewExecDiffer() |
| 40 | + // Loop on all test/*.yml files |
| 41 | + for _, snapshot := range snapshotSuites { |
| 42 | + |
| 43 | + t.Run(snapshot.Name, func(t *testing.T) { |
| 44 | + |
| 45 | + // create a temp folder for the test |
| 46 | + tempFolder, err := os.MkdirTemp(t.TempDir(), strings.ReplaceAll(snapshot.Name, " ", "")) |
| 47 | + // descrive the base snapshot data of the test |
| 48 | + snapshotFolder := filepath.Join(tempFolder, "snapshot") |
| 49 | + assert.Nil(t, err, "could not create temp folder") |
| 50 | + defer os.RemoveAll(tempFolder) |
| 51 | + |
| 52 | + if len(snapshot.InitSnapshot) > 0 { |
| 53 | + err = snapshot.CreateSnapshotData(snapshot.InitSnapshot, snapshotFolder) |
| 54 | + assert.Nil(t, err) |
| 55 | + } |
| 56 | + |
| 57 | + if snapshot.ConfigFileName != "" { |
| 58 | + err = snapshot.CrateConfig(snapshotFolder) |
| 59 | + assert.Nil(t, err) |
| 60 | + } |
| 61 | + |
| 62 | + flagsCommand := strings.TrimPrefix(snapshot.Command, removeBinaryPlaceholder) |
| 63 | + stdout, stderr, err := testutils.ExecCmd(binaryPath, strings.Split(flagsCommand, " "), snapshotFolder) |
| 64 | + assert.Nil(t, err, stderr) |
| 65 | + |
| 66 | + // In case the stdout/stderr include the dynamic folder path, we want to replace with static-content for better snapshot text compare |
| 67 | + stdout, stderr = replaceFolderName(stdout, stderr, snapshotFolder) |
| 68 | + |
| 69 | + if snapshot.ExpectedStdOut != "" { |
| 70 | + assert.Equal(t, snapshot.ExpectedStdOut, stdout) |
| 71 | + } |
| 72 | + |
| 73 | + if snapshot.ExpectedStdErr != "" { |
| 74 | + assert.Equal(t, snapshot.ExpectedStdErr, stderr) |
| 75 | + } |
| 76 | + |
| 77 | + if len(snapshot.ExpectedSnapshot) > 0 { |
| 78 | + destSnapshotFolder := filepath.Join(tempFolder, "dest") |
| 79 | + err = snapshot.CreateSnapshotData(snapshot.ExpectedSnapshot, destSnapshotFolder) |
| 80 | + assert.Nil(t, err) |
| 81 | + |
| 82 | + diffResult, err := testutils.FolderDiff(differ, destSnapshotFolder, snapshotFolder, []string{snapshot.ConfigFileName}) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("snapshot folder is not equal. results: %v", diffResult) |
| 85 | + } |
| 86 | + assert.Nil(t, err) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + // loop on register suites (from *.go files) |
| 92 | + for name, suite := range register.GetSuites() { |
| 93 | + t.Run(name, func(t *testing.T) { |
| 94 | + |
| 95 | + // creates temp dir for test path. |
| 96 | + tempFolder, err := os.MkdirTemp(t.TempDir(), strings.ReplaceAll(name, " ", "")) |
| 97 | + assert.Nil(t, err, "could not create temp folder") |
| 98 | + |
| 99 | + defer os.RemoveAll(tempFolder) |
| 100 | + |
| 101 | + // initialized test case |
| 102 | + testInstance := suite(tempFolder) |
| 103 | + |
| 104 | + err = testInstance.SetupTest() |
| 105 | + assert.Nil(t, err) |
| 106 | + |
| 107 | + // get Teller flags command |
| 108 | + flags := testInstance.GetFlags() |
| 109 | + |
| 110 | + stdout, stderr, err := testutils.ExecCmd(binaryPath, flags, tempFolder) |
| 111 | + assert.Nil(t, err) |
| 112 | + |
| 113 | + stdout, stderr = replaceFolderName(stdout, stderr, tempFolder) |
| 114 | + err = testInstance.Check(stdout, stderr) |
| 115 | + assert.Nil(t, err) |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func replaceFolderName(stdout, stderr, workingDirectory string) (string, string) { |
| 121 | + stdout = strings.ReplaceAll(stdout, workingDirectory, replaceToStaticPath) |
| 122 | + stderr = strings.ReplaceAll(stderr, workingDirectory, replaceToStaticPath) |
| 123 | + shortFolderPath := workingDirectory[0:13] |
| 124 | + stdout = strings.ReplaceAll(stdout, shortFolderPath, replaceToShortStaticPath) |
| 125 | + stderr = strings.ReplaceAll(stderr, shortFolderPath, replaceToShortStaticPath) |
| 126 | + |
| 127 | + return stdout, stderr |
| 128 | +} |
| 129 | + |
| 130 | +func getBinaryPath() (string, error) { |
| 131 | + binaryPath, isExists := os.LookupEnv("BINARY_PATH") |
| 132 | + if !isExists { |
| 133 | + return "", errors.New("missing `BINARY_PATH`") |
| 134 | + } |
| 135 | + |
| 136 | + info, err := os.Stat(binaryPath) |
| 137 | + errors.Is(err, os.ErrNotExist) |
| 138 | + |
| 139 | + if err != nil || info.IsDir() { |
| 140 | + return "", fmt.Errorf("%s not found", binaryPath) |
| 141 | + } |
| 142 | + return binaryPath, nil |
| 143 | +} |
0 commit comments