forked from openshift/compliance-operator
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We have a smattering of assertions in different files within the `framework` package. Even if you rely on your IDE to fill these in for you, organizing them is good practice. This commit adds a new file specifically for assertions, and populates it with an existing assertion used in tests. It also implements the ability for assertions to handle test outcomes, instead of returning booleans, forcing the callers to handle test outcomes. Although having test outcomes (e.g., `FailNow`) in the test is clear, we check and call `Fatal` a lot across the test code. Moving the outcome into the assertion allows the code to be a little more dry, and clear if everyone agrees assertions should accept that reponsibility.
- Loading branch information
Showing
3 changed files
with
39 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package framework | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
compv1alpha1 "github.com/ComplianceAsCode/compliance-operator/pkg/apis/compliance/v1alpha1" | ||
|
||
"k8s.io/apimachinery/pkg/labels" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func (f *Framework) AssertMustHaveParsedProfiles(t *testing.T, pbName, productType, productName string) { | ||
var l compv1alpha1.ProfileList | ||
o := &client.ListOptions{ | ||
LabelSelector: labels.SelectorFromSet(map[string]string{ | ||
compv1alpha1.ProfileBundleOwnerLabel: pbName, | ||
}), | ||
} | ||
if err := f.Client.List(context.TODO(), &l, o); err != nil { | ||
t.Fatalf("failed checking profiles in ProfileBundle: %s", err) | ||
} | ||
if len(l.Items) <= 0 { | ||
t.Fatalf("failed to get profiles from ProfileBundle %s. Expected at least one but got %d", pbName, len(l.Items)) | ||
} | ||
|
||
for _, p := range l.Items { | ||
if p.Annotations[compv1alpha1.ProductTypeAnnotation] != productType { | ||
t.Fatalf("expected %s to be %s, got %s instead", compv1alpha1.ProductTypeAnnotation, productType, p.Annotations[compv1alpha1.ProductTypeAnnotation]) | ||
} | ||
|
||
if p.Annotations[compv1alpha1.ProductAnnotation] != productName { | ||
t.Fatalf("expected %s to be %s, got %s instead", compv1alpha1.ProductAnnotation, productName, p.Annotations[compv1alpha1.ProductAnnotation]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters