Skip to content

Commit

Permalink
refactor: move snapd client into runner
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsgruk committed Oct 31, 2024
1 parent d806c08 commit deec1c0
Show file tree
Hide file tree
Showing 24 changed files with 343 additions and 412 deletions.
8 changes: 4 additions & 4 deletions internal/concierge/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// Plan represents a set of packages and providers that are to be prepared/restored.
type Plan struct {
Providers []providers.Provider
Snaps []packages.SnapPackage
Snaps []*runner.Snap
Debs []*packages.Deb

config *config.Config
Expand All @@ -26,12 +26,12 @@ func NewPlan(config *config.Config, runner runner.CommandRunner) *Plan {
plan := &Plan{config: config, runner: runner}

for _, s := range append(config.Host.Snaps, config.Overrides.ExtraSnaps...) {
snap := packages.NewSnapFromString(s)
snap := runner.NewSnapFromString(s)

// Check if the channel has been overridden by a CLI argument/env var
channelOverride := getSnapChannelOverride(config, snap.Name())
channelOverride := getSnapChannelOverride(config, snap.Name)
if channelOverride != "" {
snap.SetChannel(channelOverride)
snap.Channel = channelOverride
}

plan.Snaps = append(plan.Snaps, snap)
Expand Down
4 changes: 2 additions & 2 deletions internal/concierge/plan_validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"testing"

"github.com/jnsgruk/concierge/internal/config"
"github.com/jnsgruk/concierge/internal/runnertest"
"github.com/jnsgruk/concierge/internal/runner"
)

func TestSingleK8sValidator(t *testing.T) {
runner := runnertest.NewMockRunner()
runner := runner.NewMockRunner()

twoK8s := &config.Config{}
twoK8s.Providers.K8s.Enable = true
Expand Down
8 changes: 4 additions & 4 deletions internal/juju/juju.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// NewJujuHandler constructs a new JujuHandler instance.
func NewJujuHandler(config *config.Config, runner runner.CommandRunner, providers []providers.Provider) *JujuHandler {
func NewJujuHandler(config *config.Config, r runner.CommandRunner, providers []providers.Provider) *JujuHandler {
var channel string
if config.Overrides.JujuChannel != "" {
channel = config.Overrides.JujuChannel
Expand All @@ -30,8 +30,8 @@ func NewJujuHandler(config *config.Config, runner runner.CommandRunner, provider
bootstrapConstraints: config.Juju.BootstrapConstraints,
modelDefaults: config.Juju.ModelDefaults,
providers: providers,
runner: runner,
snaps: []packages.SnapPackage{packages.NewSnap("juju", channel)},
runner: r,
snaps: []*runner.Snap{{Name: "juju", Channel: channel}},
}
}

Expand All @@ -42,7 +42,7 @@ type JujuHandler struct {
modelDefaults map[string]string
providers []providers.Provider
runner runner.CommandRunner
snaps []packages.SnapPackage
snaps []*runner.Snap
}

// Prepare bootstraps Juju on the configured providers.
Expand Down
30 changes: 6 additions & 24 deletions internal/juju/juju_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package juju

import (
"fmt"
"os"
"reflect"
"testing"

"github.com/jnsgruk/concierge/internal/config"
"github.com/jnsgruk/concierge/internal/packages"
"github.com/jnsgruk/concierge/internal/providers"
"github.com/jnsgruk/concierge/internal/runnertest"
"github.com/jnsgruk/concierge/internal/runner"
)

var fakeGoogleCreds = []byte(`auth-type: oauth2
Expand All @@ -22,12 +20,12 @@ private-key: |
project-id: concierge
`)

func setupHandlerWithPreset(preset string) (*runnertest.MockRunner, *JujuHandler, error) {
func setupHandlerWithPreset(preset string) (*runner.MockRunner, *JujuHandler, error) {
var err error
var cfg *config.Config
var provider providers.Provider

runner := runnertest.NewMockRunner()
runner := runner.NewMockRunner()
runner.MockCommandReturn("sudo -u test-user juju show-controller concierge-lxd", []byte("not found"), fmt.Errorf("Test error"))
runner.MockCommandReturn("sudo -u test-user juju show-controller concierge-microk8s", []byte("not found"), fmt.Errorf("Test error"))
runner.MockCommandReturn("sudo -u test-user juju show-controller concierge-k8s", []byte("not found"), fmt.Errorf("Test error"))
Expand All @@ -47,17 +45,17 @@ func setupHandlerWithPreset(preset string) (*runnertest.MockRunner, *JujuHandler
}

handler := NewJujuHandler(cfg, runner, []providers.Provider{provider})
handler.snaps = []packages.SnapPackage{runnertest.NewTestSnap("juju", "", false, false)}

return runner, handler, nil
}

func setupHandlerWithGoogleProvider() (*runnertest.MockRunner, *JujuHandler, error) {
func setupHandlerWithGoogleProvider() (*runner.MockRunner, *JujuHandler, error) {
cfg := &config.Config{}
cfg.Providers.Google.Enable = true
cfg.Providers.Google.Bootstrap = true
cfg.Providers.Google.CredentialsFile = "google.yaml"

runner := runnertest.NewMockRunner()
runner := runner.NewMockRunner()
runner.MockFile("google.yaml", fakeGoogleCreds)

provider := providers.NewProvider("google", runner, cfg)
Expand All @@ -68,15 +66,9 @@ func setupHandlerWithGoogleProvider() (*runnertest.MockRunner, *JujuHandler, err
}

handler := NewJujuHandler(cfg, runner, []providers.Provider{provider})
handler.snaps = []packages.SnapPackage{runnertest.NewTestSnap("juju", "", false, false)}
return runner, handler, nil
}
func TestJujuHandlerCommandsPresets(t *testing.T) {
// Prevent the path of the test machine interfering with the test results.
path := os.Getenv("PATH")
os.Setenv("PATH", "")
defer os.Setenv("PATH", path)

type test struct {
preset string
expectedCommands []string
Expand Down Expand Up @@ -171,11 +163,6 @@ func TestJujuHandlerWithCredentialedProvider(t *testing.T) {
}

func TestJujuRestoreNoKillController(t *testing.T) {
// Prevent the path of the test machine interfering with the test results.
path := os.Getenv("PATH")
os.Setenv("PATH", "")
defer os.Setenv("PATH", path)

runner, handler, err := setupHandlerWithPreset("machine")
if err != nil {
t.Fatal(err.Error())
Expand All @@ -196,11 +183,6 @@ func TestJujuRestoreNoKillController(t *testing.T) {
}

func TestJujuRestoreKillController(t *testing.T) {
// Prevent the path of the test machine interfering with the test results.
path := os.Getenv("PATH")
os.Setenv("PATH", "")
defer os.Setenv("PATH", path)

runner, handler, err := setupHandlerWithGoogleProvider()
if err != nil {
t.Fatal(err.Error())
Expand Down
11 changes: 0 additions & 11 deletions internal/packages/apt.go

This file was deleted.

10 changes: 10 additions & 0 deletions internal/packages/deb_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
"github.com/jnsgruk/concierge/internal/runner"
)

// NewDeb constructs a new Deb instance.
func NewDeb(name string) *Deb {
return &Deb{Name: name}
}

// Deb is a simple representation of a package installed from the Ubuntu archive.
type Deb struct {
Name string
}

// NewDebHandler constructs a new instance of a DebHandler.
func NewDebHandler(runner runner.CommandRunner, debs []*Deb) *DebHandler {
return &DebHandler{
Expand Down
10 changes: 2 additions & 8 deletions internal/packages/deb_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package packages

import (
"os"
"reflect"
"testing"

"github.com/jnsgruk/concierge/internal/runnertest"
"github.com/jnsgruk/concierge/internal/runner"
)

func TestDebHandlerCommands(t *testing.T) {
Expand All @@ -14,11 +13,6 @@ func TestDebHandlerCommands(t *testing.T) {
expected []string
}

// Prevent the path of the test machine interfering with the test results.
path := os.Getenv("PATH")
defer os.Setenv("PATH", path)
os.Setenv("PATH", "")

tests := []test{
{
func(d *DebHandler) { d.Prepare() },
Expand All @@ -44,7 +38,7 @@ func TestDebHandlerCommands(t *testing.T) {
}

for _, tc := range tests {
runner := runnertest.NewMockRunner()
runner := runner.NewMockRunner()
tc.testFunc(NewDebHandler(runner, debs))

if !reflect.DeepEqual(tc.expected, runner.ExecutedCommands) {
Expand Down
135 changes: 0 additions & 135 deletions internal/packages/snap.go

This file was deleted.

Loading

0 comments on commit deec1c0

Please sign in to comment.