-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setup integration test suite for db package #387
Merged
mergify
merged 6 commits into
tinkerbell:master
from
gianarb:chore/bootstrap-integration-test-for-dbpkg
Dec 16, 2020
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ef24d5
Bootstrap db pkg integration test with testcontainers
4c92915
Run integration test in CI
524190f
Added test parity with #361 for templates
a2a4eb4
Move tests to db pkg and skip if docker is not running
46d156f
Test MustParse and MustParseFromFile
b780f41
Merge branch 'master' into chore/bootstrap-integration-test-for-dbpkg
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/packethost/pkg/log" | ||
"github.com/pkg/errors" | ||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
"github.com/tinkerbell/tink/db" | ||
) | ||
|
||
type NewPostgresDatabaseRequest struct { | ||
ApplyMigration bool | ||
} | ||
|
||
// NewPostgresDatabaseClient returns a SQL client ready to be used. Behind the | ||
// scene it is starting a Docker container that will get cleaned up when the | ||
// test is over. Tests using this function are safe to run in parallel | ||
func NewPostgresDatabaseClient(t *testing.T, ctx context.Context, req NewPostgresDatabaseRequest) (*sql.DB, *db.TinkDB, func() error) { | ||
postgresC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ | ||
ContainerRequest: testcontainers.ContainerRequest{ | ||
Image: "postgres:13.1", | ||
ExposedPorts: []string{"5432/tcp"}, | ||
WaitingFor: wait.ForLog("database system is ready to accept connections"), | ||
Env: map[string]string{ | ||
"POSTGRES_PASSWORD": "tinkerbell", | ||
"POSTGRES_USER": "tinkerbell", | ||
"POSTGRES_DB": "tinkerbell", | ||
}, | ||
}, | ||
Started: true, | ||
}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
port, err := postgresC.MappedPort(ctx, "5432") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
dbCon, err := sql.Open( | ||
"postgres", | ||
fmt.Sprintf( | ||
"host=localhost port=%d user=%s password=%s dbname=%s sslmode=disable", | ||
port.Int(), | ||
"tinkerbell", | ||
"tinkerbell", | ||
"tinkerbell")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
CHECK_DB: | ||
for ii := 0; ii < 5; ii++ { | ||
err = dbCon.Ping() | ||
if err != nil { | ||
t.Log(errors.Wrap(err, "db check")) | ||
time.Sleep(1 * time.Second) | ||
goto CHECK_DB | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this kind of doesn't make any sense right? If not ready go back to before the loop forever. If it is ready... make sure its ready 5 times? |
||
} | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tinkDB := db.Connect(dbCon, log.Test(t, "db-test")) | ||
if req.ApplyMigration { | ||
n, err := tinkDB.Migrate() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
t.Log(fmt.Sprintf("applied %d migrations", n)) | ||
} | ||
return dbCon, tinkDB, func() error { | ||
return postgresC.Terminate(ctx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/protobuf/ptypes/timestamp" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/uuid" | ||
_ "github.com/lib/pq" | ||
"github.com/tinkerbell/tink/db" | ||
"github.com/tinkerbell/tink/workflow" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
func TestCreateTemplate(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
table := []struct { | ||
// Name identifies the single test in a table test scenario | ||
Name string | ||
// Input is a list of workflows that will be used to pre-populate the database | ||
Input []*workflow.Workflow | ||
// 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, []*workflow.Workflow, *db.TinkDB) | ||
// ExpectedErr is used to check for error during | ||
// CreateTemplate execution. If you expect a particular error | ||
// and you want to assert it, you can use this function | ||
ExpectedErr func(*testing.T, error) | ||
}{ | ||
{ | ||
Name: "happy-path-single-crete-template", | ||
Input: []*workflow.Workflow{ | ||
func() *workflow.Workflow { | ||
w := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") | ||
w.ID = "545f7ce9-5313-49c6-8704-0ed98814f1f7" | ||
return w | ||
}(), | ||
}, | ||
Expectation: func(t *testing.T, input []*workflow.Workflow, tinkDB *db.TinkDB) { | ||
wID, wName, wData, err := tinkDB.GetTemplate(context.Background(), map[string]string{"id": input[0].ID}, false) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
w := workflow.MustParse([]byte(wData)) | ||
w.ID = wID | ||
w.Name = wName | ||
if dif := cmp.Diff(input[0], w); dif != "" { | ||
t.Errorf(dif) | ||
} | ||
}, | ||
}, | ||
{ | ||
Name: "create-two-template-same-name", | ||
Input: []*workflow.Workflow{ | ||
func() *workflow.Workflow { | ||
w := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") | ||
w.ID = "545f7ce9-5313-49c6-8704-0ed98814f1f7" | ||
return w | ||
}(), | ||
func() *workflow.Workflow { | ||
w := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") | ||
w.ID = "aaaaaaaa-5313-49c6-8704-bbbbbbbbbbbb" | ||
return w | ||
}(), | ||
}, | ||
ExpectedErr: func(t *testing.T, err error) { | ||
if err == nil { | ||
t.Error("expected error, got nil") | ||
} | ||
if !strings.Contains(err.Error(), "pq: duplicate key value violates unique constraint \"uidx_template_name\"") { | ||
t.Errorf("\nexpected err: %s\ngot: %s", "pq: duplicate key value violates unique constraint \"uidx_template_name\"", err) | ||
} | ||
}, | ||
}, | ||
{ | ||
Name: "update-on-create", | ||
Input: []*workflow.Workflow{ | ||
func() *workflow.Workflow { | ||
w := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") | ||
w.ID = "545f7ce9-5313-49c6-8704-0ed98814f1f7" | ||
return w | ||
}(), | ||
func() *workflow.Workflow { | ||
w := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") | ||
w.ID = "545f7ce9-5313-49c6-8704-0ed98814f1f7" | ||
w.Name = "updated-name" | ||
return w | ||
}(), | ||
}, | ||
Expectation: func(t *testing.T, input []*workflow.Workflow, tinkDB *db.TinkDB) { | ||
_, wName, _, err := tinkDB.GetTemplate(context.Background(), map[string]string{"id": input[0].ID}, false) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if wName != "updated-name" { | ||
t.Errorf("expected name to be \"%s\", got \"%s\"", "updated-name", wName) | ||
} | ||
}, | ||
}, | ||
{ | ||
Name: "create-stress-test", | ||
InputAsync: true, | ||
Input: func() []*workflow.Workflow { | ||
input := []*workflow.Workflow{} | ||
for ii := 0; ii < 20; ii++ { | ||
w := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") | ||
w.ID = uuid.New().String() | ||
w.Name = fmt.Sprintf("id_%d", rand.Int()) | ||
t.Log(w.Name) | ||
input = append(input, w) | ||
} | ||
return input | ||
}(), | ||
ExpectedErr: func(t *testing.T, err error) { | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}, | ||
Expectation: func(t *testing.T, input []*workflow.Workflow, tinkDB *db.TinkDB) { | ||
count := 0 | ||
err := tinkDB.ListTemplates("%", func(id, n string, in, del *timestamp.Timestamp) error { | ||
count = count + 1 | ||
return nil | ||
}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if len(input) != count { | ||
t.Errorf("expected %d templates stored in the database but we got %d", len(input), count) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, s := range table { | ||
t.Run(s.Name, func(t *testing.T) { | ||
t.Parallel() | ||
_, tinkDB, cl := NewPostgresDatabaseClient(t, ctx, NewPostgresDatabaseRequest{ | ||
ApplyMigration: true, | ||
}) | ||
defer cl() | ||
var wg sync.WaitGroup | ||
wg.Add(len(s.Input)) | ||
for _, tt := range s.Input { | ||
if s.InputAsync { | ||
go func(ctx context.Context, tinkDB *db.TinkDB, tt *workflow.Workflow) { | ||
defer wg.Done() | ||
err := createTemplateFromWorkflowType(ctx, tinkDB, tt) | ||
if err != nil { | ||
s.ExpectedErr(t, err) | ||
} | ||
}(ctx, tinkDB, tt) | ||
} else { | ||
wg.Done() | ||
err := createTemplateFromWorkflowType(ctx, tinkDB, tt) | ||
if err != nil { | ||
s.ExpectedErr(t, err) | ||
} | ||
} | ||
} | ||
wg.Wait() | ||
s.Expectation(t, s.Input, tinkDB) | ||
}) | ||
} | ||
} | ||
|
||
func TestCreateTemplate_TwoTemplateWithSameNameButFirstOneIsDeleted(t *testing.T) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
_, tinkDB, cl := NewPostgresDatabaseClient(t, ctx, NewPostgresDatabaseRequest{ | ||
ApplyMigration: true, | ||
}) | ||
defer cl() | ||
w := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") | ||
w.ID = "545f7ce9-5313-49c6-8704-0ed98814f1f7" | ||
err := createTemplateFromWorkflowType(ctx, tinkDB, w) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
err = tinkDB.DeleteTemplate(ctx, w.ID) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
ww := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") | ||
ww.ID = "1111aaaa-5313-49c6-8704-222222aaaaaa" | ||
err = createTemplateFromWorkflowType(ctx, tinkDB, ww) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func createTemplateFromWorkflowType(ctx context.Context, tinkDB *db.TinkDB, tt *workflow.Workflow) error { | ||
uID := uuid.MustParse(tt.ID) | ||
content, err := yaml.Marshal(tt) | ||
if err != nil { | ||
return err | ||
} | ||
err = tinkDB.CreateTemplate(ctx, tt.Name, string(content), uID) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: '0.1' | ||
name: packet_osie_provision | ||
global_timeout: 600 | ||
tasks: | ||
- name: "run_one_worker" | ||
worker: "{{.device_1}}" | ||
environment: | ||
MIRROR_HOST: 192.168.1.2 | ||
actions: | ||
- name: "server_partitioning" | ||
image: update-data | ||
timeout: 60 | ||
environment: | ||
NGINX_HOST: 192.168.1.2 | ||
- name: "update_db" | ||
image: update-data | ||
timeout: 50 | ||
environment: | ||
MIRROR_HOST: 192.168.1.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should add support to wait for something like wait.Exec and then we can just call
pg_isready
.