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) } // func TestProcessorList(t *testing.T) { // } func TestProcessorGet(t *testing.T) { err := processor.Create(testAppTemplate) assert.Nil(t, err) app, err := processor.Get(false) assert.Nil(t, err) assert.Equal(t, "running", app.State) processor.Delete() assert.Nil(t, err) } func TestProcessorRegister(t *testing.T) { err := processor.Register(testAppTemplate) assert.Nil(t, err) app, err := processor.Get(false) assert.Nil(t, err) assert.Equal(t, "no-container", app.State) processor.Delete() assert.Nil(t, err) } func TestProcessorUpdate(t *testing.T) { err := processor.Create(testAppTemplate) assert.Nil(t, err) app, err := processor.Get(false) 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(false) 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) { err := processor.Create(testAppTemplate) assert.Nil(t, err) err = processor.Stop() assert.Nil(t, err) app, err := processor.Get(false) assert.Nil(t, err) assert.Equal(t, "exited", app.State) processor.Delete() assert.Nil(t, err) } func TestProcessorStart(t *testing.T) { err := processor.Create(testAppTemplate) assert.Nil(t, err) err = processor.Stop() assert.Nil(t, err) app, err := processor.Get(false) assert.Nil(t, err) assert.Equal(t, "exited", app.State) err = processor.Start() assert.Nil(t, err) app, err = processor.Get(false) assert.Nil(t, err) assert.Equal(t, "running", app.State) processor.Delete() assert.Nil(t, err) } // func TestProcessorRestart(t *testing.T) { // } // func TestProcessorUpdateKeys(t *testing.T) { // } // func TestProcessorSetPassword(t *testing.T) { // } // func TestProcessorProcesses(t *testing.T) { // } // func TestProcessorEnableTech(t *testing.T) { // } // func TestProcessorRebuild(t *testing.T) { // } // func TestProcessorAddLabel(t *testing.T) { // } // func TestProcessorRemoveLabel(t *testing.T) { // } // func TestProcessorGetNode(t *testing.T) { // } // func TestProcessorCreateSnapshot(t *testing.T) { // } // func TestProcessorRestoreFromSnapshot(t *testing.T) { // } // func TestProcessorListSnapshots(t *testing.T) { // } // func TestProcessorListAppsSnapshots(t *testing.T) { // } // func TestProcessorListSnapshotsByLabel(t *testing.T) { // } // func TestProcessorGetSnapshot(t *testing.T) { // } // func TestProcessorGetSnapshotDownloadLink(t *testing.T) { // } // func TestProcessorDeleteSnapshot(t *testing.T) { // } // func TestProcessorDeleteAppSnapshots(t *testing.T) { // }