Skip to content

Commit

Permalink
refactor(tests): assert.Error -> require.Error (other workflow/
Browse files Browse the repository at this point in the history
… dirs) (#13402)

Signed-off-by: Alan Clucas <[email protected]>
  • Loading branch information
Joibel authored Aug 10, 2024
1 parent df66dbe commit 24de3c6
Show file tree
Hide file tree
Showing 32 changed files with 837 additions and 949 deletions.
93 changes: 47 additions & 46 deletions workflow/artifactrepositories/artifactrepositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubefake "k8s.io/client-go/kubernetes/fake"
Expand All @@ -31,22 +32,22 @@ s3:
keyFormat: bar
`},
}, metav1.CreateOptions{})
assert.NoError(t, err)
require.NoError(t, err)

ref, err := i.Resolve(ctx, &wfv1.ArtifactRepositoryRef{Key: "my-key"}, "my-wf-ns")
if assert.NoError(t, err) {
assert.Equal(t, "my-wf-ns", ref.Namespace)
assert.Equal(t, "artifact-repositories", ref.ConfigMap)
assert.Equal(t, "my-key", ref.Key)
assert.False(t, ref.Default)
assert.NotNil(t, ref.ArtifactRepository)
}
require.NoError(t, err)
assert.Equal(t, "my-wf-ns", ref.Namespace)
assert.Equal(t, "artifact-repositories", ref.ConfigMap)
assert.Equal(t, "my-key", ref.Key)
assert.False(t, ref.Default)
assert.NotNil(t, ref.ArtifactRepository)

repo, err := i.Get(ctx, ref)
if assert.NoError(t, err) {
assert.Equal(t, &wfv1.ArtifactRepository{S3: &wfv1.S3ArtifactRepository{KeyFormat: "bar"}}, repo)
}
require.NoError(t, err)
assert.Equal(t, &wfv1.ArtifactRepository{S3: &wfv1.S3ArtifactRepository{KeyFormat: "bar"}}, repo)

err = k.CoreV1().ConfigMaps("my-wf-ns").Delete(ctx, "artifact-repositories", metav1.DeleteOptions{})
assert.NoError(t, err)
require.NoError(t, err)
})
t.Run("Explicit.ControllerNamespace", func(t *testing.T) {
ctx := context.Background()
Expand All @@ -57,40 +58,40 @@ s3:
keyFormat: bar
`},
}, metav1.CreateOptions{})
assert.NoError(t, err)
require.NoError(t, err)

ref, err := i.Resolve(ctx, &wfv1.ArtifactRepositoryRef{Key: "my-key"}, "my-wf-ns")
if assert.NoError(t, err) {
assert.Equal(t, "my-ctrl-ns", ref.Namespace)
assert.Equal(t, "artifact-repositories", ref.ConfigMap)
assert.Equal(t, "my-key", ref.Key)
assert.False(t, ref.Default)
assert.NotNil(t, ref.ArtifactRepository)
}
require.NoError(t, err)
assert.Equal(t, "my-ctrl-ns", ref.Namespace)
assert.Equal(t, "artifact-repositories", ref.ConfigMap)
assert.Equal(t, "my-key", ref.Key)
assert.False(t, ref.Default)
assert.NotNil(t, ref.ArtifactRepository)

repo, err := i.Get(ctx, ref)
if assert.NoError(t, err) {
assert.Equal(t, &wfv1.ArtifactRepository{S3: &wfv1.S3ArtifactRepository{KeyFormat: "bar"}}, repo)
}
require.NoError(t, err)
assert.Equal(t, &wfv1.ArtifactRepository{S3: &wfv1.S3ArtifactRepository{KeyFormat: "bar"}}, repo)

err = k.CoreV1().ConfigMaps("my-ctrl-ns").Delete(ctx, "artifact-repositories", metav1.DeleteOptions{})
assert.NoError(t, err)
require.NoError(t, err)
})
t.Run("Explicit.ConfigMapNotFound", func(t *testing.T) {
ctx := context.Background()
_, err := i.Resolve(ctx, &wfv1.ArtifactRepositoryRef{}, "my-wf-ns")
assert.Error(t, err)
require.Error(t, err)
})
t.Run("Explicit.ConfigMapMissingKey", func(t *testing.T) {
ctx := context.Background()
_, err := k.CoreV1().ConfigMaps("my-ns").Create(ctx, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: "artifact-repositories"},
}, metav1.CreateOptions{})
assert.NoError(t, err)
require.NoError(t, err)

_, err = i.Resolve(ctx, &wfv1.ArtifactRepositoryRef{}, "my-wf-ns")
assert.Error(t, err)
require.Error(t, err)

err = k.CoreV1().ConfigMaps("my-ns").Delete(ctx, "artifact-repositories", metav1.DeleteOptions{})
assert.NoError(t, err)
require.NoError(t, err)
})
t.Run("WorkflowNamespaceDefault", func(t *testing.T) {
ctx := context.Background()
Expand All @@ -104,22 +105,22 @@ s3:
keyFormat: bar
`},
}, metav1.CreateOptions{})
assert.NoError(t, err)
require.NoError(t, err)

ref, err := i.Resolve(ctx, nil, "my-wf-ns")
if assert.NoError(t, err) {
assert.Equal(t, "my-wf-ns", ref.Namespace)
assert.Equal(t, "artifact-repositories", ref.ConfigMap)
assert.Equal(t, "default-v1", ref.Key)
assert.False(t, ref.Default)
assert.NotNil(t, ref.ArtifactRepository)
}
require.NoError(t, err)
assert.Equal(t, "my-wf-ns", ref.Namespace)
assert.Equal(t, "artifact-repositories", ref.ConfigMap)
assert.Equal(t, "default-v1", ref.Key)
assert.False(t, ref.Default)
assert.NotNil(t, ref.ArtifactRepository)

repo, err := i.Get(ctx, ref)
if assert.NoError(t, err) {
assert.Equal(t, &wfv1.ArtifactRepository{S3: &wfv1.S3ArtifactRepository{KeyFormat: "bar"}}, repo)
}
require.NoError(t, err)
assert.Equal(t, &wfv1.ArtifactRepository{S3: &wfv1.S3ArtifactRepository{KeyFormat: "bar"}}, repo)

err = k.CoreV1().ConfigMaps("my-wf-ns").Delete(ctx, "artifact-repositories", metav1.DeleteOptions{})
assert.NoError(t, err)
require.NoError(t, err)
})
t.Run("DefaultWithNamespace", func(t *testing.T) {
ctx := context.Background()
Expand All @@ -128,19 +129,19 @@ s3:
Name: "artifact-repositories",
},
}, metav1.CreateOptions{})
assert.NoError(t, err)
require.NoError(t, err)

ref, err := i.Resolve(ctx, nil, "my-wf-ns")
if assert.NoError(t, err) {
assert.Equal(t, defaultArtifactRepositoryRefStatus, ref)
}
require.NoError(t, err)
assert.Equal(t, defaultArtifactRepositoryRefStatus, ref)

err = k.CoreV1().ConfigMaps("my-wf-ns").Delete(ctx, "artifact-repositories", metav1.DeleteOptions{})
assert.NoError(t, err)
require.NoError(t, err)
})
t.Run("Default", func(t *testing.T) {
ctx := context.Background()
ref, err := i.Resolve(ctx, nil, "my-wf-ns")
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, defaultArtifactRepositoryRefStatus, ref)
})
}
19 changes: 10 additions & 9 deletions workflow/artifacts/azure/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDetermineAccountName(t *testing.T) {
Expand All @@ -33,9 +34,9 @@ func TestDetermineAccountName(t *testing.T) {
}
for _, u := range validUrls {
u, err := url.Parse(u)
assert.NoError(t, err)
require.NoError(t, err)
accountName, err := determineAccountName(u)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "accountname", accountName)
}

Expand All @@ -44,9 +45,9 @@ func TestDetermineAccountName(t *testing.T) {
}
for _, u := range invalidUrls {
u, err := url.Parse(u)
assert.NoError(t, err)
require.NoError(t, err)
accountName, err := determineAccountName(u)
assert.Error(t, err)
require.Error(t, err)
assert.Equal(t, "", accountName)
}
}
Expand All @@ -62,11 +63,11 @@ func TestArtifactDriver_WithServiceKey_DownloadDirectory_Subdir(t *testing.T) {

// ensure container exists
containerClient, err := driver.newAzureContainerClient()
assert.NoError(t, err)
require.NoError(t, err)
_, err = containerClient.Create(context.Background(), nil)
var responseError *azcore.ResponseError
if err != nil && !(errors.As(err, &responseError) && responseError.ErrorCode == "ContainerAlreadyExists") {
assert.NoError(t, err)
require.NoError(t, err)
}

// test read/write operations to the azurite container using the container client
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestArtifactDriver_WithSASToken_DownloadDirectory_Subdir(t *testing.T) {

// ensure container exists
containerClient, err := driver.newAzureContainerClient()
assert.NoError(t, err)
require.NoError(t, err)

// test read/write operations to the azurite container using the container client
testContainerClientReadWriteOperations(t, containerClient, driver)
Expand All @@ -118,7 +119,7 @@ func testContainerClientReadWriteOperations(t *testing.T, containerClient *conta
// download the dir, containing a subdir
blobClient := containerClient.NewBlockBlobClient("dir/subdir/file-in-subdir.txt")
_, err := blobClient.UploadBuffer(context.Background(), []byte("foo"), nil)
assert.NoError(t, err)
require.NoError(t, err)

azureArtifact := wfv1.AzureArtifact{
Blob: "dir",
Expand All @@ -130,7 +131,7 @@ func testContainerClientReadWriteOperations(t *testing.T, containerClient *conta
}
dstDir := t.TempDir()
err = driver.DownloadDirectory(containerClient, &argoArtifact, filepath.Join(dstDir, "dir"))
assert.NoError(t, err)
require.NoError(t, err)
assert.FileExists(t, filepath.Join(dstDir, "dir", "subdir", "file-in-subdir.txt"))
}

Expand Down
5 changes: 3 additions & 2 deletions workflow/artifacts/common/load_to_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
Expand Down Expand Up @@ -104,7 +105,7 @@ func TestLoadToStream(t *testing.T) {

stream, err := LoadToStream(&wfv1.Artifact{}, tc.artifactDriver)
if tc.errMsg == "" {
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, stream)
stream.Close()

Expand All @@ -115,7 +116,7 @@ func TestLoadToStream(t *testing.T) {
}
assert.Equal(t, len(filesBefore), len(filesAfter))
} else {
assert.Error(t, err)
require.Error(t, err)
assert.Equal(t, tc.errMsg, err.Error())
}
})
Expand Down
Loading

0 comments on commit 24de3c6

Please sign in to comment.