|
| 1 | +package sentry |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/resource" |
| 9 | + "github.com/hashicorp/terraform/terraform" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAccSentryKeyDataSource_basic(t *testing.T) { |
| 13 | + resource.Test(t, resource.TestCase{ |
| 14 | + PreCheck: func() { testAccPreCheck(t) }, |
| 15 | + Providers: testAccProviders, |
| 16 | + Steps: []resource.TestStep{ |
| 17 | + { |
| 18 | + Config: testAccSentryKeyDataSourceConfig, |
| 19 | + Check: resource.ComposeTestCheckFunc( |
| 20 | + testAccCheckSentryKeyDataSourceID("data.sentry_key.test_key"), |
| 21 | + resource.TestCheckResourceAttrSet("data.sentry_key.test_key", "name"), |
| 22 | + resource.TestMatchResourceAttr("data.sentry_key.test_key", "public", regexp.MustCompile(`^[0-9a-f]+$`)), |
| 23 | + resource.TestMatchResourceAttr("data.sentry_key.test_key", "secret", regexp.MustCompile(`^[0-9a-f]+$`)), |
| 24 | + resource.TestMatchResourceAttr("data.sentry_key.test_key", "project_id", regexp.MustCompile(`^\d+$`)), |
| 25 | + resource.TestCheckResourceAttrSet("data.sentry_key.test_key", "is_active"), |
| 26 | + resource.TestMatchResourceAttr("data.sentry_key.test_key", "dsn_secret", regexp.MustCompile(`^https://`)), |
| 27 | + resource.TestMatchResourceAttr("data.sentry_key.test_key", "dsn_public", regexp.MustCompile(`^https://`)), |
| 28 | + resource.TestMatchResourceAttr("data.sentry_key.test_key", "dsn_csp", regexp.MustCompile(`^https://`)), |
| 29 | + ), |
| 30 | + }, |
| 31 | + }, |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +func TestAccSentryKeyDataSource_first(t *testing.T) { |
| 36 | + resource.Test(t, resource.TestCase{ |
| 37 | + PreCheck: func() { testAccPreCheck(t) }, |
| 38 | + Providers: testAccProviders, |
| 39 | + Steps: []resource.TestStep{ |
| 40 | + { |
| 41 | + Config: testAccSentryKeyDataSourceFirstConfig, |
| 42 | + Check: resource.ComposeTestCheckFunc( |
| 43 | + testAccCheckSentryKeyDataSourceID("data.sentry_key.test_key"), |
| 44 | + ), |
| 45 | + }, |
| 46 | + }, |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func TestAccSentryKeyDataSource_name(t *testing.T) { |
| 51 | + resource.Test(t, resource.TestCase{ |
| 52 | + PreCheck: func() { testAccPreCheck(t) }, |
| 53 | + Providers: testAccProviders, |
| 54 | + Steps: []resource.TestStep{ |
| 55 | + { |
| 56 | + Config: testAccSentryKeyDataSourceNameConfig, |
| 57 | + Check: resource.ComposeTestCheckFunc( |
| 58 | + testAccCheckSentryKeyDataSourceID("data.sentry_key.default_key"), |
| 59 | + ), |
| 60 | + }, |
| 61 | + }, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +func testAccCheckSentryKeyDataSourceID(n string) resource.TestCheckFunc { |
| 66 | + return func(s *terraform.State) error { |
| 67 | + rs, ok := s.RootModule().Resources[n] |
| 68 | + if !ok { |
| 69 | + return fmt.Errorf("Can't find Sentry key: %s", n) |
| 70 | + } |
| 71 | + |
| 72 | + if rs.Primary.ID == "" { |
| 73 | + return fmt.Errorf("Sentry key data source ID not set") |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +var testAccSentryKeyDataSourceConfig = fmt.Sprintf(` |
| 81 | +resource "sentry_team" "test_team" { |
| 82 | + organization = "%s" |
| 83 | + name = "Test team" |
| 84 | +} |
| 85 | +
|
| 86 | +resource "sentry_project" "test_project" { |
| 87 | + organization = "%s" |
| 88 | + team = "${sentry_team.test_team.id}" |
| 89 | + name = "Test project" |
| 90 | +} |
| 91 | +
|
| 92 | +data "sentry_key" "test_key" { |
| 93 | + organization = "%s" |
| 94 | + project = "${sentry_project.test_project.id}" |
| 95 | +} |
| 96 | +`, testOrganization, testOrganization, testOrganization) |
| 97 | + |
| 98 | +// Testing first parameter |
| 99 | +var testAccSentryKeyDataSourceFirstConfig = fmt.Sprintf(` |
| 100 | +resource "sentry_team" "test_team" { |
| 101 | + organization = "%s" |
| 102 | + name = "Test team" |
| 103 | +} |
| 104 | +
|
| 105 | +resource "sentry_project" "test_project" { |
| 106 | + organization = "%s" |
| 107 | + team = "${sentry_team.test_team.id}" |
| 108 | + name = "Test project" |
| 109 | +} |
| 110 | +
|
| 111 | +resource "sentry_key" "test_key_2" { |
| 112 | + organization = "%s" |
| 113 | + project = "${sentry_project.test_project.id}" |
| 114 | + name = "Test key 2" |
| 115 | +} |
| 116 | +
|
| 117 | +data "sentry_key" "test_key" { |
| 118 | + organization = "%s" |
| 119 | + project = "${sentry_project.test_project.id}" |
| 120 | + first = true |
| 121 | +} |
| 122 | +`, testOrganization, testOrganization, testOrganization, testOrganization) |
| 123 | + |
| 124 | +// Testing name parameter |
| 125 | +// A key named "Default" is always created along with the project |
| 126 | +var testAccSentryKeyDataSourceNameConfig = fmt.Sprintf(` |
| 127 | +resource "sentry_team" "test_team" { |
| 128 | + organization = "%s" |
| 129 | + name = "Test team" |
| 130 | +} |
| 131 | +
|
| 132 | +resource "sentry_project" "test_project" { |
| 133 | + organization = "%s" |
| 134 | + team = "${sentry_team.test_team.id}" |
| 135 | + name = "Test project" |
| 136 | +} |
| 137 | +
|
| 138 | +data "sentry_key" "default_key" { |
| 139 | + organization = "%s" |
| 140 | + project = "${sentry_project.test_project.id}" |
| 141 | + name = "Default" |
| 142 | +} |
| 143 | +`, testOrganization, testOrganization, testOrganization) |
0 commit comments