packager_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package serverless
  2. import (
  3. "archive/tar"
  4. "compress/gzip"
  5. "embed"
  6. "io"
  7. "io/fs"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "testing"
  12. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
  13. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  14. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  15. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/constants"
  16. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  17. )
  18. type TPluginRuntime struct {
  19. plugin_entities.PluginRuntime
  20. positive_manager.PositivePluginRuntime
  21. }
  22. func (r *TPluginRuntime) InitEnvironment() error {
  23. return nil
  24. }
  25. func (r *TPluginRuntime) Checksum() string {
  26. return "test_checksum"
  27. }
  28. func (r *TPluginRuntime) Identity() (plugin_entities.PluginUniqueIdentifier, error) {
  29. return plugin_entities.PluginUniqueIdentifier("test_identity"), nil
  30. }
  31. func (r *TPluginRuntime) StartPlugin() error {
  32. return nil
  33. }
  34. func (r *TPluginRuntime) Type() plugin_entities.PluginRuntimeType {
  35. return plugin_entities.PLUGIN_RUNTIME_TYPE_LOCAL
  36. }
  37. func (r *TPluginRuntime) Wait() (<-chan bool, error) {
  38. return nil, nil
  39. }
  40. func (r *TPluginRuntime) Listen(string) *entities.Broadcast[plugin_entities.SessionMessage] {
  41. return nil
  42. }
  43. func (r *TPluginRuntime) Write(string, []byte) {
  44. }
  45. //go:embed packager_test_plugin/*
  46. var test_plugin embed.FS
  47. func TestPackager_Pack(t *testing.T) {
  48. // create a temp dir
  49. tmpDir, err := os.MkdirTemp("", "test_plugin")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. defer os.RemoveAll(tmpDir)
  54. // copy the test_plugin to the temp dir
  55. if err := fs.WalkDir(test_plugin, ".", func(path string, d fs.DirEntry, err error) error {
  56. if err != nil {
  57. return err
  58. }
  59. if d.IsDir() {
  60. // create the dir
  61. os.MkdirAll(filepath.Join(tmpDir, path), 0755)
  62. } else {
  63. // copy the file
  64. origin_file, err := test_plugin.Open(path)
  65. if err != nil {
  66. return err
  67. }
  68. defer origin_file.Close()
  69. content, err := io.ReadAll(origin_file)
  70. if err != nil {
  71. return err
  72. }
  73. if err := os.WriteFile(filepath.Join(tmpDir, path), content, 0644); err != nil {
  74. return err
  75. }
  76. }
  77. return nil
  78. }); err != nil {
  79. t.Fatal(err)
  80. }
  81. decoder, err := decoder.NewFSPluginDecoder(path.Join(tmpDir, "packager_test_plugin"))
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. packager := NewPackager(&TPluginRuntime{
  86. PluginRuntime: plugin_entities.PluginRuntime{
  87. Config: plugin_entities.PluginDeclaration{
  88. PluginDeclarationWithoutAdvancedFields: plugin_entities.PluginDeclarationWithoutAdvancedFields{
  89. Meta: plugin_entities.PluginMeta{
  90. Runner: plugin_entities.PluginRunner{
  91. Language: constants.Python,
  92. Version: "3.12",
  93. Entrypoint: "main",
  94. },
  95. Arch: []constants.Arch{
  96. constants.AMD64,
  97. },
  98. },
  99. },
  100. },
  101. },
  102. }, decoder)
  103. f, err := packager.Pack()
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. defer func() {
  108. f.Close()
  109. os.Remove(f.Name())
  110. }()
  111. gzip_reader, err := gzip.NewReader(f)
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. defer gzip_reader.Close()
  116. // Create a new tar reader
  117. tar_reader := tar.NewReader(gzip_reader)
  118. dockerfile_found := false
  119. requirements_found := false
  120. main_py_found := false
  121. // Iterate through the files in the tar.gz archive
  122. for {
  123. header, err := tar_reader.Next()
  124. if err == io.EOF {
  125. break // End of archive
  126. }
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. switch header.Name {
  131. case "Dockerfile":
  132. dockerfile_found = true
  133. case "requirements.txt":
  134. requirements_found = true
  135. case "main.py":
  136. main_py_found = true
  137. }
  138. }
  139. // Check if all required files are present
  140. if !dockerfile_found {
  141. t.Error("Dockerfile not found in the packed archive")
  142. }
  143. if !requirements_found {
  144. t.Error("requirements.txt not found in the packed archive")
  145. }
  146. if !main_py_found {
  147. t.Error("main.py not found in the packed archive")
  148. }
  149. }