Skip to content

Commit 0eb39bf

Browse files
committed
fixup! tests: add more unit tests
1 parent e8b1a8d commit 0eb39bf

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

pkg/handlers/generic/mutation/mirrors/inject_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package mirrors
55

66
import (
7+
"fmt"
78
"testing"
89

910
. "github.com/onsi/ginkgo/v2"
1011
"github.com/onsi/gomega"
1112
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1214
corev1 "k8s.io/api/core/v1"
1315
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1416
"k8s.io/apimachinery/pkg/runtime"
@@ -499,6 +501,67 @@ func newMirrorSecretWithoutCA(name, namespace string) *corev1.Secret {
499501
}
500502
}
501503

504+
func Test_containerdConfigFromRegistryAddon(t *testing.T) {
505+
t.Parallel()
506+
tests := []struct {
507+
name string
508+
cluster *clusterv1.Cluster
509+
want containerdConfig
510+
wantErr error
511+
}{
512+
{
513+
name: "valid input",
514+
cluster: &clusterv1.Cluster{
515+
ObjectMeta: metav1.ObjectMeta{
516+
Name: request.ClusterName,
517+
Namespace: request.Namespace,
518+
},
519+
Spec: clusterv1.ClusterSpec{
520+
ClusterNetwork: &clusterv1.ClusterNetwork{
521+
Services: &clusterv1.NetworkRanges{
522+
CIDRBlocks: []string{"192.168.0.1/16"},
523+
},
524+
},
525+
},
526+
},
527+
want: containerdConfig{
528+
URL: "http://192.168.0.20",
529+
Mirror: true,
530+
},
531+
},
532+
{
533+
name: "missing Services CIDR",
534+
cluster: &clusterv1.Cluster{
535+
ObjectMeta: metav1.ObjectMeta{
536+
Name: request.ClusterName,
537+
Namespace: request.Namespace,
538+
},
539+
Spec: clusterv1.ClusterSpec{
540+
ClusterNetwork: &clusterv1.ClusterNetwork{},
541+
},
542+
},
543+
wantErr: fmt.Errorf(
544+
"error getting service IP for the registry addon: " +
545+
"error getting a service IP for a cluster: " +
546+
"unexpected empty service Subnets",
547+
),
548+
},
549+
}
550+
for idx := range tests {
551+
tt := tests[idx]
552+
t.Run(tt.name, func(t *testing.T) {
553+
t.Parallel()
554+
got, err := containerdConfigFromRegistryAddon(tt.cluster)
555+
if tt.wantErr != nil {
556+
require.EqualError(t, err, tt.wantErr.Error())
557+
} else {
558+
require.NoError(t, err)
559+
}
560+
assert.Equal(t, tt.want, got)
561+
})
562+
}
563+
}
564+
502565
func Test_needContainerdConfiguration(t *testing.T) {
503566
t.Parallel()
504567
tests := []struct {

0 commit comments

Comments
 (0)