From bdb087578a0b667eae670a94b1e0e3cf635be7e9 Mon Sep 17 00:00:00 2001 From: mihaelabalutoiu Date: Mon, 12 Sep 2022 17:45:34 +0300 Subject: [PATCH 1/2] Add `controller.go` unit tests --- database/sql/controller_test.go | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 database/sql/controller_test.go diff --git a/database/sql/controller_test.go b/database/sql/controller_test.go new file mode 100644 index 00000000..3542dc82 --- /dev/null +++ b/database/sql/controller_test.go @@ -0,0 +1,95 @@ +// Copyright 2022 Cloudbase Solutions SRL +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +package sql + +import ( + "context" + "fmt" + "garm/config" + dbCommon "garm/database/common" + runnerErrors "garm/errors" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/suite" +) + +var ( + encryptionPassphrase = "bocyasicgatEtenOubwonIbsudNutDom" +) + +func getTestSqliteDBConfig(t *testing.T) config.Database { + dir, err := os.MkdirTemp("", "garm-config-test") + if err != nil { + t.Fatalf("failed to create temporary directory: %s", err) + } + t.Cleanup(func() { os.RemoveAll(dir) }) + + return config.Database{ + Debug: false, + DbBackend: config.SQLiteBackend, + Passphrase: encryptionPassphrase, + SQLite: config.SQLite{ + DBFile: filepath.Join(dir, "garm.db"), + }, + } +} + +type CtrlTestSuite struct { + suite.Suite + Store dbCommon.Store +} + +func (s *CtrlTestSuite) SetupTest() { + db, err := NewSQLDatabase(context.Background(), getTestSqliteDBConfig(s.T())) + if err != nil { + s.FailNow(fmt.Sprintf("failed to create db connection: %s", err)) + } + s.Store = db +} + +func (s *CtrlTestSuite) TestControllerInfo() { + initCtrlInfo, err := s.Store.InitController() + if err != nil { + s.FailNow(fmt.Sprintf("cannot init controller: %v", err)) + } + + ctrlInfo, err := s.Store.ControllerInfo() + + s.Require().Nil(err) + s.Require().Equal(initCtrlInfo.ControllerID, ctrlInfo.ControllerID) +} + +func (s *CtrlTestSuite) TestControllerInfoErrNotFound() { + _, err := s.Store.ControllerInfo() + + s.Require().Regexp("fetching controller info: not found", err.Error()) +} + +func (s *CtrlTestSuite) TestInitControllerAlreadyInitialized() { + _, err := s.Store.InitController() + if err != nil { + s.FailNow(fmt.Sprintf("cannot init controller: %v", err)) + } + + _, err = s.Store.InitController() + + s.Require().Regexp(runnerErrors.NewConflictError("controller already initialized"), err) +} + +func TestCtrlTestSuite(t *testing.T) { + suite.Run(t, new(CtrlTestSuite)) +} From 6fe6680e52eb166b1d310154907b7f7cd6d92e72 Mon Sep 17 00:00:00 2001 From: mihaelabalutoiu Date: Tue, 13 Sep 2022 15:01:05 +0300 Subject: [PATCH 2/2] Fix `database.go` typo --- database/database.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/database/database.go b/database/database.go index 39d57d10..e74364e2 100644 --- a/database/database.go +++ b/database/database.go @@ -28,7 +28,6 @@ func NewDatabase(ctx context.Context, cfg config.Database) (common.Store, error) case config.MySQLBackend, config.SQLiteBackend: return sql.NewSQLDatabase(ctx, cfg) default: - return nil, fmt.Errorf("no team manager backend available for db backend %s", dbBackend) + return nil, fmt.Errorf("db backend not available: %s", dbBackend) } - }