Skip to content

Commit

Permalink
Added a mock driver
Browse files Browse the repository at this point in the history
  • Loading branch information
rajch committed Dec 22, 2021
1 parent 9e2ab79 commit 82cbf8b
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 0 deletions.
49 changes: 49 additions & 0 deletions drivercore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package drivercore_test

import (
"testing"

"github.com/kuttiproject/drivercore"
"github.com/kuttiproject/drivercore/drivercoretest"
"github.com/kuttiproject/drivercore/drivercoretest/drivermock"
)

const TESTK8SVERSION = "1.22"

func init() {
mockdriver1 := drivermock.New("mock1", "Mock driver with NAT Networking", true)
drivercore.RegisterDriver("mock1", mockdriver1)

mockdriver1.UpdateRemoteImage(TESTK8SVERSION, false)
}

func TestDriverCore(t *testing.T) {
cnt := drivercore.DriverCount()
if cnt < 1 {
t.Fatal("no drivers found")
}

rd := drivercore.RegisteredDrivers()
if len(rd) < 1 {
t.Fatal("no drivers found")
}

drivercore.ForEachDriver(func(d drivercore.Driver) bool {
if d.Status() != "Ready" {
t.Fatalf("Driver status was %v instead of Ready", d.Status())
}
return true
})

ok := drivercore.RegisteredDriver("mock1")
if !ok {
t.Fatal("Driver mock1 not registered")
}

_, ok = drivercore.GetDriver("mock1")
if !ok {
t.Fatal("Driver mock1 not found")
}

drivercoretest.TestDriver(t, "mock1", TESTK8SVERSION)
}
3 changes: 3 additions & 0 deletions drivercoretest/drivermock/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package drivermock implements a mock driver. It should be used
// only in tests.
package drivermock
77 changes: 77 additions & 0 deletions drivercoretest/drivermock/driver-image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package drivermock

import (
"errors"

"github.com/kuttiproject/drivercore"
)

var (
localimages = map[string]*Image{}
remoteimages = map[string]*Image{}
)

// UpdateRemoteImage adds or updates a mock remote image.
func (vd *Driver) UpdateRemoteImage(k8sversion string, deprecated bool) {
remoteimages[k8sversion] = &Image{
k8sversion: k8sversion,
status: drivercore.ImageStatusNotDownloaded,
deprecated: deprecated,
}
}

// DeleteRemoteImage deletes a mock remote image.
func (vd *Driver) DeleteRemoteImage(k8sversion string) {
delete(remoteimages, k8sversion)
}

// UpdateImageList updates the image list
func (vd *Driver) UpdateImageList() error {
localimages = make(map[string]*Image, len(remoteimages))
for key, value := range remoteimages {
localimages[key] = value
}

return nil
}

// ValidK8sVersion returns true if the specified k8s version
// is available in localimages, false otherwise.
func (vd *Driver) ValidK8sVersion(k8sversion string) bool {
_, ok := localimages[k8sversion]
return ok
}

// K8sVersions lists locally available k8s versions.
func (vd *Driver) K8sVersions() []string {
result := make([]string, len(localimages))
index := 0
for _, value := range localimages {
result[index] = value.k8sversion
index++
}
return result
}

// ListImages returns the local images.
func (vd *Driver) ListImages() ([]drivercore.Image, error) {
result := make([]drivercore.Image, len(localimages))
index := 0
for _, value := range localimages {
result[index] = value
index++
}

return result, nil
}

// GetImage returns the image for the specified k8s version,
// or nil.
func (vd *Driver) GetImage(k8sversion string) (drivercore.Image, error) {
result, ok := localimages[k8sversion]
if !ok {
return nil, errors.New("image not available locally")
}

return result, nil
}
60 changes: 60 additions & 0 deletions drivercoretest/drivermock/driver-machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package drivermock

import (
"errors"

"github.com/kuttiproject/drivercore"
)

var mockmachines = map[string]*Machine{}

// QualifiedMachineName returns the driver-qualified machine name.
func (vd *Driver) QualifiedMachineName(machinename string, clustername string) string {
return clustername + "-" + machinename
}

// ListMachines returns the list of machines.
func (vd *Driver) ListMachines() ([]drivercore.Machine, error) {
result := make([]drivercore.Machine, len(mockmachines))
index := 0
for _, value := range mockmachines {
result[index] = value
index++
}

return result, nil
}

// GetMachine returns the specified machine, or an error.
func (vd *Driver) GetMachine(machinename string, clustername string) (drivercore.Machine, error) {
result, ok := mockmachines[vd.QualifiedMachineName(machinename, clustername)]
if !ok {
return nil, errors.New("machine not found")
}
return result, nil
}

// DeleteMachine deletes the specified machine.
func (vd *Driver) DeleteMachine(machinename string, clustername string) error {
mname := vd.QualifiedMachineName(machinename, clustername)
if _, ok := mockmachines[mname]; !ok {
return errors.New("machine not found")
}
delete(mockmachines, mname)
return nil
}

// NewMachine creates a new machine.
func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion string) (drivercore.Machine, error) {
qname := vd.QualifiedMachineName(machinename, clustername)
_, ok := mockmachines[qname]
if ok {
return nil, errors.New("machine already exists")
}
newmachine := &Machine{
machinename: machinename,
status: drivercore.MachineStatusStopped,
}
mockmachines[qname] = newmachine
return newmachine, nil
}
54 changes: 54 additions & 0 deletions drivercoretest/drivermock/driver-network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package drivermock

import (
"errors"

"github.com/kuttiproject/drivercore"
)

var mocknetworks = map[string]*Network{}

// QualifiedNetworkName adds a suffix to the specified cluster name.
func (vd *Driver) QualifiedNetworkName(clustername string) string {
return clustername + vd.networkNameSuffix
}

// ListNetworks gets a list of networks.
func (vd *Driver) ListNetworks() ([]drivercore.Network, error) {
panic("not implemented") // Will not implement
}

// GetNetwork gets a network.
func (vd *Driver) GetNetwork(clustername string) (drivercore.Network, error) {
result, ok := mocknetworks[vd.QualifiedNetworkName(clustername)]
if !ok {
return nil, errors.New("network not found")
}
return result, nil
}

// DeleteNetwork deletes a network.
func (vd *Driver) DeleteNetwork(clustername string) error {
netname := vd.QualifiedNetworkName(clustername)
if _, ok := mocknetworks[netname]; !ok {
return errors.New("network not found")
}
delete(mocknetworks, netname)
return nil
}

// NewNetwork creates a network for a cluster.
func (vd *Driver) NewNetwork(clustername string) (drivercore.Network, error) {
qnname := vd.QualifiedNetworkName(clustername)
_, ok := mocknetworks[qnname]
if ok {
return nil, errors.New("network already exists")
}
newnetwork := &Network{
driver: vd,
clustername: clustername,
cidr: DefaultNetCIDR,
}
mocknetworks[qnname] = newnetwork
return newnetwork, nil
}
51 changes: 51 additions & 0 deletions drivercoretest/drivermock/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package drivermock

const (
networkNameSuffix = "kuttinet"
networkNamePattern = "*" + networkNameSuffix
dhcpaddress = "192.168.125.3"
dhcpnetmask = "255.255.255.0"
ipNetAddr = "192.168.125"
iphostbase = 10
forwardedPortBase = 10000
)

// DefaultNetCIDR is the address range used by NAT networks.
var DefaultNetCIDR = "192.168.125.0/24"

// Driver implements the drivercore.Driver interface
type Driver struct {
driverName string
driverDescription string
usesNATNetworking bool
errormessage string
networkNameSuffix string
status string
}

// Name returns the driver name.
func (vd *Driver) Name() string {
return vd.driverName
}

// Description returns the driver description.
func (vd *Driver) Description() string {
return vd.driverDescription
}

// UsesNATNetworking returns if this particular mock
// driver instance uses NAT Networking
func (vd *Driver) UsesNATNetworking() bool {
return vd.usesNATNetworking
}

// Status returns the driver status.
func (vd *Driver) Status() string {
return vd.status
}

// Error returns the last error returned by the driver, or
// an empty string.
func (vd *Driver) Error() string {
return vd.errormessage
}
12 changes: 12 additions & 0 deletions drivercoretest/drivermock/drivermock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package drivermock

// New creates a new mock driver.
func New(drivername string, description string, usesNAT bool) *Driver {
return &Driver{
driverName: drivername,
driverDescription: description,
usesNATNetworking: usesNAT,
networkNameSuffix: drivername + "net",
status: "Ready",
}
}
42 changes: 42 additions & 0 deletions drivercoretest/drivermock/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package drivermock

import "github.com/kuttiproject/drivercore"

// Image implements the drivercore.Image interface.
type Image struct {
k8sversion string
status drivercore.ImageStatus
deprecated bool
}

// K8sVersion returns the k8s version of the image.
func (img *Image) K8sVersion() string {
return img.k8sversion
}

// Status returns the status of the image.
func (img *Image) Status() drivercore.ImageStatus {
return img.status
}

// Deprecated returns the deprecation status of the image.
func (img *Image) Deprecated() bool {
return img.deprecated
}

// Fetch fetches an image.
func (img *Image) Fetch() error {
img.status = drivercore.ImageStatusDownloaded
return nil
}

// FromFile fetches an image from a file.
func (img *Image) FromFile(filepath string) error {
return img.Fetch()
}

// PurgeLocal removes the local copy of an image.
func (img *Image) PurgeLocal() error {
img.status = drivercore.ImageStatusNotDownloaded
return nil
}
Loading

0 comments on commit 82cbf8b

Please sign in to comment.