-
Notifications
You must be signed in to change notification settings - Fork 768
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ package github | |
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
"testing" | ||
|
||
|
@@ -11,21 +13,60 @@ import ( | |
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestSuppressDeployKeyDiff(t *testing.T) { | ||
testCases := []struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"), | ||
), | ||
}, | ||
|
@@ -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", | ||
|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.