-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3a4f85
commit a99e5c3
Showing
6 changed files
with
159 additions
and
50 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,76 @@ | ||
package sideload_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/go-logr/logr" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/env" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/sideload" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/testhelpers" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/versions" | ||
) | ||
|
||
var ( | ||
testLog logr.Logger | ||
ctx context.Context | ||
) | ||
|
||
func TestEnv(t *testing.T) { | ||
testLog = testhelpers.GetLogger() | ||
ctx = logr.NewContext(context.Background(), testLog) | ||
|
||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Sideload Suite") | ||
} | ||
|
||
var _ = Describe("Sideload", func() { | ||
var ( | ||
prefix = "a-test-package" | ||
input io.Reader | ||
) | ||
BeforeEach(func() { | ||
contents, err := testhelpers.ContentsFor(prefix) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
input = bytes.NewReader(contents) | ||
}) | ||
|
||
It("should fail if a non-concrete version is given", func() { | ||
err := sideload.Sideload(ctx, versions.LatestVersion) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
|
||
It("should fail if a non-concrete platform is given", func() { | ||
err := sideload.Sideload(ctx, versions.Spec{Selector: &versions.Concrete{Major: 1, Minor: 2, Patch: 3}}, sideload.WithPlatform("*", "*")) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
|
||
It("should load the given tarball into our store as the given version", func() { | ||
v := &versions.Concrete{Major: 1, Minor: 2, Patch: 3} | ||
store := testhelpers.NewMockStore() | ||
Expect(sideload.Sideload( | ||
ctx, | ||
versions.Spec{Selector: v}, | ||
sideload.WithInput(input), | ||
sideload.WithEnvOptions(env.WithStore(store)), | ||
)).To(Succeed()) | ||
|
||
baseName := versions.Platform{OS: runtime.GOOS, Arch: runtime.GOARCH}.BaseName(*v) | ||
expectedPath := filepath.Join("k8s", baseName, prefix) | ||
|
||
outFile, err := store.Root.Open(expectedPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
DeferCleanup(outFile.Close) | ||
contents, err := io.ReadAll(outFile) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(contents).To(HavePrefix(prefix)) | ||
}) | ||
}) |
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,52 @@ | ||
package testhelpers | ||
|
||
import ( | ||
"archive/tar" | ||
"bytes" | ||
"compress/gzip" | ||
"crypto/md5" //nolint:gosec | ||
"crypto/rand" | ||
"encoding/base64" | ||
"fmt" | ||
"path/filepath" | ||
) | ||
|
||
func ContentsFor(filename string) ([]byte, error) { //nolint:revive | ||
var chunk [1024 * 48]byte // 1.5 times our chunk read size in GetVersion | ||
copy(chunk[:], filename) | ||
if _, err := rand.Read(chunk[len(filename):]); err != nil { | ||
return nil, err | ||
} | ||
|
||
out := new(bytes.Buffer) | ||
gzipWriter := gzip.NewWriter(out) | ||
|
||
tarWriter := tar.NewWriter(gzipWriter) | ||
|
||
if err := tarWriter.WriteHeader(&tar.Header{ | ||
Name: filepath.Join("kubebuilder/bin", filename), | ||
Size: int64(len(chunk)), | ||
Mode: 0777, // so we can check that we fix this later | ||
}); err != nil { | ||
return nil, fmt.Errorf("write tar header: %w", err) | ||
} | ||
if _, err := tarWriter.Write(chunk[:]); err != nil { | ||
return nil, fmt.Errorf("write tar contents: %w", err) | ||
} | ||
|
||
// can't defer these, because they need to happen before out.Bytes() below | ||
tarWriter.Close() | ||
gzipWriter.Close() | ||
|
||
return out.Bytes(), nil | ||
} | ||
|
||
func verWith(name string, contents []byte) item { | ||
res := item{ | ||
meta: bucketObject{Name: name}, | ||
contents: contents, | ||
} | ||
hash := md5.Sum(res.contents) //nolint:gosec | ||
res.meta.Hash = base64.StdEncoding.EncodeToString(hash[:]) | ||
return res | ||
} |
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