Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed May 13, 2024
1 parent 275395b commit ef5aded
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions pkg/tfbridge/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,79 @@ func TestDelegateIDField(t *testing.T) {
})
})
}

func TestDelegateIDProperty(t *testing.T) {
t.Parallel()

const (
providerName = "test-provider"
repoURL = "https://example.git"
)

errMsg := func(msg string, a ...any) error {
return delegateIDPropertyError{
msg: fmt.Sprintf(msg, a...),
providerName: providerName,
repoURL: repoURL,
}
}

tests := []struct {
delegate resource.PropertyPath
state resource.PropertyMap
expectedID resource.ID
expectedError error
expectedLogMsg string
}{
{
delegate: resource.PropertyPath{"key"},
state: resource.PropertyMap{
"key": resource.NewProperty("some-id"),
"other": resource.NewProperty(3.0),
},
expectedID: "some-id",
},
{
delegate: resource.PropertyPath{"nested", "id"},
state: resource.PropertyMap{
"nested": resource.NewProperty(resource.PropertyMap{
"id": resource.NewProperty("my-nested-id"),
}),
"other": resource.NewProperty(3.0),
},
expectedID: "my-nested-id",
},
{
delegate: resource.PropertyPath{"nested", "id"},
state: resource.PropertyMap{
"other": resource.NewProperty(3.0),
},
expectedError: errMsg("Could not find required property 'nested.id' in state"),
},
}

for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
var logs bytes.Buffer

ctx = logging.InitLogging(ctx, logging.LogOptions{
LogSink: &testLogSink{&logs},
})

computeID := DelegateIDProperty(tt.delegate, providerName, repoURL)
id, err := computeID(ctx, tt.state)

if tt.expectedError == nil {
assert.NoError(t, err)
assert.Equal(t, tt.expectedID, id)
} else {
assert.ErrorIs(t, err, tt.expectedError)
}

assert.Equal(t, tt.expectedLogMsg, logs.String())
})
}
}

0 comments on commit ef5aded

Please sign in to comment.