compiler_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package compiler
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. "github.com/remogatto/cloud"
  6. "github.com/remogatto/prettytest"
  7. "gogs.carducci-dante.gov.it/karmen/config"
  8. )
  9. type testSuite struct {
  10. prettytest.Suite
  11. }
  12. var (
  13. compiler *Compiler
  14. client *cloud.Client
  15. simpleMain string = `
  16. package main
  17. func main() {
  18. println("Hello World!")
  19. }
  20. `
  21. cloudTemplateProgram string = `
  22. package main
  23. import (
  24. "bytes"
  25. "fmt"
  26. "text/template"
  27. "github.com/remogatto/cloud"
  28. "gogs.carducci-dante.gov.it/karmen/config"
  29. )
  30. func main() {
  31. var (
  32. WebDAVUrl = "http://karmen_cloud_test/remote.php/webdav/"
  33. WebDavUsername = "admin"
  34. WebDAVPassword = "password"
  35. config config.ConfigT
  36. data struct{ Data string }
  37. output bytes.Buffer
  38. )
  39. content, err := client.Download("Config/config.yaml")
  40. if err != nil {
  41. fmt.Print(err)
  42. }
  43. if err := config.Read(configData, &config); err != nil {
  44. fmt.Print(err)
  45. }
  46. client, err := cloud.Dial(config.Cloud.Url, config.Cloud.Username, config.Cloud.Password)
  47. if err != nil {
  48. fmt.Print(err)
  49. }
  50. content, err := client.Download("Templates/template.tpl")
  51. if err != nil {
  52. fmt.Print(err)
  53. }
  54. tpl, err := template.New("template.tpl").Parse(string(content))
  55. if err != nil {
  56. fmt.Print(err)
  57. }
  58. data.Data = "Foo"
  59. err = tpl.Execute(&output, data)
  60. if err != nil {
  61. fmt.Print(err)
  62. }
  63. err = client.Mkdir("Test")
  64. if err != nil {
  65. fmt.Print(err)
  66. }
  67. err = client.Upload(output.Bytes(), "Test/foo.txt")
  68. if err != nil {
  69. fmt.Print(err)
  70. }
  71. fmt.Print("ok")
  72. }
  73. `
  74. )
  75. func TestRunner(t *testing.T) {
  76. prettytest.Run(
  77. t,
  78. new(testSuite),
  79. )
  80. }
  81. func (t *testSuite) BeforeAll() {
  82. var err error
  83. compiler = New("http://localhost:8080/compile")
  84. client, err = cloud.Dial(
  85. config.Config.Cloud.Url,
  86. config.Config.Cloud.Username,
  87. config.Config.Cloud.Password,
  88. )
  89. if err != nil {
  90. panic(err)
  91. }
  92. if !client.Exists("Templates") {
  93. if err := client.Mkdir("Templates"); err != nil {
  94. panic(err)
  95. }
  96. }
  97. templateData, err := ioutil.ReadFile("testdata/template.tpl")
  98. if err != nil {
  99. panic(err)
  100. }
  101. if err := client.Upload(templateData, "Templates/template.tpl"); err != nil {
  102. panic(err)
  103. }
  104. if !client.Exists("Config") {
  105. if err := client.Mkdir("Config"); err != nil {
  106. panic(err)
  107. }
  108. }
  109. configData, err := ioutil.ReadFile("testdata/config.yaml")
  110. if err != nil {
  111. panic(err)
  112. }
  113. if err := client.Upload(configData, "Config/config.yaml"); err != nil {
  114. panic(err)
  115. }
  116. }
  117. func (t *testSuite) TestRunSimpleMain() {
  118. program := &Program{simpleMain}
  119. result, err := compiler.Run(program)
  120. t.Nil(err)
  121. if err == nil {
  122. t.Equal("Hello World!\n", result.Events[0].Message)
  123. }
  124. }
  125. func (t *testSuite) TestCloudTemplate() {
  126. program := &Program{cloudTemplateProgram}
  127. result, err := compiler.Run(program)
  128. t.Nil(err)
  129. if err == nil {
  130. t.Equal("", result.Errors)
  131. if result.Errors == "" {
  132. t.Equal("ok", result.Events[0].Message)
  133. data, err := client.Download("Test/foo.txt")
  134. t.Nil(err)
  135. t.Equal("Foo", string(data))
  136. }
  137. }
  138. }
  139. func (t *testSuite) After() {
  140. client.Delete("Test")
  141. }