Switch exec to look at exit code not output status.

This commit is contained in:
Brendan Burns
2015-05-08 09:48:31 -07:00
parent 35c644a45f
commit c9324e6e38
8 changed files with 98 additions and 12 deletions

View File

@@ -17,34 +17,35 @@ limitations under the License.
package exec
import (
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
uexec "github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec"
"github.com/golang/glog"
)
const defaultHealthyOutput = "ok"
func New() ExecProber {
return execProber{}
}
type ExecProber interface {
Probe(e uexec.Cmd) (probe.Result, error)
Probe(e exec.Cmd) (probe.Result, error)
}
type execProber struct{}
func (pr execProber) Probe(e uexec.Cmd) (probe.Result, error) {
func (pr execProber) Probe(e exec.Cmd) (probe.Result, error) {
data, err := e.CombinedOutput()
glog.V(4).Infof("health check response: %s", string(data))
if err != nil {
exit, ok := err.(exec.ExitError)
if ok {
if exit.ExitStatus() == 0 {
return probe.Success, nil
} else {
return probe.Failure, nil
}
}
return probe.Unknown, err
}
if !strings.HasPrefix(strings.ToLower(string(data)), defaultHealthyOutput) {
return probe.Failure, nil
}
return probe.Success, nil
}

View File

@@ -41,16 +41,40 @@ type healthCheckTest struct {
err error
}
type fakeExitError struct {
exited bool
statusCode int
}
func (f *fakeExitError) String() string {
return f.Error()
}
func (f *fakeExitError) Error() string {
return "fake exit"
}
func (f *fakeExitError) Exited() bool {
return f.exited
}
func (f *fakeExitError) ExitStatus() int {
return f.statusCode
}
func TestExec(t *testing.T) {
prober := New()
fake := FakeCmd{}
tests := []healthCheckTest{
// Ok
{probe.Success, false, []byte("OK"), nil},
// Ok
{probe.Success, false, []byte("OK"), &fakeExitError{true, 0}},
// Run returns error
{probe.Unknown, true, []byte("OK, NOT"), fmt.Errorf("test error")},
// Unhealthy
{probe.Failure, false, []byte("Fail"), nil},
{probe.Failure, false, []byte("Fail"), &fakeExitError{true, 1}},
}
for _, test := range tests {
fake.out = test.output