func TestDownloadAgentMD5Mismatch(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() md5response := &http.Response{} md5sum := "md5sum" tarballResponse := &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(&bytes.Buffer{}), } mockFS := NewMockfileSystem(mockCtrl) mockFS.EXPECT().MkdirAll(config.CacheDirectory(), os.ModeDir|0700) mockGetter := NewMockhttpGetter(mockCtrl) mockGetter.EXPECT().Get(config.AgentRemoteTarballMD5()).Return(md5response, nil) mockFS.EXPECT().ReadAll(md5response.Body).Return([]byte(md5sum), nil) mockGetter.EXPECT().Get(config.AgentRemoteTarball()).Return(tarballResponse, nil) tempfile, err := ioutil.TempFile("", "test") if err != nil { t.Fail() } defer tempfile.Close() mockFS.EXPECT().TempFile("", "ecs-agent.tar").Return(tempfile, nil) mockFS.EXPECT().TeeReader(tarballResponse.Body, gomock.Any()) mockFS.EXPECT().Copy(tempfile, gomock.Any()).Return(int64(0), nil) mockFS.EXPECT().Remove(tempfile.Name()) d := &Downloader{ getter: mockGetter, fs: mockFS, } d.DownloadAgent() }
func TestDownloadAgentTempFile(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() md5response := &http.Response{} md5sum := "md5sum" tarballResponse := &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(&bytes.Buffer{}), } mockFS := NewMockfileSystem(mockCtrl) mockFS.EXPECT().MkdirAll(config.CacheDirectory(), os.ModeDir|0700) mockGetter := NewMockhttpGetter(mockCtrl) mockGetter.EXPECT().Get(config.AgentRemoteTarballMD5()).Return(md5response, nil) mockFS.EXPECT().ReadAll(md5response.Body).Return([]byte(md5sum), nil) mockGetter.EXPECT().Get(config.AgentRemoteTarball()).Return(tarballResponse, nil) mockFS.EXPECT().TempFile("", "ecs-agent.tar").Return(nil, errors.New("test error")) d := &Downloader{ getter: mockGetter, fs: mockFS, } d.DownloadAgent() }
func (d *Downloader) getPublishedMd5Sum() (string, error) { log.Debugf("Downloading published md5sum from %s", config.AgentRemoteTarballMD5()) resp, err := d.getter.Get(config.AgentRemoteTarballMD5()) if err != nil { return "", err } defer func() { if resp != nil && resp.Body != nil { resp.Body.Close() } }() body, err := d.fs.ReadAll(resp.Body) if err != nil { return "", err } return strings.TrimSpace(string(body)), nil }
func TestDownloadAgentDownloadMD5Failure(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockFS := NewMockfileSystem(mockCtrl) mockGetter := NewMockhttpGetter(mockCtrl) mockFS.EXPECT().MkdirAll(config.CacheDirectory(), os.ModeDir|0700) mockGetter.EXPECT().Get(config.AgentRemoteTarballMD5()).Return(nil, errors.New("test error")) d := &Downloader{ getter: mockGetter, fs: mockFS, } d.DownloadAgent() }
func TestDownloadAgentDownloadTarballFailure(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() md5response := &http.Response{} md5sum := "md5sum" mockFS := NewMockfileSystem(mockCtrl) mockFS.EXPECT().MkdirAll(config.CacheDirectory(), os.ModeDir|0700) mockGetter := NewMockhttpGetter(mockCtrl) mockGetter.EXPECT().Get(config.AgentRemoteTarballMD5()).Return(md5response, nil) mockFS.EXPECT().ReadAll(md5response.Body).Return([]byte(md5sum), nil) mockGetter.EXPECT().Get(config.AgentRemoteTarball()).Return(nil, errors.New("test error")) d := &Downloader{ getter: mockGetter, fs: mockFS, } d.DownloadAgent() }
func TestDownloadAgentReadPublishedMd5Failure(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() md5response := &http.Response{ Body: ioutil.NopCloser(&bytes.Buffer{}), } mockFS := NewMockfileSystem(mockCtrl) mockFS.EXPECT().MkdirAll(config.CacheDirectory(), os.ModeDir|0700) mockGetter := NewMockhttpGetter(mockCtrl) mockGetter.EXPECT().Get(config.AgentRemoteTarballMD5()).Return(md5response, nil) mockFS.EXPECT().ReadAll(md5response.Body).Return(nil, errors.New("test error")) d := &Downloader{ getter: mockGetter, fs: mockFS, } d.DownloadAgent() }
func TestDownloadAgentSuccess(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() tarballContents := "tarball contents" tarballResponse := &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString(tarballContents)), } expectedMd5Sum := fmt.Sprintf("%x\n", md5.Sum([]byte(tarballContents))) md5response := &http.Response{} mockFS := NewMockfileSystem(mockCtrl) mockFS.EXPECT().MkdirAll(config.CacheDirectory(), os.ModeDir|0700) mockGetter := NewMockhttpGetter(mockCtrl) mockGetter.EXPECT().Get(config.AgentRemoteTarballMD5()).Return(md5response, nil) mockFS.EXPECT().ReadAll(md5response.Body).Return([]byte(expectedMd5Sum), nil) mockGetter.EXPECT().Get(config.AgentRemoteTarball()).Return(tarballResponse, nil) tempfile, err := ioutil.TempFile("", "test") if err != nil { t.Fail() } defer tempfile.Close() mockFS.EXPECT().TempFile("", "ecs-agent.tar").Return(tempfile, nil) mockFS.EXPECT().TeeReader(tarballResponse.Body, gomock.Any()).Do(func(reader io.Reader, writer io.Writer) { _, err = io.Copy(writer, reader) if err != nil { t.Fail() } }) mockFS.EXPECT().Copy(tempfile, gomock.Any()).Return(int64(0), nil) mockFS.EXPECT().Rename(tempfile.Name(), config.AgentTarball()) d := &Downloader{ getter: mockGetter, fs: mockFS, } d.DownloadAgent() }