Skip to content

Commit c301a1d

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

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

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

Lines changed: 59 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,63 @@ 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("error getting service IP for the registry addon: error getting a service IP for a cluster: unexpected empty service Subnets"),
544+
},
545+
}
546+
for idx := range tests {
547+
tt := tests[idx]
548+
t.Run(tt.name, func(t *testing.T) {
549+
t.Parallel()
550+
got, err := containerdConfigFromRegistryAddon(tt.cluster)
551+
if tt.wantErr != nil {
552+
require.EqualError(t, err, tt.wantErr.Error())
553+
} else {
554+
require.NoError(t, err)
555+
}
556+
assert.Equal(t, tt.want, got)
557+
})
558+
}
559+
}
560+
502561
func Test_needContainerdConfiguration(t *testing.T) {
503562
t.Parallel()
504563
tests := []struct {

0 commit comments

Comments
 (0)