Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/github_repository_deploy_key: Trim key #132

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions github/resource_github_repository_deploy_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package github

import (
"context"
"regexp"
"strconv"
"strings"

"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -12,17 +14,18 @@ func resourceGithubRepositoryDeployKey() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryDeployKeyCreate,
Read: resourceGithubRepositoryDeployKeyRead,
// Deploy keys are defined immutable in the API. Updating results in force new.
Delete: resourceGithubRepositoryDeployKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

// Deploy keys are defined immutable in the API. Updating results in force new.
Schema: map[string]*schema.Schema{
"key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: suppressDeployKeyDiff,
},
"read_only": &schema.Schema{
Type: schema.TypeBool,
Expand Down Expand Up @@ -118,3 +121,11 @@ func resourceGithubRepositoryDeployKeyDelete(d *schema.ResourceData, meta interf

return err
}

func suppressDeployKeyDiff(k, oldV, newV string, d *schema.ResourceData) bool {
newV = strings.TrimSpace(newV)
keyRe := regexp.MustCompile(`^([a-z0-9-]+ [^\s]+)( [^\s]+)?$`)
newTrimmed := keyRe.ReplaceAllString(newV, "$1")

return oldV == newTrimmed
}
57 changes: 49 additions & 8 deletions github/resource_github_repository_deploy_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package github
import (
"context"
"fmt"
"path/filepath"
"regexp"
"strconv"
"testing"

Expand All @@ -11,21 +13,60 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestSuppressDeployKeyDiff(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really strongly opinionated on this, but typically I do 1 validation per test and typically the function name is quite verbose, almost describing the test circumstance. What is more important though is our unit test style is consistent, so if this is consistent with everything it's all good with me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair point, I'll break it apart.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we do have many unit tests (set aside acceptance tests which can have multiple steps) which have multiple test cases in them and I think it's fine? e.g. https://github.com/terraform-providers/terraform-provider-aws/blob/master/aws/validators_test.go#L12-L54

The thing which I agree needs changing though is the error message, so it's more obvious where each failure is coming from. 👍 I will fix that.

testCases := []struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice table driven tests!

OldValue, NewValue string
ExpectSuppression bool
}{
{
"ssh-rsa AAAABB...cd+==",
"ssh-rsa AAAABB...cd+== terraform-acctest@hashicorp.com\n",
true,
},
{
"ssh-rsa AAAABB...cd+==",
"ssh-rsa AAAABB...cd+==",
true,
},
{
"ssh-rsa AAAABV...cd+==",
"ssh-rsa DIFFERENT...cd+==",
false,
},
}

tcCount := len(testCases)
for i, tc := range testCases {
suppressed := suppressDeployKeyDiff("test", tc.OldValue, tc.NewValue, nil)
if tc.ExpectSuppression && !suppressed {
t.Fatalf("%d/%d: Expected %q and %q to be suppressed",
i+1, tcCount, tc.OldValue, tc.NewValue)
}
if !tc.ExpectSuppression && suppressed {
t.Fatalf("%d/%d: Expected %q and %q NOT to be suppressed",
i+1, tcCount, tc.OldValue, tc.NewValue)
}
}

}

func TestAccGithubRepositoryDeployKey_basic(t *testing.T) {
rs := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
repositoryName := fmt.Sprintf("acctest-%s", rs)
keyPath := filepath.Join("test-fixtures", "id_rsa.pub")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubRepositoryDeployKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubRepositoryDeployKeyConfig(repositoryName),
Config: testAccGithubRepositoryDeployKeyConfig(repositoryName, keyPath),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryDeployKeyExists("github_repository_deploy_key.test_repo_deploy_key"),
resource.TestCheckResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "read_only", "false"),
resource.TestCheckResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "repository", repositoryName),
resource.TestCheckResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "key", testAccGithubRepositoryDeployKeytestDeployKey),
resource.TestMatchResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "key", regexp.MustCompile(`^ssh-rsa [^\s]+$`)),
resource.TestCheckResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "title", "title"),
),
},
Expand All @@ -36,13 +77,15 @@ func TestAccGithubRepositoryDeployKey_basic(t *testing.T) {
func TestAccGithubRepositoryDeployKey_importBasic(t *testing.T) {
rs := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
repositoryName := fmt.Sprintf("acctest-%s", rs)
keyPath := filepath.Join("test-fixtures", "id_rsa.pub")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubRepositoryDeployKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubRepositoryDeployKeyConfig(repositoryName),
Config: testAccGithubRepositoryDeployKeyConfig(repositoryName, keyPath),
},
{
ResourceName: "github_repository_deploy_key.test_repo_deploy_key",
Expand Down Expand Up @@ -115,19 +158,17 @@ func testAccCheckGithubRepositoryDeployKeyExists(n string) resource.TestCheckFun
}
}

const testAccGithubRepositoryDeployKeytestDeployKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDnDk1liOxXwE27fjOVVHl6RNVgQznGqGIfhsoa5QNfLOcoWJR3EIv44dSUx1GSvxQ7uR9qBY/i/SEdAbKdupo3Ru5sykc0GqaMRVys+Cin/Lgnl6+ntmTZOudNjIbz10Vfu/dKmexSzqlD3XWzPGXRI5WyKWzvc2XKjRdfnOOzogJpqJ5kh/CN0ZhCzBPTu/b4mJl2ionTEzEeLK2g4Re4IuU/dGoyf0LGLidjmqhSY7dQtL+mfte9m3x/BQTrDf0+AW3kGWXR8EL0EyIJ2HRtHW67YnoOcTAFK0hDCuKgvt78rqdUQ2bVjcsIhNqnvQMPf3ZeZ5bP2JqB9zKaFl8uaRJv+TdxEeFTkgnbYb85M+aBggBYr6xxeb24g7WlU0iPxJ8GmjvCizxe2I1DOJDRDozn1sinKjapNRdJy00iuo46TJC5Wgmid0vnMJ7SMZtubz+btxhoFLt4F4U2JnILaYG4/buJg4H/GkqmkE8G3hr4b4mgsFXBtBFgK6uCTFQSvvV7TyyWkZxHL6DRCxL/Dp0bSj+EM8Tw1K304EvkBEO3rMyvPs4nXL7pepyKWalmUI8U4Qp2xMXSq7fmfZY55osb03MUAtKl0wJ/ykyKOwYWeLbubSVcc6VPx5bXZmnM5bTcZdYW9+vNt86X2F2b0h/sIkGNEPpqQQBzElY+fQ=="

func testAccGithubRepositoryDeployKeyConfig(name string) string {
func testAccGithubRepositoryDeployKeyConfig(name, keyPath string) string {
return fmt.Sprintf(`
resource "github_repository" "test_repo" {
name = "%s"
}

resource "github_repository_deploy_key" "test_repo_deploy_key" {
key = "%s"
key = "${file("%s")}"
read_only = "false"
repository = "${github_repository.test_repo.name}"
title = "title"
}
`, name, testAccGithubRepositoryDeployKeytestDeployKey)
`, name, keyPath)
}
1 change: 1 addition & 0 deletions github/test-fixtures/id_rsa.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDnDk1liOxXwE27fjOVVHl6RNVgQznGqGIfhsoa5QNfLOcoWJR3EIv44dSUx1GSvxQ7uR9qBY/i/SEdAbKdupo3Ru5sykc0GqaMRVys+Cin/Lgnl6+ntmTZOudNjIbz10Vfu/dKmexSzqlD3XWzPGXRI5WyKWzvc2XKjRdfnOOzogJpqJ5kh/CN0ZhCzBPTu/b4mJl2ionTEzEeLK2g4Re4IuU/dGoyf0LGLidjmqhSY7dQtL+mfte9m3x/BQTrDf0+AW3kGWXR8EL0EyIJ2HRtHW67YnoOcTAFK0hDCuKgvt78rqdUQ2bVjcsIhNqnvQMPf3ZeZ5bP2JqB9zKaFl8uaRJv+TdxEeFTkgnbYb85M+aBggBYr6xxeb24g7WlU0iPxJ8GmjvCizxe2I1DOJDRDozn1sinKjapNRdJy00iuo46TJC5Wgmid0vnMJ7SMZtubz+btxhoFLt4F4U2JnILaYG4/buJg4H/GkqmkE8G3hr4b4mgsFXBtBFgK6uCTFQSvvV7TyyWkZxHL6DRCxL/Dp0bSj+EM8Tw1K304EvkBEO3rMyvPs4nXL7pepyKWalmUI8U4Qp2xMXSq7fmfZY55osb03MUAtKl0wJ/ykyKOwYWeLbubSVcc6VPx5bXZmnM5bTcZdYW9+vNt86X2F2b0h/sIkGNEPpqQQBzElY+fQ== terraform-acctest@hashicorp.com