diff --git a/pkg/api/BUILD.bazel b/pkg/api/BUILD.bazel index 38dd15449..637e0fd26 100644 --- a/pkg/api/BUILD.bazel +++ b/pkg/api/BUILD.bazel @@ -14,7 +14,6 @@ go_library( "//pkg/apis/meta/v1:go_default_library", "@io_k8s_api//auditregistration/v1alpha1:go_default_library", "@io_k8s_apiextensions_apiserver//pkg/apis/apiextensions/v1beta1:go_default_library", - "@io_k8s_apimachinery//pkg/apis/meta/internalversion:go_default_library", "@io_k8s_apimachinery//pkg/apis/meta/v1:go_default_library", "@io_k8s_apimachinery//pkg/runtime:go_default_library", "@io_k8s_apimachinery//pkg/runtime/schema:go_default_library", diff --git a/pkg/api/scheme.go b/pkg/api/scheme.go index e7cf455f3..9fb03c9f9 100644 --- a/pkg/api/scheme.go +++ b/pkg/api/scheme.go @@ -19,7 +19,6 @@ package api import ( auditreg "k8s.io/api/auditregistration/v1alpha1" apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" - metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" diff --git a/test/integration/BUILD.bazel b/test/integration/BUILD.bazel index 81ff5d13b..efe77f399 100644 --- a/test/integration/BUILD.bazel +++ b/test/integration/BUILD.bazel @@ -11,6 +11,7 @@ filegroup( ":package-srcs", "//test/integration/certificates:all-srcs", "//test/integration/conversion:all-srcs", + "//test/integration/ctl:all-srcs", "//test/integration/framework:all-srcs", "//test/integration/webhook:all-srcs", ], diff --git a/test/integration/ctl/BUILD.bazel b/test/integration/ctl/BUILD.bazel new file mode 100644 index 000000000..edef9c235 --- /dev/null +++ b/test/integration/ctl/BUILD.bazel @@ -0,0 +1,25 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") + +go_test( + name = "go_default_test", + srcs = ["ctl_convert_test.go"], + data = glob(["testdata/**"]), + deps = [ + "//cmd/ctl/pkg/convert:go_default_library", + "@io_k8s_cli_runtime//pkg/genericclioptions:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/test/integration/ctl/ctl_convert_test.go b/test/integration/ctl/ctl_convert_test.go new file mode 100644 index 000000000..a91d667d9 --- /dev/null +++ b/test/integration/ctl/ctl_convert_test.go @@ -0,0 +1,260 @@ +/* +Copyright 2020 The Jetstack cert-manager contributors. + +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 ctl + +import ( + "strings" + "testing" + + "k8s.io/cli-runtime/pkg/genericclioptions" + + "github.com/jetstack/cert-manager/cmd/ctl/pkg/convert" +) + +const ( + testdataResource1 = "./testdata/convert_resource1.yaml" + testdataResource2 = "./testdata/convert_resource2.yaml" + testdataResource3 = "./testdata/convert_resource3.yaml" + + targetv1alpha2 = "cert-manager.io/v1alpha2" + targetv1alpha3 = "cert-manager.io/v1alpha3" +) + +func TestCtlConvert(t *testing.T) { + tests := map[string]struct { + input, expOutput string + targetVersion string + expErr bool + }{ + "a single cert-manager resource should convert to v1alpha2 with no target": { + input: testdataResource1, + expOutput: resource1v1alpha2, + }, + "a single cert-manager resource should convert to v1alpha2 with target v1alpha2": { + input: testdataResource1, + targetVersion: targetv1alpha2, + expOutput: resource1v1alpha2, + }, + "a single cert-manager resource should convert to v1alpha3 with target v1alpha3": { + input: testdataResource1, + targetVersion: targetv1alpha3, + expOutput: resource1v1alpha3, + }, + "a list of cert-manager resources should convert to v1alpha2 with no target": { + input: testdataResource2, + expOutput: resource2v1alpha2, + }, + "a list of cert-manager resources should convert to v1alpha2 with target v1alpha2": { + input: testdataResource2, + expOutput: resource2v1alpha2, + }, + "a list of cert-manager resources should convert to v1alpha3 with target v1alpha3": { + input: testdataResource2, + targetVersion: targetv1alpha3, + expOutput: resource2v1alpha3, + }, + "a list of a mix of cert-manager and non cert-manager resources should convert to v1alpha2 with no target": { + input: testdataResource2, + targetVersion: targetv1alpha3, + expOutput: resource2v1alpha3, + }, + "a list of a mix of cert-manager and non cert-manager resources should error with target v1alpha2": { + input: testdataResource3, + targetVersion: targetv1alpha2, + expErr: true, + }, + "a list of a mix of cert-manager and non cert-manager resources should error with target v1alpha3": { + input: testdataResource3, + targetVersion: targetv1alpha3, + expErr: true, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + // Run ctl convert command with input options + streams, _, outBuf, _ := genericclioptions.NewTestIOStreams() + + opts := convert.NewOptions(streams) + opts.OutputVersion = test.targetVersion + opts.Filenames = []string{test.input} + + if err := opts.Complete(); err != nil { + t.Fatal(err) + } + + err := opts.Run() + if test.expErr != (err != nil) { + t.Errorf("got unexpected error, exp=%t got=%v", + test.expErr, err) + } + + if strings.TrimSpace(test.expOutput) != strings.TrimSpace(outBuf.String()) { + t.Errorf("got unexpected output, exp=%s got=%s", + strings.TrimSpace(test.expOutput), strings.TrimSpace(outBuf.String())) + } + }) + } +} + +const ( + resource1v1alpha2 = ` +apiVersion: cert-manager.io/v1alpha2 +kind: Certificate +metadata: + creationTimestamp: null + name: ca-issuer + namespace: sandbox +spec: + commonName: my-csi-app + isCA: true + issuerRef: + group: cert-manager.io + kind: Issuer + name: selfsigned-issuer + secretName: ca-key-pair +status: {}` + resource1v1alpha3 = ` +apiVersion: cert-manager.io/v1alpha3 +kind: Certificate +metadata: + creationTimestamp: null + name: ca-issuer + namespace: sandbox +spec: + commonName: my-csi-app + isCA: true + issuerRef: + group: cert-manager.io + kind: Issuer + name: selfsigned-issuer + secretName: ca-key-pair +status: {}` + + resource2v1alpha2 = ` +apiVersion: v1 +items: +- apiVersion: cert-manager.io/v1alpha2 + kind: Certificate + metadata: + creationTimestamp: null + name: ca-issuer + namespace: sandbox + spec: + commonName: my-csi-app + isCA: true + issuerRef: + group: cert-manager.io + kind: Issuer + name: selfsigned-issuer + secretName: ca-key-pair + status: {} +- apiVersion: cert-manager.io/v1alpha2 + kind: Issuer + metadata: + creationTimestamp: null + name: ca-issuer + namespace: sandbox + spec: + ca: + secretName: ca-key-pair + status: {} +- apiVersion: cert-manager.io/v1alpha2 + kind: Certificate + metadata: + creationTimestamp: null + name: ca-issuer-2 + namespace: sandbox + spec: + commonName: my-csi-app + isCA: true + issuerRef: + group: cert-manager.io + kind: Issuer + name: ca-issuer + secretName: ca-key-pair + status: {} +kind: List +metadata: {}` + resource2v1alpha3 = ` +apiVersion: v1 +items: +- apiVersion: cert-manager.io/v1alpha3 + kind: Certificate + metadata: + creationTimestamp: null + name: ca-issuer + namespace: sandbox + spec: + commonName: my-csi-app + isCA: true + issuerRef: + group: cert-manager.io + kind: Issuer + name: selfsigned-issuer + secretName: ca-key-pair + status: {} +- apiVersion: cert-manager.io/v1alpha3 + kind: Issuer + metadata: + creationTimestamp: null + name: ca-issuer + namespace: sandbox + spec: + ca: + secretName: ca-key-pair + status: {} +- apiVersion: cert-manager.io/v1alpha3 + kind: Certificate + metadata: + creationTimestamp: null + name: ca-issuer-2 + namespace: sandbox + spec: + commonName: my-csi-app + isCA: true + issuerRef: + group: cert-manager.io + kind: Issuer + name: ca-issuer + secretName: ca-key-pair + status: {} +kind: List +metadata: {}` + + resource3v1alpha2 = ` +apiVersion: v1 +items: +- apiVersion: v1 + kind: Namespace + metadata: + creationTimestamp: null + name: sandbox + spec: {} + status: {} +- apiVersion: cert-manager.io/v1alpha2 + kind: Issuer + metadata: + creationTimestamp: null + name: selfsigned-issuer + namespace: sandbox + spec: + selfSigned: {} + status: {} +kind: List +metadata: {}` +) diff --git a/test/integration/ctl/testdata/convert_resource1.yaml b/test/integration/ctl/testdata/convert_resource1.yaml new file mode 100644 index 000000000..9b8d488c3 --- /dev/null +++ b/test/integration/ctl/testdata/convert_resource1.yaml @@ -0,0 +1,13 @@ +apiVersion: cert-manager.io/v1alpha2 +kind: Certificate +metadata: + name: ca-issuer + namespace: sandbox +spec: + isCA: true + secretName: ca-key-pair + commonName: my-csi-app + issuerRef: + name: selfsigned-issuer + kind: Issuer + group: cert-manager.io diff --git a/test/integration/ctl/testdata/convert_resource2.yaml b/test/integration/ctl/testdata/convert_resource2.yaml new file mode 100644 index 000000000..f24b51cae --- /dev/null +++ b/test/integration/ctl/testdata/convert_resource2.yaml @@ -0,0 +1,39 @@ +--- +# my comment +apiVersion: cert-manager.io/v1alpha2 +kind: Certificate +metadata: + name: ca-issuer + namespace: sandbox +spec: + isCA: true + secretName: ca-key-pair + commonName: my-csi-app + issuerRef: + name: selfsigned-issuer + kind: Issuer + group: cert-manager.io +--- +apiVersion: cert-manager.io/v1alpha2 +kind: Issuer +metadata: + name: ca-issuer + namespace: sandbox +spec: + ca: + secretName: ca-key-pair +--- +apiVersion: cert-manager.io/v1alpha2 +kind: Certificate +metadata: + name: ca-issuer-2 + namespace: sandbox +spec: + isCA: true + secretName: ca-key-pair + commonName: my-csi-app + issuerRef: + name: ca-issuer + kind: Issuer + group: cert-manager.io +--- diff --git a/test/integration/ctl/testdata/convert_resource3.yaml b/test/integration/ctl/testdata/convert_resource3.yaml new file mode 100644 index 000000000..8deca6a07 --- /dev/null +++ b/test/integration/ctl/testdata/convert_resource3.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: sandbox +--- +apiVersion: cert-manager.io/v1alpha2 +kind: Issuer +metadata: + name: selfsigned-issuer + namespace: sandbox +spec: + selfSigned: {}