From 9c52579d09e44b18c61d0553e0599b2a923eca14 Mon Sep 17 00:00:00 2001 From: Julia Teslia Date: Sun, 7 Apr 2024 20:53:19 +0200 Subject: [PATCH] Add unit-tests for argoutil/log.go, argoutil/tls.go, argoutil/volume.go Signed-off-by: Julia Teslia --- pkg/argoutil/log_test.go | 73 ++++++++++++++++++ pkg/argoutil/tls_test.go | 145 ++++++++++++++++++++++++++++++++++++ pkg/argoutil/volume_test.go | 83 +++++++++++++++++++++ 3 files changed, 301 insertions(+) create mode 100644 pkg/argoutil/log_test.go create mode 100644 pkg/argoutil/tls_test.go create mode 100644 pkg/argoutil/volume_test.go diff --git a/pkg/argoutil/log_test.go b/pkg/argoutil/log_test.go new file mode 100644 index 000000000..6756d247a --- /dev/null +++ b/pkg/argoutil/log_test.go @@ -0,0 +1,73 @@ +package argoutil + +import ( + "testing" + + "github.com/argoproj-labs/argocd-operator/common" +) + +func TestGetLogLevel(t *testing.T) { + tests := []struct { + name string + expected string + }{ + { + "error", + "error", + }, + { + "warn", + "warn", + }, + { + "info", + "info", + }, + { + "debug", + "debug", + }, + { + "default", + common.ArgoCDDefaultLogLevel, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetLogLevel(tt.name) + if got != tt.expected { + t.Errorf("GetLogLevel() = %v, want %v", got, tt.expected) + } + }) + } +} + +func TestGetLogFormat(t *testing.T) { + tests := []struct { + name string + expected string + }{ + { + "text", + "text", + }, + { + "json", + "json", + }, + { + "default", + common.ArgoCDDefaultLogFormat, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetLogFormat(tt.name) + if got != tt.expected { + t.Errorf("GetLogLevel() = %v, want %v", got, tt.expected) + } + }) + } +} diff --git a/pkg/argoutil/tls_test.go b/pkg/argoutil/tls_test.go new file mode 100644 index 000000000..9faf67701 --- /dev/null +++ b/pkg/argoutil/tls_test.go @@ -0,0 +1,145 @@ +// Copyright 2019 ArgoCD Operator Developers +// +// 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 argoutil + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "crypto/x509/pkix" + "encoding/pem" + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + "math/big" + "testing" + "time" +) + +func TestNewPrivateKey(t *testing.T) { + key, err := NewPrivateKey() + assert.NoError(t, err) + assert.NotNil(t, key) +} + +func TestEncodePrivateKeyPEM(t *testing.T) { + rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048) + pemKey := EncodePrivateKeyPEM(rsaKey) + block, _ := pem.Decode(pemKey) + assert.NotNil(t, block) + assert.Equal(t, "RSA PRIVATE KEY", block.Type) +} + +func TestEncodeCertificatePEM(t *testing.T) { + cert := &x509.Certificate{ + SerialNumber: big.NewInt(1), + Subject: pkix.Name{ + CommonName: "test-certificate.com", + Organization: []string{"Test Org"}, + }, + NotBefore: time.Now(), + NotAfter: time.Now().AddDate(0, 0, 1), + } + pemCert := EncodeCertificatePEM(cert) + block, _ := pem.Decode(pemCert) + assert.NotNil(t, block) + assert.Equal(t, "CERTIFICATE", block.Type) +} + +/*func TestParsePEMEncodedCert(t *testing.T) { + cert := &x509.Certificate{ + SerialNumber: big.NewInt(1), + Subject: pkix.Name{ + CommonName: "test-cert.com", + Organization: []string{"Test Org"}, + }, + NotBefore: time.Now(), + NotAfter: time.Now().AddDate(1, 0, 0), + } + pemCert := EncodeCertificatePEM(cert) + parsedCert, err := ParsePEMEncodedCert(pemCert) + assert.NoError(t, err) + assert.NotNil(t, parsedCert) + assert.Equal(t, pemCert, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: parsedCert.Raw})) +}*/ + +func TestParsePEMEncodedPrivateKey(t *testing.T) { + rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048) + pemKey := EncodePrivateKeyPEM(rsaKey) + parsedKey, err := ParsePEMEncodedPrivateKey(pemKey) + assert.NoError(t, err) + assert.NotNil(t, parsedKey) + assert.Equal(t, pemKey, pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(parsedKey)})) +} + +func TestNewCACertAndKey(t *testing.T) { + instName := "test-instance" + cert, key, err := NewCACertAndKey(instName) + + assert.NoError(t, err) + assert.NotNil(t, cert) + assert.NotNil(t, key) + assert.IsType(t, &x509.Certificate{}, cert) + assert.IsType(t, &rsa.PrivateKey{}, key) + assert.Equal(t, "argocd-operator@test-instance", cert.Subject.CommonName) +} + +func TestNewSelfSignedCACertificate(t *testing.T) { + name := "test" + key, _ := rsa.GenerateKey(rand.Reader, 2048) + cert, err := NewSelfSignedCACertificate(name, key) + + assert.NoError(t, err) + assert.NotNil(t, cert) + assert.IsType(t, &x509.Certificate{}, cert) + assert.Equal(t, "argocd-operator@test", cert.Subject.CommonName) +} + +func TestNewTLSCertAndKey(t *testing.T) { + secName := "test-secret" + instName := "test-instance" + namespace := "test-namespace" + c := &x509.Certificate{} + k, _ := rsa.GenerateKey(rand.Reader, 2048) + cert, key, err := NewTLSCertAndKey(secName, instName, namespace, c, k) + + assert.NoError(t, err) + assert.NotNil(t, cert) + assert.NotNil(t, key) + assert.IsType(t, &x509.Certificate{}, cert) + assert.IsType(t, &rsa.PrivateKey{}, key) + assert.Equal(t, "test-secret", cert.Subject.CommonName) +} + +func TestHasArgoTLSChanged(t *testing.T) { + actual := &corev1.Secret{ + Data: map[string][]byte{ + "tls.crt": []byte("actual-cert"), + "tls.key": []byte("actual-key"), + }, + } + expected := &corev1.Secret{ + Data: map[string][]byte{ + "tls.crt": []byte("expected-cert"), + "tls.key": []byte("expected-key"), + }, + } + + assert.True(t, HasArgoTLSChanged(actual, expected)) + + actual.Data["tls.crt"] = []byte("expected-cert") + actual.Data["tls.key"] = []byte("expected-key") + + assert.False(t, HasArgoTLSChanged(actual, expected)) +} diff --git a/pkg/argoutil/volume_test.go b/pkg/argoutil/volume_test.go new file mode 100644 index 000000000..419403ad5 --- /dev/null +++ b/pkg/argoutil/volume_test.go @@ -0,0 +1,83 @@ +package argoutil + +import ( + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "testing" +) + +func TestNewPVCResourceRequirements(t *testing.T) { + tests := []struct { + name string + capacity resource.Quantity + expected resource.Quantity + }{ + { + "Capacity 512 Mi", + resource.MustParse("512Mi"), + resource.MustParse("512Mi"), + }, + { + "Capacity 1 Gi", + resource.MustParse("1Gi"), + resource.MustParse("1Gi"), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewPVCResourceRequirements(tt.capacity) + assert.Equal(t, corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + "storage": tt.expected, + }, + }, got) + }) + } +} + +func TestNewPersistentVolumeClaim(t *testing.T) { + meta := metav1.ObjectMeta{ + Name: "test-pvc", + Namespace: "test-namespace", + } + + expectedLabels := map[string]string{"app.kubernetes.io/managed-by": "test-pvc", "app.kubernetes.io/name": "test-pvc", "app.kubernetes.io/part-of": "argocd"} + pvc := NewPersistentVolumeClaim(meta) + assert.Equal(t, "test-pvc", pvc.Name) + assert.Equal(t, "test-namespace", pvc.Namespace) + assert.Equal(t, expectedLabels, pvc.Labels) +} + +func TestNewPersistentVolumeClaimWithName(t *testing.T) { + meta := metav1.ObjectMeta{ + Name: "test-pvc", + Namespace: "test-namespace", + } + + tests := []struct { + name string + expected map[string]string + }{ + { + "test-pvc-1", + map[string]string{"app.kubernetes.io/managed-by": "test-pvc", "app.kubernetes.io/name": "test-pvc-1", "app.kubernetes.io/part-of": "argocd"}, + }, + { + "test-pvc-2", + map[string]string{"app.kubernetes.io/managed-by": "test-pvc", "app.kubernetes.io/name": "test-pvc-2", "app.kubernetes.io/part-of": "argocd"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + pvc := NewPersistentVolumeClaimWithName(tt.name, meta) + + assert.Equal(t, tt.name, pvc.Name) + assert.Equal(t, "test-namespace", pvc.Namespace) + assert.Equal(t, tt.expected, pvc.Labels) + }) + } +}