Skip to content
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

FS-1234; Expand DB Schema to allow storing Config Sets #43

Merged
merged 17 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
go-version: '1.22.1'

- name: Install cockroach binary
run: curl https://binaries.cockroachdb.com/cockroach-v23.1.11.linux-amd64.tgz | tar -xz && sudo cp -i cockroach-v23.1.11.linux-amd64/cockroach /usr/local/bin/
run: curl https://binaries.cockroachdb.com/cockroach-v21.1.21.linux-amd64.tgz | tar -xz && sudo cp -i cockroach-v21.1.21.linux-amd64/cockroach /usr/local/bin/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting the CRDB error because the deprecated "crdb drop" command is no longer deprecated, and is now gone. The SQLBoiler version we use still uses drop.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like us to roll forward with versions and not use older ones in testing.

When you say SQLBoiler, I guess you're referring to the CrDB driver, specifically this https://github.com/metal-toolbox/sqlboiler-crdb/blob/18c5774bdcce987f22ef5ea1b542c2e390faae78/driver/override/templates_test/singleton/crdb_main_test.go.tpl#L56

Can you take a look if that can be updated - as a separate issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a ticket for it. FS-1400


- name: Start test database
run: cockroach start-single-node --insecure --background
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.19
FROM golang:1.22-alpine3.19

# Copy the binary that goreleaser built
COPY fleetdb /fleetdb
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19 as builder
FROM golang:1.22 as builder

# Create and change to the app directory.
WORKDIR /app
Expand Down
37 changes: 30 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ test: | unit-test integration-test
## run integration tests
integration-test: test-database
@echo Running integration tests...
@FLEETDB_CRDB_URI="${TEST_DB}" go test -cover -tags testtools,integration -p 1 -timeout 1m ./...
@FLEETDB_CRDB_URI="${TEST_DB}" go test -cover -tags testtools,integration -p 1 -timeout 1m ./... | \
grep -v "could not be registered in Prometheus\" error=\"duplicate metrics collector registration attempted\"" # TODO; Figure out why this message spams when tests fail
joelrebel marked this conversation as resolved.
Show resolved Hide resolved

## run lint and unit tests
unit-test: | lint
## run unit tests
unit-test: | test-database
@echo Running unit tests...
@FLEETDB_CRDB_URI="${TEST_DB}" go test -cover -short -tags testtools ./...

## run single integration test. Example: make single-test test=TestIntegrationServerListComponents
single-test:
@FLEETDB_CRDB_URI="${TEST_DB}" go test -timeout 30s -tags testtools -run ^${test}$$ github.com/metal-toolbox/fleetdb/pkg/api/v1 -v

## check test coverage
coverage: | test-database
@echo Generating coverage report...
Expand All @@ -29,19 +34,21 @@ coverage: | test-database
@go tool cover -html=coverage.out

## lint
lint: golint

golint: | vendor
lint: | vendor
@echo Linting Go files...
@golangci-lint run

## clean docker files
clean: docker-clean
clean: docker-clean test-clean
@echo Cleaning...
@rm -rf ./dist/
@rm -rf coverage.out

## clean test env
test-clean:
@go clean -testcache

## download/tidy go modules
vendor:
@go mod download
@go mod tidy
Expand Down Expand Up @@ -71,6 +78,22 @@ test-database: | vendor
@FLEETDB_CRDB_URI="${TEST_DB}" go run main.go migrate up
@cockroach sql --insecure -e "use fleetdb_test; ALTER TABLE attributes DROP CONSTRAINT check_server_id_server_component_id; ALTER TABLE versioned_attributes DROP CONSTRAINT check_server_id_server_component_id;"

## purge dev environment, build new image, and run tests
fresh-test: clean
joelrebel marked this conversation as resolved.
Show resolved Hide resolved
@make push-image-devel
joelrebel marked this conversation as resolved.
Show resolved Hide resolved
@make docker-up
@make test

## boil sql
boil:
make docker-up
make test-database
sqlboiler crdb --add-soft-deletes

## log into database
psql:
@psql -d "${TEST_DB}"

## Build linux bin
build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${PROJECT_NAME}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The `quickstart.yml` compose file will run server service from released images a
docker-compose -f quickstart.yml -f quickstart-dev.yml up --build
```

NOTE: `--build` is required to get docker-compose to rebuild the container if you have changes. You make also include the `quickstart-tracing.yml` file if you wish to have tracing support.
NOTE: `--build` is required to get docker-compose to rebuild the container if you have changes. You may also include the `quickstart-tracing.yml` file if you wish to have tracing support.


### Adding/Changing database schema
Expand Down
43 changes: 43 additions & 0 deletions db/migrations/00002_config_sets.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- +goose Up
-- +goose StatementBegin

CREATE TABLE public.bios_config_sets (
id UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
name STRING UNIQUE NOT NULL,
version STRING NOT NULL,
created_at TIMESTAMPTZ NULL,
updated_at TIMESTAMPTZ NULL
);

CREATE TABLE public.bios_config_components (
id UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
fk_bios_config_set_id UUID NOT NULL REFERENCES public.bios_config_sets(id) ON DELETE CASCADE,
name STRING NOT NULL,
vendor STRING NOT NULL,
model STRING NOT NULL,
created_at TIMESTAMPTZ NULL,
updated_at TIMESTAMPTZ NULL,
UNIQUE (fk_bios_config_set_id, name)
);

CREATE TABLE public.bios_config_settings (
id UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
fk_bios_config_component_id UUID NOT NULL REFERENCES public.bios_config_components(id) ON DELETE CASCADE,
settings_key STRING NOT NULL,
settings_value STRING NOT NULL,
raw JSONB NULL,
created_at TIMESTAMPTZ NULL,
updated_at TIMESTAMPTZ NULL,
UNIQUE (fk_bios_config_component_id, settings_key)
);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin

DROP TABLE public.bios_config_settings;
DROP TABLE public.bios_config_components;
DROP TABLE public.bios_config_sets;

-- +goose StatementEnd
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (

require (
github.com/bmc-toolbox/common v0.0.0-20231204194243-7bcbccab7116
github.com/hetiansu5/urlquery v1.2.7
github.com/metal-toolbox/rivets v1.0.3
github.com/volatiletech/sqlboiler v3.7.1+incompatible
go.hollow.sh/toolbox v0.6.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/
github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4=
github.com/hashicorp/serf v0.9.7/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4=
github.com/hetiansu5/urlquery v1.2.7 h1:jn0h+9pIRqUziSPnRdK/gJK8S5TCnk+HZZx5fRHf8K0=
github.com/hetiansu5/urlquery v1.2.7/go.mod h1:wFpZdTHRdwt7mk0EM/DdZEWtEN4xf8HJoH/BLXm/PG0=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down
86 changes: 86 additions & 0 deletions internal/dbtools/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ var (

// Inventory fixtures
FixtureInventoryServer *models.Server

FixtureBiosConfigSet *models.BiosConfigSet
FixtureBiosConfigComponents []*models.BiosConfigComponent
FixtureBiosConfigSettings [][]*models.BiosConfigSetting
)

func addFixtures(t *testing.T) error {
Expand Down Expand Up @@ -151,6 +155,10 @@ func addFixtures(t *testing.T) error {
return err
}

if err := setupConfigSet(ctx, testDB); err != nil {
return err
}

// excluding Chuckles here since that server is deleted
FixtureServers = models.ServerSlice{FixtureNemo, FixtureDory, FixtureMarlin}
FixtureDeletedServers = models.ServerSlice{FixtureChuckles}
Expand Down Expand Up @@ -649,3 +657,81 @@ func setupInventoryFixture(ctx context.Context, db *sqlx.DB) error {

return nil
}

func setupConfigSet(ctx context.Context, db *sqlx.DB) error {
settings := [][]*models.BiosConfigSetting{
{
{
SettingsKey: "BootOrder",
SettingsValue: "dev2,dev3,dev4",
},
{
SettingsKey: "Mode",
SettingsValue: "UEFI",
},
},
{
{
SettingsKey: "PXEEnable",
SettingsValue: "true",
Raw: null.NewJSON([]byte(`{}`), true),
},
{
SettingsKey: "SRIOVEnable",
SettingsValue: "false",
},
{
SettingsKey: "position",
SettingsValue: "1",
Raw: null.NewJSON([]byte(`{ "lanes": 8 }`), true),
},
},
}

components := []*models.BiosConfigComponent{
{
Name: "Fixture Test SM Motherboard",
Vendor: "SUPERMICRO",
Model: "ATX",
},
{
Name: "Fixture Test Intel Network Adapter",
Vendor: "Intel",
Model: "PCIE",
},
}

configSet := models.BiosConfigSet{
Name: "Fixture Test Config Set",
Version: "version",
}

err := configSet.Insert(ctx, db, boil.Infer())
if err != nil {
return err
}

for c := range components {
components[c].FKBiosConfigSetID = configSet.ID

err = configSet.AddFKBiosConfigSetBiosConfigComponents(ctx, db, true, components[c])
if err != nil {
return err
}

for s := range settings[c] {
settings[c][s].FKBiosConfigComponentID = components[c].ID
}

err = components[c].AddFKBiosConfigComponentBiosConfigSettings(ctx, db, true, settings[c]...)
if err != nil {
return err
}
}

FixtureBiosConfigSet = &configSet
FixtureBiosConfigComponents = components
FixtureBiosConfigSettings = settings

return nil
}
5 changes: 5 additions & 0 deletions internal/dbtools/testtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func cleanDB(t *testing.T) {
if _, err := models.Servers(qm.WithDeleted()).DeleteAll(ctx, boil.GetContextDB(), true); err != nil {
t.Error(errors.Wrap(err, "table: model.Servers"))
}

deleteFixture(ctx, t, models.AttributesFirmwareSets())
deleteFixture(ctx, t, models.ComponentFirmwareSets())
deleteFixture(ctx, t, models.ComponentFirmwareSetMaps())
Expand All @@ -122,6 +123,10 @@ func cleanDB(t *testing.T) {
deleteFixture(ctx, t, models.BMCMacAddresses())
deleteFixture(ctx, t, models.BomInfos())

deleteFixture(ctx, t, models.BiosConfigSettings())
deleteFixture(ctx, t, models.BiosConfigComponents())
deleteFixture(ctx, t, models.BiosConfigSets())

testDB.Exec("SET sql_safe_updates = true;")
}

Expand Down
Loading
Loading