-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* status update checks Signed-off-by: Amir Malka <[email protected]> * added tests and updated readme Signed-off-by: Amir Malka <[email protected]> * fix test Signed-off-by: Amir Malka <[email protected]> * CR Signed-off-by: Amir Malka <[email protected]> --------- Signed-off-by: Amir Malka <[email protected]>
- Loading branch information
Showing
7 changed files
with
418 additions
and
5 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
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
92 changes: 92 additions & 0 deletions
92
pkg/registry/softwarecomposition/applicationprofile/strategy_test.go
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,92 @@ | ||
package applicationprofile | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers" | ||
"github.com/kubescape/storage/pkg/apis/softwarecomposition" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestPrepareForUpdate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
oldAnnotations map[string]string | ||
newAnnotations map[string]string | ||
expected map[string]string | ||
}{ | ||
{ | ||
name: "transition from complete (with status) to partial - rejected", | ||
oldAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
helpers.StatusMetadataKey: "initializing", | ||
}, | ||
newAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
expected: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
helpers.StatusMetadataKey: "initializing", | ||
}, | ||
}, | ||
{ | ||
name: "transition from partial (with status) to complete - accepted", | ||
oldAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "initializing", | ||
}, | ||
newAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
expected: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
}, | ||
{ | ||
name: "transition from partial (without status) to complete - accepted", | ||
oldAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
}, | ||
newAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
expected: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
}, | ||
{ | ||
name: "transition from complete (without status) to partial - rejected", | ||
oldAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
}, | ||
newAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "initializing", | ||
}, | ||
expected: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := applicationProfileStrategy{} | ||
|
||
obj := &softwarecomposition.ApplicationProfile{ObjectMeta: metav1.ObjectMeta{Annotations: tt.newAnnotations}} | ||
old := &softwarecomposition.ApplicationProfile{ObjectMeta: metav1.ObjectMeta{Annotations: tt.oldAnnotations}} | ||
|
||
s.PrepareForUpdate(context.Background(), obj, old) | ||
if !reflect.DeepEqual(obj.Annotations, tt.expected) { | ||
t.Errorf("PrepareForUpdate() = %v, want %v", obj.Annotations, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
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
92 changes: 92 additions & 0 deletions
92
pkg/registry/softwarecomposition/networkneighbors/strategy_test.go
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,92 @@ | ||
package networkneighbors | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers" | ||
"github.com/kubescape/storage/pkg/apis/softwarecomposition" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestPrepareForUpdate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
oldAnnotations map[string]string | ||
newAnnotations map[string]string | ||
expected map[string]string | ||
}{ | ||
{ | ||
name: "transition from complete (with status) to partial - rejected", | ||
oldAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
helpers.StatusMetadataKey: "initializing", | ||
}, | ||
newAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
expected: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
helpers.StatusMetadataKey: "initializing", | ||
}, | ||
}, | ||
{ | ||
name: "transition from partial (with status) to complete - accepted", | ||
oldAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "initializing", | ||
}, | ||
newAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
expected: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
}, | ||
{ | ||
name: "transition from partial (without status) to complete - accepted", | ||
oldAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
}, | ||
newAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
expected: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
helpers.StatusMetadataKey: "ready", | ||
}, | ||
}, | ||
{ | ||
name: "transition from complete (without status) to partial - rejected", | ||
oldAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
}, | ||
newAnnotations: map[string]string{ | ||
helpers.CompletionMetadataKey: "partial", | ||
helpers.StatusMetadataKey: "initializing", | ||
}, | ||
expected: map[string]string{ | ||
helpers.CompletionMetadataKey: "complete", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := networkNeighborsStrategy{} | ||
|
||
obj := &softwarecomposition.NetworkNeighbors{ObjectMeta: metav1.ObjectMeta{Annotations: tt.newAnnotations}} | ||
old := &softwarecomposition.NetworkNeighbors{ObjectMeta: metav1.ObjectMeta{Annotations: tt.oldAnnotations}} | ||
|
||
s.PrepareForUpdate(context.Background(), obj, old) | ||
if !reflect.DeepEqual(obj.Annotations, tt.expected) { | ||
t.Errorf("PrepareForUpdate() = %v, want %v", obj.Annotations, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
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,31 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
func ValidateCompletionAnnotation(annotations map[string]string) *field.Error { | ||
if v, ok := annotations[helpers.CompletionMetadataKey]; ok { | ||
switch v { | ||
case helpers.Complete, helpers.Partial: | ||
return nil | ||
default: | ||
return field.Invalid(field.NewPath("metadata").Child("annotations").Child(helpers.CompletionMetadataKey), v, "invalid value") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func ValidateStatusAnnotation(annotations map[string]string) *field.Error { | ||
if v, ok := annotations[helpers.StatusMetadataKey]; ok { | ||
switch v { | ||
case helpers.Initializing, helpers.Ready, helpers.Completed, helpers.Incomplete, helpers.Unauthorize, helpers.MissingRuntime, helpers.TooLarge: | ||
return nil | ||
default: | ||
return field.Invalid(field.NewPath("metadata").Child("annotations").Child(helpers.StatusMetadataKey), v, "invalid value") | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.