Skip to content

Commit

Permalink
add unit test for pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
pulak-opti committed Nov 24, 2023
1 parent 12773d1 commit 0ebd517
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 14 deletions.
42 changes: 28 additions & 14 deletions pkg/syncer/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,41 @@ type PubSub interface {

func NewPubSub(conf config.SyncConfig) (PubSub, error) {
if conf.Notification.Default == PubSubRedis {
host, ok := conf.Pubsub[PubSubRedis].(map[string]interface{})["host"].(string)
pubsubConf, found := conf.Pubsub[PubSubRedis]
if !found {
return nil, errors.New("pubsub redis config not found")
}

redisConf, ok := pubsubConf.(map[string]interface{})
if !ok {
return nil, errors.New("host is not valid")
return nil, errors.New("pubsub redis config not valid")
}

hostVal, found := redisConf["host"]
if !found {
return nil, errors.New("pubsub redis host not found")
}
password, ok := conf.Pubsub[PubSubRedis].(map[string]interface{})["password"].(string)
host, ok := hostVal.(string)
if !ok {
return nil, errors.New("password is not valid")
return nil, errors.New("pubsub redis host not valid, host must be string")
}
database, ok := conf.Pubsub[PubSubRedis].(map[string]interface{})["database"].(int)

passwordVal, found := redisConf["password"]
if !found {
return nil, errors.New("pubsub redis password not found")
}
password, ok := passwordVal.(string)
if !ok {
return nil, errors.New("database is not valid")
return nil, errors.New("pubsub redis password not valid, password must be string")
}

client := redis.NewClient(&redis.Options{
Addr: host,
Password: password,
DB: database,
})
defer client.Close()
if err := client.Ping(context.Background()).Err(); err != nil {
return nil, err
databaseVal, found := redisConf["database"]
if !found {
return nil, errors.New("pubsub redis database not found")
}
database, ok := databaseVal.(int)
if !ok {
return nil, errors.New("pubsub redis database not valid, database must be int")
}

return &pubsubRedis{
Expand Down
142 changes: 142 additions & 0 deletions pkg/syncer/pubsub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/****************************************************************************
* Copyright 2023 Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

// Package syncer provides synchronization across Agent nodes
package syncer

import (
"reflect"
"testing"

"github.com/optimizely/agent/config"
)

func TestNewPubSub(t *testing.T) {
type args struct {
conf config.SyncConfig
}
tests := []struct {
name string
args args
want PubSub
wantErr bool
}{
{
name: "Test with valid config",
args: args{
conf: config.SyncConfig{
Pubsub: map[string]interface{}{
"redis": map[string]interface{}{
"host": "localhost:6379",
"password": "",
"database": 0,
},
},
Notification: config.FeatureSyncConfig{
Default: "redis",
Enable: true,
},
},
},
want: &pubsubRedis{
host: "localhost:6379",
password: "",
database: 0,
},
wantErr: false,
},
{
name: "Test with invalid config",
args: args{
conf: config.SyncConfig{
Pubsub: map[string]interface{}{
"nopt-redis": map[string]interface{}{},
},
Notification: config.FeatureSyncConfig{
Default: "redis",
Enable: true,
},
},
},
want: nil,
wantErr: true,
},
{
name: "Test with nil config",
args: args{
conf: config.SyncConfig{
Pubsub: map[string]interface{}{
"redis": nil,
},
Notification: config.FeatureSyncConfig{
Default: "redis",
Enable: true,
},
},
},
want: nil,
wantErr: true,
},
{
name: "Test with empty config",
args: args{
conf: config.SyncConfig{
Pubsub: map[string]interface{}{
"redis": nil,
},
Notification: config.FeatureSyncConfig{
Default: "redis",
Enable: true,
},
},
},
want: nil,
wantErr: true,
},
{
name: "Test with invalid redis config",
args: args{
conf: config.SyncConfig{
Pubsub: map[string]interface{}{
"redis": map[string]interface{}{
"host": 123,
"password": "",
"database": "invalid-db",
},
},
Notification: config.FeatureSyncConfig{
Default: "redis",
Enable: true,
},
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewPubSub(tt.args.conf)
if (err != nil) != tt.wantErr {
t.Errorf("NewPubSub() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewPubSub() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 0ebd517

Please sign in to comment.