Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace print with logger in image_cache.go, fixes formatting #1012

Conversation

harshalmittal4
Copy link
Contributor

@harshalmittal4 harshalmittal4 commented Feb 17, 2023

This attempts to fix #880 by using a logger

Hey @jjbustamante, @natalieparellano,
For testing, the invocation of logger.Warnf() in image_cache.go requires c.DeleteOrigImage() to return a non-nil error. I tried simulating it using a spy for ImageCache and mock method DeleteOrigImage() that returns an error but it did not help -

when("attempting to delete original image gives non nil error", func() {
	it.Before(func() {
		fakeOriginalImage = fakes.NewImage("fake-image", "", local.IDIdentifier{ImageID: "fakeOrigImage"})
		fakeNewImage = fakes.NewImage("fake-image", "", local.IDIdentifier{ImageID: "fakeNewImage"})
	})
	it("should log error as warning", func() {
		spyErrImageCache := &SpyErrImageCache{origImage: fakeOriginalImage, newImage: fakeNewImage, logger: cmd.DefaultLogger,}
		err := spyErrImageCache.Commit()
		h.AssertNil(t, err)
		h.AssertEq(t, fakeOriginalImage.Found(), false)
	})
})
type SpyErrImageCache struct{
	committed bool
	origImage imgutil.Image
	newImage  imgutil.Image
	logger    log.Logger
}

func (c *SpyErrImageCache) DeleteOrigImage() error {
	return errors.New("error deleting original image")
}

Could you please suggest how can it be tested and review other changes

@jjbustamante
Copy link
Member

jjbustamante commented Feb 17, 2023

Hi @harshalmittal4 thanks for working on this!

Taking a look, I think maybe you can try mocking the origImage imgutil.Image. You need the c.DeleteOrigImage() to throw an error to test your code. Something like:

type fakeErrorImage struct {
	imgutil.Image
}

func newFakeImageErrIdentifier(origImage imgutil.Image) *fakeErrorImage {
	return &fakeErrorImage{Image: origImage}
}

func (f *fakeErrorImage) Identifier() (imgutil.Identifier, error) {
	return nil, errors.New("error deleting original image")
}

Then in your test case:

var fakeErrorImage *fakeErrorImage
it.Before(func() {
	fakeOriginalImage = fakes.NewImage("fake-image", "", local.IDIdentifier{ImageID: "fakeOrigImage"})
       // wrap your fake image into the one that throws the error you need
	fakeErrorImage = newFakeImageErrIdentifier(fakeOriginalImage)
       // ... more code
})

That should help you to go into your warning message, also provide a mock logger to record the method was called and you can do some assert in the test

@natalieparellano
Copy link
Member

@harshalmittal4 this looks good! I think we also need to update the function call here:

subject, err := cache.NewImageCacheFromName(cacheImageName, authn.DefaultKeychain)

Signed-off-by: Harshal Mittal <[email protected]>
@harshalmittal4
Copy link
Contributor Author

Thanks a lot @jjbustamante , @natalieparellano ! I've updated the PR including both the comments, please take a look.

@harshalmittal4
Copy link
Contributor Author

harshalmittal4 commented Feb 18, 2023

Unrelated to this, can you also please take a look at this issue when running acceptance tests -
I am using mac M1 (arm64) and getting the following error on running command make test-

> Running acceptance tests...
go test  -v -count=1 -tags=acceptance -timeout=2400s ./acceptance/...
=== RUN   TestVersion
    binaries.go:28: Building binaries: [make build-darwin-arm64]
    binaries.go:29: Expected nil: failed to execute command: [make build-darwin-arm64], exit status 2, make[1]: *** No rule to make target `build-darwin-arm64'.  Stop.
    .
    .
    === RUN   TestRestorer
    restorer_test.go:35: Restorer acceptance tests are not yet supported on non-amd64
--- SKIP: TestRestorer (0.00s)

So is there a way to run these on M1 mac?

Copy link
Contributor

@jabrown85 jabrown85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good!

As far as running the acceptance tests on m1 - I have not tried it myself as I don't yet have an arm machine. Maybe someone else can advise.

Copy link
Member

@jjbustamante jjbustamante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome job @harshalmittal4! Thanks a lot for helping us on this. ❤️

@harshalmittal4
Copy link
Contributor Author

Thanks much @jjbustamante , @natalieparellano for all the help! I'm an open-source beginner and really glad to be able to contribute 😃

@natalieparellano natalieparellano merged commit 6a10b63 into buildpacks:main Feb 21, 2023
@natalieparellano
Copy link
Member

Awesome work @harshalmittal4!

natalieparellano added a commit that referenced this pull request Mar 7, 2023
natalieparellano added a commit that referenced this pull request Mar 8, 2023
* Make a single constructor for lifecycle inputs

- The logic to update the default path for TOML files was repeated across phases
- In general it is safe to provide default values for inputs that might not be relevant to the current phase,
  as these will be ignored when constructing a new service for the phase;
  e.g., platform.LifecycleInputs.OrderPath will be ignored when constructing a lifecycle.Exporter
- As more inputs are shared across phases (e.g., analyzed.toml is now an input to the detect phase),
  duplicating the logic for providing default values is becoming more cumbersome

Signed-off-by: Natalie Arellano <[email protected]>

* Read values from environment

Signed-off-by: Natalie Arellano <[email protected]>

* Buildpack API: run.Dockerfiles are allowed instructions on versions >= 0.10

Signed-off-by: Natalie Arellano <[email protected]>

* Platform API: the detector accepts a new -run flag

Signed-off-by: Natalie Arellano <[email protected]>

* Move responsibility for validating Dockerfiles into the buildpack package

Signed-off-by: Natalie Arellano <[email protected]>

* When verifying Dockerfiles, return the new base image name if necessary

Signed-off-by: Natalie Arellano <[email protected]>

* When determining the new runtime base image, use criteria outlined in the platform spec

Signed-off-by: Natalie Arellano <[email protected]>

* Platform API: the schema of analyzed.toml is updated to include run-image.extend = <true or false, default false>

Signed-off-by: Natalie Arellano <[email protected]>

* TESTME: Update analyzed.toml with new run image if needed

Signed-off-by: Natalie Arellano <[email protected]>

* If extensions are used to switch the runtime base image, the detector should fail if the selected base image is not found in run.toml.

Signed-off-by: Natalie Arellano <[email protected]>

* Add fixture to test re-writing of analyzed.toml

Signed-off-by: Natalie Arellano <[email protected]>

* Move updating analyzed.toml into lifecycle package for easier testing

Signed-off-by: Natalie Arellano <[email protected]>

* Fix acceptance

Signed-off-by: Natalie Arellano <[email protected]>

* Don't redefine -layers

Signed-off-by: Natalie Arellano <[email protected]>

* Revert "Replace print with logger in image_cache.go, fixes formatting (#1012)"

This reverts commit 6a10b63.

* Revert "Revert "Replace print with logger in image_cache.go, fixes formatting (#1012)""

This reverts commit 5780910.

* Rename image -> images in run.toml

We pluralize all other list elements

Signed-off-by: Natalie Arellano <[email protected]>

* Don't enforce constraints for older extensions

Signed-off-by: Natalie Arellano <[email protected]>

* Rename function for clarity

Signed-off-by: Natalie Arellano <[email protected]>

---------

Signed-off-by: Natalie Arellano <[email protected]>
natalieparellano pushed a commit that referenced this pull request Mar 20, 2023
* Replace print with logger in image_cache.go, fixes formatting

Signed-off-by: Harshal Mittal <[email protected]>

* Add tests for image_cache logger

Signed-off-by: Harshal Mittal <[email protected]>

---------

Signed-off-by: Harshal Mittal <[email protected]>
natalieparellano added a commit that referenced this pull request Mar 20, 2023
* Fix log message when run image not found (#1004)

Before: "Previous image with name <run image name> not found"
After: "Image with name <run image name> not found"

Signed-off-by: Natalie Arellano <[email protected]>

* Bump containerd (#1015)

Signed-off-by: Natalie Arellano <[email protected]>

* Replace print with logger in image_cache.go, fixes formatting (#1012)

* Replace print with logger in image_cache.go, fixes formatting

Signed-off-by: Harshal Mittal <[email protected]>

* Add tests for image_cache logger

Signed-off-by: Harshal Mittal <[email protected]>

---------

Signed-off-by: Harshal Mittal <[email protected]>

* Bump golang.org/x/net from 0.5.0 to 0.7.0 (#1017)

Bumps [golang.org/x/net](https://github.com/golang/net) from 0.5.0 to 0.7.0.
- [Release notes](https://github.com/golang/net/releases)
- [Commits](golang/net@v0.5.0...v0.7.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Merge pull request #1036 from benri/bl/archive-pax-global-header

Ignore pax global header in tar extract

* Bump golang.org/x/sys from 0.5.0 to 0.6.0 (#1029)

Bumps [golang.org/x/sys](https://github.com/golang/sys) from 0.5.0 to 0.6.0.
- [Release notes](https://github.com/golang/sys/releases)
- [Commits](golang/sys@v0.5.0...v0.6.0)

---
updated-dependencies:
- dependency-name: golang.org/x/sys
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump github.com/containerd/containerd from 1.6.18 to 1.6.19 (#1040)

* Bump github.com/containerd/containerd from 1.6.18 to 1.7.0

Signed-off-by: Natalie Arellano <[email protected]>

* Only bump to 1.6.19 instead of 1.7.x until we can upgrade docker/docker

docker/docker 20.10.23 is incompatible with containerd 1.7.x+ due to the removal of sys/userns_deprecated.go
(upgrading containerd results in lifecycle compile errors like go/pkg/mod/github.com/docker/[email protected]+incompatible/pkg/archive/archive_unix.go:96:42: undefined: sys.RunningInUserNS)

Signed-off-by: Natalie Arellano <[email protected]>

---------

Signed-off-by: Natalie Arellano <[email protected]>

---------

Signed-off-by: Natalie Arellano <[email protected]>
Signed-off-by: Harshal Mittal <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Harshal Mittal <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jesse Brown <[email protected]>
natalieparellano added a commit that referenced this pull request Mar 29, 2023
#1049)

* Ready release/0.16.1 (#1041)

* Fix log message when run image not found (#1004)

Before: "Previous image with name <run image name> not found"
After: "Image with name <run image name> not found"

Signed-off-by: Natalie Arellano <[email protected]>

* Bump containerd (#1015)

Signed-off-by: Natalie Arellano <[email protected]>

* Replace print with logger in image_cache.go, fixes formatting (#1012)

* Replace print with logger in image_cache.go, fixes formatting

Signed-off-by: Harshal Mittal <[email protected]>

* Add tests for image_cache logger

Signed-off-by: Harshal Mittal <[email protected]>

---------

Signed-off-by: Harshal Mittal <[email protected]>

* Bump golang.org/x/net from 0.5.0 to 0.7.0 (#1017)

Bumps [golang.org/x/net](https://github.com/golang/net) from 0.5.0 to 0.7.0.
- [Release notes](https://github.com/golang/net/releases)
- [Commits](golang/net@v0.5.0...v0.7.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Merge pull request #1036 from benri/bl/archive-pax-global-header

Ignore pax global header in tar extract

* Bump golang.org/x/sys from 0.5.0 to 0.6.0 (#1029)

Bumps [golang.org/x/sys](https://github.com/golang/sys) from 0.5.0 to 0.6.0.
- [Release notes](https://github.com/golang/sys/releases)
- [Commits](golang/sys@v0.5.0...v0.6.0)

---
updated-dependencies:
- dependency-name: golang.org/x/sys
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump github.com/containerd/containerd from 1.6.18 to 1.6.19 (#1040)

* Bump github.com/containerd/containerd from 1.6.18 to 1.7.0

Signed-off-by: Natalie Arellano <[email protected]>

* Only bump to 1.6.19 instead of 1.7.x until we can upgrade docker/docker

docker/docker 20.10.23 is incompatible with containerd 1.7.x+ due to the removal of sys/userns_deprecated.go
(upgrading containerd results in lifecycle compile errors like go/pkg/mod/github.com/docker/[email protected]+incompatible/pkg/archive/archive_unix.go:96:42: undefined: sys.RunningInUserNS)

Signed-off-by: Natalie Arellano <[email protected]>

---------

Signed-off-by: Natalie Arellano <[email protected]>

---------

Signed-off-by: Natalie Arellano <[email protected]>
Signed-off-by: Harshal Mittal <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Harshal Mittal <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jesse Brown <[email protected]>

* Add sleep to "publish images" workflow (#1042)

It takes a few seconds for the image to be available

Signed-off-by: Natalie Arellano <[email protected]>

* Fix buildpacksio/lifecycle manifest create (#1043)

* Force lifecycle images to have docker media types

Signed-off-by: Natalie Arellano <[email protected]>

* Update go.mod to use latest imgutil

Signed-off-by: Natalie Arellano <[email protected]>

---------

Signed-off-by: Natalie Arellano <[email protected]>

* Bump kaniko & docker and unpin deps (#1045)

* Update kaniko & docker, unpin deps

Signed-off-by: Natalie Arellano <[email protected]>

* Update containerd

Signed-off-by: Natalie Arellano <[email protected]>

* Remove CVE ignores now that runc is unpinned

Signed-off-by: Natalie Arellano <[email protected]>

* Update to released kaniko

Signed-off-by: Natalie Arellano <[email protected]>

---------

Signed-off-by: Natalie Arellano <[email protected]>

* Ignore non-impactful runc CVE (#1047)

Signed-off-by: Natalie Arellano <[email protected]>

* Fix unit

Signed-off-by: Natalie Arellano <[email protected]>

---------

Signed-off-by: Natalie Arellano <[email protected]>
Signed-off-by: Harshal Mittal <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Harshal Mittal <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jesse Brown <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning when attempting to delete cache image breaks formating
4 participants