@@ -4,110 +4,152 @@ import (
44 "context"
55 "strings"
66 "testing"
7+
8+ "github.com/stretchr/testify/assert"
79)
810
911var secretsClient * SecretsClient
10-
11- // helpers
12- func testErr (err error , t * testing.T ) {
13- if err != nil {
14- t .Fatal (err .Error ())
15- }
12+ var expectedSecretName = "test-secret"
13+ var defaultNamespace = "default"
14+ var err error
15+
16+ // setupTestClient initializes a new secretsClient before each test
17+ func setupTestClient (namespace string ) {
18+ mockConfig := MockClientConfig ()
19+ secretsClient , err = MockNewSecretsClient (mockConfig , namespace )
1620}
1721
18- // unit tests
1922func TestNewSecretsClient (t * testing.T ) {
20- secretsClient = MockNewSecretsClient ()
23+ setupTestClient (defaultNamespace )
24+
25+ assert .NoError (t , err )
26+ assert .Equal (t , defaultNamespace , secretsClient .Namespace )
27+ assert .Equal (t , "testuser" , secretsClient .AuthInfo )
28+ assert .NotNil (t , secretsClient .secretInterface )
29+ }
30+
31+ func TestNewSecretsClientUndefinedNamespace (t * testing.T ) {
32+ // undefined namespace, expected to fail
33+ setupTestClient ("" )
34+ assert .Error (t , err )
2135}
2236
2337func TestCreate (t * testing.T ) {
38+ setupTestClient (defaultNamespace )
2439 ctx := context .Background ()
25- _ , err := secretsClient .Create (ctx , "test-secret" )
26- testErr (err , t )
40+
41+ secret , err := secretsClient .Create (ctx , expectedSecretName )
42+ assert .NoError (t , err , "Creating secret should not return an error" )
43+
44+ assert .NotNil (t , secret , "Created secret should not be nil" )
45+ assert .Equal (t , secret .Name , expectedSecretName )
2746}
2847
2948func TestList (t * testing.T ) {
49+ setupTestClient (defaultNamespace )
3050 ctx := context .Background ()
31- secrets , err := secretsClient .List (ctx )
32- testErr (err , t )
3351
34- if secrets .Items [0 ].Name != "test-secret" {
35- t .Fatal (err .Error ())
36- }
52+ _ , err := secretsClient .Create (ctx , expectedSecretName )
53+ assert .NoError (t , err , "Creating secret should not return an error" )
54+
55+ secrets , err := secretsClient .List (ctx )
56+ assert .NoError (t , err , "Listing secrets should not return an error" )
57+ assert .NotEmpty (t , secrets .Items , "Secrets list should not be empty" )
58+ assert .Equal (t , expectedSecretName , secrets .Items [0 ].Name , "First secret name should be %s" , expectedSecretName )
3759}
3860
3961func TestCreateWithData (t * testing.T ) {
62+ setupTestClient (defaultNamespace )
63+ ctx := context .Background ()
64+
4065 dataArgs := []string {
4166 "key=value" ,
4267 "secret-key=secret-value" ,
4368 "ENV_VAR=~!@#$%^&*()_+" ,
4469 "DB_URL=mongodb://host1.example.com:27017,host2.example.com:27017/prod?replicaSet=prod" ,
4570 }
71+
4672 data := make (map [string ][]byte )
4773
4874 for _ , item := range dataArgs {
4975 split := strings .SplitN (item , "=" , 2 )
50- if len (split ) != 2 {
51- t .Errorf ("Data is not formatted correctly: %s" , item )
52- }
76+ assert .Len (t , split , 2 , "Data is not formatted correctly" )
5377 data [split [0 ]] = []byte (split [1 ])
5478 }
5579
56- ctx := context .Background ()
57- _ , err := secretsClient .CreateWithData (ctx , "test-secret-with-data" , data )
58- testErr (err , t )
80+ secret , err := secretsClient .CreateWithData (ctx , expectedSecretName , data )
81+ assert .NoError (t , err , "Creating secret with data should not return an error" )
82+
83+ assert .NotNil (t , secret .Data , "Created secret with data should not be nil" )
84+ assert .Equal (t , data , secret .Data )
5985}
6086
6187func TestGet (t * testing.T ) {
88+ setupTestClient (defaultNamespace )
6289 ctx := context .Background ()
63- secret , err := secretsClient .Get (ctx , "test-secret-with-data" )
64- testErr (err , t )
6590
66- if string (secret .Data ["DB_URL" ]) != "mongodb://host1.example.com:27017,host2.example.com:27017/prod?replicaSet=prod" {
67- t .Fatal (err .Error ())
68- }
91+ _ , err := secretsClient .CreateWithData (ctx , expectedSecretName , map [string ][]byte {
92+ "DB_URL" : []byte ("mongodb://host1.example.com:27017,host2.example.com:27017/prod?replicaSet=prod" ),
93+ })
94+ assert .NoError (t , err , "Creating secret with data should not return an error" )
95+
96+ secret , err := secretsClient .Get (ctx , expectedSecretName )
97+ assert .NoError (t , err , "Getting secret should not return an error" )
98+ assert .Equal (t , "mongodb://host1.example.com:27017,host2.example.com:27017/prod?replicaSet=prod" , string (secret .Data ["DB_URL" ]), "DB_URL should match the expected value" )
6999}
70100
71101func TestGetKey (t * testing.T ) {
102+ setupTestClient (defaultNamespace )
72103 ctx := context .Background ()
73- value , err := secretsClient .GetKey (ctx , "test-secret-with-data" , "secret-key" )
74- testErr (err , t )
75104
76- if value != "secret-value" {
77- t .Fatal (err .Error ())
78- }
105+ _ , err := secretsClient .CreateWithData (ctx , expectedSecretName , map [string ][]byte {
106+ "secret-key" : []byte ("secret-value" ),
107+ })
108+ assert .NoError (t , err , "Creating secret with data should not return an error" )
79109
80- value , err = secretsClient .GetKey (ctx , "test-secret-with_data" , "thiskeydoesnotexist" )
81- if err == nil {
82- t .Fatal ("non-existent key should have received an error" )
83- }
110+ value , err := secretsClient .GetKey (ctx , expectedSecretName , "secret-key" )
111+ assert .NoError (t , err , "Getting secret key should not return an error" )
112+ assert .Equal (t , "secret-value" , value , "Secret key value should match the expected value" )
113+
114+ value , err = secretsClient .GetKey (ctx , expectedSecretName , "thiskeydoesnotexist" )
115+ assert .Error (t , err , "Getting a non-existent key should return an error" )
116+ assert .Empty (t , value , "Non-existent key should return an empty value" )
84117}
85118
86119func TestUpdate (t * testing.T ) {
120+ setupTestClient (defaultNamespace )
87121 ctx := context .Background ()
88- secret , err := secretsClient .Get (ctx , "test-secret-with-data" )
89- testErr (err , t )
122+
123+ secret , err := secretsClient .CreateWithData (ctx , expectedSecretName , map [string ][]byte {
124+ "key" : []byte ("oldvalue" ),
125+ })
126+ assert .NoError (t , err , "Creating secret with data should not return an error" )
90127
91128 data := map [string ][]byte {
92129 "key" : []byte ("newvalue" ),
93130 }
94131
95- secretsClient .Update (ctx , secret , data )
96-
97- if string (secret .Data ["key" ]) != "newvalue" {
98- t .Fatal (err .Error ())
99- }
132+ secret , err = secretsClient .Update (ctx , secret , data )
133+ assert .NoError (t , err , "Updating secret should not return an error" )
134+ assert .Equal (t , "newvalue" , string (secret .Data ["key" ]), "Key value should be updated to 'newvalue'" )
100135}
101136
102137func TestUpsert (t * testing.T ) {
138+ setupTestClient (defaultNamespace )
139+ ctx := context .Background ()
140+
103141 data := map [string ][]byte {
104142 "key" : []byte ("upsert" ),
105143 }
106- ctx := context .Background ()
107- secret , err := secretsClient .Upsert (ctx , "upsert-secret" , data )
108- testErr (err , t )
109144
110- if string (secret .Data ["key" ]) != "upsert" {
111- t .Fatal (err .Error ())
112- }
145+ // First upsert (should create the secret)
146+ secret , err := secretsClient .Upsert (ctx , expectedSecretName , data )
147+ assert .NoError (t , err , "Upserting (creating) secret should not return an error" )
148+ assert .Equal (t , "upsert" , string (secret .Data ["key" ]), "Key value should be 'upsert'" )
149+
150+ // Second upsert (should update the secret)
151+ data ["key" ] = []byte ("upserted" )
152+ secret , err = secretsClient .Upsert (ctx , expectedSecretName , data )
153+ assert .NoError (t , err , "Upserting (updating) secret should not return an error" )
154+ assert .Equal (t , "upserted" , string (secret .Data ["key" ]), "Key value should be 'upserted'" )
113155}
0 commit comments