Skip to content

Commit

Permalink
Use the repository cache as dir
Browse files Browse the repository at this point in the history
Signed-off-by: Kartikey Mamgain <[email protected]>
  • Loading branch information
Kartikey-star committed Sep 23, 2022
1 parent 01ae1e7 commit 0923cfc
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 28 deletions.
6 changes: 5 additions & 1 deletion internal/chartverifier/api/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"github.com/spf13/viper"
"helm.sh/helm/v3/pkg/cli"

"time"

Expand Down Expand Up @@ -29,6 +30,7 @@ type RunOptions struct {
SuppressErrorLog bool
ClientTimeout time.Duration
ChartUri string
Settings *cli.EnvSettings
}

func Run(options RunOptions) (*apireport.Report, error) {
Expand All @@ -39,7 +41,8 @@ func Run(options RunOptions) (*apireport.Report, error) {

verifierBuilder.SetValues(options.Values).
SetConfig(options.ViperConfig).
SetOverrides(options.Overrides)
SetOverrides(options.Overrides).
SetSettings(options.Settings)

profileChecks := profiles.New(options.Overrides).FilterChecks(allChecks)

Expand All @@ -59,6 +62,7 @@ func Run(options RunOptions) (*apireport.Report, error) {
SetOpenShiftVersion(options.OpenShiftVersion).
SetProviderDelivery(options.ProviderDelivery).
SetTimeout(options.ClientTimeout).
SetSettings(options.Settings).
Build()

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/chartverifier/checks/charttesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func ChartTesting(opts *CheckOptions) (Result, error) {
return NewResult(false, err.Error()), nil
}

_, path, err := LoadChartFromURI(opts.URI)
_, path, err := LoadChartFromURI(opts)
if err != nil {
utils.LogError("End chart install and test check with LoadChartFromURI error")
return NewResult(false, err.Error()), nil
Expand Down
22 changes: 11 additions & 11 deletions internal/chartverifier/checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func notImplemented() (Result, error) {
}

func IsHelmV3(opts *CheckOptions) (Result, error) {
c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return Result{}, err
}
Expand All @@ -85,7 +85,7 @@ func IsHelmV3(opts *CheckOptions) (Result, error) {
}

func HasReadme(opts *CheckOptions) (Result, error) {
c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return Result{}, err
}
Expand All @@ -101,7 +101,7 @@ func HasReadme(opts *CheckOptions) (Result, error) {
}

func ContainsTest(opts *CheckOptions) (Result, error) {
c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return Result{}, err
}
Expand All @@ -120,7 +120,7 @@ func ContainsTest(opts *CheckOptions) (Result, error) {
}

func ContainsValues(opts *CheckOptions) (Result, error) {
c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return Result{}, err
}
Expand All @@ -135,7 +135,7 @@ func ContainsValues(opts *CheckOptions) (Result, error) {
}

func ContainsValuesSchema(opts *CheckOptions) (Result, error) {
c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return Result{}, err
}
Expand All @@ -162,7 +162,7 @@ func IsCommunityChart(opts *CheckOptions) (Result, error) {
}

func HasKubeVersion(opts *CheckOptions) (Result, error) {
c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return NewResult(false, err.Error()), err
}
Expand All @@ -177,7 +177,7 @@ func HasKubeVersion(opts *CheckOptions) (Result, error) {

func HasKubeVersion_V1_1(opts *CheckOptions) (Result, error) {

c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return NewResult(false, err.Error()), err
}
Expand All @@ -197,7 +197,7 @@ func HasKubeVersion_V1_1(opts *CheckOptions) (Result, error) {
}

func NotContainCRDs(opts *CheckOptions) (Result, error) {
c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return NewResult(false, err.Error()), err
}
Expand All @@ -213,7 +213,7 @@ func NotContainCRDs(opts *CheckOptions) (Result, error) {
}

func HelmLint(opts *CheckOptions) (Result, error) {
_, p, err := LoadChartFromURI(opts.URI)
_, p, err := LoadChartFromURI(opts)
if err != nil {
return NewResult(false, err.Error()), err
}
Expand All @@ -234,7 +234,7 @@ func NotContainsInfraPluginsAndDrivers(opts *CheckOptions) (Result, error) {
}

func NotContainCSIObjects(opts *CheckOptions) (Result, error) {
c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return Result{}, err
}
Expand Down Expand Up @@ -306,7 +306,7 @@ func ImagesAreCertified(opts *CheckOptions) (Result, error) {
}

func RequiredAnnotationsPresent(opts *CheckOptions) (Result, error) {
c, _, err := LoadChartFromURI(opts.URI)
c, _, err := LoadChartFromURI(opts)
if err != nil {
return NewResult(false, err.Error()), err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/chartverifier/checks/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ func TestIsHelmV3(t *testing.T) {

for _, tc := range negativeTestCases {
config := viper.New()
settings := cli.New()
t.Run(tc.description, func(t *testing.T) {
r, err := IsHelmV3(&CheckOptions{URI: tc.uri, ViperConfig: config})
r, err := IsHelmV3(&CheckOptions{URI: tc.uri, ViperConfig: config, HelmEnvSettings: settings})
require.NoError(t, err)
require.NotNil(t, r)
require.False(t, r.Ok)
Expand Down
24 changes: 16 additions & 8 deletions internal/chartverifier/checks/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,18 @@ func (c *chartCache) Get(uri string) (ChartCacheItem, bool, error) {
}
}

func (c *chartCache) Add(uri string, chrt *chart.Chart) (ChartCacheItem, error) {
userCacheDir, err := os.UserCacheDir()
if err != nil {
return ChartCacheItem{}, err
func (c *chartCache) Add(uri string, repositoryCache string, chrt *chart.Chart) (ChartCacheItem, error) {
var (
err error
userCacheDir string
)
if repositoryCache == "" {
userCacheDir, err = os.UserCacheDir()
if err != nil {
return ChartCacheItem{}, err
}
} else {
userCacheDir = repositoryCache
}
cacheDir := path.Join(userCacheDir, "chart-verifier")
key := c.MakeKey(uri)
Expand All @@ -143,17 +151,17 @@ func init() {

// LoadChartFromURI attempts to retrieve a chart from the given uri string. It accepts "http", "https", "file" schemes,
// and defaults to "file" if there isn't one.
func LoadChartFromURI(uri string) (*chart.Chart, string, error) {
func LoadChartFromURI(opts *CheckOptions) (*chart.Chart, string, error) {
var (
chrt *chart.Chart
err error
)

if cached, ok, _ := defaultChartCache.Get(uri); ok {
if cached, ok, _ := defaultChartCache.Get(opts.URI); ok {
return cached.Chart, cached.Path, nil
}

u, err := url.Parse(uri)
u, err := url.Parse(opts.URI)
if err != nil {
return nil, "", err
}
Expand All @@ -171,7 +179,7 @@ func LoadChartFromURI(uri string) (*chart.Chart, string, error) {
return nil, "", err
}

if cached, err := defaultChartCache.Add(uri, chrt); err != nil {
if cached, err := defaultChartCache.Add(opts.URI, opts.HelmEnvSettings.RepositoryCache, chrt); err != nil {
return nil, "", err
} else {
return cached.Chart, cached.Path, nil
Expand Down
70 changes: 68 additions & 2 deletions internal/chartverifier/checks/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"io/ioutil"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"helm.sh/helm/v3/pkg/cli"

"github.com/redhat-certification/chart-verifier/internal/testutil"
)
Expand Down Expand Up @@ -56,27 +58,91 @@ func TestLoadChartFromURI(t *testing.T) {
},
}

repositoryCacheSetCases := []testCase{
{
uri: "chart-0.1.0-v3.valid.tgz",
description: "temporary cache defined",
},
}

negativeCasesRepositoryCache := []testCase{
{
uri: "chart-0.1.0-v3.non-existing.tgz",
description: "non existing file with repository cache set",
},
{
uri: "http://" + addr + "/charts/chart-0.1.0-v3.non-existing.tgz",
description: "non existing remote file with repository cache set",
},
}

ctx, cancel := context.WithCancel(context.Background())
require.NoError(t, testutil.ServeCharts(ctx, addr, "./"))

for _, tc := range positiveCases {
t.Run(tc.description, func(t *testing.T) {
c, _, err := LoadChartFromURI(tc.uri)
opts := CheckOptions{
URI: tc.uri,
Values: map[string]interface{}{
"k8Project": "bogus",
},
ViperConfig: viper.New(),
HelmEnvSettings: cli.New(),
}
c, _, err := LoadChartFromURI(&opts)
require.NoError(t, err)
require.NotNil(t, c)
})
}

for _, tc := range negativeCases {
t.Run(tc.description, func(t *testing.T) {
c, _, err := LoadChartFromURI(tc.uri)
opts := CheckOptions{
URI: tc.uri,
Values: map[string]interface{}{
"k8Project": "bogus",
},
ViperConfig: viper.New(),
HelmEnvSettings: cli.New(),
}
c, _, err := LoadChartFromURI(&opts)
require.Error(t, err)
require.True(t, IsChartNotFound(err))
require.Equal(t, "chart not found: "+tc.uri, err.Error())
require.Nil(t, c)
})
}

for _, tc := range repositoryCacheSetCases {
t.Run(tc.description, func(t *testing.T) {
settings := cli.New()
settings.RepositoryCache = "/tmp"
opts := CheckOptions{
URI: tc.uri,
ViperConfig: viper.New(),
HelmEnvSettings: settings,
}
c, _, err := LoadChartFromURI(&opts)
require.NoError(t, err)
require.NotNil(t, c)
})
}
for _, tc := range negativeCasesRepositoryCache {
t.Run(tc.description, func(t *testing.T) {
settings := cli.New()
settings.RepositoryCache = "/tmp"
opts := CheckOptions{
URI: tc.uri,
ViperConfig: viper.New(),
HelmEnvSettings: settings,
}
c, _, err := LoadChartFromURI(&opts)
require.Error(t, err)
require.True(t, IsChartNotFound(err))
require.Equal(t, "chart not found: "+tc.uri, err.Error())
require.Nil(t, c)
})
}
cancel()
}

Expand Down
16 changes: 14 additions & 2 deletions internal/chartverifier/reportbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"strings"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"helm.sh/helm/v3/pkg/cli"

"github.com/redhat-certification/chart-verifier/internal/chartverifier/checks"
"github.com/stretchr/testify/require"
Expand All @@ -26,7 +28,12 @@ func TestSha(t *testing.T) {
// get shas for each file
for _, chart := range charts {
t.Run("Sha generation : "+chart, func(t *testing.T) {
helmChart, _, err := checks.LoadChartFromURI(chart)
opts := checks.CheckOptions{
URI: chart,
ViperConfig: viper.New(),
HelmEnvSettings: cli.New(),
}
helmChart, _, err := checks.LoadChartFromURI(&opts)
require.NoError(t, err)
sha := GenerateSha(helmChart.Raw)
require.NotNil(t, sha)
Expand All @@ -39,7 +46,12 @@ func TestSha(t *testing.T) {
for _, chart := range charts {

t.Run("Sha must not change : "+chart, func(t *testing.T) {
helmChart, _, err := checks.LoadChartFromURI(chart)
opts := checks.CheckOptions{
URI: chart,
ViperConfig: viper.New(),
HelmEnvSettings: cli.New(),
}
helmChart, _, err := checks.LoadChartFromURI(&opts)
require.NoError(t, err)
sha := GenerateSha(helmChart.Raw)
require.NotNil(t, sha)
Expand Down
5 changes: 3 additions & 2 deletions internal/chartverifier/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
package chartverifier

import (
"time"

"github.com/redhat-certification/chart-verifier/internal/chartverifier/checks"
"github.com/redhat-certification/chart-verifier/internal/chartverifier/profiles"
apiReport "github.com/redhat-certification/chart-verifier/pkg/chartverifier/report"
"github.com/spf13/viper"
helmcli "helm.sh/helm/v3/pkg/cli"
"time"
)

type CheckNotFoundErr string
Expand Down Expand Up @@ -87,7 +88,7 @@ func (c *verifier) Verify(uri string) (*apiReport.Report, error) {
}
}

chrt, _, err := checks.LoadChartFromURI(uri)
chrt, _, err := checks.LoadChartFromURI(&checks.CheckOptions{HelmEnvSettings: c.settings, URI: uri})
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/chartverifier/verifier/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func (v *Verifier) Run(chart_uri string) (ApiVerifier, error) {
settings := cli.New()

setHelmEnv(settings, v.Inputs.Flags.StringFlags)
runOptions.Settings = settings

if valueMap, ok := v.Inputs.Flags.ValuesFlags[ChartSet]; ok {
opts.Values = mapToStringSlice(valueMap)
Expand Down

0 comments on commit 0923cfc

Please sign in to comment.