forked from ko-build/ko
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve YAML comments & style when resolving/applying (ko-build#103)
* Preserve YAML comments & style when resolving/applying This is accomplished by adopting the yaml.v3 lib. It exposes a Node struct that's used internally by the yaml encoder/decoder ko internally now manipulates YAML documents using this struct Fixes ko-build#101 * add/remove vendored modules * Apply suggestions from code review Fix comments Co-Authored-By: jonjohnsonjr <jonjohnson@google.com> * update doc link * Fix use of yaml.Decoder in a test When the yaml.Decoder returns an io.EOF it implies there were no YAML documents decoded and that there are no more! * Update pkg/resolve/resolve.go resolve comment suggestion Co-Authored-By: jonjohnsonjr <jonjohnson@google.com> * leave ko prefix if we're not operating in strict mode * move testutils to internal/testing
- Loading branch information
1 parent
be4e1ff
commit 4833bb4
Showing
53 changed files
with
12,425 additions
and
1,594 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
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,145 @@ | ||
// Copyright 2018 Google LLC All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package commands | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/random" | ||
"github.com/google/ko/pkg/commands/options" | ||
kotesting "github.com/google/ko/pkg/internal/testing" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var ( | ||
fooRef = "github.com/awesomesauce/foo" | ||
foo = mustRandom() | ||
fooHash = mustDigest(foo) | ||
barRef = "github.com/awesomesauce/bar" | ||
bar = mustRandom() | ||
barHash = mustDigest(bar) | ||
testBuilder = kotesting.NewFixedBuild(map[string]v1.Image{ | ||
fooRef: foo, | ||
barRef: bar, | ||
}) | ||
testHashes = map[string]v1.Hash{ | ||
fooRef: fooHash, | ||
barRef: barHash, | ||
} | ||
) | ||
|
||
func TestResolveMultiDocumentYAMLs(t *testing.T) { | ||
refs := []string{fooRef, barRef} | ||
hashes := []v1.Hash{fooHash, barHash} | ||
base := mustRepository("gcr.io/multi-pass") | ||
|
||
buf := bytes.NewBuffer(nil) | ||
encoder := yaml.NewEncoder(buf) | ||
for _, input := range refs { | ||
if err := encoder.Encode(input); err != nil { | ||
t.Fatalf("Encode(%v) = %v", input, err) | ||
} | ||
} | ||
|
||
inputYAML := buf.Bytes() | ||
|
||
outYAML, err := resolveFile( | ||
yamlToTmpFile(t, buf.Bytes()), | ||
testBuilder, | ||
kotesting.NewFixedPublish(base, testHashes), | ||
&options.SelectorOptions{}, | ||
&options.StrictOptions{}) | ||
|
||
if err != nil { | ||
t.Fatalf("ImageReferences(%v) = %v", string(inputYAML), err) | ||
} | ||
|
||
buf = bytes.NewBuffer(outYAML) | ||
decoder := yaml.NewDecoder(buf) | ||
var outStructured []string | ||
for { | ||
var output string | ||
if err := decoder.Decode(&output); err == nil { | ||
outStructured = append(outStructured, output) | ||
} else if err == io.EOF { | ||
break | ||
} else { | ||
t.Errorf("yaml.Unmarshal(%v) = %v", string(outYAML), err) | ||
} | ||
} | ||
|
||
expectedStructured := []string{ | ||
kotesting.ComputeDigest(base, refs[0], hashes[0]), | ||
kotesting.ComputeDigest(base, refs[1], hashes[1]), | ||
} | ||
|
||
if want, got := len(expectedStructured), len(outStructured); want != got { | ||
t.Errorf("resolveFile(%v) = %v, want %v", string(inputYAML), got, want) | ||
} | ||
|
||
if diff := cmp.Diff(expectedStructured, outStructured, cmpopts.EquateEmpty()); diff != "" { | ||
t.Errorf("resolveFile(%v); (-want +got) = %v", string(inputYAML), diff) | ||
} | ||
} | ||
|
||
func mustRepository(s string) name.Repository { | ||
n, err := name.NewRepository(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return n | ||
} | ||
|
||
func mustDigest(img v1.Image) v1.Hash { | ||
d, err := img.Digest() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return d | ||
} | ||
|
||
func mustRandom() v1.Image { | ||
img, err := random.Image(1024, 5) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return img | ||
} | ||
|
||
func yamlToTmpFile(t *testing.T, yaml []byte) string { | ||
t.Helper() | ||
|
||
tmpfile, err := ioutil.TempFile("", "doc") | ||
if err != nil { | ||
t.Fatalf("error creating temp file: %v", err) | ||
} | ||
|
||
if _, err := tmpfile.Write(yaml); err != nil { | ||
t.Fatalf("error writing temp file: %v", err) | ||
} | ||
|
||
if err := tmpfile.Close(); err != nil { | ||
t.Fatalf("error closing temp file: %v", err) | ||
} | ||
|
||
return tmpfile.Name() | ||
} |
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,16 @@ | ||
// Copyright 2018 Google LLC All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package testing holds a variety test doubles to help with testing | ||
package testing |
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,80 @@ | ||
// Copyright 2018 Google LLC All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package testing | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/ko/pkg/build" | ||
"github.com/google/ko/pkg/publish" | ||
) | ||
|
||
type fixedBuild struct { | ||
entries map[string]v1.Image | ||
} | ||
|
||
// NewFixedBuild returns a build.Interface implementation that simply resolves | ||
// particular references to fixed v1.Image objects | ||
func NewFixedBuild(entries map[string]v1.Image) build.Interface { | ||
return &fixedBuild{entries} | ||
} | ||
|
||
// IsSupportedReference implements build.Interface | ||
func (f *fixedBuild) IsSupportedReference(s string) bool { | ||
_, ok := f.entries[s] | ||
return ok | ||
} | ||
|
||
// Build implements build.Interface | ||
func (f *fixedBuild) Build(s string) (v1.Image, error) { | ||
if img, ok := f.entries[s]; ok { | ||
return img, nil | ||
} | ||
return nil, fmt.Errorf("unsupported reference: %q", s) | ||
} | ||
|
||
type fixedPublish struct { | ||
base name.Repository | ||
entries map[string]v1.Hash | ||
} | ||
|
||
// NewFixedPublish returns a publish.Interface implementation that simply | ||
// resolves particular references to fixed name.Digest references. | ||
func NewFixedPublish(base name.Repository, entries map[string]v1.Hash) publish.Interface { | ||
return &fixedPublish{base, entries} | ||
} | ||
|
||
// Publish implements publish.Interface | ||
func (f *fixedPublish) Publish(_ v1.Image, s string) (name.Reference, error) { | ||
h, ok := f.entries[s] | ||
if !ok { | ||
return nil, fmt.Errorf("unsupported importpath: %q", s) | ||
} | ||
d, err := name.NewDigest(fmt.Sprintf("%s/%s@%s", f.base, s, h)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &d, nil | ||
} | ||
|
||
func ComputeDigest(base name.Repository, ref string, h v1.Hash) string { | ||
d, err := name.NewDigest(fmt.Sprintf("%s/%s@%s", base, ref, h)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return d.String() | ||
} |
Oops, something went wrong.