Skip to content

Commit

Permalink
FEAT: Add lxd test
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred78290 committed Sep 3, 2024
1 parent fac0ce9 commit b197d4c
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 18 deletions.
73 changes: 73 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,49 @@
"request": "custom",
"type": "lldb"
},
{
"name": "Test Test_Server lxd",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/server/server_test.go",
"env": {
"DEFAULT_MACHINE": "medium",
"PLATEFORM": "lxd",
"TEST_SERVER_CONFIG": "${workspaceFolder}/test/config/server.json",
"TEST_PROVIDER_CONFIG": "${workspaceFolder}/test/config/lxd/provider.json",
"TEST_CONFIG": "${workspaceFolder}/test/config/lxd/config.json",
"TEST_MACHINES_CONFIG": "${workspaceFolder}/test/config/lxd/machines.json",
"TestServer": "YES",
"TestServer_NodeGroups": "YES",
"TestServer_NodeGroupForNode": "YES",
"TestServer_HasInstance": "YES",
"TestServer_Pricing": "YES",
"TestServer_GetAvailableMachineTypes": "YES",
"TestServer_NewNodeGroup": "YES",
"TestServer_GetResourceLimiter": "YES",
"TestServer_Cleanup": "YES",
"TestServer_Refresh": "YES",
"TestServer_TargetSize": "YES",
"TestServer_IncreaseSize": "YES",
"TestServer_DecreaseTargetSize": "YES",
"TestServer_DeleteNodes": "YES",
"TestServer_Id": "YES",
"TestServer_Debug": "YES",
"TestServer_Nodes": "YES",
"TestServer_TemplateNodeInfo": "YES",
"TestServer_Exist": "YES",
"TestServer_Create": "YES",
"TestServer_Delete": "YES",
"TestServer_Autoprovisioned": "YES",
"TestServer_Belongs": "YES",
"TestServer_NodePrice": "YES",
"TestServer_PodPrice": "YES"
},
"envFile": "${workspaceFolder}/.env",
"showLog": false,
"args": ["-test.run", "^Test_Server$"]
},
{
"name": "Test Test_Server vsphere",
"type": "go",
Expand Down Expand Up @@ -877,6 +920,36 @@
"showLog": false,
"args": ["-test.run", "^Test_Providers$"]
},
{
"name": "Test Test_Providers lxd",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/providers/providers_test.go",
"envFile": "${workspaceFolder}/.env",
"env": {
"DEFAULT_MACHINE": "medium",
"GITHUB_RUN_ID": "123456",
"SSH_KEYFILE": "id_rsa",
"SEED_USER": "ubuntu",
"SEED_IMAGE": "DC0_H0_VM0",
"TEST_MODE": "0",
"TEST_SERVER_CONFIG": "${workspaceFolder}/test/config/server.json",
"TEST_PROVIDER_CONFIG": "${workspaceFolder}/test/config/lxd/provider.json",
"TEST_CONFIG": "${workspaceFolder}/test/config/lxd/config.json",
"TEST_MACHINES_CONFIG": "${workspaceFolder}/test/config/lxd/machines.json",
"PLATEFORM": "lxd",
"Test_createVM": "YES",
"Test_getVM": "YES",
"Test_statusVM": "YES",
"Test_powerOnVM": "YES",
"Test_powerOffVM": "YES",
"Test_shutdownGuest": "YES",
"Test_deleteVM": "YES"
},
"showLog": false,
"args": ["-test.run", "^Test_Providers$"]
},
{
"name": "Test Test_Providers aws",
"type": "go",
Expand Down
96 changes: 79 additions & 17 deletions providers/lxd/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ import (
"gopkg.in/yaml.v2"
)

type Remote struct {
Addr string `yaml:"addr"`
AuthType string `yaml:"auth_type,omitempty"`
Project string `yaml:"project,omitempty"`
Protocol string `yaml:"protocol,omitempty"`
Public bool `yaml:"public"`
Global bool `yaml:"-"`
Static bool `yaml:"-"`
}

type NetworkInterface struct {
Enabled *bool `json:"enabled,omitempty" yaml:"primary,omitempty"`
Primary bool `json:"primary,omitempty" yaml:"primary,omitempty"`
Expand Down Expand Up @@ -56,6 +66,7 @@ type Configuration struct {
Bind9Host string `json:"bind9-host"`
RndcKeyFile string `json:"rndc-key-file"`
Network Network `json:"network"`
Remotes map[string]Remote `json:"remotes,omitempty"`
}

type lxdWrapper struct {
Expand Down Expand Up @@ -143,6 +154,72 @@ func (wrapper *lxdWrapper) CreateInstance(instanceName, instanceType string, con
return
}

func (wrapper *lxdWrapper) findImageByName(imageServer golxd.ImageServer, name string) (fingerprint string, err error) {
var alias *api.ImageAliasesEntry

if alias, _, err = imageServer.GetImageAlias(name); err != nil || alias == nil {
var images []api.Image

if images, err = imageServer.GetImages(); err != nil {
err = fmt.Errorf("image: %s not found, reason: %v", name, err)
} else {
for _, image := range images {
if image.Properties["name"] == name {
fingerprint = images[0].Fingerprint
break
}
}
}
} else {
fingerprint = alias.Target
}

if len(fingerprint) == 0 {
err = fmt.Errorf("image: %s not found", name)
}

return
}

func (wrapper *lxdWrapper) findImage(name string) (fingerprint string, err error) {
if strings.Contains(name, ":") {
var imageServer golxd.ImageServer
var image *api.Image
var op golxd.RemoteOperation
remote := providers.StringBefore(name, ":")
name = providers.StringAfter(name, ":")

glog.Infof("Get remote image information for %s:%s", remote, name)

if remoteServer, found := wrapper.Remotes[remote]; found {
if imageServer, err = golxd.ConnectSimpleStreams(remoteServer.Addr, nil); err == nil {
if fingerprint, err = wrapper.findImageByName(imageServer, name); err == nil {
if _, _, err = wrapper.client.GetImage(fingerprint); err != nil {
if image, _, err = imageServer.GetImage(fingerprint); err == nil {
glog.Infof("Copy remote image %s:%s to local", remote, name)

if op, err = wrapper.client.CopyImage(imageServer, *image, nil); err == nil {
err = op.Wait()
}

glog.Infof("Copy done %s:%s, err: %v", remote, name, err)
}
} else {
glog.Infof("remote image %s:%s found locally", remote, name)
}
}
} else {
err = fmt.Errorf("remote image server %s failed: reason: %v", remoteServer.Addr, err)
}
} else {
err = fmt.Errorf("remote image server: %s not found", remote)
}
} else {
fingerprint, err = wrapper.findImageByName(wrapper.client, name)
}

return
}
func (wrapper *lxdWrapper) ConfigurationDidLoad() (err error) {
if wrapper.Configuration.UseBind9 {
if wrapper.bind9Provider, err = rfc2136.NewDNSRFC2136ProviderCredentials(wrapper.Configuration.Bind9Host, wrapper.Configuration.RndcKeyFile); err != nil {
Expand Down Expand Up @@ -179,23 +256,8 @@ func (wrapper *lxdWrapper) ConfigurationDidLoad() (err error) {

wrapper.client = wrapper.client.UseProject(wrapper.Project)

var alias *api.ImageAliasesEntry

if alias, _, err = wrapper.client.GetImageAlias(wrapper.TemplateName); err != nil || alias == nil {
var images []api.Image

if images, err = wrapper.client.GetImages(); err != nil {
return fmt.Errorf("image: %s not found, reason: %v", wrapper.TemplateName, err)
} else {
for _, image := range images {
if image.Properties["name"] == wrapper.TemplateName {
wrapper.imageFingerPrint = images[0].Fingerprint
break
}
}
}
} else {
wrapper.imageFingerPrint = alias.Target
if wrapper.imageFingerPrint, err = wrapper.findImage(wrapper.TemplateName); err != nil {
return
}

if len(wrapper.imageFingerPrint) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion providers/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (config *configurationTest) createVM() {
NodeGroup: config.appConfig.NodeGroup,
UserName: config.appConfig.SSH.UserName,
AuthKey: config.appConfig.SSH.AuthKeys,
CloudInit: nil,
CloudInit: config.appConfig.CloudInit,
Machine: &machine,
}

Expand Down
38 changes: 38 additions & 0 deletions test/bin/lxd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
set -e

pushd $(dirname $0) &>/dev/null
CURDIR=${PWD}
popd &>/dev/null

go clean -testcache
go mod vendor

export LOCALDEFS=${CURDIR}/../local.env
export VERBOSE=-test.v
export TEST_MODE=1
export TEST_SERVER_CONFIG=${CURDIR}/../config/server.json
export TEST_PROVIDER_CONFIG=${CURDIR}/../config/lxd/provider.json
export TEST_CONFIG=${CURDIR}/../config/lxd/config.json
export TEST_MACHINES_CONFIG=${CURDIR}/../config/lxd/machines.json
export PLATEFORM=lxd
export DEFAULT_MACHINE=medium

if [ ! -f "${LOCALDEFS}" ]; then
echo "File ${LOCALDEFS} not found, exit test"
exit 1
fi

source ${LOCALDEFS}

if [ -n "${SSH_PRIVATEKEY}" ] && [ ! -f ${HOME}/.ssh/${SSH_KEYFILE} ]; then
mkdir -p ${HOME}/.ssh

echo -n ${SSH_PRIVATEKEY} | base64 -d > ${HOME}/.ssh/${SSH_KEYFILE}

chmod 0600 ${HOME}/.ssh/${SSH_KEYFILE}
fi

source "${CURDIR}/providers.sh"
source "${CURDIR}/server.sh"
source "${CURDIR}/nodegroup.sh"
11 changes: 11 additions & 0 deletions test/bin/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ if [ -z "${GITHUB_RUN_ID}" ]; then
fi

if [ ! -f "${CURDIR}/../local.env" ]; then

if [ -n "${SSH_PRIVATEKEY}" ] && [ ! -f ${HOME}/.ssh/${SSH_KEYFILE} ]; then
mkdir -p ${HOME}/.ssh

echo -n ${SSH_PRIVATEKEY} | base64 -d > ${HOME}/.ssh/${SSH_KEYFILE}

chmod 0600 ${HOME}/.ssh/${SSH_KEYFILE}
ssh-keygen -f ${HOME}/.ssh/${SSH_KEYFILE} -y > ${HOME}/.ssh/${SSH_KEYFILE}.pub
fi

cat > ${CURDIR}/../local.env <<EOF
export SSH_KEYFILE=test_rsa
export SSH_PRIVATEKEY=$SSH_PRIVATEKEY
export SSH_PUBLIC_KEY="$(cat ${HOME}/.ssh/${SSH_KEYFILE}.pub)"
export SEED_IMAGE=$SEED_IMAGE
export SEED_USER=$SEED_USER
export KUBE_ENGINE=external
Expand Down
5 changes: 5 additions & 0 deletions test/config/lxd/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"instance-name": "vm-autoscaled-test-${GITHUB_RUN_ID}",
"instance-type": "tiny",
"disk-size": 20480
}
37 changes: 37 additions & 0 deletions test/config/lxd/machines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"tiny": {
"memsize": 2048,
"vcpus": 2,
"disksize": 10240
},
"small": {
"memsize": 4096,
"vcpus": 2,
"disksize": 20480
},
"medium": {
"memsize": 4096,
"vcpus": 4,
"disksize": 20480
},
"large": {
"memsize": 8192,
"vcpus": 4,
"disksize": 51200
},
"xlarge": {
"memsize": 16384,
"vcpus": 4,
"disksize": 102400
},
"2xlarge": {
"memsize": 16384,
"vcpus": 8,
"disksize": 102400
},
"4xlarge": {
"memsize": 32768,
"vcpus": 8,
"disksize": 102400
}
}
71 changes: 71 additions & 0 deletions test/config/lxd/provider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"lxd-server-url": "unix:",
"container-type": "container",
"storage-pool": "default",
"profiles": [
"default"
],
"project": "default",
"nodegroup": "lxd-dev-k3s",
"timeout": 300,
"template-name": "ubuntu:noble",
"remotes": {
"images": {
"Addr": "https://images.lxd.canonical.com",
"AuthType": "",
"Project": "",
"Protocol": "simplestreams",
"Public": true,
"Global": false,
"Static": false
},
"ubuntu": {
"Addr": "https://cloud-images.ubuntu.com/releases",
"AuthType": "",
"Project": "",
"Protocol": "simplestreams",
"Public": true,
"Global": false,
"Static": true
},
"ubuntu-daily": {
"Addr": "https://cloud-images.ubuntu.com/daily",
"AuthType": "",
"Project": "",
"Protocol": "simplestreams",
"Public": true,
"Global": false,
"Static": true
},
"ubuntu-minimal": {
"Addr": "https://cloud-images.ubuntu.com/minimal/releases/",
"AuthType": "",
"Project": "",
"Protocol": "simplestreams",
"Public": true,
"Global": false,
"Static": true
},
"ubuntu-minimal-daily": {
"Addr": "https://cloud-images.ubuntu.com/minimal/daily/",
"AuthType": "",
"Project": "",
"Protocol": "simplestreams",
"Public": true,
"Global": false,
"Static": true
}
},
"network": {
"domain": "aldunelabs.private",
"interfaces": [
{
"enabled": true,
"primary": true,
"network": "lxdbr0",
"nic": "eth0",
"dhcp": true
}
]
}
}
Loading

0 comments on commit b197d4c

Please sign in to comment.