Skip to content

Commit 5e804d6

Browse files
committed
ci: add more verifications to validate the resource
1 parent ba273c8 commit 5e804d6

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

sysdig/resource_sysdig_secure_vulnerability_rule_bundle.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func vulnerabilityRuleSchemaImageConfigLabel() *schema.Schema {
2020
Type: schema.TypeSet,
2121
Optional: true,
2222
MaxItems: 1,
23-
MinItems: 1,
2423
Elem: &schema.Resource{
2524
Schema: map[string]*schema.Schema{
2625
"id": {
@@ -93,8 +92,10 @@ func resourceSysdigSecureVulnerabilityRuleBundle() *schema.Resource {
9392
},
9493

9594
"rule": {
96-
Type: schema.TypeList,
97-
Required: true,
95+
Type: schema.TypeList,
96+
Required: true,
97+
MinItems: 1,
98+
Description: "Rules for this bundle",
9899
Elem: &schema.Resource{
99100
Schema: map[string]*schema.Schema{
100101
"image_label": vulnerabilityRuleSchemaImageConfigLabel(),
@@ -307,6 +308,9 @@ func vulnerabilityRulesFromList(list []any) ([]v2.VulnerabilityRule, error) {
307308
var out []v2.VulnerabilityRule
308309

309310
for _, ruleRaw := range list {
311+
if ruleRaw == nil {
312+
return nil, errors.New("empty rule detected, you need to specify one")
313+
}
310314
rule, err := vulnerabilityRuleFromMap(ruleRaw.(map[string]any))
311315
if err != nil {
312316
return nil, err
@@ -381,5 +385,9 @@ func vulnerabilityRuleImageConfigLabelFromMap(ruleBody map[string]any) (v2.Vulne
381385
})
382386
}
383387

388+
if len(rule.Predicates) == 0 {
389+
return v2.VulnerabilityRule{}, errors.New("no predicate has been specified for image label rule")
390+
}
391+
384392
return rule, nil
385393
}

sysdig/resource_sysdig_secure_vulnerability_rule_bundle_test.go

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package sysdig_test
55
import (
66
"fmt"
77
"os"
8+
"regexp"
89
"testing"
910

1011
"github.com/draios/terraform-provider-sysdig/sysdig"
@@ -26,22 +27,82 @@ func TestAccVulnerabilityRuleBundle(t *testing.T) {
2627
"sysdig": func() (*schema.Provider, error) { return sysdig.Provider(), nil },
2728
},
2829
Steps: []resource.TestStep{
30+
{
31+
Config: incorrectVulnerabilityRuleBundleConfig(random()),
32+
ExpectError: regexp.MustCompile("empty rule detected, you need to specify one"),
33+
},
34+
{
35+
Config: incorrectVulnerabilityRuleBundleConfig2(random()),
36+
ExpectError: regexp.MustCompile(`No more than 1 "image_label" blocks are allowed`),
37+
},
38+
{
39+
Config: incorrectVulnerabilityRuleBundleConfig3(random()),
40+
ExpectError: regexp.MustCompile(`no predicate has been specified for image label rule`),
41+
},
2942
{
3043
Config: minimalVulnerabilityRuleBundleConfig(random()),
3144
},
32-
// {
33-
// ResourceName: "sysdig_secure_vulnerability_rule_bundle.sample",
34-
// ImportState: true,
35-
// ImportStateVerify: true,
36-
// },
45+
{
46+
Config: fullVulnerabilityRuleBundleConfig(random()),
47+
},
48+
{
49+
ResourceName: "sysdig_secure_vulnerability_rule_bundle.sample",
50+
ImportState: true,
51+
ImportStateVerify: true,
52+
},
3753
},
3854
})
3955
}
4056

57+
func incorrectVulnerabilityRuleBundleConfig(suffix string) string {
58+
return fmt.Sprintf(`
59+
resource "sysdig_secure_vulnerability_rule_bundle" "sample" {
60+
name = "TERRAFORM TEST %s"
61+
rule {}
62+
}
63+
`, suffix)
64+
}
65+
66+
func incorrectVulnerabilityRuleBundleConfig2(suffix string) string {
67+
return fmt.Sprintf(`
68+
resource "sysdig_secure_vulnerability_rule_bundle" "sample" {
69+
name = "TERRAFORM TEST %s"
70+
rule {
71+
image_label {}
72+
image_label {}
73+
}
74+
}
75+
`, suffix)
76+
}
77+
78+
func incorrectVulnerabilityRuleBundleConfig3(suffix string) string {
79+
return fmt.Sprintf(`
80+
resource "sysdig_secure_vulnerability_rule_bundle" "sample" {
81+
name = "TERRAFORM TEST %s"
82+
rule {
83+
image_label {}
84+
}
85+
}
86+
`, suffix)
87+
}
88+
4189
func minimalVulnerabilityRuleBundleConfig(suffix string) string {
4290
return fmt.Sprintf(`
4391
resource "sysdig_secure_vulnerability_rule_bundle" "sample" {
4492
name = "TERRAFORM TEST %s"
93+
rule {
94+
image_label {
95+
label_must_exist = "required-label"
96+
}
97+
}
98+
}
99+
`, suffix)
100+
}
101+
102+
func fullVulnerabilityRuleBundleConfig(suffix string) string {
103+
return fmt.Sprintf(`
104+
resource "sysdig_secure_vulnerability_rule_bundle" "sample" {
105+
name = "TERRAFORM TEST %s"
45106
46107
rule {
47108
image_label {

0 commit comments

Comments
 (0)