Write log files to artifacts directory instead of stdout

Signed-off-by: James Munnelly <james@munnelly.eu>
This commit is contained in:
James Munnelly 2019-01-07 11:31:15 +00:00
parent 6b76983165
commit 0237d5a4c2
8 changed files with 54 additions and 41 deletions

View File

@ -17,6 +17,11 @@ limitations under the License.
package e2e
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/onsi/ginkgo"
"github.com/jetstack/cert-manager/test/e2e/framework"
@ -49,7 +54,7 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
}
})
var globalLogs string
var globalLogs map[string]string
var _ = ginkgo.SynchronizedAfterSuite(func() {},
func() {
@ -60,7 +65,21 @@ var _ = ginkgo.SynchronizedAfterSuite(func() {},
ginkgo.GinkgoWriter.Write([]byte("Failed to retrieve global addon logs: " + err.Error()))
}
ginkgo.GinkgoWriter.Write([]byte(globalLogs))
for k, v := range globalLogs {
outPath := path.Join(framework.DefaultConfig.Ginkgo.ReportDirectory, k)
// Create a directory for the file if needed
err := os.MkdirAll(path.Dir(outPath), 0755)
if err != nil {
ginkgo.GinkgoWriter.Write([]byte(fmt.Sprintf("Failed to create directory for logs: %v", err)))
continue
}
err = ioutil.WriteFile(outPath, []byte(v), 0644)
if err != nil {
ginkgo.GinkgoWriter.Write([]byte(fmt.Sprintf("Failed to write log file: %v", err)))
continue
}
}
ginkgo.By("Cleaning up the provisioned globals")
err = addon.DeprovisionGlobals(cfg)

View File

@ -19,7 +19,6 @@ package e2e
import (
"flag"
"fmt"
"log"
"path"
"testing"
"time"
@ -74,9 +73,5 @@ func TestE2E(t *testing.T) {
ginkgoconfig.GinkgoConfig.ParallelNode))))
}
if !ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "cert-manager e2e suite", r) {
if len(globalLogs) > 0 {
log.Printf("Test suite failed, global addon logs: \n%v", globalLogs)
}
}
ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "cert-manager e2e suite", r)
}

View File

@ -113,6 +113,6 @@ func (p *Certmanager) SupportsGlobal() bool {
return true
}
func (p *Certmanager) Logs() (string, error) {
func (p *Certmanager) Logs() (map[string]string, error) {
return p.chart.Logs()
}

View File

@ -21,7 +21,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"strings"
"path"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -247,15 +247,17 @@ func (c *Chart) SupportsGlobal() bool {
return true
}
func (c *Chart) Logs() (string, error) {
func (c *Chart) Logs() (map[string]string, error) {
kc := c.Tiller.Base.Details().KubeClient
pods, err := kc.CoreV1().Pods(c.Namespace).List(metav1.ListOptions{LabelSelector: "release=" + c.ReleaseName})
if err != nil {
return "", err
return nil, err
}
builder := strings.Builder{}
out := make(map[string]string)
for _, pod := range pods.Items {
// Only grab logs from the first container in the pod
// TODO: grab logs from all containers
containerName := pod.Spec.Containers[0].Name
resp := kc.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{
Container: containerName,
@ -263,22 +265,16 @@ func (c *Chart) Logs() (string, error) {
err := resp.Error()
if err != nil {
return "", err
return nil, err
}
logs, err := resp.Raw()
if err != nil {
return "", err
return nil, err
}
_, err = builder.WriteString(fmt.Sprintf("Pod logs for %s:\n", pod.Name))
if err != nil {
return "", err
}
_, err = builder.Write(logs)
if err != nil {
return "", err
}
outPath := path.Join(c.Namespace, pod.Name)
out[outPath] = string(logs)
}
return builder.String(), nil
return out, nil
}

View File

@ -18,7 +18,6 @@ package addon
import (
"fmt"
"strings"
"github.com/golang/glog"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
@ -120,26 +119,30 @@ func SetupGlobals(cfg *config.Config) error {
}
type loggableAddon interface {
Logs() (string, error)
Logs() (map[string]string, error)
}
func GlobalLogs() (string, error) {
b := &strings.Builder{}
func GlobalLogs() (map[string]string, error) {
out := make(map[string]string)
for _, p := range provisioned {
if p, ok := p.(loggableAddon); ok {
l, err := p.Logs()
p, ok := p.(loggableAddon)
if !ok {
continue
}
if err != nil {
return "", err
}
l, err := p.Logs()
if err != nil {
return nil, err
}
_, err = b.WriteString(fmt.Sprintf("Got pods logs for addon: \n%s\n\n", l))
if err != nil {
return "", err
}
// TODO: namespace logs from each addon to their addon type to avoid
// conflicts. Realistically, it's unlikely a conflict will occur though
// so this will probably be fine for now.
for k, v := range l {
out[k] = v
}
}
return b.String(), nil
return out, nil
}
// DeprovisionGlobals deprovisions all of the global addons.

View File

@ -144,7 +144,7 @@ func (n *Nginx) SupportsGlobal() bool {
return true
}
func (n *Nginx) Logs() (string, error) {
func (n *Nginx) Logs() (map[string]string, error) {
return n.chart.Logs()
}

View File

@ -102,6 +102,6 @@ func (p *Pebble) SupportsGlobal() bool {
return true
}
func (p *Pebble) Logs() (string, error) {
func (p *Pebble) Logs() (map[string]string, error) {
return p.chart.Logs()
}

View File

@ -188,7 +188,7 @@ func (v *Vault) SupportsGlobal() bool {
return false
}
func (v *Vault) Logs() (string, error) {
func (v *Vault) Logs() (map[string]string, error) {
return v.chart.Logs()
}