|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "sigs.k8s.io/e2e-framework/klient/wait" |
| 11 | + "sigs.k8s.io/e2e-framework/klient/wait/conditions" |
| 12 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 13 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 14 | + "sigs.k8s.io/e2e-framework/third_party/helm" |
| 15 | + |
| 16 | + storagev1alpha1 "github.com/rancher/sbombastic/api/storage/v1alpha1" |
| 17 | + v1alpha1 "github.com/rancher/sbombastic/api/v1alpha1" |
| 18 | +) |
| 19 | + |
| 20 | +func EqualReference(img storagev1alpha1.ImageMetadata, registryURI, registryRepository, tag string) bool { |
| 21 | + return img.RegistryURI == registryURI && |
| 22 | + img.Repository == registryRepository && |
| 23 | + img.Tag == tag |
| 24 | +} |
| 25 | + |
| 26 | +func TestRegistryCreation(t *testing.T) { |
| 27 | + releaseName := "sbombastic" |
| 28 | + registryName := "test-registry" |
| 29 | + registryURI := "ghcr.io" |
| 30 | + registryRepository := "rancher-sandbox/sbombastic/test-assets/golang" |
| 31 | + |
| 32 | + crName := "dfe56d8371e7df15a3dde25c33a78b84b79766de2ab5a5897032019c878b5932" |
| 33 | + |
| 34 | + f := features.New("Start Registry CR Creation test"). |
| 35 | + Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 36 | + manager := helm.New(cfg.KubeconfigFile()) |
| 37 | + err := manager.RunInstall(helm.WithName(releaseName), |
| 38 | + helm.WithNamespace(cfg.Namespace()), |
| 39 | + helm.WithChart("../../helm"), |
| 40 | + helm.WithWait(), |
| 41 | + helm.WithArgs("--set", "controller.image.tag=latest", |
| 42 | + "--set", "storage.image.tag=latest", |
| 43 | + "--set", "worker.image.tag=latest"), |
| 44 | + helm.WithTimeout("3m")) |
| 45 | + |
| 46 | + require.NoError(t, err, "sbombastic helm chart is not installed correctly") |
| 47 | + |
| 48 | + err = storagev1alpha1.AddToScheme(cfg.Client().Resources(cfg.Namespace()).GetScheme()) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + err = v1alpha1.AddToScheme(cfg.Client().Resources(cfg.Namespace()).GetScheme()) |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + return ctx |
| 55 | + }). |
| 56 | + Assess("Create Registry CR", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 57 | + registry := &v1alpha1.Registry{ |
| 58 | + ObjectMeta: metav1.ObjectMeta{ |
| 59 | + Name: registryName, |
| 60 | + Namespace: cfg.Namespace(), |
| 61 | + }, |
| 62 | + Spec: v1alpha1.RegistrySpec{ |
| 63 | + URI: registryURI, |
| 64 | + Repositories: []string{registryRepository}, |
| 65 | + }, |
| 66 | + } |
| 67 | + err := cfg.Client().Resources().Create(ctx, registry) |
| 68 | + require.NoError(t, err) |
| 69 | + return ctx |
| 70 | + }). |
| 71 | + Assess("Verify the Image is created", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 72 | + images := storagev1alpha1.ImageList{ |
| 73 | + Items: []storagev1alpha1.Image{ |
| 74 | + {ObjectMeta: metav1.ObjectMeta{Name: crName, Namespace: cfg.Namespace()}}, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + err := wait.For(conditions.New(cfg.Client().Resources()).ResourcesFound(&images)) |
| 79 | + if err != nil { |
| 80 | + t.Error(err) |
| 81 | + } |
| 82 | + return ctx |
| 83 | + }). |
| 84 | + Assess("Verify the SPDX SBOM is created", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 85 | + sboms := storagev1alpha1.SBOMList{ |
| 86 | + Items: []storagev1alpha1.SBOM{ |
| 87 | + {ObjectMeta: metav1.ObjectMeta{Name: crName, Namespace: cfg.Namespace()}}, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + err := wait.For(conditions.New(cfg.Client().Resources()).ResourcesFound(&sboms)) |
| 92 | + if err != nil { |
| 93 | + t.Error(err) |
| 94 | + } |
| 95 | + return ctx |
| 96 | + }). |
| 97 | + Assess("Verify the VulnerabilityReport is created", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 98 | + vulnReports := storagev1alpha1.VulnerabilityReportList{ |
| 99 | + Items: []storagev1alpha1.VulnerabilityReport{ |
| 100 | + {ObjectMeta: metav1.ObjectMeta{Name: crName, Namespace: cfg.Namespace()}}, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + err := wait.For(conditions.New(cfg.Client().Resources()).ResourcesFound(&vulnReports)) |
| 105 | + if err != nil { |
| 106 | + t.Error(err) |
| 107 | + } |
| 108 | + return ctx |
| 109 | + }). |
| 110 | + Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 111 | + manager := helm.New(cfg.KubeconfigFile()) |
| 112 | + err := manager.RunUninstall( |
| 113 | + helm.WithName(releaseName), |
| 114 | + helm.WithNamespace(cfg.Namespace()), |
| 115 | + ) |
| 116 | + assert.NoError(t, err, "sbombastic helm chart is not deleted correctly") |
| 117 | + return ctx |
| 118 | + }) |
| 119 | + |
| 120 | + testenv.Test(t, f.Feature()) |
| 121 | +} |
0 commit comments