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

sops_test: add more tests on errors #1624

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
65 changes: 63 additions & 2 deletions sops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,23 @@ func TestDecrypt(t *testing.T) {
}
}

type WrongType struct{}

func TestEncryptWrongType(t *testing.T) {
branches := TreeBranches{
TreeBranch{
TreeItem{
Key: "foo",
Value: WrongType{},
},
},
}
tree := Tree{Branches: branches, Metadata: Metadata{UnencryptedSuffix: DefaultUnencryptedSuffix}}
result, err := tree.Encrypt(bytes.Repeat([]byte{'f'}, 32), MockCipher{})
assert.Equal(t, "", result)
assert.ErrorContains(t, err, "unknown type: sops.WrongType")
}

func TestTruncateTree(t *testing.T) {
tree := TreeBranch{
TreeItem{
Expand Down Expand Up @@ -875,6 +892,45 @@ func TestTruncateTree(t *testing.T) {
assert.Equal(t, expected, result)
}

func TestTruncateTreeNotFound(t *testing.T) {
tree := TreeBranch{
TreeItem{
Key: "foo",
Value: 2,
},
}
result, err := tree.Truncate([]interface{}{"baz"})
assert.ErrorContains(t, err, "baz")
assert.Nil(t, result, "Truncate result was not nil upon %s", err)
}

func TestTruncateTreeNotArray(t *testing.T) {
tree := TreeBranch{
TreeItem{
Key: "foo",
Value: 2,
},
}
result, err := tree.Truncate([]interface{}{"foo", 99})
assert.ErrorContains(t, err, "99")
assert.Nil(t, result, "Truncate result was not nil upon %s", err)
}

func TestTruncateTreeArrayOutOfBounds(t *testing.T) {
tree := TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
"one",
"two",
},
},
}
result, err := tree.Truncate([]interface{}{"foo", 99})
assert.ErrorContains(t, err, "99")
assert.Nil(t, result, "Truncate result was not nil upon %s", err)
}

func TestEncryptComments(t *testing.T) {
tree := Tree{
Branches: TreeBranches{
Expand Down Expand Up @@ -1360,8 +1416,9 @@ func TestUnsetKeyNotFound(t *testing.T) {
},
},
}
unset, err := branch.Unset([]interface{}{"foo", "unknown"})
assert.Equal(t, err.(*SopsKeyNotFound).Key, "unknown")
unset, err := branch.Unset([]interface{}{"foo", "unknown-value"})
assert.Equal(t, err.(*SopsKeyNotFound).Key, "unknown-value")
assert.ErrorContains(t, err, "unknown-value")
assert.Nil(t, unset, "Unset result was not nil upon %s", err)
}

Expand Down Expand Up @@ -1435,6 +1492,10 @@ func TestEmitAsMap(t *testing.T) {
Key: "number",
Value: 42,
},
TreeItem{
Key: Comment{"comment"},
Value: nil,
},
},
TreeBranch{
TreeItem{
Expand Down
Loading