|
1 | 1 | package controllers
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/go-logr/logr" |
5 |
| - routev1 "github.com/openshift/api/route/v1" |
6 |
| - oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1" |
7 |
| - "k8s.io/apimachinery/pkg/runtime" |
8 |
| - "k8s.io/apimachinery/pkg/types" |
9 |
| - |
10 |
| - //"k8s.io/apimachinery/pkg/types" |
| 4 | + "context" |
11 | 5 | "reflect"
|
12 | 6 | "testing"
|
13 | 7 |
|
14 |
| - "k8s.io/client-go/tools/record" |
15 |
| - "sigs.k8s.io/controller-runtime/pkg/client" |
16 |
| - "sigs.k8s.io/controller-runtime/pkg/client/fake" |
17 |
| - |
| 8 | + "github.com/go-logr/logr" |
| 9 | + routev1 "github.com/openshift/api/route/v1" |
| 10 | + oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1" |
18 | 11 | "github.com/openshift/oadp-operator/pkg/common"
|
19 | 12 | velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
20 | 13 | appsv1 "k8s.io/api/apps/v1"
|
21 | 14 | corev1 "k8s.io/api/core/v1"
|
22 | 15 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
| 16 | + "k8s.io/apimachinery/pkg/runtime" |
| 17 | + "k8s.io/apimachinery/pkg/types" |
23 | 18 | "k8s.io/apimachinery/pkg/util/intstr"
|
24 | 19 | "k8s.io/client-go/kubernetes/scheme"
|
| 20 | + "k8s.io/client-go/tools/record" |
25 | 21 | "k8s.io/utils/pointer"
|
| 22 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
26 | 24 | )
|
27 | 25 |
|
28 | 26 | func getSchemeForFakeClientForRegistry() (*runtime.Scheme, error) {
|
|
91 | 89 | "aws_secret_access_key=" + testSecretAccessKey + "=" + testSecretAccessKey,
|
92 | 90 | ),
|
93 | 91 | }
|
| 92 | + secretDataWithCarriageReturnInSecret = map[string][]byte{ |
| 93 | + "cloud": []byte( |
| 94 | + "\n[" + testBslProfile + "]\r\n" + |
| 95 | + "aws_access_key_id=" + testBslAccessKey + "\n" + |
| 96 | + "aws_secret_access_key=" + testBslSecretAccessKey + "=" + testBslSecretAccessKey + |
| 97 | + "\n[default]" + "\n" + |
| 98 | + "aws_access_key_id=" + testAccessKey + "\n" + |
| 99 | + "aws_secret_access_key=" + testSecretAccessKey + "=" + testSecretAccessKey + |
| 100 | + "\r\n[test-profile]\n" + |
| 101 | + "aws_access_key_id=" + testAccessKey + "\r\n" + |
| 102 | + "aws_secret_access_key=" + testSecretAccessKey + "=" + testSecretAccessKey, |
| 103 | + ), |
| 104 | + } |
94 | 105 | secretDataWithMixedQuotesAndSpacesInSecret = map[string][]byte{
|
95 | 106 | "cloud": []byte(
|
96 | 107 | "\n[" + testBslProfile + "]\n" +
|
@@ -261,6 +272,56 @@ func TestDPAReconciler_buildRegistryDeployment(t *testing.T) {
|
261 | 272 | },
|
262 | 273 | dpa: &oadpv1alpha1.DataProtectionApplication{},
|
263 | 274 | },
|
| 275 | + { |
| 276 | + name: "given a valid bsl with carriageReturn in secret val get appropriate registry deployment", |
| 277 | + registryDeployment: &appsv1.Deployment{ |
| 278 | + ObjectMeta: metav1.ObjectMeta{ |
| 279 | + Name: "test-registry", |
| 280 | + Namespace: "test-ns", |
| 281 | + }, |
| 282 | + Spec: appsv1.DeploymentSpec{ |
| 283 | + Selector: &metav1.LabelSelector{ |
| 284 | + MatchLabels: map[string]string{ |
| 285 | + "component": "oadp-" + "test-bsl" + "-" + "aws" + "-registry", |
| 286 | + }, |
| 287 | + }, |
| 288 | + }, |
| 289 | + }, |
| 290 | + bsl: &velerov1.BackupStorageLocation{ |
| 291 | + ObjectMeta: metav1.ObjectMeta{ |
| 292 | + Name: "test-bsl", |
| 293 | + Namespace: "test-ns", |
| 294 | + }, |
| 295 | + Spec: velerov1.BackupStorageLocationSpec{ |
| 296 | + Provider: AWSProvider, |
| 297 | + StorageType: velerov1.StorageType{ |
| 298 | + ObjectStorage: &velerov1.ObjectStorageLocation{ |
| 299 | + Bucket: "aws-bucket", |
| 300 | + }, |
| 301 | + }, |
| 302 | + Config: map[string]string{ |
| 303 | + Region: "aws-region", |
| 304 | + S3URL: "https://sr-url-aws-domain.com", |
| 305 | + InsecureSkipTLSVerify: "false", |
| 306 | + }, |
| 307 | + }, |
| 308 | + }, |
| 309 | + secret: &corev1.Secret{ |
| 310 | + ObjectMeta: metav1.ObjectMeta{ |
| 311 | + Name: "cloud-credentials", |
| 312 | + Namespace: "test-ns", |
| 313 | + }, |
| 314 | + Data: secretDataWithCarriageReturnInSecret, |
| 315 | + }, |
| 316 | + registrySecret: &corev1.Secret{ |
| 317 | + ObjectMeta: metav1.ObjectMeta{ |
| 318 | + Name: "oadp-test-bsl-aws-registry-secret", |
| 319 | + Namespace: "test-ns", |
| 320 | + }, |
| 321 | + Data: secretDataWithCarriageReturnInSecret, |
| 322 | + }, |
| 323 | + dpa: &oadpv1alpha1.DataProtectionApplication{}, |
| 324 | + }, |
264 | 325 | {
|
265 | 326 | name: "given a valid bsl with use of quotes in secret val get appropriate registry deployment",
|
266 | 327 | registryDeployment: &appsv1.Deployment{
|
@@ -1851,3 +1912,43 @@ func TestDPAReconciler_populateAzureRegistrySecret(t *testing.T) {
|
1851 | 1912 | })
|
1852 | 1913 | }
|
1853 | 1914 | }
|
| 1915 | + |
| 1916 | +func Test_replaceCarriageReturn(t *testing.T) { |
| 1917 | + type args struct { |
| 1918 | + data map[string][]byte |
| 1919 | + logger logr.Logger |
| 1920 | + } |
| 1921 | + tests := []struct { |
| 1922 | + name string |
| 1923 | + args args |
| 1924 | + want map[string][]byte |
| 1925 | + }{ |
| 1926 | + { |
| 1927 | + name: "Given a map with carriage return, carriage return is replaced with new line", |
| 1928 | + args: args{ |
| 1929 | + data: map[string][]byte{ |
| 1930 | + "test": []byte("test\r\n"), |
| 1931 | + }, |
| 1932 | + logger: logr.FromContextOrDiscard(context.TODO()), |
| 1933 | + }, |
| 1934 | + want: map[string][]byte{ |
| 1935 | + "test": []byte("test\n"), |
| 1936 | + }, |
| 1937 | + }, |
| 1938 | + { |
| 1939 | + name: "Given secret data with carriage return, carriage return is replaced with new line", |
| 1940 | + args: args{ |
| 1941 | + data: secretDataWithCarriageReturnInSecret, |
| 1942 | + logger: logr.FromContextOrDiscard(context.TODO()), |
| 1943 | + }, |
| 1944 | + want: secretDataWithEqualInSecret, |
| 1945 | + }, |
| 1946 | + } |
| 1947 | + for _, tt := range tests { |
| 1948 | + t.Run(tt.name, func(t *testing.T) { |
| 1949 | + if got := replaceCarriageReturn(tt.args.data, tt.args.logger); !reflect.DeepEqual(got, tt.want) { |
| 1950 | + t.Errorf("replaceCarriageReturn() = %v, want %v", got, tt.want) |
| 1951 | + } |
| 1952 | + }) |
| 1953 | + } |
| 1954 | +} |
0 commit comments