-
Notifications
You must be signed in to change notification settings - Fork 4.8k
[WIP] Use Docker parser when manipulating Dockerfiles #1079
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package builder | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestReplaceValidCmd(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add a test to verify the output is also parsable, meaning valid Dockerfile.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be useful but it's out of scope of the current changes ie. command replacement vs Dockerfile validation
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Au contraire the resulting file MUST be valid Dockerfile as it's being the source of the docker build. So I insist on having such test.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add a test where we'll parse both the original and the new Dockerfile and compare their ASTs. Validation of the Dockerfile though is something different than what this change is supposed to do. @bparees what do you think?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That should suffice IMHO, just running |
||
| tests := []struct { | ||
| name string | ||
| cmd string | ||
| replaceStr string | ||
| fileData []byte | ||
| expectedOutput string | ||
| expectedErr error | ||
| }{ | ||
| { | ||
| name: "from-replacement", | ||
| cmd: "from", | ||
| replaceStr: "FROM other/image", | ||
| fileData: []byte(dockerFile), | ||
| expectedOutput: expectedFROM, | ||
| expectedErr: nil, | ||
| }, | ||
| { | ||
| name: "run-replacement", | ||
| cmd: "run", | ||
| replaceStr: "This test kind-of-fails before string replacement so this string won't be used", | ||
| fileData: []byte(dockerFile), | ||
| expectedOutput: dockerFile, | ||
| expectedErr: nil, | ||
| }, | ||
| { | ||
| name: "invalid-dockerfile-cmd", | ||
| cmd: "blabla", | ||
| replaceStr: "This test fails at start so this string won't be used", | ||
| fileData: []byte(dockerFile), | ||
| expectedOutput: "", | ||
| expectedErr: invalidCmdErr, | ||
| }, | ||
| { | ||
| name: "no-cmd-in-dockerfile", | ||
| cmd: "cmd", | ||
| replaceStr: "CMD runme.sh", | ||
| fileData: []byte(dockerFile), | ||
| expectedOutput: dockerFile, | ||
| expectedErr: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| out, err := replaceValidCmd(test.cmd, test.replaceStr, test.fileData) | ||
| if err != test.expectedErr { | ||
| t.Errorf("%s: Unexpected error: Expected %v, got %v", test.name, test.expectedErr, err) | ||
| } | ||
| if out != test.expectedOutput { | ||
| t.Errorf("%s: Unexpected output: Expected %s, got %s", test.name, test.expectedOutput, out) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const dockerFile = ` | ||
| FROM openshift/origin-base | ||
| FROM candidate | ||
|
|
||
| RUN mkdir -p /var/lib/openshift | ||
|
|
||
| ADD bin/openshift /usr/bin/openshift | ||
| RUN ln -s /usr/bin/openshift /usr/bin/osc && \ | ||
|
|
||
| ENV HOME /root | ||
| WORKDIR /var/lib/openshift | ||
| ENTRYPOINT ["/usr/bin/openshift"] | ||
| ` | ||
|
|
||
| const expectedFROM = ` | ||
| FROM openshift/origin-base | ||
| FROM other/image | ||
|
|
||
| RUN mkdir -p /var/lib/openshift | ||
|
|
||
| ADD bin/openshift /usr/bin/openshift | ||
| RUN ln -s /usr/bin/openshift /usr/bin/osc && \ | ||
|
|
||
| ENV HOME /root | ||
| WORKDIR /var/lib/openshift | ||
| ENTRYPOINT ["/usr/bin/openshift"] | ||
| ` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this will always return the last occurrence of the command? should probably document that behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, you are right. I wanted to add a sanity check inside that function but since it's recursive it would probably affect performance a bit. So I agree with you about documenting it and I will address it.