Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid coupling tests with hardcoded test CA certs #6086

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pkg/util/membercluster_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package util

import (
"bytes"
"context"
"encoding/pem"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -458,6 +460,13 @@ func TestNewClusterClientSet_ClientWorks(t *testing.T) {
}))
defer s.Close()

testCA := new(bytes.Buffer)
err := pem.Encode(testCA, &pem.Block{
Type: "CERTIFICATE",
Bytes: s.Certificate().Raw,
})
assert.NoError(t, err)

const clusterName = "test"
hostClient := fakeclient.NewClientBuilder().WithScheme(gclient.NewSchema()).WithObjects(
&clusterv1alpha1.Cluster{
Expand All @@ -469,7 +478,7 @@ func TestNewClusterClientSet_ClientWorks(t *testing.T) {
},
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Namespace: "ns1", Name: "secret1"},
Data: map[string][]byte{clusterv1alpha1.SecretTokenKey: []byte("token"), clusterv1alpha1.SecretCADataKey: testCA},
Data: map[string][]byte{clusterv1alpha1.SecretTokenKey: []byte("token"), clusterv1alpha1.SecretCADataKey: testCA.Bytes()},
}).Build()

clusterClient, err := NewClusterClientSet(clusterName, hostClient, nil)
Expand Down Expand Up @@ -666,6 +675,13 @@ func TestNewClusterDynamicClientSet_ClientWorks(t *testing.T) {
}))
defer s.Close()

testCA := new(bytes.Buffer)
err := pem.Encode(testCA, &pem.Block{
Type: "CERTIFICATE",
Bytes: s.Certificate().Raw,
})
assert.NoError(t, err)

const clusterName = "test"
hostClient := fakeclient.NewClientBuilder().WithScheme(gclient.NewSchema()).WithObjects(
&clusterv1alpha1.Cluster{
Expand All @@ -677,7 +693,7 @@ func TestNewClusterDynamicClientSet_ClientWorks(t *testing.T) {
},
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Namespace: "ns1", Name: "secret1"},
Data: map[string][]byte{clusterv1alpha1.SecretTokenKey: []byte("token"), clusterv1alpha1.SecretCADataKey: testCA},
Data: map[string][]byte{clusterv1alpha1.SecretTokenKey: []byte("token"), clusterv1alpha1.SecretCADataKey: testCA.Bytes()},
}).Build()

clusterClient, err := NewClusterDynamicClientSet(clusterName, hostClient)
Expand Down