Skip to content

Commit

Permalink
Bulk convert to using testify/suite for tests (#1185)
Browse files Browse the repository at this point in the history
There were a couple of places where `cmdExec` or similar was changed but not
changed back.

Now by this being a test suite it's handled for us.

As part of this work a number of other "leaking" side-effects
in tests are cleaned up properly. However I
wasn't able
to fix all of them, so those packages are left as stdlib testing only.

I also didn't convert everything we could have to use Setup functions.
  • Loading branch information
ashb authored May 30, 2024
1 parent c11669d commit fc96bb3
Show file tree
Hide file tree
Showing 104 changed files with 6,416 additions and 6,550 deletions.
316 changes: 162 additions & 154 deletions airflow-client/airflow-client_test.go

Large diffs are not rendered by default.

58 changes: 23 additions & 35 deletions airflow/airflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,60 @@ package airflow
import (
"os"
"path/filepath"
"testing"

"github.com/astronomer/astro-cli/pkg/fileutil"
"github.com/stretchr/testify/assert"
)

func TestInitDirs(t *testing.T) {
func (s *Suite) TestInitDirs() {
tmpDir, err := os.MkdirTemp("", "temp")
if err != nil {
t.Fatal(err)
}
s.Require().NoError(err)
defer os.RemoveAll(tmpDir)

dirs := []string{"dags"}

err = initDirs(tmpDir, dirs)
assert.NoError(t, err)
s.NoError(err)

exist, err := fileutil.Exists(filepath.Join(tmpDir, "dags"), nil)

assert.NoError(t, err)
assert.True(t, exist)
s.NoError(err)
s.True(exist)
}

func TestInitDirsEmpty(t *testing.T) {
func (s *Suite) TestInitDirsEmpty() {
tmpDir, err := os.MkdirTemp("", "temp")
if err != nil {
t.Fatal(err)
}
s.Require().NoError(err)
defer os.RemoveAll(tmpDir)

err = initDirs(tmpDir, nil)
assert.NoError(t, err)
s.NoError(err)
}

func TestInitFiles(t *testing.T) {
func (s *Suite) TestInitFiles() {
tmpDir, err := os.MkdirTemp("", "temp")
if err != nil {
t.Fatal(err)
}
s.Require().NoError(err)
defer os.RemoveAll(tmpDir)

files := map[string]string{
"requirements.txt": "",
}

err = initFiles(tmpDir, files)
assert.NoError(t, err)
s.NoError(err)

exist, err := fileutil.Exists(filepath.Join(tmpDir, "requirements.txt"), nil)

assert.NoError(t, err)
assert.True(t, exist)
s.NoError(err)
s.True(exist)
}

func TestInit(t *testing.T) {
func (s *Suite) TestInit() {
tmpDir, err := os.MkdirTemp("", "temp")
if err != nil {
t.Fatal(err)
}
s.Require().NoError(err)
defer os.RemoveAll(tmpDir)

err = Init(tmpDir, "astro-runtime", "test")
assert.NoError(t, err)
s.NoError(err)

expectedFiles := []string{
".dockerignore",
Expand All @@ -82,27 +72,25 @@ func TestInit(t *testing.T) {
}
for _, file := range expectedFiles {
exist, err := fileutil.Exists(filepath.Join(tmpDir, file), nil)
assert.NoError(t, err)
assert.True(t, exist)
s.NoError(err)
s.True(exist)
}
}

func TestInitConflictTest(t *testing.T) {
func (s *Suite) TestInitConflictTest() {
tmpDir, err := os.MkdirTemp("", "temp")
if err != nil {
t.Fatal(err)
}
s.Require().NoError(err)
defer os.RemoveAll(tmpDir)

err = initConflictTest(tmpDir, "astro-runtime", "test")
assert.NoError(t, err)
s.NoError(err)

expectedFiles := []string{
"conflict-check.Dockerfile",
}
for _, file := range expectedFiles {
exist, err := fileutil.Exists(filepath.Join(tmpDir, file), nil)
assert.NoError(t, err)
assert.True(t, exist)
s.NoError(err)
s.True(exist)
}
}
5 changes: 3 additions & 2 deletions airflow/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,16 +716,17 @@ func (d *DockerCompose) dagTest(testHomeDirectory, newAirflowVersion, newDockerF
return 1, err
}

reqFile := d.airflowHome + "/requirements.txt"
// add pytest-html to the requirements
err = fileutil.AddLineToFile("./requirements.txt", "pytest-html", "# This package is needed for the upgrade dag test. It will be removed once the test is over")
err = fileutil.AddLineToFile(reqFile, "pytest-html", "# This package is needed for the upgrade dag test. It will be removed once the test is over")
if err != nil {
fmt.Printf("Adding 'pytest-html' package to requirements.txt unsuccessful: %s\nManually add package to requirements.txt", err.Error())
}
fmt.Println("\nBuilding image for new Airflow version")
imageBuildErr := d.imageHandler.Build(newDockerFile, buildSecretString, airflowTypes.ImageBuildConfig{Path: d.airflowHome, Output: true})

// remove pytest-html to the requirements
err = fileutil.RemoveLineFromFile("./requirements.txt", "pytest-html", " # This package is needed for the upgrade dag test. It will be removed once the test is over")
err = fileutil.RemoveLineFromFile(reqFile, "pytest-html", " # This package is needed for the upgrade dag test. It will be removed once the test is over")
if err != nil {
fmt.Printf("Removing package 'pytest-html' from requirements.txt unsuccessful: %s\n", err.Error())
}
Expand Down
1 change: 0 additions & 1 deletion airflow/docker_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ func (d *DockerImage) Push(remoteImage, username, token string) error {
log.Debugf("Exec Push %s creds %v \n", dockerCommand, authConfig)

err = d.pushWithClient(&authConfig, remoteImage)

if err != nil {
// if it does not work with the go library use bash to run docker commands. Support for (old?) versions of Colima
err = pushWithBash(&authConfig, remoteImage)
Expand Down
Loading

0 comments on commit fc96bb3

Please sign in to comment.