Skip to content

Commit

Permalink
feat: add test for controller (#415)
Browse files Browse the repository at this point in the history
- Move datasource case to main file
- Add test for controller

Signed-off-by: jcriadomarco <jcriadomarco@vmware.com>
  • Loading branch information
javiercri authored Aug 9, 2023
1 parent 82d2021 commit 663aafb
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 25 deletions.
31 changes: 11 additions & 20 deletions config-reloader/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package controller

import (
"context"
"time"

"github.com/vmware/kube-fluentd-operator/config-reloader/config"
"github.com/vmware/kube-fluentd-operator/config-reloader/datasource"
Expand All @@ -18,6 +17,7 @@ import (
type Controller interface {
Run(ctx context.Context, stop <-chan struct{})
RunOnce(ctx context.Context) error
GetTotalConfigNS() int
}

type controllerInstance struct {
Expand All @@ -32,32 +32,19 @@ type controllerInstance struct {
var _ Controller = &controllerInstance{}

// New creates new controller
func New(ctx context.Context, cfg *config.Config) (Controller, error) {
var ds datasource.Datasource
var up Updater
var err error
func New(ctx context.Context, cfg *config.Config, ds datasource.Datasource, up Updater) (Controller, error) {

var reloader *fluentd.Reloader
gen := generator.New(ctx, cfg)
gen.SetStatusUpdater(ctx, ds)

switch cfg.Datasource {
case "fake":
ds = datasource.NewFakeDatasource(ctx)
up = NewFixedTimeUpdater(ctx, cfg.IntervalSeconds)
case "fs":
ds = datasource.NewFileSystemDatasource(ctx, cfg.FsDatasourceDir, cfg.OutputDir)
up = NewFixedTimeUpdater(ctx, cfg.IntervalSeconds)
case "fake", "fs":
logrus.Infof("Setting reloader to null because is running locally")
default:
updateChan := make(chan time.Time, 1)
ds, err = datasource.NewKubernetesInformerDatasource(ctx, cfg, updateChan)
if err != nil {
return nil, err
}
reloader = fluentd.NewReloader(ctx, cfg.FluentdRPCPort)
up = NewOnDemandUpdater(ctx, updateChan)
}

gen := generator.New(ctx, cfg)
gen.SetStatusUpdater(ctx, ds)

return &controllerInstance{
Updater: up,
Reloader: reloader,
Expand Down Expand Up @@ -133,3 +120,7 @@ func (c *controllerInstance) Run(ctx context.Context, stop <-chan struct{}) {
}
}
}

func (c *controllerInstance) GetTotalConfigNS() int {
return c.numTotalConfigNS
}
64 changes: 64 additions & 0 deletions config-reloader/controller/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package controller

import (
"context"
"os"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/vmware/kube-fluentd-operator/config-reloader/config"
"github.com/vmware/kube-fluentd-operator/config-reloader/datasource"
)

// UnitTest for RunOnce
func TestRunOnceController(t *testing.T) {
// 1. Create new controller
// 2. RunOnce controller
// 3. Create a new namespace in the folder
// 4. runOnce controller
assert := assert.New(t)
config := config.Config{
Datasource: "fs",
FsDatasourceDir: "../examples",
TemplatesDir: "../templates",
ID: "default",
OutputDir: "../tmp",
LogLevel: "debug",
}
expectedResult := 3
// Prepare TestCase
ctx := context.Background()
ds := datasource.NewFileSystemDatasource(ctx, config.FsDatasourceDir, config.OutputDir)

// Create controller
up := NewFixedTimeUpdater(ctx, config.IntervalSeconds)
// 1. Create new controller
ctrl, err := New(ctx, &config, ds, up)
if err != nil {
logrus.Fatalf(err.Error())
}

// 2. RunOnce controller
err = ctrl.RunOnce(ctx)
if err != nil {
logrus.Fatalf(err.Error())
}
assert.Equal(expectedResult, ctrl.GetTotalConfigNS())

// 3. Create a new namespace in the folder
newNamespaceFile := config.FsDatasourceDir + "/new-namespace.conf"
configData := []byte("<match **>\n@type stdout\n</match>")
defer os.Remove(newNamespaceFile)
err = os.WriteFile(newNamespaceFile, configData, 0644)
if err != nil {
logrus.Fatalf(err.Error())
}

// 4. RunOnce controller
err = ctrl.RunOnce(ctx)
if err != nil {
logrus.Fatalf(err.Error())
}
assert.Equal(expectedResult+1, ctrl.GetTotalConfigNS())
}
3 changes: 1 addition & 2 deletions config-reloader/datasource/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package datasource
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -41,7 +40,7 @@ func (d *fsDatasource) GetNamespaces(ctx context.Context) ([]*NamespaceConfig, e
for _, f := range files {
base := filepath.Base(f)
ns := base[0 : len(base)-5]
contents, err := ioutil.ReadFile(f)
contents, err := os.ReadFile(f)
if err != nil {
logrus.Infof("Cannot read file %s: %+v", f, err)
continue
Expand Down
4 changes: 2 additions & 2 deletions config-reloader/fluentd/reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package fluentd
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -38,7 +38,7 @@ func (r *Reloader) ReloadConfiguration() {
logrus.Errorf("fluentd config.gracefulReload request failed: %+v", err)
} else if resp.StatusCode != 200 {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
logrus.Errorf("fluentd config.gracefulReload endpoint returned statuscode %v; response: %v", resp.StatusCode, string(body))
}
}
25 changes: 24 additions & 1 deletion config-reloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/vmware/kube-fluentd-operator/config-reloader/config"
"github.com/vmware/kube-fluentd-operator/config-reloader/controller"
"github.com/vmware/kube-fluentd-operator/config-reloader/datasource"
"github.com/vmware/kube-fluentd-operator/config-reloader/fluentd"
"github.com/vmware/kube-fluentd-operator/config-reloader/metrics"

Expand Down Expand Up @@ -43,7 +44,29 @@ func main() {

logrus.SetLevel(cfg.GetLogLevel())

ctrl, err := controller.New(ctx, cfg)
// Create datasource and updater base in config
var ds datasource.Datasource
var up controller.Updater

var err error

switch cfg.Datasource {
case "fake":
ds = datasource.NewFakeDatasource(ctx)
up = controller.NewFixedTimeUpdater(ctx, cfg.IntervalSeconds)
case "fs":
ds = datasource.NewFileSystemDatasource(ctx, cfg.FsDatasourceDir, cfg.OutputDir)
up = controller.NewFixedTimeUpdater(ctx, cfg.IntervalSeconds)
default:
updateChan := make(chan time.Time, 1)
ds, err = datasource.NewKubernetesInformerDatasource(ctx, cfg, updateChan)
if err != nil {
logrus.Fatalf("Cannot start informer %+v", err)
}
up = controller.NewOnDemandUpdater(ctx, updateChan)
}

ctrl, err := controller.New(ctx, cfg, ds, up)
if err != nil {
logrus.Fatalf("Cannot start control loop %+v", err)
}
Expand Down
1 change: 1 addition & 0 deletions config-reloader/validate-logging-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ for a in $@; do
fi

p="$(realpath "$a")"
echo -e "Validating $p"
docker run --entrypoint=/bin/validate-from-dir.sh \
--net=host \
--rm \
Expand Down

0 comments on commit 663aafb

Please sign in to comment.