github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/logs/utils.go (about) 1 package logs 2 3 import ( 4 "fmt" 5 "os" 6 "time" 7 8 . "github.com/onsi/ginkgo/v2" 9 . "github.com/redhat-appstudio/e2e-tests/pkg/utils" 10 "sigs.k8s.io/yaml" 11 ) 12 13 // createArtifactDirectory creates directory for storing artifacts of current spec. 14 func createArtifactDirectory() (string, error) { 15 wd, _ := os.Getwd() 16 artifactDir := GetEnv("ARTIFACT_DIR", fmt.Sprintf("%s/tmp", wd)) 17 classname := ShortenStringAddHash(CurrentSpecReport()) 18 testLogsDir := fmt.Sprintf("%s/%s", artifactDir, classname) 19 20 if err := os.MkdirAll(testLogsDir, os.ModePerm); err != nil { 21 return "", err 22 } 23 24 return testLogsDir, nil 25 } 26 27 // StoreResourceYaml stores yaml of given resource. 28 func StoreResourceYaml(resource any, name string) error { 29 resourceYaml, err := yaml.Marshal(resource) 30 if err != nil { 31 return fmt.Errorf("error getting resource yaml: %v", err) 32 } 33 34 resources := map[string][]byte{ 35 name + ".yaml": resourceYaml, 36 } 37 38 return StoreArtifacts(resources) 39 } 40 41 // StoreArtifacts stores given artifacts under artifact directory. 42 func StoreArtifacts(artifacts map[string][]byte) error { 43 artifactsDirectory, err := createArtifactDirectory() 44 if err != nil { 45 return err 46 } 47 48 for artifact_name, artifact_value := range artifacts { 49 filePath := fmt.Sprintf("%s/%s", artifactsDirectory, artifact_name) 50 if err := os.WriteFile(filePath, []byte(artifact_value), 0644); err != nil { 51 return err 52 } 53 } 54 55 return nil 56 } 57 58 func StoreTestTiming() error { 59 artifactsDirectory, err := createArtifactDirectory() 60 if err != nil { 61 return err 62 } 63 64 testTime := "Test started at: " + CurrentSpecReport().StartTime.String() + "\nTest ended at: " + time.Now().String() 65 filePath := fmt.Sprintf("%s/test-timing", artifactsDirectory) 66 if err := os.WriteFile(filePath, []byte(testTime), 0644); err != nil { 67 return fmt.Errorf("failed to store test timing: %v", err) 68 } 69 70 return nil 71 }