forked from argoproj-labs/argocd-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit-tests for argoutil/log.go, argoutil/tls.go, argoutil/volume.go
Signed-off-by: Julia Teslia <[email protected]>
- Loading branch information
Julia Teslia
committed
Apr 7, 2024
1 parent
81c9e17
commit 9c52579
Showing
3 changed files
with
301 additions
and
0 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,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) | ||
} | ||
}) | ||
} | ||
} |
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,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)) | ||
} |
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,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) | ||
}) | ||
} | ||
} |