Skip to content

Commit

Permalink
feat(tests): added test
Browse files Browse the repository at this point in the history
  • Loading branch information
StanGirard committed Apr 9, 2023
1 parent 0a35d93 commit 19f46bb
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 3 deletions.
71 changes: 71 additions & 0 deletions plugins/commons/checConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commons

import (
"testing"
"time"
)

func TestCheck_AddResult(t *testing.T) {
Expand Down Expand Up @@ -177,3 +178,73 @@ func TestCheck_InitCheck(t *testing.T) {
})
}
}

func TestCheckConfig_Init2(t *testing.T) {
cfg := &Config{}
checkCfg := &CheckConfig{}
checkCfg.Init(cfg)

if checkCfg.Wg == nil {
t.Error("CheckConfig Init failed to initialize WaitGroup")
}

if checkCfg.Queue == nil {
t.Error("CheckConfig Init failed to initialize Queue")
}

if checkCfg.ConfigYatas != cfg {
t.Error("CheckConfig Init failed to assign ConfigYatas")
}
}

func TestCheck_EndCheck2(t *testing.T) {
check := Check{
Name: "testName",
Description: "testDescription",
Status: "OK",
Id: "testID",
StartTime: time.Now(),
}

time.Sleep(100 * time.Millisecond)
check.EndCheck()

if check.EndTime.Before(check.StartTime) {
t.Error("EndTime is not after StartTime")
}

if check.Duration < 100*time.Millisecond {
t.Error("Duration is incorrect")
}
}

func TestCheck_InitCheckAndUpdateStatus2(t *testing.T) {
check := Check{}
check.InitCheck("testName", "testDescription", "testID", []string{"TestCategory"})

if check.Name != "testName" {
t.Errorf("Name should be testName, got %s", check.Name)
}

if check.Description != "testDescription" {
t.Errorf("Description should be testDescription, got %s", check.Description)
}

if check.Status != "OK" {
t.Errorf("Status should be OK, got %s", check.Status)
}

if check.Id != "testID" {
t.Errorf("Id should be testID, got %s", check.Id)
}

check.AddResult(Result{
Message: "Test message",
Status: "FAIL",
ResourceID: "testResourceID",
})

if check.Status != "FAIL" {
t.Errorf("Status should be FAIL after adding a failing result, got %s", check.Status)
}
}
5 changes: 2 additions & 3 deletions plugins/commons/commons.go → plugins/commons/pluginRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"net/rpc"

"github.com/hashicorp/go-plugin"
"github.com/padok-team/yatas/plugins/logger"
)

func (g *YatasRPC) Run(c *Config) []Tests {
var resp []Tests
err := g.client.Call("Plugin.Run", c, &resp)
if err != nil {
// You usually want your interfaces to return errors. If they don't,
// there isn't much other choice here.
panic(err)
logger.Error(err.Error())
}

return resp
Expand Down
84 changes: 84 additions & 0 deletions plugins/commons/pluginRPC_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package commons

import (
"bytes"
"net/rpc"
"testing"
)

type mockYatasImpl struct{}

func (m *mockYatasImpl) Run(c *Config) []Tests {
return []Tests{
{
Account: "testAccount",
Checks: []Check{},
},
}
}

type MockRPCConn struct {
server *YatasRPCServer
buffer bytes.Buffer
}

func (m *MockRPCConn) Read(p []byte) (n int, err error) {
return m.buffer.Read(p)
}

func (m *MockRPCConn) Write(p []byte) (n int, err error) {
return m.buffer.Write(p)
}

func (m *MockRPCConn) Close() error {
return nil
}

func TestYatasRPCServer_Run(t *testing.T) {
impl := &mockYatasImpl{}
server := &YatasRPCServer{Impl: impl}
cfg := &Config{}
var resp []Tests

err := server.Run(cfg, &resp)
if err != nil {
t.Errorf("Error in YatasRPCServer Run: %v", err)
}

if len(resp) != 1 {
t.Errorf("Expected 1 test result, got %d", len(resp))
}

if resp[0].Account != "testAccount" {
t.Errorf("Expected account name 'testAccount', got '%s'", resp[0].Account)
}
}

func TestYatasPlugin_Server(t *testing.T) {
impl := &mockYatasImpl{}
plugin := &YatasPlugin{Impl: impl}
server, err := plugin.Server(nil)

if err != nil {
t.Errorf("Error in YatasPlugin Server: %v", err)
}

_, ok := server.(*YatasRPCServer)
if !ok {
t.Error("YatasPlugin Server did not return a YatasRPCServer")
}
}

func TestYatasPlugin_Client(t *testing.T) {
plugin := &YatasPlugin{}
client, err := plugin.Client(nil, rpc.NewClient(&MockRPCConn{}))

if err != nil {
t.Errorf("Error in YatasPlugin Client: %v", err)
}

_, ok := client.(*YatasRPC)
if !ok {
t.Error("YatasPlugin Client did not return a YatasRPC")
}
}
File renamed without changes.
52 changes: 52 additions & 0 deletions plugins/commons/yatas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,55 @@ func TestCheckConfig_Init(t *testing.T) {
})
}
}

func TestCheckTest(t *testing.T) {
var wg sync.WaitGroup
config := &Config{
Plugins: []Plugin{
{
Name: "test",
Include: []string{"test"},
Exclude: []string{},
},
},
}

id := "test"

testFunc := func(a, b, c int) {
wg.Done()
}

wrappedTest := CheckTest(&wg, config, id, testFunc)

wrappedTest(1, 2, 3)
wg.Wait()

config.Plugins[0].Exclude = []string{"test"}

wrappedTestExcluded := CheckTest(&wg, config, id, testFunc)

wrappedTestExcluded(1, 2, 3)
}

func TestCheckMacroTest(t *testing.T) {
var wg sync.WaitGroup
config := &Config{
Plugins: []Plugin{
{
Name: "test",
Include: []string{},
Exclude: []string{},
},
},
}

testFunc := func(a, b, c, d int) {
wg.Done()
}

wrappedTest := CheckMacroTest(&wg, config, testFunc)

wrappedTest(1, 2, 3, 4)
wg.Wait()
}

0 comments on commit 19f46bb

Please sign in to comment.