Skip to content

Commit

Permalink
create stress test
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
  • Loading branch information
gauravgahlot committed Dec 24, 2020
1 parent 97fb342 commit 7c64ae5
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 6 deletions.
71 changes: 66 additions & 5 deletions db/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@ import (
"fmt"
"io/ioutil"
"strings"
"sync"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/tinkerbell/tink/client/informers"
"github.com/tinkerbell/tink/db"
"github.com/tinkerbell/tink/pkg"
"github.com/tinkerbell/tink/protos/events"
"github.com/tinkerbell/tink/protos/hardware"
)

func TestEventsForHardware(t *testing.T) {
func TestCreateEventsForHardware(t *testing.T) {
tests := []struct {
// Name identifies the single test in a table test scenario
Name string
// Input is a list of hardwares that will be used to pre-populate the database
Input []*hardware.Hardware
// InputAsync if set to true inserts all the input concurrently
InputAsync bool
// Expectation is the function used to apply the assertions.
// You can use it to validate if the Input are created as you expect
Expectation func(*testing.T, []*hardware.Hardware, *db.TinkDB)
// ExpectedErr is used to check for error during
// CreateTemplate execution. If you expect a particular error
// createHardware execution. If you expect a particular error
// and you want to assert it, you can use this function
ExpectedErr func(*testing.T, error)
}{
Expand Down Expand Up @@ -61,11 +65,54 @@ func TestEventsForHardware(t *testing.T) {
}
},
},
{
Name: "create-stress-test",
InputAsync: true,
Input: func() []*hardware.Hardware {
input := []*hardware.Hardware{}
for ii := 0; ii < 10; ii++ {
hw := readHardwareData("./testdata/hardware.json")
hw.Id = uuid.New().String()
hw.Network.Interfaces[0].Dhcp.Mac = strings.Replace(hw.Network.Interfaces[0].Dhcp.Mac, "00", fmt.Sprintf("0%d", ii), 1)
}
return input
}(),
ExpectedErr: func(t *testing.T, err error) {
if err != nil {
t.Error(err)
}
},
Expectation: func(t *testing.T, input []*hardware.Hardware, tinkDB *db.TinkDB) {
err := tinkDB.Events(&events.WatchRequest{}, func(n informers.Notification) error {
event, err := n.ToEvent()
if err != nil {
return err
}

if event.EventType != events.EventType_EVENT_TYPE_CREATED {
return fmt.Errorf("unexpected event type: %s", event.EventType)
}

hw, err := getHardwareFromEventData(event)
if err != nil {
return err
}
if dif := cmp.Diff(input[0], hw, cmp.Comparer(hardwareComparer)); dif != "" {
t.Errorf(dif)
}
return nil
})
if err != nil {
t.Error(err)
}
},
},
}

ctx := context.Background()
for _, s := range tests {
t.Run(s.Name, func(t *testing.T) {
t.Parallel()
_, tinkDB, cl := NewPostgresDatabaseClient(t, ctx, NewPostgresDatabaseRequest{
ApplyMigration: true,
})
Expand All @@ -75,12 +122,26 @@ func TestEventsForHardware(t *testing.T) {
t.Error(err)
}
}()
var wg sync.WaitGroup
wg.Add(len(s.Input))
for _, hw := range s.Input {
err := createHardware(ctx, tinkDB, hw)
if err != nil {
s.ExpectedErr(t, err)
if s.InputAsync {
go func(ctx context.Context, tinkDB *db.TinkDB, hw *hardware.Hardware) {
defer wg.Done()
err := createHardware(ctx, tinkDB, hw)
if err != nil {
s.ExpectedErr(t, err)
}
}(ctx, tinkDB, hw)
} else {
wg.Done()
err := createHardware(ctx, tinkDB, hw)
if err != nil {
s.ExpectedErr(t, err)
}
}
}
wg.Wait()
s.Expectation(t, s.Input, tinkDB)
})
}
Expand Down
71 changes: 70 additions & 1 deletion db/testdata/hardware.json
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
{"metadata":{"state":"provisioning","manufacturer":{},"instance":{"operating_system_version":{"distro":"ubuntu","version":"18.04","os_slug":"ubuntu_18_04"},"crypted_root_password":"$6$xyz$/pdZy4hazXmqu1t0TACitLlKZPD4bFyRUw6ycXiOTdf4kcnkmpgmtg9zUpEE8rG9KtOWwX7kp1Gl96NCGbDk60","storage":{"disks":[{"device":"/dev/sda","wipe_table":true,"partitions":[{"label":"BIOS","number":1,"size":4096},{"label":"SWAP","number":2,"size":3993600},{"label":"ROOT","number":3,"size":15993600}]}],"filesystems":[{"mount":{"device":"/dev/sda3","format":"ext4","create":{"options":["-L","ROOT"]},"point":"/"}},{"mount":{"device":"/dev/sda2","format":"swap","create":{"options":["-L","SWAP"]},"point":"none"}}]}},"facility":{"plan_slug":"c2.medium.x86","facility_code":"onprem"}},"network":{"interfaces":[{"dhcp":{"mac":"08:00:27:00:00:01","hostname":"server001","lease_time":86400,"arch":"x86_64","ip":{"address":"192.168.1.5","netmask":"255.255.255.248","gateway":"192.168.1.1"}},"netboot":{"allow_pxe":true,"allow_workflow":true,"osie":{"kernel":"vmlinuz-x86_64"}}}]},"id":"0eba0bf8-3772-4b4a-ab9f-6ebe93b90a94"}
{
"metadata": {
"state": "provisioning",
"manufacturer": {},
"instance": {
"operating_system_version": {
"distro": "ubuntu",
"version": "18.04",
"os_slug": "ubuntu_18_04"
},
"crypted_root_password":
"$6$xyz$/pdZy4hazXmqu1t0TACitLlKZPD4bFyRUw6ycXiOTdf4kcnkmpgmtg9zUpEE8rG9KtOWwX7kp1Gl96NCGbDk60",
"storage": {
"disks": [
{
"device": "/dev/sda",
"wipe_table": true,
"partitions": [
{ "label": "BIOS", "number": 1, "size": 4096 },
{ "label": "SWAP", "number": 2, "size": 3993600 },
{ "label": "ROOT", "number": 3, "size": 15993600 }
]
}
],
"filesystems": [
{
"mount": {
"device": "/dev/sda3",
"format": "ext4",
"create": { "options": ["-L", "ROOT"] },
"point": "/"
}
},
{
"mount": {
"device": "/dev/sda2",
"format": "swap",
"create": { "options": ["-L", "SWAP"] },
"point": "none"
}
}
]
}
},
"facility": { "plan_slug": "c2.medium.x86", "facility_code": "onprem" }
},
"network": {
"interfaces": [
{
"dhcp": {
"mac": "08:00:27:00:00:01",
"hostname": "server001",
"lease_time": 86400,
"arch": "x86_64",
"ip": {
"address": "192.168.1.5",
"netmask": "255.255.255.248",
"gateway": "192.168.1.1"
}
},
"netboot": {
"allow_pxe": true,
"allow_workflow": true,
"osie": { "kernel": "vmlinuz-x86_64" }
}
}
]
},
"id": "0eba0bf8-3772-4b4a-ab9f-6ebe93b90a95"
}

0 comments on commit 7c64ae5

Please sign in to comment.