garm/config/external_test.go
Gabriel Adrian Samfira f52accc47f Add idempotency when stopping a VM and some tests
When deleting a VM, we try to force stop it. If the VM is already stopped,
LXD will return an error. Unfortunately, we can't import the drivers package
from LXD without also pulling in a bunch of linux specific CGO dependencies
which we want to avoid.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2022-06-28 15:13:02 +00:00

88 lines
1.7 KiB
Go

package config
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func getDefaultExternalConfig(t *testing.T) External {
dir, err := ioutil.TempDir("", "garm-test")
if err != nil {
t.Fatalf("failed to create temporary directory: %s", err)
}
t.Cleanup(func() { os.RemoveAll(dir) })
err = ioutil.WriteFile(filepath.Join(dir, "garm-external-provider"), []byte{}, 0755)
if err != nil {
t.Fatalf("failed to write file: %s", err)
}
return External{
ConfigFile: "",
ProviderDir: dir,
}
}
func TestExternal(t *testing.T) {
cfg := getDefaultExternalConfig(t)
tests := []struct {
name string
cfg External
errString string
}{
{
name: "Config is valid",
cfg: cfg,
errString: "",
},
{
name: "Config path cannot be relative path",
cfg: External{
ConfigFile: "../test",
ProviderDir: cfg.ProviderDir,
},
errString: "path to config file must be an absolute path",
},
{
name: "Config must exist if specified",
cfg: External{
ConfigFile: "/there/is/no/config/here",
ProviderDir: cfg.ProviderDir,
},
errString: "failed to access config file /there/is/no/config/here",
},
{
name: "Missing provider dir",
cfg: External{
ConfigFile: "",
ProviderDir: "",
},
errString: "missing provider dir",
},
{
name: "Provider dir must not be relative",
cfg: External{
ConfigFile: "",
ProviderDir: "../test",
},
errString: "path to provider dir must be absolute",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := tc.cfg.Validate()
if tc.errString == "" {
assert.Nil(t, err)
} else {
assert.NotNil(t, err)
assert.EqualError(t, err, tc.errString)
}
})
}
}