tests: try to get logs

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto 2024-06-25 09:11:46 +02:00
parent e84b31935c
commit f41a519a2c

View File

@ -1,6 +1,7 @@
package e2e_test package e2e_test
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"os" "os"
@ -38,7 +39,7 @@ var _ = BeforeSuite(func() {
var defaultConfig openai.ClientConfig var defaultConfig openai.ClientConfig
if apiEndpoint == "" { if apiEndpoint == "" {
startDockerImage() startDockerImage("")
defaultConfig = openai.DefaultConfig(apiKey) defaultConfig = openai.DefaultConfig(apiKey)
apiEndpoint = "http://localhost:" + apiPort + "/v1" // So that other tests can reference this value safely. apiEndpoint = "http://localhost:" + apiPort + "/v1" // So that other tests can reference this value safely.
defaultConfig.BaseURL = apiEndpoint defaultConfig.BaseURL = apiEndpoint
@ -58,9 +59,41 @@ var _ = BeforeSuite(func() {
}) })
var _ = AfterSuite(func() { var _ = AfterSuite(func() {
// if the suite failed, logs will be printed
// to the console
if CurrentGinkgoTestDescription().Failed {
if resource != nil {
logs := bytes.NewBufferString("")
err := pool.Client.Logs(docker.LogsOptions{
Container: resource.Container.ID,
OutputStream: logs,
ErrorStream: logs,
Stdout: true,
Stderr: true,
Timestamps: true,
})
if err != nil {
fmt.Println("Could not take logs for failed suite", err.Error())
}
fmt.Println("Suite failed, printing logs")
fmt.Println(logs.String())
c, err := pool.Client.InspectContainer(resource.Container.ID)
if err != nil {
fmt.Println("Could not inspect container", err.Error())
}
fmt.Println("Container state")
fmt.Println("Running:", c.State.Running)
fmt.Println("ExitCode:", c.State.ExitCode)
fmt.Println("Error:", c.State.Error)
}
}
if resource != nil { if resource != nil {
Expect(pool.Purge(resource)).To(Succeed()) Expect(pool.Purge(resource)).To(Succeed())
} }
//dat, err := os.ReadFile(resource.Container.LogPath) //dat, err := os.ReadFile(resource.Container.LogPath)
//Expect(err).To(Not(HaveOccurred())) //Expect(err).To(Not(HaveOccurred()))
//Expect(string(dat)).To(ContainSubstring("GRPC Service Ready")) //Expect(string(dat)).To(ContainSubstring("GRPC Service Ready"))
@ -71,8 +104,8 @@ var _ = AfterEach(func() {
//Expect(dbClient.Clear()).To(Succeed()) //Expect(dbClient.Clear()).To(Succeed())
}) })
func startDockerImage() { func startDockerImage(endpoint string) {
p, err := dockertest.NewPool("") p, err := dockertest.NewPool(endpoint)
Expect(err).To(Not(HaveOccurred())) Expect(err).To(Not(HaveOccurred()))
Expect(p.Client.Ping()).To(Succeed()) Expect(p.Client.Ping()).To(Succeed())