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

test: add test for bare-metal deployer #125

Merged
merged 6 commits into from
Aug 23, 2023
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
4 changes: 4 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage:
status:
project: on
patch: on
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# gtctl

[![codecov](https://codecov.io/github/GreptimeTeam/gtctl/branch/main/graph/badge.svg?token=287NUSEH5D)](https://codecov.io/github/GreptimeTeam/gtctl)
[![codecov](https://codecov.io/github/GreptimeTeam/gtctl/branch/develop/graph/badge.svg?token=287NUSEH5D)](https://app.codecov.io/github/GreptimeTeam/gtctl/tree/develop)

## Overview

Expand Down
17 changes: 13 additions & 4 deletions pkg/deployer/baremetal/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ func NewDeployer(l logger.Logger, clusterName string, opts ...Option) (Interface
return nil, err
}

homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
if len(d.baseDir) == 0 {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
d.baseDir = path.Join(homeDir, config.GtctlDir)
}
d.baseDir = path.Join(homeDir, config.GtctlDir)

if err := fileutils.CreateDirIfNotExists(d.baseDir); err != nil {
return nil, err
}
Expand Down Expand Up @@ -148,6 +151,12 @@ func WithAlawaysDownload(alwaysDownload bool) Option {
}
}

func WithBaseDir(baseDir string) Option {
return func(d *Deployer) {
d.baseDir = baseDir
}
}

func (d *Deployer) GetGreptimeDBCluster(ctx context.Context, name string, options *GetGreptimeDBClusterOptions) (*GreptimeDBCluster, error) {
return nil, fmt.Errorf("unsupported operation")
}
Expand Down
108 changes: 108 additions & 0 deletions pkg/deployer/baremetal/deployer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2023 Greptime Team
//
// 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 baremetal

import (
"context"
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"

"github.com/GreptimeTeam/gtctl/pkg/deployer/baremetal/component"
"github.com/GreptimeTeam/gtctl/pkg/deployer/baremetal/config"
"github.com/GreptimeTeam/gtctl/pkg/logger"
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
)

var L = logger.New(os.Stdout, 1, logger.WithColored())

func TestNewDeployer(t *testing.T) {
homedir := "gtctl-test-new-deployer"
clusterName := "test"
defer func() {
err := fileutils.DeleteDirIfExists(homedir)
assert.NoError(t, err)
}()

// New Deployer with no options
deployer, err := NewDeployer(L, clusterName, WithBaseDir(homedir))
assert.NoError(t, err)
d1, ok := deployer.(*Deployer)
assert.True(t, ok)
assert.NotNil(t, d1)
assert.Equal(t, d1.baseDir, homedir)
assert.Equal(t, d1.clusterDir, path.Join(homedir, clusterName))
assert.Equal(t, d1.workingDirs, component.WorkingDirs{
DataDir: path.Join(homedir, clusterName, "data"),
LogsDir: path.Join(homedir, clusterName, "logs"),
PidsDir: path.Join(homedir, clusterName, "pids"),
})
assert.False(t, d1.alwaysDownload)

// New Deployer with always download option
deployer, err = NewDeployer(L, clusterName, WithAlawaysDownload(true))
zyy17 marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
d2, ok := deployer.(*Deployer)
assert.True(t, ok)
assert.NotNil(t, d2)
assert.True(t, d2.alwaysDownload)

// New Deployer with config option
newConfig := config.DefaultConfig()
newConfig.Cluster.Frontend.Replicas = 3
deployer, err = NewDeployer(L, clusterName, WithConfig(newConfig))
assert.NoError(t, err)
d3, ok := deployer.(*Deployer)
assert.True(t, ok)
assert.NotNil(t, d3)
assert.Equal(t, newConfig, d3.config)

// New Deployer with specific version
version := "foobar"
deployer, err = NewDeployer(L, clusterName, WithGreptimeVersion(version))
assert.NoError(t, err)
d4, ok := deployer.(*Deployer)
assert.True(t, ok)
assert.NotNil(t, d4)
assert.Equal(t, version, d4.config.Cluster.Artifact.Version)
}

func TestDeleteGreptimeClusterForeground(t *testing.T) {
homedir := "gtctl-test-foreground-delete"
clusterName := "test"
defer func() {
err := fileutils.DeleteDirIfExists(homedir)
assert.NoError(t, err)
}()

d, err := NewDeployer(L, clusterName, WithBaseDir(homedir))
assert.NoError(t, err)
deployer, ok := d.(*Deployer)
assert.True(t, ok)
assert.NotNil(t, deployer)

err = deployer.deleteGreptimeDBClusterForeground(context.Background(), component.DeleteOptions{})
assert.NoError(t, err)

info, err := os.Stat(deployer.clusterDir)
if os.IsExist(err) {
t.Errorf("cluster dir %s should not exist: %v", deployer.clusterDir, err)
}
if info != nil && !info.IsDir() {
t.Errorf("cluster dir %s is not a dir", deployer.clusterDir)
}
}
Loading