-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docker graceful exits when image pull fails #382
added check for Decode statement added unit tests added unit tests added unit tests
- Loading branch information
Showing
2 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/packethost/pkg/log" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func setupTestLogger() log.Logger { | ||
service := "github.com/tinkerbell/tink" | ||
logger, err := log.Init(service) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return logger | ||
} | ||
|
||
type imagePullerMock struct{} | ||
|
||
func (d *imagePullerMock) ImagePull(ctx context.Context, str string, op types.ImagePullOptions) (io.ReadCloser, error) { | ||
var s string | ||
var err error | ||
if str == "test/success" { | ||
s = "{\"status\": \"hello\",\"error\":\"\"}{\"status\":\"world\",\"error\":\"\"}" | ||
} else if str == "test/fail" { | ||
s = "{\"error\": \"\"}" | ||
err = errors.New("Tested, failure of the image pull") | ||
} else if str == "test/fail_partial" { | ||
s = "{\"status\": \"hello\",\"error\":\"\"}{\"status\":\"world\",\"error\":\"Tested, failure of No space left on device\"}" | ||
} | ||
stringReader := strings.NewReader(s) | ||
stringReadCloser := ioutil.NopCloser(stringReader) | ||
return stringReadCloser, err | ||
} | ||
|
||
func TestPullImageAnyFailure(t *testing.T) { | ||
for _, test := range []struct { | ||
testName string | ||
err error | ||
}{ | ||
{ | ||
testName: "success", | ||
err: nil, | ||
}, | ||
{ | ||
testName: "fail", | ||
err: errors.New("Tested, failure of the image pull"), | ||
}, | ||
{ | ||
testName: "fail_partial", | ||
err: errors.New("Tested, failure of No space left on device"), | ||
}, | ||
} { | ||
t.Run(test.testName, func(t *testing.T) { | ||
ctx := context.Background() | ||
rcon := NewRegistryConnDetails("test", "testUser", "testPwd", setupTestLogger()) | ||
cli := &imagePullerMock{} | ||
err := rcon.pullImage(ctx, cli, test.testName) | ||
if test.err != nil { | ||
assert.Error(t, err) | ||
return | ||
} | ||
}) | ||
} | ||
} |