-
Notifications
You must be signed in to change notification settings - Fork 9
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
Showing
32 changed files
with
1,777 additions
and
322 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 |
---|---|---|
@@ -1 +1,83 @@ | ||
package chartutils | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"testing" | ||
|
||
tu "github.com/vmware-labs/distribution-tooling-for-helm/internal/testutil" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type ChartUtilsTestSuite struct { | ||
suite.Suite | ||
sb *tu.Sandbox | ||
} | ||
|
||
func (suite *ChartUtilsTestSuite) TearDownSuite() { | ||
_ = suite.sb.Cleanup() | ||
} | ||
|
||
func (suite *ChartUtilsTestSuite) SetupSuite() { | ||
suite.sb = tu.NewSandbox() | ||
} | ||
|
||
func TestChartUtilsTestSuite(t *testing.T) { | ||
suite.Run(t, new(ChartUtilsTestSuite)) | ||
} | ||
|
||
func (suite *ChartUtilsTestSuite) TestAnnotateChart() { | ||
t := suite.T() | ||
require := suite.Require() | ||
|
||
sb := suite.sb | ||
serverURL := "localhost" | ||
scenarioName := "plain-chart" | ||
defaultAnnotationsKey := "images" | ||
// customAnnotationsKey := "artifacthub.io/images" | ||
scenarioDir := fmt.Sprintf("../testdata/scenarios/%s", scenarioName) | ||
|
||
type testImage struct { | ||
Name string | ||
Registry string | ||
Repository string | ||
Tag string | ||
Digest string | ||
} | ||
|
||
images := []testImage{ | ||
{ | ||
Name: "bitnami-shell", | ||
Registry: "docker.io", | ||
Repository: "bitnami/bitnami-shell", | ||
Tag: "1.0.0", | ||
}, | ||
{ | ||
Name: "wordpress", | ||
Registry: "docker.io", | ||
Repository: "bitnami/wordpress", | ||
Tag: "latest", | ||
}, | ||
} | ||
t.Run("Annotates a chart", func(t *testing.T) { | ||
dest := sb.TempFile() | ||
chartDir := filepath.Join(dest, scenarioName) | ||
annotationsKey := defaultAnnotationsKey | ||
require.NoError(tu.RenderScenario(scenarioDir, dest, | ||
map[string]interface{}{"ServerURL": serverURL, "ValuesImages": images}, | ||
)) | ||
|
||
expectedImages := make([]tu.AnnotationEntry, 0) | ||
for _, img := range images { | ||
url := fmt.Sprintf("%s/%s:%s", img.Registry, img.Repository, img.Tag) | ||
expectedImages = append(expectedImages, tu.AnnotationEntry{ | ||
Name: img.Name, | ||
Image: url, | ||
}) | ||
} | ||
|
||
require.NoError(AnnotateChart(chartDir, WithAnnotationsKey(annotationsKey))) | ||
tu.AssertChartAnnotations(t, chartDir, annotationsKey, expectedImages) | ||
}) | ||
} |
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,150 @@ | ||
package chartutils | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/go-containerregistry/pkg/crane" | ||
"github.com/google/go-containerregistry/pkg/registry" | ||
"github.com/vmware-labs/distribution-tooling-for-helm/imagelock" | ||
tu "github.com/vmware-labs/distribution-tooling-for-helm/internal/testutil" | ||
) | ||
|
||
func (suite *ChartUtilsTestSuite) TestPullImages() { | ||
require := suite.Require() | ||
t := suite.T() | ||
|
||
silentLog := log.New(io.Discard, "", 0) | ||
s := httptest.NewServer(registry.New(registry.Logger(silentLog))) | ||
defer s.Close() | ||
u, err := url.Parse(s.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
imageName := "test:mytag" | ||
|
||
images, err := tu.AddSampleImagesToRegistry(imageName, u.Host) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
sb := suite.sb | ||
|
||
serverURL := u.Host | ||
scenarioName := "complete-chart" | ||
chartName := "test" | ||
|
||
scenarioDir := fmt.Sprintf("../testdata/scenarios/%s", scenarioName) | ||
|
||
suite.T().Run("Pulls images", func(t *testing.T) { | ||
dest := sb.TempFile() | ||
require.NoError(tu.RenderScenario(scenarioDir, dest, | ||
map[string]interface{}{"ServerURL": serverURL, "Images": images, "Name": chartName, "RepositoryURL": serverURL}, | ||
)) | ||
chartDir := filepath.Join(dest, scenarioName) | ||
imagesDir := filepath.Join(chartDir, "images") | ||
|
||
require.NoError(err) | ||
|
||
lock, err := imagelock.FromYAMLFile(filepath.Join(chartDir, "Images.lock")) | ||
require.NoError(err) | ||
require.NoError(PullImages(lock, imagesDir)) | ||
|
||
require.DirExists(imagesDir) | ||
|
||
for _, imgData := range images { | ||
for _, digestData := range imgData.Digests { | ||
imgFile := filepath.Join(imagesDir, fmt.Sprintf("%s.tar", digestData.Digest.Encoded())) | ||
suite.Assert().FileExists(imgFile) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func (suite *ChartUtilsTestSuite) TestPushImages() { | ||
|
||
t := suite.T() | ||
sb := suite.sb | ||
require := suite.Require() | ||
assert := suite.Assert() | ||
|
||
silentLog := log.New(io.Discard, "", 0) | ||
s := httptest.NewServer(registry.New(registry.Logger(silentLog))) | ||
defer s.Close() | ||
|
||
u, err := url.Parse(s.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
serverURL := u.Host | ||
|
||
t.Run("Pushing works", func(t *testing.T) { | ||
scenarioName := "complete-chart" | ||
chartName := "test" | ||
|
||
scenarioDir := fmt.Sprintf("../testdata/scenarios/%s", scenarioName) | ||
imageName := "test:mytag" | ||
|
||
imageData := tu.ImageData{Name: "test", Image: "test:mytag"} | ||
architectures := []string{ | ||
"linux/amd64", | ||
"linux/arm", | ||
} | ||
craneImgs, err := tu.CreateSampleImages(&imageData, architectures) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
require.Equal(len(architectures), len(imageData.Digests)) | ||
|
||
images := []tu.ImageData{imageData} | ||
dest := sb.TempFile() | ||
require.NoError(tu.RenderScenario(scenarioDir, dest, | ||
map[string]interface{}{ | ||
"ServerURL": serverURL, "Images": images, | ||
"Name": chartName, "RepositoryURL": serverURL, | ||
}, | ||
)) | ||
|
||
chartDir := filepath.Join(dest, scenarioName) | ||
imagesDir := filepath.Join(chartDir, "images") | ||
require.NoError(os.MkdirAll(imagesDir, 0755)) | ||
for _, img := range craneImgs { | ||
d, err := img.Digest() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
imgFile := filepath.Join(imagesDir, fmt.Sprintf("%s.tar", d.Hex)) | ||
if err := crane.Save(img, imageName, imgFile); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
t.Run("Push images", func(t *testing.T) { | ||
require.NoError(err) | ||
lock, err := imagelock.FromYAMLFile(filepath.Join(chartDir, "Images.lock")) | ||
require.NoError(err) | ||
require.NoError(PushImages(lock, imagesDir)) | ||
|
||
// Verify the images were pushed | ||
for _, img := range images { | ||
src := fmt.Sprintf("%s/%s", u.Host, img.Image) | ||
remoteDigests, err := tu.ReadRemoteImageManifest(src) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for _, dgstData := range img.Digests { | ||
assert.Equal(dgstData.Digest.Hex(), remoteDigests[dgstData.Arch].Digest.Hex()) | ||
} | ||
} | ||
}) | ||
}) | ||
} |
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,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func (suite *CmdSuite) TestChartsHelp() { | ||
t := suite.T() | ||
t.Run("Shows Help", func(t *testing.T) { | ||
res := dt("charts") | ||
res.AssertSuccess(t) | ||
for _, reStr := range []string{ | ||
`annotate\s+Annotates a Helm chart`, | ||
`relocate\s+Relocates a Helm chart`, | ||
} { | ||
res.AssertSuccessMatch(t, fmt.Sprintf(`(?s).*Available Commands:.*\n\s*%s.*`, reStr)) | ||
} | ||
}) | ||
} |
Oops, something went wrong.