-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move policy validation code to dedicated file.
The validations called to validate AdmissionPolicy data were in the file used to validate ClusterAdmissionPolicy policies. This commit moves the code to a dedicated file and call it from both files. The unit tests have been adjust to reflect this change as well. Signed-off-by: José Guilherme Vanz <[email protected]>
- Loading branch information
Showing
6 changed files
with
491 additions
and
308 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,54 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/kubewarden/kubewarden-controller/internal/pkg/constants" | ||
) | ||
|
||
func TestAdmissionPolicyDefault(t *testing.T) { | ||
policy := admissionPolicyFactory(nil, "", "protect") | ||
policy.Default() | ||
|
||
require.Equal(t, constants.DefaultPolicyServer, policy.GetPolicyServer()) | ||
require.Contains(t, policy.GetFinalizers(), constants.KubewardenFinalizer) | ||
} | ||
|
||
func TestAdmissionPolicyValidateCreate(t *testing.T) { | ||
policy := admissionPolicyFactory(nil, "", "protect") | ||
warnings, err := policy.ValidateCreate() | ||
require.NoError(t, err) | ||
require.Empty(t, warnings) | ||
} | ||
|
||
func TestAdmissionPolicyValidateUpdate(t *testing.T) { | ||
oldPolicy := admissionPolicyFactory(nil, "", "protect") | ||
newPolicy := admissionPolicyFactory(nil, "", "protect") | ||
warnings, err := newPolicy.ValidateUpdate(oldPolicy) | ||
require.NoError(t, err) | ||
require.Empty(t, warnings) | ||
} | ||
|
||
func TestAdmissionPolicyValidateUpdateWithInvalidOldPolicy(t *testing.T) { | ||
oldPolicy := clusterAdmissionPolicyFactory(nil, "", "protect") | ||
newPolicy := admissionPolicyFactory(nil, "", "protect") | ||
warnings, err := newPolicy.ValidateUpdate(oldPolicy) | ||
require.Empty(t, warnings) | ||
require.ErrorContains(t, err, "object is not of type AdmissionPolicy") | ||
} |
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
Oops, something went wrong.