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

pkg/daemon: write bytes, use buffered writer and compare bytes #411

Merged
merged 1 commit into from
Feb 12, 2019

Conversation

runcom
Copy link
Member

@runcom runcom commented Feb 12, 2019

contents.Data is a byte slice and can be anything (i.e. a binary after
deconding). Use a bytes writer to prevent misbehaves in such scenarios.
Along with that, use a buffered writer around the file in case we're
dealing with big files. Also, compare byte slices as well instead of
using strings.

Signed-off-by: Antonio Murdaca <runcom@linux.com>

@openshift-ci-robot openshift-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Feb 12, 2019
@cgwalters
Copy link
Member

/lgtm
/approved

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Feb 12, 2019
@jlebon
Copy link
Member

jlebon commented Feb 12, 2019

Good catch! /lgtm

I think we need the same thing in checkFileContentsAndMode().

@ashcrow
Copy link
Member

ashcrow commented Feb 12, 2019

/hold

@runcom do you want to update ``checkFileContentsAndMode()` in this PR or keep them separate?

@openshift-ci-robot openshift-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 12, 2019
@runcom
Copy link
Member Author

runcom commented Feb 12, 2019

@runcom do you want to update ``checkFileContentsAndMode()` in this PR or keep them separate?

I'll push to this PR

@openshift-ci-robot openshift-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label Feb 12, 2019
@runcom
Copy link
Member Author

runcom commented Feb 12, 2019

pushed with changes to the other func, ptal

@openshift-ci-robot openshift-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 12, 2019
@ashcrow
Copy link
Member

ashcrow commented Feb 12, 2019

All unit tests passed, but reported failure 😕

/test unit

@ashcrow
Copy link
Member

ashcrow commented Feb 12, 2019

Ahh the failure was only noted in the full log:

ok  	github.com/openshift/machine-config-operator/lib/resourceread	0.047s
make: *** [test-unit] Error 1
2019/02/12 14:54:38 Container test in pod unit failed, exit code 2, reason Error
2019/02/12 14:54:39 Ran for 37s
error: could not run steps: test "unit" failed: the pod ci-op-iwn3fmvt/unit failed after 35s (failed containers: test): ContainerFailed one or more containers exited
Container test exited with code 2, reason Error
---
e
--- 

@runcom
Copy link
Member Author

runcom commented Feb 12, 2019

yeah, there's an error I'm fixing

@jlebon
Copy link
Member

jlebon commented Feb 12, 2019

Looks good to me. Though the commit message title could be tweaked now that it also fixes the reading side as well.

@runcom
Copy link
Member Author

runcom commented Feb 12, 2019

Looks good to me. Though the commit message title could be tweaked now that it also fixes the reading side as well.

yup, I'll change it once I fix the unit tests 👍

@runcom runcom changed the title pkg/daemon: write bytes and use buffered writer pkg/daemon: write bytes, use buffered writer and compare bytes Feb 12, 2019
@runcom
Copy link
Member Author

runcom commented Feb 12, 2019

updated and changed commit message/title as well

@@ -881,8 +882,8 @@ func checkFileContentsAndMode(filePath, expectedContent string, mode os.FileMode
glog.Errorf("could not read file: %q, error: %v", filePath, err)
return false
}
if strings.Compare(string(contents), expectedContent) != 0 {
glog.Errorf("content mismatch for file: %q; expected: %v; received: %v", filePath, expectedContent, string(contents))
if !bytes.Equal(contents, expectedContent) {
Copy link
Member Author

Choose a reason for hiding this comment

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

this should now be much faster compared to string methods as it's directly implemented in assembly as well

if strings.Compare(string(contents), expectedContent) != 0 {
glog.Errorf("content mismatch for file: %q; expected: %v; received: %v", filePath, expectedContent, string(contents))
if !bytes.Equal(contents, expectedContent) {
glog.Errorf("content mismatch for file: %q; expected: %v; received: %v", filePath, expectedContent, contents)
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, this will print the contents as a byte array, right? We could not print the raw contents at all, though it's definitely helpful if it is indeed human-readable text (which it likely is in most cases). WDYT about casting to string here before printing and otherwise fallback on printing as a byte array?

I'm good with this as is too though and doing that as a follow up.

Copy link
Member Author

Choose a reason for hiding this comment

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

the issue is casting to string a bytes slice always succeeds, not sure how to differentiate if we can't show something, I'm not really sure this info is helpful for big files as well 🤔

Copy link
Member

Choose a reason for hiding this comment

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

How about we just tweak it to "content mismatch for file: %q" for now?

Copy link
Member Author

Choose a reason for hiding this comment

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

sounds great 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

done

`contents.Data` is a byte slice and can be anything (i.e. a binary after
deconding). Use a bytes writer to prevent misbehaves in such scenarios.
Along with that, use a buffered writer around the file in case we're
dealing with big files. Also, compare byte slices as well instead of
using strings.

Signed-off-by: Antonio Murdaca <runcom@linux.com>
@jlebon
Copy link
Member

jlebon commented Feb 12, 2019

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Feb 12, 2019
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ashcrow, cgwalters, jlebon, runcom

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [ashcrow,cgwalters,jlebon,runcom]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 12, 2019
@ashcrow
Copy link
Member

ashcrow commented Feb 12, 2019

/hold cancel

@openshift-ci-robot openshift-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 12, 2019
@runcom
Copy link
Member Author

runcom commented Feb 12, 2019

/retest

@openshift-merge-robot openshift-merge-robot merged commit f6d7208 into openshift:master Feb 12, 2019
@runcom runcom deleted the buffer-file-encoding branch February 12, 2019 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants