func (ts *chartTestSuite) AssertChartEqual(exp, got *chart.Chart) bool {
	ts.t.Helper()
	expSet := exp == nil
	gotSet := got == nil
	if expSet != gotSet {
		assert.Fail(ts.t, fmt.Sprintf("mismatch on charts existence, exp set: %t, got set: %t", expSet, gotSet))
		return false
	}
	expMetaSet := exp.Metadata == nil
	gotMetaSet := got.Metadata == nil
	if expMetaSet != gotMetaSet {
		assert.Fail(ts.t, fmt.Sprintf("mismatch on metadata status, exp set: %t, got set: %t", expMetaSet, gotMetaSet))
		return false
	}
	result := assert.Equal(ts.t, exp.Metadata.Name, got.Metadata.Name, "mismatch on Name")
	result = assert.Equal(ts.t, exp.Metadata.Version, got.Metadata.Version, "mismatch on Version") && result
	result = assert.Equal(ts.t, exp.Metadata.Description, got.Metadata.Description, "mismatch on Description") && result
	return result
}
AssertAddonEqual performs partial match for addon.
It's suitable only for tests as match is PARTIAL.
package testing
import (
	"encoding/json"
	"fmt"
	"testing"
	"github.com/Masterminds/semver"
	"github.com/stretchr/testify/assert"
	"helm.sh/helm/v3/pkg/chart"
	"github.com/kyma-project/helm-broker/internal"
	"github.com/kyma-project/helm-broker/internal/storage"
	"github.com/stretchr/testify/require"
	"helm.sh/helm/v3/pkg/chart/loader"
)
func TestChartGet(t *testing.T) {
	tRunDrivers(t, "Found", func(t *testing.T, sf storage.Factory) {
		ts := newChartTestSuite(t, sf)
		ts.PopulateStorage()
		exp := ts.MustGetFixture("A1")
		got, err := ts.s.Get(internal.ClusterWide, internal.ChartName(exp.Metadata.Name), *semver.MustParse(exp.Metadata.Version))
		assert.NoError(t, err)
		ts.AssertChartEqual(exp, got)
	})
	tRunDrivers(t, "NotFound", func(t *testing.T, sf storage.Factory) {
		ts := newChartTestSuite(t, sf)
		exp := ts.MustGetFixture("A1")
		got, err := ts.s.Get(internal.ClusterWide, internal.ChartName(exp.Metadata.Name), *semver.MustParse(exp.Metadata.Version))
		ts.AssertNotFoundError(err)
		assert.Nil(t, got)
	})
}
func TestChartUpsert(t *testing.T) {
	tRunDrivers(t, "Success/New", func(t *testing.T, sf storage.Factory) {
		ts := newChartTestSuite(t, sf)
		fix := ts.MustGetFixture("A1")
		replace, err := ts.s.Upsert(internal.ClusterWide, fix)
		assert.NoError(t, err)
		assert.False(t, replace)
	})
	tRunDrivers(t, "Success/Replace", func(t *testing.T, sf storage.Factory) {
		expDesc := "updated description"
		ts := newChartTestSuite(t, sf)
		fix := ts.MustGetFixture("A1")
		ts.s.Upsert(internal.ClusterWide, fix)
		fixNew := ts.MustCopyFixture(fix)
		fixNew.Metadata.Description = expDesc
		replace, err := ts.s.Upsert(internal.ClusterWide, fixNew)
		assert.NoError(t, err)
		assert.True(t, replace)
		got, err := ts.s.Get(internal.ClusterWide, internal.ChartName(fixNew.Metadata.Name), *semver.MustParse(fixNew.Metadata.Version))
		assert.NoError(t, err)
		ts.AssertChartEqual(fixNew, got)
	})
	tRunDrivers(t, "Failure/EmptyVersion", func(t *testing.T, sf storage.Factory) {
		ts := newChartTestSuite(t, sf)
		fix := ts.MustGetFixture("A1")
		fix.Metadata.Version = ""
		_, err := ts.s.Upsert(internal.ClusterWide, fix)
		assert.EqualError(t, err, "both name and version must be set")
	})
}
func TestHappyPath(t *testing.T) {
	tRunDrivers(t, "Upsert and get preserves dependencies", func(t *testing.T, sf storage.Factory) {
		ts := newChartTestSuite(t, sf)
		chrt, err := loader.LoadDir("testing")
		require.NoError(t, err)
		ts.s.Upsert("ns1", chrt)
		got, err := ts.s.Get("ns1", internal.ChartName(chrt.Name()), *semver.MustParse(chrt.Metadata.Version))
		require.NoError(t, err)
		assert.Len(t, chrt.Dependencies(), len(got.Dependencies()))
	})
}
func TestChartRemove(t *testing.T) {
	tRunDrivers(t, "Found", func(t *testing.T, sf storage.Factory) {
		ts := newChartTestSuite(t, sf)
		ts.PopulateStorage()
		exp := ts.MustGetFixture("A1")
		err := ts.s.Remove(internal.ClusterWide, internal.ChartName(exp.Metadata.Name), *semver.MustParse(exp.Metadata.Version))
		assert.NoError(t, err)
		ts.AssertChartDoesNotExist(exp)
	})
	tRunDrivers(t, "NotFound", func(t *testing.T, sf storage.Factory) {
		ts := newChartTestSuite(t, sf)
		exp := ts.MustGetFixture("A1")
		err := ts.s.Remove(internal.ClusterWide, internal.ChartName(exp.Metadata.Name), *semver.MustParse(exp.Metadata.Version))
		ts.AssertNotFoundError(err)
	})
}
func TestMultipleNamespaces(t *testing.T) {
	tRunDrivers(t, "Upsert", func(t *testing.T, sf storage.Factory) {
		ts := newChartTestSuite(t, sf)
		ns1 := internal.Namespace("stage")
		chartForNS := chart.Chart{
			Metadata: &chart.Metadata{
				Name:        "redis",
				Version:     "1.0",
				Description: "description for NS",
			},
		}
		chartForCluster := chart.Chart{
			Metadata: &chart.Metadata{
				Name:        "redis",
				Version:     "1.0",
				Description: "description for Cluster",
			},
		}
		r, err := ts.s.Upsert(ns1, &chartForNS)
		require.NoError(t, err)
		require.False(t, r)
		r, err = ts.s.Upsert(internal.ClusterWide, &chartForCluster)
		require.NoError(t, err)
		assert.False(t, r, "upsert must not replace a chart stored in other namespace")
	})
	tRunDrivers(t, "Get", func(t *testing.T, sf storage.Factory) {
		ts := newChartTestSuite(t, sf)
		ns1 := internal.Namespace("stage")
		ns2 := internal.Namespace("prod")
		chartForNS := chart.Chart{
			Metadata: &chart.Metadata{
				Name:        "redis",
				Version:     "1.0",
				Description: "description for NS",
			},
		}
		chartForCluster := chart.Chart{
			Metadata: &chart.Metadata{
				Name:        "redis",
				Version:     "1.0",
				Description: "description for Cluster",
			},
		}
		_, err := ts.s.Upsert(ns1, &chartForNS)
		require.NoError(t, err)
		_, err = ts.s.Upsert(internal.ClusterWide, &chartForCluster)
		require.NoError(t, err)
		cCh, err := ts.s.Get(internal.ClusterWide, "redis", *semver.MustParse("1.0"))
		require.NoError(t, err)
		nCh, err := ts.s.Get(ns1, "redis", *semver.MustParse("1.0"))
		require.NoError(t, err)
		_, prodErr := ts.s.Get(ns2, "redis", *semver.MustParse("1.0"))
		ts.AssertChartEqual(&chartForCluster, cCh)
		ts.AssertChartEqual(&chartForNS, nCh)
		ts.AssertNotFoundError(prodErr)
	})
}
func newChartTestSuite(t *testing.T, sf storage.Factory) *chartTestSuite {
	ts := chartTestSuite{
		t:                   t,
		s:                   sf.Chart(),
		fixtures:            make(map[chartNameVersion]*chart.Chart),
		fixturesSymToKeyMap: make(map[string]chartNameVersion),
	}
	ts.generateFixtures()
	return &ts
}
type chartNameVersion string
type chartTestSuite struct {
	t                   *testing.T
	s                   storage.Chart
	fixtures            map[chartNameVersion]*chart.Chart
	fixturesSymToKeyMap map[string]chartNameVersion
}
func (chartTestSuite) key(name internal.ChartName, ver semver.Version) chartNameVersion {
	return chartNameVersion(fmt.Sprintf("%s|%s", name, ver.String()))
}
func (chartTestSuite) mustKeyFromChart(c *chart.Chart) chartNameVersion {
	if c.Metadata == nil {
		panic("metadata must not be nil")
	}
	return chartNameVersion(fmt.Sprintf("%s|%s", c.Metadata.Name, c.Metadata.Version))
}
func (ts *chartTestSuite) generateFixtures() {
	for fs, ft := range map[string]struct{ id, name, version, desc string }{
		"A1": {"id-A-001", "name-A", "0.0.1", "desc-A-001"},
		"A2": {"id-A-002", "name-A", "0.0.2", "desc-A-002"},
		"B1": {"id-B-001", "name-B", "0.0.1", "desc-B-001"},
		"B2": {"id-B-002", "name-B", "0.0.2", "desc-B-002"},
	} {
		c := &chart.Chart{
			Metadata: &chart.Metadata{
				Name:        ft.name,
				Version:     ft.version,
				Description: ft.desc,
			},
		}
		k := ts.mustKeyFromChart(c)
		ts.fixtures[k] = c
		ts.fixturesSymToKeyMap[fs] = k
	}
}
func (ts *chartTestSuite) PopulateStorage() {
	for _, b := range ts.fixtures {
		ts.s.Upsert(internal.ClusterWide, ts.MustCopyFixture(b))
	}
}
func (ts *chartTestSuite) MustGetFixture(sym string) *chart.Chart {
	k, found := ts.fixturesSymToKeyMap[sym]
	if !found {
		panic(fmt.Sprintf("fixture symbol not found, sym: %s", sym))
	}
	b, found := ts.fixtures[k]
	if !found {
		panic(fmt.Sprintf("fixture not found, sym: %s, nameVersion: %s", sym, k))
	}
	return b
}
func (ts *chartTestSuite) MustCopyFixture(in *chart.Chart) *chart.Chart {
	m, err := json.Marshal(in)
	if err != nil {
		panic(fmt.Sprintf("input chart marchaling failed, err: %s", err))
	}
	var out chart.Chart
	if err := json.Unmarshal(m, &out); err != nil {
		panic(fmt.Sprintf("input chart unmarchaling failed, err: %s", err))
	}
	return &out
}