Skip to content

Commit

Permalink
Add tests for the log level
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Oct 17, 2018
1 parent f1980fa commit 5e65832
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 44 deletions.
68 changes: 68 additions & 0 deletions pkg/skaffold/build/kaniko/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2018 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kaniko

import (
"io"
"sync"
"sync/atomic"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

// logLevel makes sure kaniko logs at least at Info level.
func logLevel() logrus.Level {
level := logrus.GetLevel()
if level < logrus.InfoLevel {
return logrus.InfoLevel
}
return level
}

func streamLogs(out io.Writer, name string, pods corev1.PodInterface) func() {
var wg sync.WaitGroup
wg.Add(1)

var retry int32 = 1
go func() {
defer wg.Done()

for atomic.LoadInt32(&retry) == 1 {
r, err := pods.GetLogs(name, &v1.PodLogOptions{
Follow: true,
Container: constants.DefaultKanikoContainerName,
}).Stream()
if err != nil {
logrus.Debugln("unable to get kaniko pod logs:", err)
time.Sleep(1 * time.Second)
continue
}

io.Copy(out, r)
return
}
}()

return func() {
atomic.StoreInt32(&retry, 0)
wg.Wait()
}
}
49 changes: 49 additions & 0 deletions pkg/skaffold/build/kaniko/logs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2018 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kaniko

import (
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"

"github.com/sirupsen/logrus"
)

func TestLogLevel(t *testing.T) {
defer func(l logrus.Level) { logrus.SetLevel(l) }(logrus.GetLevel())

tests := []struct {
logrusLevel logrus.Level
expected logrus.Level
}{
{logrusLevel: logrus.DebugLevel, expected: logrus.DebugLevel},
{logrusLevel: logrus.InfoLevel, expected: logrus.InfoLevel},
{logrusLevel: logrus.WarnLevel, expected: logrus.InfoLevel},
{logrusLevel: logrus.ErrorLevel, expected: logrus.InfoLevel},
{logrusLevel: logrus.FatalLevel, expected: logrus.InfoLevel},
{logrusLevel: logrus.PanicLevel, expected: logrus.InfoLevel},
}

for _, test := range tests {
logrus.SetLevel(test.logrusLevel)

kanikoLevel := logLevel()

testutil.CheckDeepEqual(t, test.expected, kanikoLevel)
}
}
44 changes: 0 additions & 44 deletions pkg/skaffold/build/kaniko/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,15 @@ import (
"context"
"fmt"
"io"
"sync"
"sync/atomic"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/kaniko/sources"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

func (b *Builder) run(ctx context.Context, out io.Writer, artifact *latest.Artifact, cfg *latest.KanikoBuild) (string, error) {
Expand Down Expand Up @@ -88,41 +82,3 @@ func (b *Builder) run(ctx context.Context, out io.Writer, artifact *latest.Artif

return imageDst, nil
}

func logLevel() logrus.Level {
level := logrus.GetLevel()
if level < logrus.InfoLevel {
return logrus.InfoLevel
}
return level
}

func streamLogs(out io.Writer, name string, pods corev1.PodInterface) func() {
var wg sync.WaitGroup
wg.Add(1)

var retry int32 = 1
go func() {
defer wg.Done()

for atomic.LoadInt32(&retry) == 1 {
r, err := pods.GetLogs(name, &v1.PodLogOptions{
Follow: true,
Container: constants.DefaultKanikoContainerName,
}).Stream()
if err != nil {
logrus.Debugln("unable to get kaniko pod logs:", err)
time.Sleep(1 * time.Second)
continue
}

io.Copy(out, r)
return
}
}()

return func() {
atomic.StoreInt32(&retry, 0)
wg.Wait()
}
}

0 comments on commit 5e65832

Please sign in to comment.