@@ -5,13 +5,16 @@ import (
5
5
"testing"
6
6
7
7
"github.com/backube/pvc-transfer/transport"
8
+ "github.com/backube/pvc-transfer/transport/tls/certs"
8
9
logrtesting "github.com/go-logr/logr/testing"
9
10
corev1 "k8s.io/api/core/v1"
10
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
12
"k8s.io/apimachinery/pkg/types"
12
13
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
13
14
)
14
15
16
+ var certificateBundle , _ = certs .New ()
17
+
15
18
func Test_getExistingCert (t * testing.T ) {
16
19
tests := []struct {
17
20
name string
@@ -42,7 +45,7 @@ func Test_getExistingCert(t *testing.T) {
42
45
Namespace : "bar" ,
43
46
Labels : map [string ]string {"test" : "me" },
44
47
},
45
- Data : map [string ][]byte {"tls.crt" : [] byte ( `crt` )},
48
+ Data : map [string ][]byte {"tls.crt" : certificateBundle . ServerCrt . Bytes ( )},
46
49
},
47
50
},
48
51
},
@@ -59,15 +62,32 @@ func Test_getExistingCert(t *testing.T) {
59
62
Namespace : "bar" ,
60
63
Labels : map [string ]string {"test" : "me" },
61
64
},
62
- Data : map [string ][]byte {"tls.key" : []byte (`key` )},
65
+ Data : map [string ][]byte {"tls.key" : certificateBundle .ServerKey .Bytes ()},
66
+ },
67
+ },
68
+ },
69
+ {
70
+ name : "test with secret missing ca.crt" ,
71
+ namespacedName : types.NamespacedName {Namespace : "bar" , Name : "foo" },
72
+ labels : map [string ]string {"test" : "me" },
73
+ wantErr : true ,
74
+ wantFound : false ,
75
+ objects : []ctrlclient.Object {
76
+ & corev1.Secret {
77
+ ObjectMeta : metav1.ObjectMeta {
78
+ Name : "foo-stunnel-credentials" ,
79
+ Namespace : "bar" ,
80
+ Labels : map [string ]string {"test" : "me" },
81
+ },
82
+ Data : map [string ][]byte {"tls.key" : certificateBundle .ServerKey .Bytes (), "tls.crt" : certificateBundle .ServerKey .Bytes ()},
63
83
},
64
84
},
65
85
},
66
86
{
67
87
name : "test with valid secret" ,
68
88
namespacedName : types.NamespacedName {Namespace : "bar" , Name : "foo" },
69
89
labels : map [string ]string {"test" : "me" },
70
- wantErr : false ,
90
+ wantErr : true ,
71
91
wantFound : true ,
72
92
objects : []ctrlclient.Object {
73
93
& corev1.Secret {
@@ -76,7 +96,7 @@ func Test_getExistingCert(t *testing.T) {
76
96
Namespace : "bar" ,
77
97
Labels : map [string ]string {"test" : "me" },
78
98
},
79
- Data : map [string ][]byte {"tls.key" : [] byte ( `key` ), "tls.crt" : [] byte ( ` crt` )},
99
+ Data : map [string ][]byte {"tls.key" : certificateBundle . ServerKey . Bytes ( ), "tls.crt" : certificateBundle . ServerCrt . Bytes (), "ca. crt" : certificateBundle . CACrt . Bytes ( )},
80
100
},
81
101
},
82
102
},
@@ -92,7 +112,7 @@ func Test_getExistingCert(t *testing.T) {
92
112
},
93
113
}
94
114
ctx := context .WithValue (context .Background (), "test" , tt .name )
95
- key , crt , found , err := getExistingCert (ctx , fakeClientWithObjects (tt .objects ... ), s .logger , s .namespacedName , stunnelSecret )
115
+ found , err := isSecretValid (ctx , fakeClientWithObjects (tt .objects ... ), s .logger , s .namespacedName , stunnelSecret )
96
116
if err != nil {
97
117
t .Error ("found unexpected error" , err )
98
118
}
@@ -102,14 +122,6 @@ func Test_getExistingCert(t *testing.T) {
102
122
if tt .wantFound && ! found {
103
123
t .Error ("not found unexpected" )
104
124
}
105
-
106
- if tt .wantFound && found && key == nil {
107
- t .Error ("secret found but empty key, unexpected" )
108
- }
109
-
110
- if tt .wantFound && found && crt == nil {
111
- t .Error ("secret found but empty crt, unexpected" )
112
- }
113
125
})
114
126
}
115
127
}
0 commit comments