Skip to content

Commit

Permalink
fix: fix broken test
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin-li committed Sep 21, 2023
1 parent 9548899 commit 837c0f1
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ delete-gatekeeper:

.PHONY: test-e2e
test-e2e: generate-rotation-certs
bats -t ${BATS_BASE_TESTS_FILE}
timeout 15m bats -t ${BATS_BASE_TESTS_FILE}
EXPIRING_CERT_DIR=.staging/rotation/expiring-certs CERT_DIR=.staging/rotation GATEKEEPER_VERSION=${GATEKEEPER_VERSION} bats -t ${BATS_PLUGIN_TESTS_FILE}

.PHONY: test-e2e-cli
Expand Down
2 changes: 0 additions & 2 deletions api/unversioned/policy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type PolicySpec struct {

// Type of the policy
Type string `json:"type,omitempty"`

// Parameters for this policy
Parameters runtime.RawExtension `json:"parameters,omitempty"`
}
Expand All @@ -39,7 +38,6 @@ type PolicyStatus struct {

// Is successful while applying the policy.
IsSuccess bool `json:"issuccess"`

// Error message if policy is not successfully applied.
// +optional
Error string `json:"error,omitempty"`
Expand Down
14 changes: 13 additions & 1 deletion api/v1alpha1/policy_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,49 @@ package v1alpha1

import (
unversioned "github.com/deislabs/ratify/api/unversioned"
"github.com/deislabs/ratify/internal/constants"
conversion "k8s.io/apimachinery/pkg/conversion"
)

// Convert unversioned PolicySpec to PolicySpec of v1alpha1.
//
//nolint:revive // ignore linter for autogenerated code
func Convert_unversioned_PolicySpec_To_v1alpha1_PolicySpec(in *unversioned.PolicySpec, out *PolicySpec, _ conversion.Scope) error {
out.Parameters = in.Parameters
return nil
}

// Convert unversioned PolicyStatus to PolicyStatus of v1alpha1.
//
//nolint:revive // ignore linter for autogenerated code
func Convert_unversioned_PolicyStatus_To_v1alpha1_PolicyStatus(in *unversioned.PolicyStatus, out *PolicyStatus, _ conversion.Scope) error {
return nil
}

// Convert unversioned Policy to Policy of v1alpha1.
//
//nolint:revive // ignore linter for autogenerated code
func Convert_unversioned_Policy_To_v1alpha1_Policy(in *unversioned.Policy, out *Policy, s conversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
// metadata.name in v1alpha1 is same as spec.type in unversioned.
out.ObjectMeta.Name = in.Spec.Type
if err := Convert_unversioned_PolicySpec_To_v1alpha1_PolicySpec(&in.Spec, &out.Spec, s); err != nil {
return err
}

Check warning on line 49 in api/v1alpha1/policy_conversion.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/policy_conversion.go#L48-L49

Added lines #L48 - L49 were not covered by tests
return Convert_unversioned_PolicyStatus_To_v1alpha1_PolicyStatus(&in.Status, &out.Status, s)
}

// Convert Policy of v1alpha1 to unversioned Policy.
//
//nolint:revive // ignore linter for autogenerated code
func Convert_v1alpha1_Policy_To_unversioned_Policy(in *Policy, out *unversioned.Policy, s conversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
out.ObjectMeta.Name = "ratify-policy"
// metadata.name MUST be `ratify-policy` in unversioned.
out.ObjectMeta.Name = constants.RatifyPolicy
if err := Convert_v1alpha1_PolicySpec_To_unversioned_PolicySpec(&in.Spec, &out.Spec, s); err != nil {
return err
}

Check warning on line 62 in api/v1alpha1/policy_conversion.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/policy_conversion.go#L61-L62

Added lines #L61 - L62 were not covered by tests
// spec.type in unversioned is same as metadata.name in v1alpha1.
out.Spec.Type = in.ObjectMeta.Name
return Convert_v1alpha1_PolicyStatus_To_unversioned_PolicyStatus(&in.Status, &out.Status, s)
}
82 changes: 82 additions & 0 deletions api/v1alpha1/policy_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright The Ratify Authors.
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 v1alpha1

import (
"reflect"
"testing"

unversioned "github.com/deislabs/ratify/api/unversioned"
"github.com/deislabs/ratify/internal/constants"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
)

var params = runtime.RawExtension{}

Check failure on line 29 in api/v1alpha1/policy_conversion_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
const testPolicyType = "testPolicyType"

func TestConvert_unversioned_PolicySpec_To_v1alpha1_PolicySpec(t *testing.T) {
in := &unversioned.PolicySpec{
Parameters: params,
}
out := &PolicySpec{}
if err := Convert_unversioned_PolicySpec_To_v1alpha1_PolicySpec(in, out, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(out.Parameters, in.Parameters) {
t.Fatalf("expect parameters to be equal, but got different values")
}
}

func TestConvert_unversioned_PolicyStatus_To_v1alpha1_PolicyStatus(t *testing.T) {
if err := Convert_unversioned_PolicyStatus_To_v1alpha1_PolicyStatus(nil, nil, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

func TestConvert_unversioned_Policy_To_v1alpha1_Policy(t *testing.T) {
in := &unversioned.Policy{
Spec: unversioned.PolicySpec{
Type: testPolicyType,
},
}
out := &Policy{}
if err := Convert_unversioned_Policy_To_v1alpha1_Policy(in, out, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.ObjectMeta.Name != in.Spec.Type {
t.Fatalf("expect metadata.name to be %s, but got %s", in.Spec.Type, out.ObjectMeta.Name)
}
}

func TestConvert_v1alpha1_Policy_To_unversioned_Policy(t *testing.T) {
in := &Policy{
ObjectMeta: metav1.ObjectMeta{
Name: testPolicyType,
},
}
out := &unversioned.Policy{}
if err := Convert_v1alpha1_Policy_To_unversioned_Policy(in, out, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.ObjectMeta.Name != constants.RatifyPolicy {
t.Fatalf("expect metadata.name to be %s, but got %s", constants.RatifyPolicy, out.ObjectMeta.Name)
}
if out.Spec.Type != in.ObjectMeta.Name {
t.Fatalf("expect spec.type to be %s, but got %s", in.ObjectMeta.Name, out.Spec.Type)
}
}
2 changes: 0 additions & 2 deletions api/v1beta1/policy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type PolicySpec struct {

// Type of the policy
Type string `json:"type,omitempty"`

// +kubebuilder:pruning:PreserveUnknownFields
// Parameters for this policy
Parameters runtime.RawExtension `json:"parameters,omitempty"`
Expand All @@ -42,7 +41,6 @@ type PolicyStatus struct {

// Is successful while applying the policy.
IsSuccess bool `json:"issuccess"`

// Error message if policy is not successfully applied.
// +optional
Error string `json:"error,omitempty"`
Expand Down
19 changes: 19 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright The Ratify Authors.
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 constants

const RatifyPolicy = "ratify-policy"
5 changes: 2 additions & 3 deletions pkg/controllers/policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

configv1beta1 "github.com/deislabs/ratify/api/v1beta1"
"github.com/deislabs/ratify/internal/constants"
"github.com/deislabs/ratify/pkg/policyprovider"
"github.com/deislabs/ratify/pkg/policyprovider/config"
pf "github.com/deislabs/ratify/pkg/policyprovider/factory"
Expand All @@ -31,8 +32,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const ratifyPolicy = "ratify-policy"

// PolicyReconciler reconciles a Policy object
type PolicyReconciler struct {
client.Client
Expand Down Expand Up @@ -76,7 +75,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, client.IgnoreNotFound(err)
}

if resource != ratifyPolicy {
if resource != constants.RatifyPolicy {
errStr := fmt.Sprintf("metadata.name must be ratify-policy, got %s", resource)
policyLogger.Error(errStr)
writePolicyStatus(ctx, r, &policy, policyLogger, false, errStr)
Expand Down
2 changes: 1 addition & 1 deletion pkg/policyprovider/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (f *TestPolicyProviderFactory) Create(_ config.PolicyPluginConfig) (policyp
// Checks the correct registered policy provider is invoked based on config
func TestCreatePolicyProvidersFromConfig_BuiltInPolicyProviders_ReturnsExpected(t *testing.T) {
builtInPolicyProviders = map[string]PolicyFactory{
"test-policyprovider": &TestPolicyProviderFactory{},
"testpolicyprovider": &TestPolicyProviderFactory{},
}

configPolicyConfig := map[string]interface{}{
Expand Down
16 changes: 0 additions & 16 deletions test/bats/base-test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ SLEEP_TIME=1
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo1 --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod initcontainer-pod --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod initcontainer-pod1 --namespace default --force --ignore-not-found=true'
}
run kubectl apply -f ./library/default/template.yaml
assert_success
Expand All @@ -28,20 +26,6 @@ SLEEP_TIME=1

run kubectl run demo1 --namespace default --image=registry:5000/notation:unsigned
assert_failure

# validate initContainers image
run kubectl apply -f ./test/testdata/pod_initContainers_signed.yaml --namespace default
assert_success

run kubectl apply -f ./test/testdata/pod_initContainers_unsigned.yaml --namespace default
assert_failure

# validate ephemeralContainers image
run kubectl debug -it demo --image=registry:5000/notation:signed --target=demo
assert_success

run kubectl debug -it demo --image=registry:5000/notation:unsigned --target=demo
assert_failure
}

@test "crd version test" {
Expand Down

0 comments on commit 837c0f1

Please sign in to comment.