node-api/glue/main_test.go

245 lines
4.3 KiB
Go
Raw Permalink Normal View History

package glue
import (
"log"
"os"
"path"
"testing"
"github.com/jinzhu/gorm"
"github.com/rosti-cz/node-api/apps"
"github.com/stretchr/testify/assert"
// This is line from GORM documentation that imports database dialect
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
const testDBPath = "file::memory:?cache=shared"
var testAppTemplate apps.App = apps.App{
Name: "test_1234",
SSHPort: 10000,
HTTPPort: 10001,
Image: "harbor.hq.rosti.cz/public/runtime:2022.01-1",
CPU: 50,
Memory: 256,
}
func getPWD() string {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
return dir
}
func testDB() *gorm.DB {
db, err := gorm.Open("sqlite3", testDBPath)
if err != nil {
log.Fatalln(err)
}
return db
}
func getTestDockerSock() string {
dockerSocket := os.Getenv("DOCKER_SOCKET")
if dockerSocket == "" {
return "unix:///run/user/1000/podman/podman.sock"
}
return dockerSocket
}
var processor Processor
func TestMain(m *testing.M) {
// This processor is used to test all methods
processor = Processor{
AppName: testAppTemplate.Name,
DB: testDB(),
// SnapshotProcessor *apps.SnapshotProcessor
DockerSock: getTestDockerSock(),
BindIPHTTP: "127.0.0.1",
BindIPSSH: "127.0.0.1",
AppsPath: path.Join(getPWD(), "tmp/apps"),
}
exitVal := m.Run()
os.Exit(exitVal)
}
func TestProcessorCreate(t *testing.T) {
err := processor.Create(testAppTemplate)
assert.Nil(t, err)
processor.Delete()
assert.Nil(t, err)
}
2022-02-07 17:30:42 +00:00
// func TestProcessorList(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
func TestProcessorGet(t *testing.T) {
err := processor.Create(testAppTemplate)
assert.Nil(t, err)
app, err := processor.Get()
assert.Nil(t, err)
2022-02-07 17:30:42 +00:00
assert.Equal(t, "running", app.State)
processor.Delete()
assert.Nil(t, err)
}
2022-02-07 22:59:35 +00:00
func TestProcessorRegister(t *testing.T) {
err := processor.Register(testAppTemplate)
assert.Nil(t, err)
2022-02-07 22:59:35 +00:00
app, err := processor.Get()
assert.Nil(t, err)
assert.Equal(t, "no-container", app.State)
2022-02-07 22:59:35 +00:00
processor.Delete()
assert.Nil(t, err)
}
2022-02-07 22:59:35 +00:00
func TestProcessorUpdate(t *testing.T) {
err := processor.Create(testAppTemplate)
assert.Nil(t, err)
app, err := processor.Get()
assert.Nil(t, err)
assert.Equal(t, "running", app.State)
updatedTemplate := testAppTemplate
updatedTemplate.Memory = 1024
err = processor.Update(updatedTemplate)
assert.Nil(t, err)
assert.Equal(t, "running", app.State)
app, err = processor.Get()
assert.Nil(t, err)
assert.Equal(t, 1024, app.Memory)
processor.Delete()
assert.Nil(t, err)
}
func TestProcessorDelete(t *testing.T) {
err := processor.Create(testAppTemplate)
assert.Nil(t, err)
processor.Delete()
assert.Nil(t, err)
}
func TestProcessorStop(t *testing.T) {
2022-02-07 17:30:42 +00:00
err := processor.Create(testAppTemplate)
assert.Nil(t, err)
2022-02-07 17:30:42 +00:00
err = processor.Stop()
assert.Nil(t, err)
2022-02-07 17:30:42 +00:00
app, err := processor.Get()
assert.Nil(t, err)
assert.Equal(t, "exited", app.State)
2022-02-07 17:30:42 +00:00
processor.Delete()
assert.Nil(t, err)
}
2022-02-07 22:59:35 +00:00
func TestProcessorStart(t *testing.T) {
err := processor.Create(testAppTemplate)
assert.Nil(t, err)
2022-02-07 22:59:35 +00:00
err = processor.Stop()
assert.Nil(t, err)
app, err := processor.Get()
assert.Nil(t, err)
assert.Equal(t, "exited", app.State)
err = processor.Start()
assert.Nil(t, err)
app, err = processor.Get()
assert.Nil(t, err)
assert.Equal(t, "running", app.State)
processor.Delete()
assert.Nil(t, err)
}
2022-02-07 17:30:42 +00:00
// func TestProcessorRestart(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorUpdateKeys(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorSetPassword(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorProcesses(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorEnableTech(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorRebuild(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorAddLabel(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorRemoveLabel(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorGetNode(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorCreateSnapshot(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorRestoreFromSnapshot(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorListSnapshots(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorListAppsSnapshots(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorListSnapshotsByLabel(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorGetSnapshot(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorGetSnapshotDownloadLink(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
2022-02-07 17:30:42 +00:00
// func TestProcessorDeleteSnapshot(t *testing.T) {
2022-02-07 17:30:42 +00:00
// }
// func TestProcessorDeleteAppSnapshots(t *testing.T) {
// }